<?php
/**
 * Plugin Name: Finch Cart
 * Plugin URI: https://wpbean.com/
 * Description: Ajax Added to Cart Notification Popup - Mini Side Cart - Upsell Product Recommendations for WooCommerce.
 * Version: 1.03
 * Requires at least: 5.7
 * Requires PHP: 7.4
 * Requires Plugins: woocommerce
 * Author: WPBean
 * Author URI: https://wpbean.com
 * Text Domain: finch-cart-product-recommendations
 * Domain Path: /languages
 * WC requires at least: 7.0
 * WC tested up to: 9.3.3
 * License: GPLv3
 * URI: https://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package Finch Cart
 */

namespace FinchCart;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

require_once __DIR__ . '/vendor/autoload.php';

use FinchCart\Admin\Edith\EdithFramework;
use FinchCart\Frontend\AddedtoCartPopup\AddedtoCartPopup;
use FinchCart\Frontend\InlineScripts;
use FinchCart\Admin\Settings\EdithInit;
use FinchCart\Admin\Settings\PluginSettings;
use FinchCart\Admin\Settings\ProductMeta;
use function FinchCart\fc_get_option;

/**
 * Plugin Main Class
 */
final class FinchCart {

	/**
	 * Version.
	 *
	 * @var string
	 */
	public $version = '1.03';

	/**
	 * Instance
	 *
	 * @access private
	 * @static
	 *
	 * @var Plugin The single instance of the class.
	 */
	private static $instance = null;

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @access public
	 *
	 * @return Plugin An instance of the class.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Class Constructor.
	 */
	private function __construct() {
		$this->define_constants();

		include_once ABSPATH . 'wp-admin/includes/plugin.php';

		if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
			add_action( 'before_woocommerce_init', array( $this, 'hpos_compatibility' ) );
			add_action( 'plugins_loaded', array( $this, 'plugin_init' ) );
		} else {
			add_action( 'admin_notices', array( $this, 'dependency_admin_notices' ) );
		}
	}

	/**
	 * Define plugin Constants.
	 */
	public function define_constants() {
		define( 'FINCHCART_FREE_VERSION', $this->version );
		define( 'FINCHCART_FREE_INIT', plugin_basename( __FILE__ ) );
		define( 'FINCHCART_FILE', __FILE__ );
		define( 'FINCHCART_PREMIUM_VERSION_URL', 'https://wpbean.com/?p=44891' );
	}

	/**
	 * Initialize the plugin.
	 *
	 * @return void
	 */
	public function plugin_init() {
		$this->init_classes();

		add_action( 'init', array( $this, 'localization_setup' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
	}

	/**
	 * Initialize the classes
	 *
	 * @return void
	 */
	public function init_classes() {
		new EdithFramework();
		new AddedtoCartPopup();
		new EdithInit();
		new PluginSettings();
		new ProductMeta();
	}

	/**
	 * Initialize plugin for localization.
	 *
	 * @return void
	 */
	public function localization_setup() {
		load_plugin_textdomain( 'finch-cart-product-recommendations', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
	}

	/**
	 * Enqueue scripts and styles.
	 *
	 * @return void
	 */
	public function enqueue_scripts() {
		$settings        = get_option( 'finchcart_settings_' );
		$products_layout = isset( $settings['finchcart_products_layout'] ) ? sanitize_text_field( $settings['finchcart_products_layout'] ) : 'grid';

		// jBox popup.
		wp_enqueue_style( 'finch-cart-jBox', plugins_url( 'assets/css/jBox.min.css', __FILE__ ), array(), '1.0' );
		wp_enqueue_script( 'finch-cart-jBox', plugins_url( 'assets/js/jBox.min.js', __FILE__ ), array( 'jquery' ), '1.0', true );

		// Swiper js carousel.
		if ( isset( $products_layout ) && 'slider' === $products_layout ) {
			wp_enqueue_style( 'finch-cart-swiperjs', plugins_url( 'assets/swiper/swiper-bundle.min.css', __FILE__ ), array(), '1.0' );
			wp_enqueue_script( 'finch-cart-swiperjs', plugins_url( 'assets/swiper/swiper-bundle.min.js', __FILE__ ), array( 'jquery' ), '1.0', true );
		}

		// Plugin main scripts.
		wp_enqueue_style( 'finch-cart-frontend', plugins_url( 'assets/css/finch-cart-frontend.css', __FILE__ ), array(), '1.0' );
		wp_enqueue_script( 'finch-cart-frontend', plugins_url( 'assets/js/finch-cart-frontend.js', __FILE__ ), array( 'jquery', 'wp-util', 'wc-add-to-cart', 'wc-add-to-cart-variation' ), '1.0', true );

		wp_localize_script(
			'finch-cart-frontend',
			'FinchCartVars',
			array(
				'_finchcart_popup_show_type'  => isset( $settings['finchcart_popup_show'] ) ? sanitize_text_field( $settings['finchcart_popup_show'] ) : '',
				'_finchcart_popup_show_times' => isset( $settings['finchcart_popup_show_limited_times'] ) ? absint( $settings['finchcart_popup_show_limited_times'] ) : '',
				'_finchcart_close_icon'       => isset( $settings['finchcart_popup_close_icon'] ) ? wc_string_to_bool( $settings['finchcart_popup_close_icon'] ) : '',
				'_finchcart_esc_close'        => isset( $settings['finchcart_popup_esc_close'] ) ? wc_string_to_bool( $settings['finchcart_popup_esc_close'] ) : '',
				'_finchcart_overlay_close'    => isset( $settings['finchcart_popup_overlay_close'] ) ? wc_string_to_bool( $settings['finchcart_popup_overlay_close'] ) : '',
				'_finchcart_popup_width'      => isset( $settings['finchcart_popup_width'] ) ? absint( $settings['finchcart_popup_width'] ) : '',
			)
		);

		// Adding the inline custom CSS.
		$inline_scripts = new InlineScripts();
		wp_add_inline_style( 'finch-cart-frontend', $inline_scripts->custom_inline_css() );
	}

	/**
	 * Admin notices for dependency.
	 */
	public function dependency_admin_notices() {
		?>
		<div class="notice notice-error is-dismissible">
			<p><b><?php esc_html_e( 'Finch Cart', 'finch-cart-product-recommendations' ); ?></b><?php esc_html_e( ' required ', 'finch-cart-product-recommendations' ); ?><b><a href="https://wordpress.org/plugins/woocommerce/" target="_blank"><?php esc_html_e( 'WooCommerce', 'finch-cart-product-recommendations' ); ?></a></b><?php esc_html_e( ' plugin to work with.', 'finch-cart-product-recommendations' ); ?></p>
		</div>
		<?php
	}

	/**
	 * WooCommerce High-Performance Order Storage compatibility
	 *
	 * @return void
	 */
	public function hpos_compatibility(){
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		}
	}
}

/**
 * Initialize the main plugin.
 *
 * @return \FinchCart
 */

FinchCart::instance();