
<?php
/**
 * WooCommerce Button Hooks.
 *
 * Handles WooCommerce button text customization.
 *
 * @package WooCommerce_Button_Text_Changer
 */

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

/**
 * WooCommerce button hooks class.
 */
final class WBTC_WooCommerce_Hooks {

	/**
	 * Singleton instance.
	 *
	 * @var WBTC_WooCommerce_Hooks|null
	 */
	private static $instance = null;

	/**
	 * Button text manager.
	 *
	 * @var WBTC_Button_Text_Changer|null
	 */
	private $manager = null;

	/**
	 * Get singleton instance.
	 *
	 * @return WBTC_WooCommerce_Hooks
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {

		/*
		 * Get button text manager.
		 */
		if ( class_exists( 'WBTC_Button_Text_Changer' ) ) {

			$this->manager =
				WBTC_Button_Text_Changer::instance();
		}

		/*
		 * Product archive / loop Add to Cart.
		 */
		add_filter(
			'woocommerce_product_add_to_cart_text',
			array(
				$this,
				'filter_add_to_cart_text',
			),
			99
		);

		/*
		 * Single product Add to Cart.
		 */
		add_filter(
			'woocommerce_product_single_add_to_cart_text',
			array(
				$this,
				'filter_add_to_cart_text',
			),
			99
		);

		/*
		 * Read More.
		 *
		 * This uses the same WooCommerce product button
		 * filter but only when the original button text
		 * is actually "Read more".
		 */
		add_filter(
			'woocommerce_product_add_to_cart_text',
			array(
				$this,
				'filter_read_more_text',
			),
			100
		);

		/*
		 * WooCommerce translated button strings.
		 *
		 * Handles:
		 * - View Cart
		 * - Update Cart
		 * - Proceed to Checkout
		 */
		add_filter(
			'gettext_woocommerce',
			array(
				$this,
				'filter_woocommerce_gettext',
			),
			20,
			3
		);

		/*
		 * Place Order.
		 */
		add_filter(
			'woocommerce_order_button_text',
			array(
				$this,
				'filter_place_order_text',
			),
			99
		);
	}

	/**
	 * Check whether plugin is enabled.
	 *
	 * @return bool
	 */
	private function is_enabled() {

		if ( ! $this->manager ) {
			return false;
		}

		return $this->manager->is_enabled();
	}

	/**
	 * Get custom button text.
	 *
	 * @param string $key Button setting key.
	 * @return string
	 */
	private function get_text( $key ) {

		if ( ! $this->manager ) {
			return '';
		}

		return $this->manager->get_button_text( $key );
	}

	/**
	 * Check whether text represents WooCommerce's
	 * default Read More label.
	 *
	 * @param string $text Button text.
	 * @return bool
	 */
	private function is_read_more_text( $text ) {

		$text = wp_strip_all_tags( $text );
		$text = trim( $text );

		return (
			'Read more' === $text
			||
			'Read More' === $text
		);
	}

	/**
	 * Add to Cart text.
	 *
	 * This intentionally does not replace "Read more".
	 * Read More is handled separately.
	 *
	 * @param string $text Button text.
	 * @return string
	 */
	public function filter_add_to_cart_text( $text ) {

		if ( ! $this->is_enabled() ) {
			return $text;
		}

		/*
		 * Do not let Add to Cart override Read More.
		 */
		if ( $this->is_read_more_text( $text ) ) {
			return $text;
		}

		$custom_text =
			$this->get_text( 'add_to_cart' );

		if ( '' !== $custom_text ) {
			return $custom_text;
		}

		return $text;
	}

	/**
	 * Read More text.
	 *
	 * Only modifies WooCommerce's default Read More
	 * button text.
	 *
	 * @param string $text Button text.
	 * @return string
	 */
	public function filter_read_more_text( $text ) {

		if ( ! $this->is_enabled() ) {
			return $text;
		}

		/*
		 * Only target the Read More state.
		 */
		if ( ! $this->is_read_more_text( $text ) ) {
			return $text;
		}

		$custom_text =
			$this->get_text( 'read_more' );

		if ( '' !== $custom_text ) {
			return $custom_text;
		}

		return $text;
	}

	/**
	 * Place Order text.
	 *
	 * @param string $text Button text.
	 * @return string
	 */
	public function filter_place_order_text( $text ) {

		if ( ! $this->is_enabled() ) {
			return $text;
		}

		$custom_text =
			$this->get_text( 'place_order' );

		if ( '' !== $custom_text ) {
			return $custom_text;
		}

		return $text;
	}

	/**
	 * WooCommerce translated strings.
	 *
	 * Handles:
	 * - View cart
	 * - Update cart
	 * - Proceed to checkout
	 *
	 * @param string $translated Translated string.
	 * @param string $text       Original string.
	 * @param string $domain     Text domain.
	 * @return string
	 */
	public function filter_woocommerce_gettext(
		$translated,
		$text,
		$domain
	) {

		if ( ! $this->is_enabled() ) {
			return $translated;
		}

		/*
		 * This callback is attached specifically to
		 * gettext_woocommerce, so only WooCommerce
		 * translations reach this method.
		 */
		if ( 'woocommerce' !== $domain ) {
			return $translated;
		}

		/*
		 * View Cart.
		 */
		if (
			'View cart' === $text
			||
			'View Cart' === $text
			||
			'View cart' === $translated
			||
			'View Cart' === $translated
		) {

			$custom_text =
				$this->get_text( 'view_cart' );

			if ( '' !== $custom_text ) {
				return $custom_text;
			}
		}

		/*
		 * Update Cart.
		 */
		if (
			'Update cart' === $text
			||
			'Update Cart' === $text
			||
			'Update cart' === $translated
			||
			'Update Cart' === $translated
		) {

			$custom_text =
				$this->get_text( 'update_cart' );

			if ( '' !== $custom_text ) {
				return $custom_text;
			}
		}

		/*
		 * Proceed to Checkout.
		 */
		if (
			'Proceed to checkout' === $text
			||
			'Proceed to Checkout' === $text
			||
			'Proceed to checkout' === $translated
			||
			'Proceed to Checkout' === $translated
		) {

			$custom_text =
				$this->get_text( 'checkout' );

			if ( '' !== $custom_text ) {
				return $custom_text;
			}
		}

		return $translated;
	}
}