<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Draly_Dropshipping_Starter_Pluggable_Yith_Woocommerce_Quick_View' ) ) {
	class Draly_Dropshipping_Starter_Pluggable_Yith_Woocommerce_Quick_View {
		/**
		 * Variable to hold the initialization state.
		 *
		 * @var  boolean
		 */
		protected static $initialized = false;

		public static function initialize() {
			// Do nothing if pluggable functions already initialized.
			if ( self::$initialized ) {
				return;
			}
			$enable_on_mobile = get_option( 'yith-wcqv-enable-mobile' ) == 'yes' ? true : false;
			if ( ( ! wp_is_mobile() ) || ( wp_is_mobile() && $enable_on_mobile ) ) {
				add_action( 'wp_loaded', function () {
					remove_action( 'woocommerce_after_shop_loop_item', array(
						YITH_WCQV_Frontend::get_instance(),
						'yith_add_quick_view_button'
					), 15 );
				} );
				add_action( 'draly_dropshipping_starter_function_shop_loop_item_quickview', array(
					YITH_WCQV_Frontend::get_instance(),
					'yith_add_quick_view_button'
				), 5 );
			}
			add_filter( 'yith_add_quick_view_button_html', array(
				__CLASS__,
				'draly_dropshipping_starter_add_quick_view_button_html'
			), 10, 3 );
			// State that initialization completed.
			self::$initialized = true;
		}

		public static function draly_dropshipping_starter_add_quick_view_button_html( $button, $label, $product ) {
			return sprintf(
				'<a href="%s#quick-view" class="button yith-wcqv-button" role="button" data-product_id="%d">%s</a>',
				esc_url( get_permalink( $product->get_id() ) ),
				esc_attr( $product->get_id() ),
				esc_html( $label )
			);
		}
	}
}