<?php // phpcs:ignore WordPress.Files.FileName

if ( ! defined( 'YITH_COG_VERSION' ) ) {
	exit( 'Direct access forbidden.' );
}

/**
 * Class YITH_COG
 *
 * @class      YITH_COG
 * @package    Yithemes
 * @since      Version 1.0.0
 * @author     YITH <plugins@yithemes.com>
 */

if ( ! class_exists( 'YITH_COG' ) ) {
	/**
	 * Class YITH_COG
	 *
	 */
	class YITH_COG {

		/**
		 * Plugin version
		 *
		 * @var string
		 * @since 1.0
		 */
		public $version = YITH_COG_VERSION;

		/**
		 * Main Instance
		 *
		 * @var YITH_COG
		 * @since 1.0
		 */
		protected static $instance = null;

		/**
		 * Contain the instance of the Admin class.
		 *
		 * @var YITH_COG_Admin
		 */
		public $admin = null;

		/**
		 * Construct
		 *
		 * @since 1.0
		 */
		public function __construct() {

			/**
			 * APPLY_FILTERS: yith_cog_require_class
			 *
			 * Filter the required classes for the plugin.
			 *
			 * @param array the required classes
			 *
			 * @return array
			 */
			$require = apply_filters(
				'yith_cog_require_class',
				array(
					'common'    => array(
						'includes/class.yith-cog-orders.php',
						'includes/class.yith-cog-ajax.php',
						'includes/functions.yith-cog.php',
					),
					'admin'    => array(
						'includes/class.yith-cog-admin.php',
						'includes/reports/class.yith-cog-abstract-admin-table.php',
					),
				)
			);

			$this->require( $require );
			$this->init_classes();

			/* === Plugins Init === */
			add_action( 'init', array( $this, 'init' ) );

			/* Declare HPOS compatibility */
			add_action( 'before_woocommerce_init', array( $this, 'declare_wc_features_support' ) );
		}

		/**
		 * Main plugin Instance
		 *
		 * @return YITH_COG Main instance
		 */
		public static function instance() {

			$self = __CLASS__ . ( class_exists( __CLASS__ . '_Premium' ) ? '_Premium' : '' );

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

			return $self::$instance;
		}

		/**
		 * Init required classes.
		 */
		public function init_classes() {
			YITH_COG_Orders::get_instance();
			YITH_COG_Ajax::get_instance();
		}

		/**
		 * Add the main classes file
		 * Include the admin and frontend classes
		 *
		 * @param array $main_classes The require classes file path.
		 * @since  1.0
		 */
		protected function require( $main_classes ) {

			foreach ( $main_classes as $section => $classes ) {
				foreach ( $classes as $class ) {
					if ( 'common' === $section || ( 'frontend' === $section && ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) || ( 'admin' === $section && is_admin() ) && file_exists( YITH_COG_PATH . $class ) ) {
						require_once YITH_COG_PATH . $class;
					}
				}
			}

			// Aelia Currency Switcher.
			class_exists( 'WC_Aelia_CurrencySwitcher' ) && require_once YITH_COG_PATH . 'includes/compatibilities/class-yith-cog-AeliaCS-module.php';

			// WPML Currency Switcher.
			global $woocommerce_wpml;
			if ( $woocommerce_wpml ) {
				require_once YITH_COG_PATH . 'includes/compatibilities/class-yith-cog-wpml-module.php';
			}

		}

		/**
		 * Class Initialization
		 *
		 * Instance the admin class
		 *
		 * @since  1.0
		 */
		public function init() {

			if ( is_admin() ) {
				$this->admin = YITH_COG_Admin::get_instance();
			}
		}

		/**
		 * Declare support for WooCommerce features.
		 */
		public function declare_wc_features_support() {
			if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', YITH_COG_INIT, true );
			}
		}

	}

}
