<?php

namespace Cart\notify;

defined( 'ABSPATH' ) || exit;

/**
 * Necessary Plugin handler class
 */
class Necessary {
	/**
	 * WooCommerce availability state.
	 *
	 * @var bool
	 */
	private $woocommerce_active = false;

	/**
	 * Initialize the class
	 */
	function __construct() {
		$this->check_woocommerce();
	}

	/**
	 * Run WooCommerce dependency checks and notice hooks.
	 *
	 * @return void
	 */
	public function check_woocommerce() {
		$this->woocommerce_active = is_plugin_active( 'woocommerce/woocommerce.php' ) && class_exists( 'WooCommerce' );

		if ( $this->woocommerce_active ) {
			return;
		}

		/**
		 * Check if WooCommerce is active, and if it isn't, disable the plugin.
		 *
		 * @since 1.0
		 */
		add_action( 'admin_notices', [ $this, 'cartnotify_Is_Woo' ] );

		/**
		 * Ajax install & activate WooCommerce
		 *
		 * @since 1.0
		 * @link https://developer.wordpress.org/reference/functions/wp_ajax_install_plugin/
		 */
		add_action( 'wp_ajax_cartnotify_ajax_install_plugin', 'wp_ajax_install_plugin' );
	}

	/**
	 * Backward-safe wrapper for previous method name.
	 *
	 * @return void
	 */
	public function WooComCheck() {
		$this->check_woocommerce();
	}

	/**
	 * Check whether WooCommerce is currently available.
	 *
	 * @return bool
	 */
	public function is_woocommerce_active() {
		return $this->woocommerce_active;
	}

	/**
	 * Called when WooCommerce is inactive to display an inactive notice.
	 *
	 * @since 1.0
	 */
	public function cartnotify_Is_Woo() {
		if ( current_user_can( 'activate_plugins' ) ) {
			if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) && ! file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
				?>
				<div id="message" class="notice notice-error">
					<p>
						<?php
						printf(
							wp_kses_post( __( 'Cart Notify requires %1$sWooCommerce%2$s to be activated in order to function properly.', 'cartnotify' ) ),
							'<strong><a href="https://wordpress.org/plugins/woocommerce/" target="_blank" rel="noopener noreferrer">',
							'</a></strong>'
						);
						?>
					</p>
					<p>
						<a class="cartnotify-install-woocommerce button" data-plugin-slug="woocommerce">
							<?php esc_attr_e( 'Install Now', 'cartnotify' ); ?>
						</a>
					</p>
				</div>

				<?php
			} elseif ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) && file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
				$activate_url = wp_nonce_url(
					add_query_arg(
						[
							'action' => 'activate',
							'plugin' => 'woocommerce/woocommerce.php',
						],
						admin_url( 'plugins.php' )
					),
					'activate-plugin_woocommerce/woocommerce.php'
				);
				?>

				<div id="message" class="notice notice-error">
					<p>
						<?php
						printf(
							wp_kses_post( __( 'Cart Notify requires %1$sWooCommerce%2$s to be activated.', 'cartnotify' ) ),
							'<strong><a href="https://wordpress.org/plugins/woocommerce/" target="_blank" rel="noopener noreferrer">',
							'</a></strong>'
						);
						?>
					</p>
					<p><a href="<?php echo esc_url( $activate_url ); ?>"
							class="button activate-now button-primary">
							<?php esc_attr_e( 'Activate', 'cartnotify' ); ?>
						</a></p>
				</div>

				<?php
			} elseif ( version_compare( get_option( 'woocommerce_db_version' ), '5.0', '<' ) ) {
				?>

				<div id="message" class="notice notice-error">
					<p>
						<?php
						printf(
							wp_kses_post( __( '%1$sCart Notify is inactive.%2$s This plugin requires WooCommerce 5.0 or newer. Please %3$supdate WooCommerce to version 5.0 or newer%4$s', 'cartnotify' ) ),
							'<strong>',
							'</strong>',
							'<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">',
							' &raquo;</a>'
						);
						?>
					</p>
				</div>

				<?php
			}
		}
	}


}
