<?php
/* Woocommerce support functions
------------------------------------------------------------------------------- */

// Theme init
if (!function_exists('writer_ancora_woocommerce_theme_setup')) {
	add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_woocommerce_theme_setup', 1 );
	function writer_ancora_woocommerce_theme_setup() {

		if (writer_ancora_exists_woocommerce()) {
			
			add_theme_support( 'woocommerce' );
			// Next setting from the WooCommerce 3.0+ enable built-in image zoom on the single product page
			add_theme_support( 'wc-product-gallery-zoom' );
			// Next setting from the WooCommerce 3.0+ enable built-in image slider on the single product page
			add_theme_support( 'wc-product-gallery-slider' );
			// Next setting from the WooCommerce 3.0+ enable built-in image lightbox on the single product page
			add_theme_support( 'wc-product-gallery-lightbox' );
			
			add_action('writer_ancora_action_add_styles', 				'writer_ancora_woocommerce_frontend_scripts' );

			// Detect current page type, taxonomy and title (for custom post_types use priority < 10 to fire it handles early, than for standard post types)
			add_filter('writer_ancora_filter_get_blog_type',				'writer_ancora_woocommerce_get_blog_type', 9, 2);
			add_filter('writer_ancora_filter_get_blog_title',			'writer_ancora_woocommerce_get_blog_title', 9, 2);
			add_filter('writer_ancora_filter_get_current_taxonomy',		'writer_ancora_woocommerce_get_current_taxonomy', 9, 2);
			add_filter('writer_ancora_filter_is_taxonomy',				'writer_ancora_woocommerce_is_taxonomy', 9, 2);
			add_filter('writer_ancora_filter_get_stream_page_title',		'writer_ancora_woocommerce_get_stream_page_title', 9, 2);
			add_filter('writer_ancora_filter_get_stream_page_link',		'writer_ancora_woocommerce_get_stream_page_link', 9, 2);
			add_filter('writer_ancora_filter_get_stream_page_id',		'writer_ancora_woocommerce_get_stream_page_id', 9, 2);
			add_filter('writer_ancora_filter_detect_inheritance_key',	'writer_ancora_woocommerce_detect_inheritance_key', 9, 1);
			add_filter('writer_ancora_filter_detect_template_page_id',	'writer_ancora_woocommerce_detect_template_page_id', 9, 2);
			add_filter('writer_ancora_filter_orderby_need',				'writer_ancora_woocommerce_orderby_need', 9, 2);

			add_filter('writer_ancora_filter_show_post_navi', 			'writer_ancora_woocommerce_show_post_navi');
			add_filter('writer_ancora_filter_list_post_types', 			'writer_ancora_woocommerce_list_post_types');
		}

		if (is_admin()) {
			add_filter( 'writer_ancora_filter_required_plugins',					'writer_ancora_woocommerce_required_plugins' );
		}
	}
}

if ( !function_exists( 'writer_ancora_woocommerce_settings_theme_setup2' ) ) {
	add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_woocommerce_settings_theme_setup2', 3 );
	function writer_ancora_woocommerce_settings_theme_setup2() {
		if (writer_ancora_exists_woocommerce()) {
			// Add WooCommerce pages in the Theme inheritance system
			writer_ancora_add_theme_inheritance( array( 'woocommerce' => array(
				'stream_template' => 'blog-woocommerce',		// This params must be empty
				'single_template' => 'single-woocommerce',		// They are specified to enable separate settings for blog and single wooc
				'taxonomy' => array('product_cat'),
				'taxonomy_tags' => array('product_tag'),
				'post_type' => array('product'),
				'override' => 'page'
				) )
			);

			// Add WooCommerce specific options in the Theme Options

			writer_ancora_storage_set_array_before('options', 'partition_service', array(

				"partition_woocommerce" => array(
					"title" => esc_html__('WooCommerce', 'writer-ancora'),
					"icon" => "iconadmin-basket",
					"type" => "partition"),

				"info_wooc_1" => array(
					"title" => esc_html__('WooCommerce products list parameters', 'writer-ancora'),
					"desc" => esc_html__("Select WooCommerce products list's style and crop parameters", 'writer-ancora'),
					"type" => "info"),

				"shop_mode" => array(
					"title" => esc_html__('Shop list style',  'writer-ancora'),
					"desc" => esc_html__("WooCommerce products list's style: thumbs or list with description", 'writer-ancora'),
					"std" => "thumbs",
					"divider" => false,
					"options" => array(
						'thumbs' => esc_html__('Thumbs', 'writer-ancora'),
						'list' => esc_html__('List', 'writer-ancora')
					),
					"type" => "checklist"),

				"show_mode_buttons" => array(
					"title" => esc_html__('Show style buttons',  'writer-ancora'),
					"desc" => esc_html__("Show buttons to allow visitors change list style", 'writer-ancora'),
					"std" => "yes",
					"options" => writer_ancora_get_options_param('list_yes_no'),
					"type" => "switch"),


				"show_currency" => array(
					"title" => esc_html__('Show currency selector', 'writer-ancora'),
					"desc" => esc_html__('Show currency selector in the user menu', 'writer-ancora'),
					"std" => "no",
					"options" => writer_ancora_get_options_param('list_yes_no'),
					"type" => "switch"),

				"show_cart" => array(
					"title" => esc_html__('Show cart button', 'writer-ancora'),
					"desc" => esc_html__('Show cart button in the user menu', 'writer-ancora'),
					"std" => "shop",
					"options" => array(
						'hide'   => esc_html__('Hide', 'writer-ancora'),
						'always' => esc_html__('Always', 'writer-ancora'),
						'shop'   => esc_html__('Only on shop pages', 'writer-ancora')
					),
					"type" => "checklist"),

				"crop_product_thumb" => array(
					"title" => esc_html__("Crop product's thumbnail",  'writer-ancora'),
					"desc" => esc_html__("Crop product's thumbnails on search results page or scale it", 'writer-ancora'),
					"std" => "no",
					"options" => writer_ancora_get_options_param('list_yes_no'),
					"type" => "switch")

				)
			);

		}
	}
}

// WooCommerce hooks
if (!function_exists('writer_ancora_woocommerce_theme_setup3')) {
	add_action( 'writer_ancora_action_after_init_theme', 'writer_ancora_woocommerce_theme_setup3' );
	function writer_ancora_woocommerce_theme_setup3() {

		if (writer_ancora_exists_woocommerce()) {

			add_action(    'woocommerce_before_subcategory_title',		'writer_ancora_woocommerce_open_thumb_wrapper', 9 );
			add_action(    'woocommerce_before_shop_loop_item_title',	'writer_ancora_woocommerce_open_thumb_wrapper', 9 );

			add_action(    'woocommerce_before_subcategory_title',		'writer_ancora_woocommerce_open_item_wrapper', 20 );
			add_action(    'woocommerce_before_shop_loop_item_title',	'writer_ancora_woocommerce_open_item_wrapper', 20 );

			add_action(    'woocommerce_after_subcategory',				'writer_ancora_woocommerce_close_item_wrapper', 20 );
			add_action(    'woocommerce_after_shop_loop_item',			'writer_ancora_woocommerce_close_item_wrapper', 20 );

			add_action(    'woocommerce_after_shop_loop_item_title',	'writer_ancora_woocommerce_after_shop_loop_item_title', 7);

			add_action(    'woocommerce_after_subcategory_title',		'writer_ancora_woocommerce_after_subcategory_title', 10 );

			// Wrap category title into link
			remove_action( 'woocommerce_shop_loop_subcategory_title', 'woocommerce_template_loop_category_title', 10 );
			add_action(		'woocommerce_shop_loop_subcategory_title',  'writer_ancora_woocommerce_shop_loop_subcategory_title', 9, 1);

			// Remove link around product item
			remove_action('woocommerce_before_shop_loop_item',			'woocommerce_template_loop_product_link_open', 10);
			remove_action('woocommerce_after_shop_loop_item',			'woocommerce_template_loop_product_link_close', 5);
			// Remove link around product category
			remove_action('woocommerce_before_subcategory',				'woocommerce_template_loop_category_link_open', 10);
			remove_action('woocommerce_after_subcategory',				'woocommerce_template_loop_category_link_close', 10);

		}

		if (writer_ancora_is_woocommerce_page()) {

			remove_action( 'woocommerce_sidebar', 						'woocommerce_get_sidebar', 10 );					// Remove WOOC sidebar

			remove_action( 'woocommerce_before_main_content',			'woocommerce_output_content_wrapper', 10);
			add_action(    'woocommerce_before_main_content',			'writer_ancora_woocommerce_wrapper_start', 10);

			remove_action( 'woocommerce_after_main_content',			'woocommerce_output_content_wrapper_end', 10);
			add_action(    'woocommerce_after_main_content',			'writer_ancora_woocommerce_wrapper_end', 10);

			add_action(    'woocommerce_show_page_title',				'writer_ancora_woocommerce_show_page_title', 10);

			remove_action( 'woocommerce_single_product_summary',		'woocommerce_template_single_title', 5);
			add_action(    'woocommerce_single_product_summary',		'writer_ancora_woocommerce_show_product_title', 5 );

			add_action(    'woocommerce_before_shop_loop', 				'writer_ancora_woocommerce_before_shop_loop', 10 );

			add_action(    'woocommerce_product_meta_end',				'writer_ancora_woocommerce_show_product_id', 10);

			if (writer_ancora_param_is_on(writer_ancora_get_custom_option('show_post_related'))) {
				add_filter('woocommerce_output_related_products_args', 'writer_ancora_woocommerce_output_related_products_args');
				add_filter('woocommerce_related_products_args', 'writer_ancora_woocommerce_related_products_args');
			} else {
				remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
			}

			add_filter(    'get_product_search_form',					'writer_ancora_woocommerce_get_product_search_form' );

			add_filter(    'post_class',								'writer_ancora_woocommerce_loop_shop_columns_class' );
			add_filter(    'product_cat_class',							'writer_ancora_woocommerce_loop_shop_columns_class', 10, 3 );

			add_action(    'the_title',									'writer_ancora_woocommerce_the_title');

			writer_ancora_enqueue_popup();
		}

    }
}



// Check if WooCommerce installed and activated
if ( !function_exists( 'writer_ancora_exists_woocommerce' ) ) {
	function writer_ancora_exists_woocommerce() {
		return class_exists('Woocommerce');
	}
}

// Return true, if current page is any woocommerce page
if ( !function_exists( 'writer_ancora_is_woocommerce_page' ) ) {
	function writer_ancora_is_woocommerce_page() {
		$rez = false;
		if (writer_ancora_exists_woocommerce()) {
			if (!writer_ancora_storage_empty('pre_query')) {
				$id = writer_ancora_storage_get_obj_property('pre_query', 'queried_object_id', 0);
				$rez = writer_ancora_storage_call_obj_method('pre_query', 'get', 'post_type')=='product'
						|| $id==wc_get_page_id('shop')
						|| $id==wc_get_page_id('cart')
						|| $id==wc_get_page_id('checkout')
						|| $id==wc_get_page_id('myaccount')
						|| writer_ancora_storage_call_obj_method('pre_query', 'is_tax', 'product_cat')
						|| writer_ancora_storage_call_obj_method('pre_query', 'is_tax', 'product_tag')
						|| writer_ancora_storage_call_obj_method('pre_query', 'is_tax', get_object_taxonomies('product'));

			} else
				$rez = is_shop() || is_product() || is_product_category() || is_product_tag() || is_product_taxonomy() || is_cart() || is_checkout() || is_account_page();
		}
		return $rez;
	}
}

// Filter to detect current page inheritance key
if ( !function_exists( 'writer_ancora_woocommerce_detect_inheritance_key' ) ) {
	function writer_ancora_woocommerce_detect_inheritance_key($key) {
		if (!empty($key)) return $key;
		return writer_ancora_is_woocommerce_page() ? 'woocommerce' : '';
	}
}

// Filter to detect current template page id
if ( !function_exists( 'writer_ancora_woocommerce_detect_template_page_id' ) ) {
	function writer_ancora_woocommerce_detect_template_page_id($id, $key) {
		if (!empty($id)) return $id;
		if ($key == 'woocommerce_cart')				$id = get_option('woocommerce_cart_page_id');
		else if ($key == 'woocommerce_checkout')	$id = get_option('woocommerce_checkout_page_id');
		else if ($key == 'woocommerce_account')		$id = get_option('woocommerce_account_page_id');
		else if ($key == 'woocommerce')				$id = get_option('woocommerce_shop_page_id');
		return $id;
	}
}

// Filter to detect current page type (slug)
if ( !function_exists( 'writer_ancora_woocommerce_get_blog_type' ) ) {
	function writer_ancora_woocommerce_get_blog_type($page, $query=null) {
		if (!empty($page)) return $page;

		if (is_shop()) 					$page = 'woocommerce_shop';
		else if ($query && $query->get('post_type')=='product' || is_product())		$page = 'woocommerce_product';
		else if ($query && $query->get('product_tag')!='' || is_product_tag())		$page = 'woocommerce_tag';
		else if ($query && $query->get('product_cat')!='' || is_product_category())	$page = 'woocommerce_category';
		else if (is_cart())				$page = 'woocommerce_cart';
		else if (is_checkout())			$page = 'woocommerce_checkout';
		else if (is_account_page())		$page = 'woocommerce_account';
		else if (is_woocommerce())		$page = 'woocommerce';
		return $page;
	}
}

// Filter to detect current page title
if ( !function_exists( 'writer_ancora_woocommerce_get_blog_title' ) ) {
	function writer_ancora_woocommerce_get_blog_title($title, $page) {
		if (!empty($title)) return $title;

		if ( writer_ancora_strpos($page, 'woocommerce')!==false ) {
			if ( $page == 'woocommerce_category' ) {
				$term = get_term_by( 'slug', get_query_var( 'product_cat' ), 'product_cat', OBJECT);
				$title = $term->name;
			} else if ( $page == 'woocommerce_tag' ) {
				$term = get_term_by( 'slug', get_query_var( 'product_tag' ), 'product_tag', OBJECT);
				$title = esc_html__('Tag:', 'writer-ancora') . ' ' . esc_html($term->name);
			} else if ( $page == 'woocommerce_cart' ) {
				$title = esc_html__( 'Your cart', 'writer-ancora' );
			} else if ( $page == 'woocommerce_checkout' ) {
				$title = esc_html__( 'Checkout', 'writer-ancora' );
			} else if ( $page == 'woocommerce_account' ) {
				$title = esc_html__( 'Account', 'writer-ancora' );
			} else if ( $page == 'woocommerce_product' ) {
				$title = writer_ancora_get_post_title();
			} else if (($page_id=get_option('woocommerce_shop_page_id')) > 0) {
				$title = writer_ancora_get_post_title($page_id);
			} else {
				$title = esc_html__( 'Shop', 'writer-ancora' );
			}
		}

		return $title;
	}
}

// Filter to detect stream page title
if ( !function_exists( 'writer_ancora_woocommerce_get_stream_page_title' ) ) {
	function writer_ancora_woocommerce_get_stream_page_title($title, $page) {
		if (!empty($title)) return $title;
		if (writer_ancora_strpos($page, 'woocommerce')!==false) {
			if (($page_id = writer_ancora_woocommerce_get_stream_page_id(0, $page)) > 0)
				$title = writer_ancora_get_post_title($page_id);
			else
				$title = esc_html__('Shop', 'writer-ancora');
		}
		return $title;
	}
}

// Filter to detect stream page ID
if ( !function_exists( 'writer_ancora_woocommerce_get_stream_page_id' ) ) {
	function writer_ancora_woocommerce_get_stream_page_id($id, $page) {
		if (!empty($id)) return $id;
		if (writer_ancora_strpos($page, 'woocommerce')!==false) {
			$id = get_option('woocommerce_shop_page_id');
		}
		return $id;
	}
}

// Filter to detect stream page link
if ( !function_exists( 'writer_ancora_woocommerce_get_stream_page_link' ) ) {
	function writer_ancora_woocommerce_get_stream_page_link($url, $page) {
		if (!empty($url)) return $url;
		if (writer_ancora_strpos($page, 'woocommerce')!==false) {
			$id = writer_ancora_woocommerce_get_stream_page_id(0, $page);
			if ($id) $url = get_permalink($id);
		}
		return $url;
	}
}

// Filter to detect current taxonomy
if ( !function_exists( 'writer_ancora_woocommerce_get_current_taxonomy' ) ) {
	function writer_ancora_woocommerce_get_current_taxonomy($tax, $page) {
		if (!empty($tax)) return $tax;
		if ( writer_ancora_strpos($page, 'woocommerce')!==false ) {
			$tax = 'product_cat';
		}
		return $tax;
	}
}

// Return taxonomy name (slug) if current page is this taxonomy page
if ( !function_exists( 'writer_ancora_woocommerce_is_taxonomy' ) ) {
	function writer_ancora_woocommerce_is_taxonomy($tax, $query=null) {
		if (!empty($tax))
			return $tax;
		else
			return $query!==null && $query->get('product_cat')!='' || is_product_category() ? 'product_cat' : '';
	}
}

// Return false if current plugin not need theme orderby setting
if ( !function_exists( 'writer_ancora_woocommerce_orderby_need' ) ) {
	function writer_ancora_woocommerce_orderby_need($need) {
		if ($need == false || writer_ancora_storage_empty('pre_query'))
			return $need;
		else {
			return writer_ancora_storage_call_obj_method('pre_query', 'get', 'post_type')!='product'
					&& writer_ancora_storage_call_obj_method('pre_query', 'get', 'product_cat')==''
					&& writer_ancora_storage_call_obj_method('pre_query', 'get', 'product_tag')=='';
		}
	}
}

// Add custom post type into list
if ( !function_exists( 'writer_ancora_woocommerce_list_post_types' ) ) {
	function writer_ancora_woocommerce_list_post_types($list) {
		$list['product'] = esc_html__('Products', 'writer-ancora');
		return $list;
	}
}

// Enqueue WooCommerce custom styles
if ( !function_exists( 'writer_ancora_woocommerce_frontend_scripts' ) ) {
	function writer_ancora_woocommerce_frontend_scripts() {
		if (writer_ancora_is_woocommerce_page() || writer_ancora_get_custom_option('show_cart')=='always' || true)
			if (file_exists(writer_ancora_get_file_dir('css/woo-style.css')))
				wp_enqueue_style( 'writer-ancora-plugin-woocommerce-style',  writer_ancora_get_file_url('css/woo-style.css'), array(), null );
	}
}

// Before main content
if ( !function_exists( 'writer_ancora_woocommerce_wrapper_start' ) ) {
	function writer_ancora_woocommerce_wrapper_start() {
		if (is_product() || is_cart() || is_checkout() || is_account_page()) {
			?>
			<article class="post_item post_item_single post_item_product">
			<?php
		} else {
			?>
			<div class="list_products shop_mode_<?php echo !writer_ancora_storage_empty('shop_mode') ? writer_ancora_storage_get('shop_mode') : 'thumbs'; ?>">
			<?php
		}
	}
}

// After main content
if ( !function_exists( 'writer_ancora_woocommerce_wrapper_end' ) ) {
	function writer_ancora_woocommerce_wrapper_end() {
		if (is_product() || is_cart() || is_checkout() || is_account_page()) {
			?>
			</article>	<!-- .post_item -->
			<?php
		} else {
			?>
			</div>	<!-- .list_products -->
			<?php
		}
	}
}

// Check to show page title
if ( !function_exists( 'writer_ancora_woocommerce_show_page_title' ) ) {
	function writer_ancora_woocommerce_show_page_title($defa=true) {
		return writer_ancora_get_custom_option('show_page_title')=='no';
	}
}

// Check to show product title
if ( !function_exists( 'writer_ancora_woocommerce_show_product_title' ) ) {
	function writer_ancora_woocommerce_show_product_title() {
		if (writer_ancora_get_custom_option('show_post_title')=='yes' || writer_ancora_get_custom_option('show_page_title')=='no') {
			wc_get_template( 'single-product/title.php' );
		}
	}
}

// New product excerpt with video shortcode
if ( !function_exists( 'writer_ancora_template_single_excerpt' ) ) {
    function writer_ancora_template_single_excerpt() {
        if ( ! defined( 'ABSPATH' ) ) {
            exit; // Exit if accessed directly
        }
        global $post;
        if ( ! $post->post_excerpt ) {
            return;
        }
        ?>
        <div itemprop="description">
            <?php echo writer_ancora_substitute_all(apply_filters( 'woocommerce_short_description', $post->post_excerpt )); ?>
        </div>
    <?php
    }
}

// Add list mode buttons
if ( !function_exists( 'writer_ancora_woocommerce_before_shop_loop' ) ) {
	function writer_ancora_woocommerce_before_shop_loop() {
		if (writer_ancora_get_custom_option('show_mode_buttons')=='yes') {
			echo '<div class="mode_buttons"><form action="' . esc_url(writer_ancora_get_current_url()) . '" method="post">'
				. '<input type="hidden" name="writer_ancora_shop_mode" value="'.esc_attr(writer_ancora_storage_get('shop_mode')).'" />'
				. '<a href="#" class="woocommerce_thumbs icon-th" title="'.esc_attr__('Show products as thumbs', 'writer-ancora').'"></a>'
				. '<a href="#" class="woocommerce_list icon-th-list" title="'.esc_attr__('Show products as list', 'writer-ancora').'"></a>'
				. '</form></div>';
		}
	}
}


// Open thumbs wrapper for categories and products
if ( !function_exists( 'writer_ancora_woocommerce_open_thumb_wrapper' ) ) {
	function writer_ancora_woocommerce_open_thumb_wrapper($cat='') {
		writer_ancora_storage_set('in_product_item', true);
		?>
		<div class="post_item_wrap">
			<div class="post_featured">
				<div class="post_thumb">
					<a href="<?php echo esc_url(is_object($cat) ? get_term_link($cat->slug, 'product_cat') : get_permalink()); ?>">
		<?php
	}
}


// Add Product categories
if ( !function_exists( 'writer_ancora_woocommerce_product_cats' ) ) {
	function writer_ancora_woocommerce_product_cats()
	{
		global $post;
		$post_id = $post->ID;
		$post_cats = wp_get_post_terms($post_id, 'product_cat');
		$cats_out = '';
		$i = 0;
		if (!empty($post_cats)) {
			$count_cats = count($post_cats);
			foreach ($post_cats as $term) {
				$i++;
				$term_link = get_term_link($term, 'product_cat');
				$cats_out .= !empty($term_link) ?  '<a href="' . esc_url($term_link) . '">' . esc_html($term->name) . '</a>' : '';
				$cats_out .= $i < $count_cats ? ', ' : '';
				$cats_out .= count($post_cats) > 1 && $i < count($post_cats) ? '' : '';

			}
		}
		echo '<div class="product_cats">';
		echo(!empty($cats_out) ? $cats_out : '');
		echo '</div>';
	}
}
add_action('woocommerce_after_shop_loop_item_title', 'writer_ancora_woocommerce_product_cats', 1);










// Open item wrapper for categories and products
if ( !function_exists( 'writer_ancora_woocommerce_open_item_wrapper' ) ) {
	function writer_ancora_woocommerce_open_item_wrapper($cat='') {
		?>
				</a>
			</div>
		</div>
		<div class="post_content">
		<?php
	}
}

// Close item wrapper for categories and products
if ( !function_exists( 'writer_ancora_woocommerce_close_item_wrapper' ) ) {
	function writer_ancora_woocommerce_close_item_wrapper($cat='') {
		?>
			</div>
		</div>
		<?php
		writer_ancora_storage_set('in_product_item', false);
	}
}

// Add excerpt in output for the product in the list mode
if ( !function_exists( 'writer_ancora_woocommerce_after_shop_loop_item_title' ) ) {
	function writer_ancora_woocommerce_after_shop_loop_item_title() {
		if (writer_ancora_storage_get('shop_mode') == 'list') {
		    $excerpt = apply_filters('the_excerpt', get_the_excerpt());
			echo '<div class="description">'.trim($excerpt).'</div>';
		}
	}
}

// Add excerpt in output for the product in the list mode
if ( !function_exists( 'writer_ancora_woocommerce_after_subcategory_title' ) ) {
	function writer_ancora_woocommerce_after_subcategory_title($category) {
		if (writer_ancora_storage_get('shop_mode') == 'list')
			echo '<div class="description">' . trim($category->description) . '</div>';
	}
}

// Add Product ID for single product
if ( !function_exists( 'writer_ancora_woocommerce_show_product_id' ) ) {
	function writer_ancora_woocommerce_show_product_id() {
		global $post, $product;
		echo '<span class="product_id">'.esc_html__('Product ID: ', 'writer-ancora') . '<span>' . ($post->ID) . '</span></span>';
	}
}

// Redefine number of related products
if ( !function_exists( 'writer_ancora_woocommerce_output_related_products_args' ) ) {
	function writer_ancora_woocommerce_output_related_products_args($args) {
		$ppp = $ccc = 0;
		if (writer_ancora_param_is_on(writer_ancora_get_custom_option('show_post_related'))) {
			$ccc_add = in_array(writer_ancora_get_custom_option('body_style'), array('fullwide', 'fullscreen')) ? 1 : 0;
			$ccc =  writer_ancora_get_custom_option('post_related_columns');
			$ccc = $ccc > 0 ? $ccc : (writer_ancora_param_is_off(writer_ancora_get_custom_option('show_sidebar_main')) ? 3+$ccc_add : 2+$ccc_add);
			$ppp = writer_ancora_get_custom_option('post_related_count');
			$ppp = $ppp > 0 ? $ppp : $ccc;
		}
		$args['posts_per_page'] = $ppp;
		$args['columns'] = $ccc;
		return $args;
	}
}

// Redefine post_type if number of related products == 0
if ( !function_exists( 'writer_ancora_woocommerce_related_products_args' ) ) {
	function writer_ancora_woocommerce_related_products_args($args) {
		if ($args['posts_per_page'] == 0)
			$args['post_type'] .= '_';
		return $args;
	}
}

// Add column class into product item in shop streampage
if ( !function_exists( 'writer_ancora_woocommerce_loop_shop_columns_class' ) ) {
    function writer_ancora_woocommerce_loop_shop_columns_class($class, $class2='', $cat='') {
        if (!is_product() && !is_cart() && !is_checkout() && !is_account_page()) {
            $cols = function_exists('wc_get_default_products_per_row') ? wc_get_default_products_per_row() : 2;
            $class[] = ' column-1_' . $cols;
        }
        return $class;
    }
}

// Search form
if ( !function_exists( 'writer_ancora_woocommerce_get_product_search_form' ) ) {
	function writer_ancora_woocommerce_get_product_search_form($form) {
		return '
		<form role="search" method="get" class="search_form" action="' . esc_url(home_url('/')) . '">
			<input type="text" class="search_field" placeholder="' . esc_attr__('Search for products &hellip;', 'writer-ancora') . '" value="' . get_search_query() . '" name="s" title="' . esc_attr__('Search for products:', 'writer-ancora') . '" /><button class="search_button icon-search" type="submit"></button>
			<input type="hidden" name="post_type" value="product" />
		</form>
		';
	}
}

// Wrap product title into link
if ( !function_exists( 'writer_ancora_woocommerce_the_title' ) ) {
	function writer_ancora_woocommerce_the_title($title) {
		if (writer_ancora_storage_get('in_product_item') && get_post_type()=='product') {
			$title = '<a href="'.esc_url(get_permalink()).'">'.($title).'</a>';
		}
		return $title;
	}
}

// Wrap category title into link
if ( !function_exists( 'writer_ancora_woocommerce_shop_loop_subcategory_title' ) ) {
	function writer_ancora_woocommerce_shop_loop_subcategory_title($cat) {
		$cat->name = sprintf('<a href="%s">%s</a>', esc_url(get_term_link($cat->slug, 'product_cat')), $cat->name);
		?>
        <h2 class="woocommerce-loop-category__title">
		<?php
		writer_ancora_show_layout($cat->name);

		if ( $cat->count > 0 ) {
			echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . esc_html( $cat->count ) . ')</mark>', $cat ); // WPCS: XSS ok.
		}
		?>
        </h2><?php
	}
}

// Show pagination links
if ( !function_exists( 'writer_ancora_woocommerce_pagination' ) ) {
	function writer_ancora_woocommerce_pagination() {
		$style = writer_ancora_get_custom_option('blog_pagination');
		writer_ancora_show_pagination(array(
			'class' => 'pagination_wrap pagination_' . esc_attr($style),
			'style' => $style,
			'button_class' => '',
			'first_text'=> '',
			'last_text' => '',
			'prev_text' => '',
			'next_text' => '',
			'pages_in_group' => $style=='pages' ? 10 : 20
			)
		);
	}
}

// Filter to add in the required plugins list
if ( !function_exists( 'writer_ancora_woocommerce_required_plugins' ) ) {
	function writer_ancora_woocommerce_required_plugins($list=array()) {
		if (in_array('woocommerce', writer_ancora_storage_get('required_plugins')))
			$list[] = array(
					'name' 		=> 'WooCommerce',
					'slug' 		=> 'woocommerce',
					'required' 	=> false
				);

		return $list;
	}
}

// Show products navigation
if ( !function_exists( 'writer_ancora_woocommerce_show_post_navi' ) ) {
	function writer_ancora_woocommerce_show_post_navi($show=false) {
		return $show || (writer_ancora_get_custom_option('show_page_title')=='yes' && is_single() && writer_ancora_is_woocommerce_page());
	}
}
?>