<?php
/**
 * WooCommerce Archive Loop Class Injector (Clean & Safe)
 *
 * @package Sifency_Addons
 */

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

if ( ! class_exists( 'Sifency_WC_Archive_Loop' ) ) :

	class Sifency_WC_Archive_Loop {

		public function __construct() {

			if ( is_admin() && ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				return;
			}

			if ( ! class_exists( 'WooCommerce' ) ) {
				return;
			}

			if ( ! apply_filters( 'sifency_enable_wc_archive_loop', true ) ) {
				return;
			}

			$this->init_hooks();
		}

		/**
		 * Init WooCommerce Hooks
		 */
		protected function init_hooks() {

			/*
			=========================
			 * Loop Item Wrapper
			 * ========================= */
			add_action( 'woocommerce_before_shop_loop_item', array( $this, 'open_loop_item' ), 1 );
			add_action( 'woocommerce_after_shop_loop_item', array( $this, 'close_loop_item' ), 999 );

			/*
			=========================
			 * Image Wrapper
			 * ========================= */
			add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'open_image_wrap' ), 5 );
			add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'close_image_wrap' ), 20 );

			/*
			=========================
			 * Content Wrapper
			 * ========================= */
			add_action( 'woocommerce_shop_loop_item_title', array( $this, 'open_content_wrap' ), 1 );
			add_action( 'woocommerce_after_shop_loop_item', array( $this, 'close_content_wrap' ), 1 );

			/*
			=========================
			 * Title Wrapper (DIV only)
			 * ========================= */
			add_action( 'woocommerce_shop_loop_item_title', array( $this, 'open_title_wrap' ), 5 );
			add_action( 'woocommerce_shop_loop_item_title', array( $this, 'close_title_wrap' ), 15 );

			/*
			=========================
			 * Price Wrapper (NO price class)
			 * ========================= */
			add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'open_price_wrap' ), 5 );
			add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'close_price_wrap' ), 15 );

			/*
			=========================
			 * Rating Wrapper
			 * ========================= */
			add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'open_rating_wrap' ), 16 );
			add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'close_rating_wrap' ), 25 );

			/*
			=========================
			 * Add to Cart Wrapper
			 * ========================= */
			add_action( 'woocommerce_after_shop_loop_item', array( $this, 'open_cart_wrap' ), 5 );
			add_action( 'woocommerce_after_shop_loop_item', array( $this, 'close_cart_wrap' ), 15 );

			/*
			=========================
			 * Sale Badge Class
			 * ========================= */
			add_filter( 'woocommerce_sale_flash', array( $this, 'sale_badge_class' ), 10, 3 );
		}

		/*
		=========================
		 * Markup Output
		 * ========================= */

		public function open_loop_item() {
			echo '<div class="wc-loop-item sifency-wc-loop-item">';
		}

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

		public function open_image_wrap() {
			echo '<div class="wc-loop-image sifency-wc-loop-image">';
		}

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

		public function open_content_wrap() {
			echo '<div class="wc-loop-content sifency-wc-loop-content">';
		}

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

		/**
		 * Title wrapper
		 * NOTE: Woo already outputs <h2>, we only wrap it
		 */
		public function open_title_wrap() {
			echo '<div class="wc-loop-title sifency-wc-loop-title">';
		}

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

		/**
		 * Price wrapper
		 * NOTE: Woo already outputs <span class="price">
		 */
		public function open_price_wrap() {
			echo '<div class="wc-loop-price sifency-wc-loop-price">';
		}

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

		public function open_rating_wrap() {
			echo '<div class="wc-loop-rating sifency-wc-loop-rating">';
		}

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

		public function open_cart_wrap() {
			echo '<div class="wc-loop-cart sifency-wc-loop-cart">';
		}

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

		/**
		 * Sale badge class filter
		 */
		public function sale_badge_class( $html, $post, $product ) {
			return str_replace(
				'class="onsale"',
				'class="onsale wc-loop-sale-badge sifency-wc-loop-sale"',
				$html
			);
		}
	}

endif;

// Init
new Sifency_WC_Archive_Loop();
