<?php
/**
 * Extend and customize Woocommerce
 */
class Aidoo_Extensions_Woocommerce {
    
	public function __construct()
	{
		remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
		remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
		remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
        remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
        remove_action('woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
        remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
        remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
        remove_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
        remove_action('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_loop_add_to_cart', 10);
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
        
        
        add_action( 'woocommerce_single_product_summary', array($this, 'price_rating_open'), 13 );
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 14 );
        add_action( 'woocommerce_single_product_summary', array($this, 'price_rating_close'), 16 );
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 17 );
        
        if(!aidoo_get_options('nbcore_product_rating')) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
        }

		add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
		add_filter( 'woocommerce_before_main_content', 'aidoo_page_title', 5 );
		add_filter( 'loop_shop_columns', array($this, 'loop_columns') );
		add_filter( 'loop_shop_per_page', array($this, 'products_per_page'), 20 );
		add_filter( 'woocommerce_pagination_args', array($this, 'woocommerce_pagination') );
		add_filter('woocommerce_product_description_heading', '__return_empty_string');
		add_filter('woocommerce_product_additional_information_heading', '__return_empty_string');
		add_filter('woocommerce_review_gravatar_size', array($this, 'wc_review_avatar_size'));
		add_filter('woocommerce_cross_sells_total', array($this, 'cross_sells_limit'));
		add_filter('woocommerce_upsells_total', array($this, 'upsells_limit'));
		add_filter('yith_add_quick_view_button_html', array($this, 'quickview_button'), 10, 3);
		add_filter('yith_quick_view_loader_gif', '__return_empty_string');
        add_filter( 'option_yith_woocompare_button_text',  array($this, 'compare_button_text'), 99 );

        add_action('woocommerce_before_shop_loop', array($this, 'show_product_archive_options'), 20);
		add_action('woocommerce_after_shop_loop_item', array($this, 'product_action_div_open'), 6);
        add_action('woocommerce_after_shop_loop_item', array($this, 'product_action_div_close'), 50);

        if ( aidoo_get_options('product_category_compare') && get_option('yith_woocompare_compare_button_in_products_list') == 'yes' && function_exists('yith_woocompare_constructor') ) 
            add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
        
        add_action('woocommerce_after_shop_loop_item', array($this, 'wishlist_button'), 20); 

		add_action('woocommerce_shop_loop_item_title', array($this, 'product_title'), 10);
        add_action('woocommerce_before_main_content', array($this, 'shop_banner'), 15);
        

        add_action('woocommerce_after_shop_loop_item', array($this, 'product_img_mask_div_open'), 52);
        add_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 52);
        add_action('woocommerce_after_shop_loop_item', array($this, 'product_img_mask_div_close'), 52);
        

		add_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 15);
		add_action('woocommerce_single_product_summary', array($this, 'wide_meta_left_div_open'), 9);
		add_action('woocommerce_single_product_summary', array($this, 'wide_meta_left_div_close'), 24);
		add_action('woocommerce_single_product_summary', array($this, 'wide_meta_right_div_open'), 26);
        add_action('woocommerce_single_product_summary', array($this, 'wide_meta_right_div_close'), 55);
        add_action('woocommerce_single_product_summary', array($this, 'woocommerce_template_single_sharing'), 20);
        add_action('woocommerce_after_cart_button', array($this, 'wishlist_button'), 40);

        if( function_exists('yith_woocompare_constructor') )
            add_action('woocommerce_after_cart_button', array($this, 'add_compare_link'), 45);
            
		add_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 15);
        add_action('wp_footer', array($this, 'add_cart_notice'));

        if(class_exists('WOOF')) {
            add_action('woocommerce_before_shop_loop', array($this, 'add_toggle_filter_woof_button'), 1);
        }

        add_action('woocommerce_before_shop_loop', array($this, 'add_open_shop_action_tag'), 4);
        add_action('woocommerce_before_shop_loop', array($this, 'add_close_shop_action_tag'), 31);
		
        //TODO Fix this hack?
        add_action('netbase_add_to_cart_hook', 'woocommerce_template_single_add_to_cart', 10);
        
        add_filter('woocommerce_in_cart_product', array($this, 'remove_wishlist_quickview'), 50, 1 );

        add_action('woocommerce_after_shop_loop_item_title', array($this, 'wc_shop_loop_item_desc'), 50);

    }
    
    public function wc_shop_loop_item_desc() {

        if(aidoo_get_options('nbcore_grid_product_description')) {

            global $product;

            echo '<p class="product-excerpt">'.esc_html(get_the_excerpt($product->get_id())).'</p>';
        }
    }

    public function add_toggle_filter_woof_button() {
        echo '<div class="woof_filter_button_wrap">';
        echo '<span class="woof_toggle_filter_button button" data-filter-bar-header-txt="'. esc_attr('Filter', 'aidoo').'" data-filter-bar-close-btn-txt="'. esc_attr('Close', 'aidoo').'">' . esc_html('Filter', 'aidoo') . '</span>';
        echo '</div>';
    }

    public function add_open_shop_action_tag() {
        echo '<div class="shop-action">';
    }

    public function add_close_shop_action_tag() {
        echo '</div>';
    }

    public function compare_button_text( $button_text ){
        return '<i class="icon-compare"></i><span class="tooltip">'.esc_html($button_text).'</span>';
    }

	public function remove_wishlist_quickview($a){
        add_filter('yith_add_quick_view_button_html',  '__return_empty_string', 50, 3);
    }

	public function product_action_div_open()
	{
        $product_action_style = ' ' . aidoo_get_options('nbcore_product_action_style');
        echo '<div class="product-action' . esc_attr($product_action_style) . '">';
	}

	public function product_action_div_close()
	{
        echo '</div>';
    }
    
    public function product_img_mask_div_open()
    {
        echo '<div class="product-image-mask">';
    }

    public function product_img_mask_div_close()
    {
        echo '</div>';
    }

    public function wishlist_button()
    {
        if(aidoo_get_options('product_category_wishlist')) {
            if ( class_exists( 'YITH_WCWL' ) ) {
                if(shortcode_exists('yith_wcwl_add_to_wishlist')){
                    echo '<div class="wishlist-btn button bt-4">' . do_shortcode( '[yith_wcwl_add_to_wishlist]' ) . '</div>';
                }
            }
        }
    }

	public function product_title()
	{
		echo '<h4 class="product-title"><a href="' . esc_url(get_the_permalink()) . '">' . esc_html(get_the_title()) . '</a></h4>';
	}

	public function products_per_page($cols)
	{
        $temp_per_page = isset($_GET['per_page']) && $_GET['per_page'] != '' && is_numeric($_GET['per_page']) ?  $_GET['per_page'] : 0;


        if($temp_per_page != 0)
            $products_per_page = $temp_per_page;
        else
            $products_per_page = aidoo_get_options('nbcore_products_per_page');

		return $products_per_page;
	}

	public function woocommerce_pagination()
	{
		return array(
			'prev_text' => '<i class="icon-left-open"></i>',
			'next_text' => '<i class="icon-right-open"></i>',
			'end_size' => 1,
			'mid_size' => 1,
		);
	}

	public function product_description()
	{
        if ( has_excerpt() ){
		  echo '<p class="product-description">' . strip_tags(get_the_excerpt()) . '</p>';
        }
	}

	public function product_category()
	{
		global $post;
		$terms = get_the_terms( $post->ID, 'product_cat' );
		foreach ($terms as $term) {
			echo '<a class="product-category-link" href="' . esc_url(get_term_link($term->term_id)) . '">' . esc_html($term->name) . '</a>';
		}
	}

	public function shop_banner()
	{
		if(function_exists( 'is_shop' ) && is_shop()) {
			$shop_banner_url = aidoo_get_options('nbcore_shop_banner');
			if ($shop_banner_url) {
				echo '<div class="shop-banner"><img src="' . esc_url(wp_get_attachment_url(absint($shop_banner_url))) . '" /></div>';
			}
		}
    }
    
    public function woocommerce_template_single_sharing()
    {
        global $product;
        global $post;

        $upload_form = array();
        $settings_modules = !empty(get_option('solutions_core_settings')) ? get_option('solutions_core_settings') : array();

        if(in_array('order-upload', $settings_modules)) {
            if(class_exists('NB_Solution')){
                $upload_form = NB_Solution::get_setting('order-upload');
            }
        }

        

        if ( class_exists( 'Nbdesigner_IO' ) && get_post_meta($product->get_id(), '_nbdesigner_enable', true) ):
            if(shortcode_exists('nbdesigner_button')){?>
            <div class="wc-single-online-desginer">
                <h4><?php esc_html_e('Online Desgin', 'aidoo');?></h4>
                <p><?php esc_html_e('Combine it with our layouts and fonts.', 'aidoo');?></p>
                <?php echo do_shortcode( '[nbdesigner_button]' ); ?>
            </div>
        <?php }endif; ?>
        <div class="clearfix">
            <?php if( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'grouped' ) ) {
                if( (isset($upload_form['nbt_order_upload_upload_form_position']) && $upload_form['nbt_order_upload_upload_form_position'] == 'woocommerce_single_product_summary') || get_post_meta($post->ID, '_order_upload', true) != 'on') {
                    do_action('netbase_add_to_cart_hook');
                    do_action('woocommerce_after_cart_button');
                    ?>
                    <?php if( function_exists('nbcore_share_social') && 'inside-content' === aidoo_get_options('share_buttons_position')):?>
                        <div class="social"><?php esc_html_e("SHARE THIS PRODUCT",'aidoo'); nbcore_share_social(); ?></div>
                    <?php endif;?>
                    <?php
                }
            }?>
        </div>

        <?php
    }

	public function wc_review_avatar_size()
	{
		return '80';
	}

	public function wide_meta_left_div_open()
	{
		if('wide' === aidoo_get_options('nbcore_pd_meta_layout')) {
			echo '<div class="pd-meta-left">';
		}
	}

	public function wide_meta_left_div_close()
	{
		if('wide' === aidoo_get_options('nbcore_pd_meta_layout')) {
			echo '</div>';
		}
	}

	public function wide_meta_right_div_open()
	{
		if('wide' === aidoo_get_options('nbcore_pd_meta_layout')) {
			echo '<div class="pd-meta-right">';
		}
	}

	public function wide_meta_right_div_close()
	{
		if('wide' === aidoo_get_options('nbcore_pd_meta_layout')) {
			echo '</div>';
		}
    }
    
    public function woocommerce_custom_online_design_button()
    {
    global $product;
    if ( class_exists( 'Nbdesigner_IO' ) && get_post_meta($product->get_id(), '_nbdesigner_enable', true) ):

        $nbdesigner_position_button_product_detail = nbdesigner_get_option('nbdesigner_position_button_product_detail');

        if($nbdesigner_position_button_product_detail == 4):
    ?>
            <div class="wc-custom-online-desginer">
                <p class="custom-od-title"><?php esc_html_e('Online Desgin', 'aidoo');?></p>
                <p class="custom-od-des"><?php esc_html_e('Combine it with our layouts and fonts.', 'aidoo');?></p>
                <?php echo do_shortcode( '[nbdesigner_button]' );?>
            </div>

        <?php endif; ?>
    <?php endif; ?>

    <?php
    }

	public function wc_share_social()
	{
		if(aidoo_get_options('nbcore_pd_show_social') && function_exists('nbcore_share_social')) {
			nbcore_share_social();
		}
	}

	public function cross_sells_limit()
	{
		$cross_sells_limit = aidoo_get_options('nbcore_cross_sells_limit');
		return $cross_sells_limit;
	}

	public function upsells_limit()
	{
		$upsells_limit = aidoo_get_options('nbcore_upsells_limit');
		return $upsells_limit;
	}

    public function quickview_button($button, $label, $product)
    {
        $html = '';
        if(aidoo_get_options('product_category_quickview')) {
            global $product;

            $product_id = yit_get_prop( $product, 'id', true );

            $html = '<a href="#" class="button yith-wcqv-button bt-4" data-product_id="' . esc_attr($product_id) . '"><i class="icon-opened-eye"></i><span class="tooltip">' . esc_html($label) . '</span></a>';

        }
        return $html;
	}

	public function upload_scripts()
    {
        wp_enqueue_script('media-upload');
        wp_enqueue_media();
    }

    public function add_cart_notice()
    {
        $settings_modules = !empty(get_option('solutions_core_settings')) ? get_option('solutions_core_settings') : array();
        if(! in_array('ajax-cart', $settings_modules)) {
            $url = wc_get_cart_url();
            ?>
            <div class="cart-notice-wrap">
                <div class="cart-notice">
                    <p><?php esc_html_e('Product has been added to cart', 'aidoo'); ?></p>
                    <p class="cart-url button"><a href="<?php echo esc_url($url); ?>"><?php esc_html_e('View Cart', 'aidoo'); ?></a></p>
                    <span><i class="icon-cancel-circle"></i></span>
                </div>
            </div>
            <?php
        }
       
    }

    /**
     *  Add the link to compare
     */
    public function add_compare_link( $product_id = false, $args = array() ) {
        extract( $args );

        if ( ! $product_id ) {
            global $product;
            $product_id = ! is_null( $product ) ? yit_get_prop( $product, 'id', true ) : 0;
        }

        // return if product doesn't exist
        if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) )
            return;

        $is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button' ) : $button_or_link;

        if ( ! isset( $button_text ) || $button_text == 'default' ) {
            $button_text = get_option( 'yith_woocompare_button_text', esc_html__( 'Compare', 'aidoo' ) );
            do_action ( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
            $button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
        }

        printf( '<a href="%s" class="%s" data-product_id="%d" rel="nofollow">%s</a>', $this->add_product_url( $product_id ), 'compare bt-4 real-compare-button' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text );
    }

    /**
     * The URL to add the product into the comparison table
     *
     * @param int $product_id ID of the product to add
     * @return string The url to add the product in the comparison table
     */
    public function add_product_url( $product_id ) {
        $url_args = array(
            'action' => 'yith-woocompare-add-product',
            'id' => $product_id
        );

        $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : false;
        if( $lang ) {
            $url_args['lang'] = $lang;
        }

        return apply_filters( 'yith_woocompare_add_product_url', esc_url_raw( add_query_arg( $url_args, site_url() ) ), 'yith-woocompare-add-product' );
    }

    public function thumb_open_tag() {
        echo '<div class="flex-control-wrapper">';
    }

    public function thumb_close_tag() {
        echo '</div>';
    }

    public function price_rating_open() {
        echo '<div class="price_rating">';
    }

    public function price_rating_close() {
        echo '</div>';
    }

    public function loop_columns()
	{		
        return aidoo_get_options('nbcore_loop_columns');
	}

    public static function loop_columns_resp()
    {
        
        $nbcore_vendor_loop_columns['xl'] = aidoo_get_options('nbcore_loop_columns_xl');
        $nbcore_vendor_loop_columns['lg'] = aidoo_get_options('nbcore_loop_columns_lg');
        $nbcore_vendor_loop_columns['md'] = aidoo_get_options('nbcore_loop_columns_md');
        $nbcore_vendor_loop_columns['sm'] = aidoo_get_options('nbcore_loop_columns_sm');
        // return aidoo_get_options('nbcore_loop_columns');
        
        return $nbcore_vendor_loop_columns;
    }

    public function show_product_archive_options() {

        $temp_columns       =  isset($_GET['columns']) && $_GET['columns'] != '' && is_numeric($_GET['columns']) ?  $_GET['columns'] : 0;

        if($temp_columns != 0)
            $shop_columns = $temp_columns;
        else
            $shop_columns = aidoo_convert_columns_class_to_number(aidoo_get_options('nbcore_loop_columns'));

        $temp_per_page = isset($_GET['per_page']) && $_GET['per_page'] != '' && is_numeric($_GET['per_page']) ?  $_GET['per_page'] : 0;


        if($temp_per_page != 0)
            $products_per_page = $temp_per_page;
        else
            $products_per_page = aidoo_get_options('nbcore_products_per_page');

        $activated_class        = ' activated';
        $activated_class_full   = "class=activated";
    
    ?>

        <div class="product_archive_options">
            <div class="column_options">
                
                <a class="temp-columns po-2-columns<?php echo (wp_kses_post($shop_columns) == 2) ? esc_attr($activated_class) : '';?>" href="<?php echo esc_url( add_query_arg( 'columns', '2' ) ) ?>" title="<?php esc_attr_e('2 columns', 'aidoo');?>"><i class="icon-2-cols"></i></a>

                <a class="temp-columns po-3-columns<?php echo (wp_kses_post($shop_columns) == 3) ? esc_attr($activated_class) : '';?>" href="<?php echo esc_url( add_query_arg( 'columns', '3' ) ) ?>" title="<?php esc_attr_e('3 columns', 'aidoo');?>"><i class="icon-3-cols"></i></a>

                <a class="temp-columns po-4-columns<?php echo (wp_kses_post($shop_columns) == 4) ? esc_attr($activated_class) : '';?>" href="<?php echo esc_url( add_query_arg( 'columns', '4' ) ) ?>" title="<?php esc_attr_e('4 columns', 'aidoo');?>"><i class="icon-4-cols"></i></a>

                <a class="temp-columns po-5-columns<?php echo (wp_kses_post($shop_columns) == 5) ? esc_attr($activated_class) : '';?>" href="<?php echo esc_url( add_query_arg( 'columns', '5' ) ) ?>" title="<?php esc_attr_e('5 columns', 'aidoo');?>"><i class="icon-full-cols"></i></a>

            </div>

            <div class="product_per_page_options">
                <span class="per_page_label"><?php esc_html_e('Show:','aidoo') ?></span>
                <a <?php echo (wp_kses_post($products_per_page) == 8) ? esc_attr($activated_class_full) : '';?> href="<?php echo esc_url( add_query_arg( 'per_page', '8' ) ) ?>" title="<?php esc_attr_e('Show 8 products per page', 'aidoo');?>">8</a>
                <span class="per_page_separator"> / </span>
                <a <?php echo (wp_kses_post($products_per_page) == 12) ? esc_attr($activated_class_full) : '';?> href="<?php echo esc_url( add_query_arg( 'per_page', '12' ) ) ?>" title="<?php esc_attr_e('Show 12 products per page', 'aidoo');?>">12</a>
                <span class="per_page_separator"> / </span>
                <a <?php echo (wp_kses_post($products_per_page) == 16) ? esc_attr($activated_class_full) : '';?> href="<?php echo esc_url( add_query_arg( 'per_page', '16' ) ) ?>" title="<?php esc_attr_e('Show 16 products per page', 'aidoo');?>">16</a>
                <span class="per_page_separator"> / </span>
                <a <?php echo (wp_kses_post($products_per_page) == 20) ? esc_attr($activated_class_full) : '';?> href="<?php echo esc_url( add_query_arg( 'per_page', '20' ) ) ?>" title="<?php esc_attr_e('Show 20 products per page', 'aidoo');?>">20</a>
            </div>

        </div>

    <?php
    }
}



