<?php
/**
 * Smart Offers Main file
 *
 * @package   smart-offers/
 * @since     4.0.0
 * @version   1.1.2
 */

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

if ( ! class_exists( 'SA_Smart_Offers' ) ) {

	class SA_Smart_Offers {

		public function __construct() {
			$this->includes();

			// WPML compatibility
			add_filter( 'sa_so_wpml_get_current_lang_offer_id',  array( $this, 'sa_so_wpml_get_offer_id' ) );

			if ( ! $this->is_wc_gte_30() ) {
				add_action( 'admin_notices', array( $this, 'admin_notice_sa_needs_wc_30_above' ) );
			}

			// Action to declare WooCommerce HPOS i.e. custom order tables compatibility.
			add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );

			// Action to declare WooCommerce Cart & Checkout blocks compatibility.
			add_action( 'before_woocommerce_init', array( $this, 'declare_blocks_compatibility' ) );

			add_action( 'admin_init', array( $this, 'activated' ) );

			add_action( 'wp_loaded', array( $this, 'initialize_so_upgrade' ) );
		}

		/**
		 * To handle WC compatibility related function call from appropriate class
		 *
		 * @param $function_name string
		 * @param $arguments array of arguments passed while calling $function_name
		 * @return result of function call
		 *
		 */
		public function __call( $function_name, $arguments = array() ) {

			if ( ! is_callable( 'SA_WC_Compatibility_4_3', $function_name ) ) return;

			if ( ! empty( $arguments ) ) {
				return call_user_func_array( 'SA_WC_Compatibility_4_3::'.$function_name, $arguments );
			} else {
				return call_user_func( 'SA_WC_Compatibility_4_3::'.$function_name );
			}

		}

		/**
		 * Function to be executed on activation
		 */
		public static function so_activate() {
			include_once( 'includes/admin/class-so-admin-post-type.php' );
			include_once( 'includes/admin/class-so-admin-install.php' );
		}

		/**
		 * WPML compat
		 * @since 3.7.0
		 */
		public function sa_so_wpml_get_offer_id( $offer_id ) {

			if ( is_array( $offer_id ) ) {
				foreach ( $offer_id as $key => $value ) {
					$offer_id[ $key ] = apply_filters( 'wpml_object_id', $value, 'smart_offers', true );
				}
			} else {
				$offer_id = apply_filters( 'wpml_object_id', $offer_id, 'smart_offers', true );
			}

			return $offer_id;

		}

		/**
		 * Function to show admin notice that Smart Offers works with WC 3.0+
		 */
		public function admin_notice_sa_needs_wc_30_above() {
			?>
			<div class="updated error">
				<p><?php
					echo sprintf(__( '%s Smart Offers is active but it will only work with WooCommerce 3.0+. %s.', 'smart-offers' ), '<strong>' . __( 'Important', 'smart-offers' ) . ':</strong>', '<a href="'.admin_url('plugins.php?plugin_status=upgrade').'" target="_blank" >' . __( 'Please update WooCommerce to the latest version', 'smart-offers' ) . '</a>' );
				?></p>
			</div>
			<?php
		}

		/**
		 * Function to declare WooCommerce HPOS compatibility
		 * @since 4.5.0
		 */
		public function declare_hpos_compatibility() {
			if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'smart-offers/smart-offers.php', true );
			}
		}

		/**
		 * Function to declare WooCommerce Cart & Checkout blocks incompatibility.
		 * @since 4.6.0
		 */
		public function declare_blocks_compatibility() {
			if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', 'smart-offers/smart-offers.php', false );
			}
		}

		public function activated() {
			$prefix = 'smart_offers';
			$is_check = get_option( $prefix . '_check_update', 'no' );
			if ( $is_check === 'no' ) {
				$response = wp_remote_get( 'https://www.storeapps.org/wp-admin/admin-ajax.php?action=check_update&plugin=so' );
				update_option( $prefix . '_check_update', 'yes' );
			}
		}

		/*
		 * Include class files
		 */
		public function includes() {
			global $sa_smart_offers;

			include_once( 'includes/compat/class-sa-wc-compatibility-2-5.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-2-6.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-0.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-1.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-2.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-3.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-4.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-5.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-6.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-7.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-8.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-3-9.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-4-0.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-4-1.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-4-2.php' );
			include_once( 'includes/compat/class-sa-wc-compatibility-4-3.php' );

			if ( ! $sa_smart_offers instanceof SA_Smart_Offers ) {
				$sa_smart_offers = $this;
			}

			include_once 'includes/so-functions.php';

			// Post type should be registered from backend and frontend
			include( 'includes/admin/class-so-admin-post-type.php' );

			if ( is_admin() ) {
				include_once( 'includes/admin/class-so-admin-ready-offer-designs.php' );
				include_once 'includes/admin/class-so-admin-welcome.php' ;
				include( 'includes/admin/class-so-admin-pointers.php' );
				include_once( 'includes/admin/class-so-admin-offer.php' );
				include_once( 'includes/admin/class-so-admin-save-offer.php' );
				include_once( 'includes/admin/class-so-admin-offers.php' );
				// Only include dashboard widget if allowed to view WC reports.
				if ( current_user_can( 'view_woocommerce_reports' ) ) {
					include_once( 'includes/admin/class-so-admin-dashboard-widget.php' );
				}
				include_once( 'includes/admin/class-so-admin-order-contribution.php' );
				include_once( 'includes/admin/class-so-admin-notifications.php' );
				include_once( 'includes/admin/class-so-privacy.php' );
				include_once( 'includes/admin/deactivation-survey/class-so-admin-deactivation.php' );
				// Settings file included when adding submenu pages
			}

			if ( ! is_admin() || defined('DOING_AJAX') ) {
				include_once( 'includes/frontend/class-so-shortcodes.php' );
			}

			// In file class-so-init.php & class-so-offer.php, some stats are modified based on order statuses
			// and order statuses can be changed from admin side also, hence kept open for both admin & frontend
			include_once( 'includes/frontend/class-so-session-handler.php' );
			include_once( 'includes/frontend/class-so-offer.php' );
			include_once( 'includes/frontend/class-so-offers.php' );
			include_once( 'includes/frontend/class-so-init.php' );

			// For DB updates.
			include_once( 'includes/class-so-db-update.php' );

			if ( ! function_exists( 'is_plugin_active' ) ) {
				$abspath = trailingslashit( ABSPATH );
				require_once ABSPATH  . 'wp-admin/includes/plugin.php';
			}
			if ( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ) {
				include_once( 'includes/compat/class-so-subscription.php' );
			}
			if ( is_plugin_active( 'woocommerce-aelia-currencyswitcher/woocommerce-aelia-currencyswitcher.php' ) ) {
				include_once( 'includes/compat/multi-currency-compat/class-so-aelia-cs-compatibility.php' );
			}
		}

		/**
		 * Function to fetch plugin's data
		 */
		public static function get_smart_offers_plugin_data() {
			if ( ! function_exists( 'get_plugin_data' ) ) {
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
			}

			return get_plugin_data( SO_PLUGIN_FILE );
		}

		/**
		 * Find latest StoreApps Upgrade file
		 * @return string classname
		 */
		public function get_latest_upgrade_class() {
			$available_classes = get_declared_classes();
			$available_upgrade_classes = array_filter( $available_classes, function ( $class_name ) {
																				return strpos( $class_name, 'StoreApps_Upgrade_' ) === 0;
																			} );
			$latest_class = 'StoreApps_Upgrade_4_0';
			$latest_version = 0;
			foreach ( $available_upgrade_classes as $class ) {
				$exploded = explode( '_', $class );
				$get_numbers = array_filter( $exploded, function ( $value ) {
															return is_numeric( $value );
														} );
				$version = implode( '.', $get_numbers );
				if ( version_compare( $version, $latest_version, '>' ) ) {
					$latest_version = $version;
					$latest_class = $class;
				}
			}

			return $latest_class;
		}

		/*
		 * Function to initialize StoreApps Upgrade class
		 */
		public function initialize_so_upgrade() {
			$latest_upgrade_class = $this->get_latest_upgrade_class();

			$sku = 'so';
			$prefix = 'smart_offers';
			$plugin_name = 'Smart Offers';
			$documentation_link = 'https://www.storeapps.org/knowledgebase_category/smart-offers/';
			$GLOBALS['so_upgrader'] = new $latest_upgrade_class( SO_PLUGIN_FILE, $sku, $prefix, $plugin_name, 'smart-offers', $documentation_link );
		}

	}// End of class SA_Smart_Offers

} // End class exists check
