<?php
/**
 * WooCommerce Class.
 *
 * @package WordPress
 * @subpackage Brimstone
 * @since 1.01
 * @author Chris Baldelomar <chris@webplantmedia.com>
 * @copyright Copyright (c) 2018, Chris Baldelomar
 * @link https://webplantmedia.com/product/brimstone-wordpress-theme/
 * @license http://www.gnu.org/licenses/gpl-2.0.html
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Brimstone_WooCommerce' ) ) :
	/**
	 * Class: WooCommerce Integration class.
	 *
	 * @since Brimstone 1.01
	 */
	class Brimstone_WooCommerce {
		/**
		 * __construct
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function __construct() {
			add_filter( 'woocommerce_product_get_image', array( $this, 'woocommerce_product_get_image' ) );

			add_action( 'wp', array( $this, 'disable_jetpack_infinite_scroll' ) );

			add_filter( 'woocommerce_pagination_args', array( $this, 'woocommerce_pagination_args' ) );
			add_filter( 'woocommerce_comment_pagination_args', array( $this, 'woocommerce_pagination_args' ) );

			add_filter( 'loop_shop_per_page', array( $this, 'loop_shop_per_page' ), 20 );

			add_filter( 'loop_shop_columns', array( $this, 'loop_columns' ) );

			add_filter( 'woocommerce_show_page_title', array( $this, 'hide_title' ) );

			add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 10, 1 );

			add_action( 'after_setup_theme', array( $this, 'woocommerce_setup' ) );

			add_action( 'wp_loaded', array( $this, 'check_features' ), 11 );

			add_action( 'wp', array( $this, 'check_image_size' ) );

			add_action( 'wp_enqueue_scripts', array( $this, 'woocommerce_enqueue' ) );

			add_filter( 'woocommerce_output_related_products_args', array( $this, 'related_products_args' ) );
			add_filter( 'woocommerce_upsell_display_args', array( $this, 'related_products_args' ) );

			remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
			remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );

			add_action( 'woocommerce_before_main_content', array( $this, 'output_content_wrapper' ), 10 );
			add_action( 'woocommerce_after_main_content', array( $this, 'output_content_wrapper_end' ), 10 );

			add_action( 'brimstone_cart', array( $this, 'woocommerce_cart_dropdown' ), 10 );

			add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'woocommerce_header_cart_fragments' ) );

			add_action( 'woocommerce_before_mini_cart', array( $this, 'add_header_mini_cart' ), 10 );

			// Add header for payment info.
			add_action( 'woocommerce_review_order_before_payment', array( $this, 'before_shipping_title' ), 10 );

			add_filter( 'woocommerce_short_description', 'brimstone_the_content', 11 );
		}

		/**
		 * Shop image backdrop
		 *
		 * @since Brimstone 1.01
		 *
		 * @param string $image
		 * @return string
		 */
		public function woocommerce_product_get_image( $image ) {
			global $brimstone;

			if ( $brimstone['shop_image_backdrop'] ) {
				return '<span class="woocommerce-product-image-wrapper">' . $image . '</span>';
			}

			return $image;
		}

		/**
		 * No need for infinit scroll on WooCommerce pages.
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function disable_jetpack_infinite_scroll() {
			if ( is_woocommerce() ) {
				remove_theme_support( 'infinite-scroll' );
			}
		}

		/**
		 * Add font icons to pagination
		 *
		 * @since Brimstone 1.01
		 *
		 * @param array $args
		 * @return array
		 */
		public function woocommerce_pagination_args( $args ) {
			$args['prev_text'] = '<i class="genericons-neue genericons-neue-previous"></i>';
			$args['next_text'] = '<i class="genericons-neue genericons-neue-next"></i>';

			return $args;
		}

		/**
		 * Change products per page
		 *
		 * @since Brimstone 1.01
		 *
		 * @param int $cols
		 * @return int
		 */
		public function loop_shop_per_page( $cols ) {
			global $brimstone;

			return $brimstone['shop_products_per_page'];
		}

		/**
		 * Add theme support
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function woocommerce_setup() {
			// Declare WooCommerce support.
			add_theme_support(
				'woocommerce', array(
				// 'gallery_thumbnail_image_width' => 100,
				)
			);
			add_theme_support( 'wc-product-gallery-zoom' );
			add_theme_support( 'wc-product-gallery-lightbox' );
			add_theme_support( 'wc-product-gallery-slider' );
		}

		/**
		 * Check features with user selected options from Customizer
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function check_features() {
			global $brimstone;

			if ( $brimstone['shop_disable_gallery_zoom'] ) {
				remove_theme_support( 'wc-product-gallery-zoom' );
			}

			if ( $brimstone['shop_disable_gallery_lightbox'] ) {
				remove_theme_support( 'wc-product-gallery-lightbox' );
			}

			if ( $brimstone['shop_disable_gallery_slider'] ) {
				remove_theme_support( 'wc-product-gallery-slider' );
			}

			if ( $brimstone['shop_hide_stars'] ) {
				remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
			}

			if ( $brimstone['shop_product_hide_stars'] ) {
				remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
			}

			if ( $brimstone['shop_hide_breadcrumbs'] ) {
				remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
			}

			if ( $brimstone['shop_hide_result_count'] ) {
				remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
			}

			if ( $brimstone['shop_hide_catalog_ordering'] ) {
				remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
			}

			if ( $brimstone['shop_product_hide_meta'] ) {
				remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
			}

		}

		/**
		 * Change image size depending on columns
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function check_image_size() {
			global $brimstone;

			add_filter( 'single_product_archive_thumbnail_size', array( $this, 'single_product_archive_thumbnail_size' ), 10, 1 );
			add_filter( 'subcategory_archive_thumbnail_size', array( $this, 'single_product_archive_thumbnail_size' ), 10, 1 );
		}

		/**
		 * Change product image size
		 *
		 * @since Brimstone 1.01
		 *
		 * @return string
		 */
		public function single_product_archive_thumbnail_size( $size ) {
			global $brimstone;

			if ( is_product_taxonomy() ) {
				return $brimstone['shop_archive_image_size'];
			} elseif ( is_shop() ) {
				return $brimstone['shop_image_size'];
			} elseif ( is_product() ) {
				return $brimstone['shop_related_products_image_size'];
			}

			return $size;
		}

		/**
		 * Set columns depending on type of page.
		 *
		 * @since Brimstone 1.01
		 *
		 * @param int $number_columns
		 * @return int
		 */
		public function loop_columns( $number_columns ) {
			global $brimstone;

			if ( is_product_category() || is_product_taxonomy() ) {
				return $brimstone['shop_archive_columns'];
			}

			return $brimstone['shop_columns'];
		}

		/**
		 * Hide Title
		 *
		 * @since Brimstone 1.01
		 *
		 * @param mixed $title
		 * @return bool
		 */
		public function hide_title( $title ) {
			return false;
		}

		/**
		 * Adjust shop title
		 *
		 * @since Brimstone 1.01
		 *
		 * @param string $title
		 * @return string
		 */
		public function get_the_archive_title( $title ) {
			if ( is_shop() ) {
				$title = woocommerce_page_title( false );
			} elseif ( is_product_taxonomy() ) {
				$pieces = explode( ': ', $title );
				if ( 2 === count( $pieces ) ) {
					$shop_page_id = wc_get_page_id( 'shop' );
					$page_title   = get_the_title( $shop_page_id );
					$page_title   = apply_filters( 'woocommerce_page_title', $page_title );

					$pieces[0] = $page_title;

					$title = implode( ': ', $pieces );

					return $title;
				}
			}

			return $title;
		}

		/**
		 * Enqueue scripts and styles
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function woocommerce_enqueue() {
			wp_enqueue_style( 'brimstone-woocommerce', get_template_directory_uri() . '/css/woocommerce.css', array( 'brimstone-style' ), BRIMSTONE_VERSION );

			wp_enqueue_script( 'brimstone-woocommerce', get_template_directory_uri() . '/js/woocommerce.js', array(), BRIMSTONE_VERSION, true );
		}

		/**
		 * Set related products args
		 *
		 * @since Brimstone 1.01
		 *
		 * @param array $args
		 * @return array
		 */
		public function related_products_args( $args ) {
			global $brimstone;

			$args = array_merge(
				$args, apply_filters(
					'brimstone_related_products_args', array(
						'posts_per_page' => $brimstone['shop_related_products_columns'],
						'columns'        => $brimstone['shop_related_products_columns'],
					)
				)
			);

			return $args;
		}

		/**
		 * Set before content wrapper
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function output_content_wrapper() {
			global $brimstone;

			echo '<div id="primary" class="content-area"><main id="main" class="site-main">';

			if ( is_shop() && ! $brimstone['shop_hide_title'] ) {
				$title = woocommerce_page_title( false );
				$title = '<h1 class="h1-title">' . $title . '</h1>';
				echo $title; /* WPCS: XSS OK. HTML output. */
			}
		}

		/**
		 * Set after content wrapper
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function output_content_wrapper_end() {
			echo '</main></div>';
		}

		/**
		 * Add cart button dropdown
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function woocommerce_cart_dropdown() {
			global $woocommerce;

			$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
			$link          = wc_get_cart_url();
			// $link          = get_permalink( wc_get_page_id( 'shop' ));
			$cart_items_count = $woocommerce->cart->cart_contents_count;

			$output  = '';
			$output .= '<li class="cart">';
			$output .= "<a class='cart_dropdown_link' href='" . esc_url( $link ) . "'>";
			$output .= "<i class='genericons-neue genericons-neue-cart'></i>";
			if ( 0 !== intval( WC()->cart->get_cart_contents_count() ) ) {
				$output .= "<span class='alert-count'>" . intval( $cart_items_count ) . '</span>';
			}
			$output .= '</a>';
			$output .= '<ul class="woo-sub-menu woocommerce widget_shopping_cart cart_list"><li>';
			$output .= '<div class="widget_shopping_cart_content"></div>';
			$output .= '</li></ul>';
			$output .= '</li>';

			echo $output; /* WPCS: XSS OK. HTML output. */
		}

		/**
		 * Ajax update for item count in cart
		 *
		 * @since Brimstone 1.01
		 *
		 * @param array $fragments
		 * @return array
		 */
		public function woocommerce_header_cart_fragments( $fragments ) {
			global $woocommerce;

			$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
			$link          = wc_get_cart_url();
			// $link             = get_permalink( wc_get_page_id( 'shop' ));
			$cart_items_count = $woocommerce->cart->cart_contents_count;

			$temp = '<a class="cart_dropdown_link" href="' . esc_url( $link ) . '"><i class="genericons-neue genericons-neue-cart"></i><span class="alert-count">' . $cart_items_count . '</span></a><!--<span class="cart_subtotal">' . $cart_subtotal . '</span>-->';

			$fragments['a.cart_dropdown_link'] = $temp;

			return $fragments;
		}

		/**
		 * Add header mini cart
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function add_header_mini_cart() {
			global $woocommerce;
			$cart_items_count = $woocommerce->cart->cart_contents_count;

			$output = '<h3 class="widget-sub-title">' . $cart_items_count . ' ' . esc_html__( 'items in your cart', 'brimstone' ) . '</h3>';

			echo $output; /* WPCS: XSS OK. HTML output. */
		}

		/**
		 * Add header for payment info
		 *
		 * @since Brimstone 1.01
		 *
		 * @return void
		 */
		public function before_shipping_title() {
			echo '<h3 id="payment_method_heading">' . esc_html__( 'Payment info', 'brimstone' ) . '</h3>';
		}
	}
endif;

return new Brimstone_WooCommerce();

/**
 * Show subcategory thumbnails.
 *
 * @since Brimstone 1.01
 *
 * @param object $category
 * @return void
 */
function woocommerce_subcategory_thumbnail( $category ) {
	global $brimstone;

	$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'shop_catalog' );
	$dimensions           = wc_get_image_size( $small_thumbnail_size );
	$thumbnail_id         = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );

	if ( $thumbnail_id ) {
		$image        = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
		$image        = $image[0];
		$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
		$image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
	} else {
		$image        = wc_placeholder_img_src();
		$image_srcset = false;
		$image_sizes  = false;
	}

	if ( $image ) {
		// Prevent esc_url from breaking spaces in urls for image embeds.
		// Ref: https://core.trac.wordpress.org/ticket/23605.
		$image = str_replace( ' ', '%20', $image );

		if ( $brimstone['shop_image_backdrop'] ) {
			echo '<span class="woocommerce-product-image-wrapper">';
		}

		// Add responsive image markup if available.
		if ( $image_srcset && $image_sizes ) {
			echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
		} else {
			echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
		}

		if ( $brimstone['shop_image_backdrop'] ) {
			echo '</span>';
		}
	}
}
