<?php

/**
 * Plugin Name:       Shop Manager for Woocommerce
 * Plugin URI:        https://oxilab.dev/shop-manager/
 * Description:       This is woocommerce Shop Manager plugin. You can manager everything from frontend.
 * Version:           1.3.1
 * Author:            Oxilab
 * Author URI:        https://oxilab.dev
 * Text Domain:       shop-manager
 * License:           GPLv2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 *
 * @package oxilab shop manager.
 */

if (! defined('ABSPATH')) {
	wp_die(esc_html__('You can\'t access this page', 'shop-manager'));
}

/**
 * Included Autoload File
 */
require_once __DIR__ . '/vendor/autoload.php';

if (! function_exists('wpkin_sm_v')) {
	function wpkin_sm_v()
	{
		global $wpkin_sm_v;

		if (! isset($wpkin_sm_v)) {
			$wpkin_sm_v = fs_dynamic_init(
				[
					'id'              => '19024',
					'slug'            => 'shop-manager',
					'premium_slug'    => 'shop-manager-pro',
					'type'            => 'plugin',
					'public_key'      => 'pk_d84c93b61f9874523e3b7da8a14a3',
					'is_premium'      => false,
					'has_premium_version' => false,
					'has_addons'      => false,
					'has_paid_plans'  => true,
					'has_affiliation' => 'selected',
					'menu'            => [
						'slug'       => 'shop-manager-settings',
						'first-path' => 'admin.php?page=shop-manager-settings',
						'contact'    => false,
						'support'    => false,
						'pricing'    => false,
					],
				]
			);
		}

		return $wpkin_sm_v;
	}

	wpkin_sm_v();
	do_action('wpkin_sm_v_loaded');
}

/** If class `WPKIN_Shop_Manager` doesn't exists yet. */
if (! class_exists('WPKIN_Shop_Manager')) {

	/**
	 * Set up and initializes the plugin.
	 * Main initiation class
	 *
	 * @since 1.0.0
	 */
	final class WPKIN_Shop_Manager
	{


		/**
		 * Plugin Version
		 */
		const VERSION = '1.3.1';

		/**
		 * Php Version
		 */
		const MIN_PHP_VERSION = '7.4';

		/**
		 * WC Version
		 */
		const MIN_WC_VERSION = '8.0.0';

		/**
		 * WordPress Version
		 */
		const MIN_WP_VERSION = '6.2';

		/**
		 * Class Constractor
		 */
		private function __construct()
		{
			$this->wpkin_define_constance();
			$this->wpkin_sm_load_page_template();
			register_activation_hook(__FILE__, [$this, 'wpkin_activate']);
			register_deactivation_hook(__FILE__, [$this, 'wpkin_deactivate']);
			add_action('plugins_loaded', [$this, 'wpkin_init_plugin']);
			add_action('init', [$this, 'wpkin_i18n']);
			add_action('admin_init', [$this, 'wpkin_activation_redirect']);
			add_filter('plugin_action_links_' . plugin_basename(WPKIN_SM_FILE), [__CLASS__, 'wpkin_sm_action_links']);

			// Declare WooCommerce HPOS (High-Performance Order Storage) compatibility.
			add_action( 'before_woocommerce_init', function () {
				if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
					\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
						'custom_order_tables',
						WPKIN_SM_FILE,
						true
					);
				}
			} );
		}

			// public function wpkin_sm_plugin_update($upgrader_object, $options) {
			//  // If this is an update of our plugin
			//  if ($options['action'] == 'update' && $options['type'] == 'plugin') {
			//      foreach($options['plugins'] as $plugin) {
			//          if ($plugin == plugin_basename(__FILE__)) {
			//              // Do something on update
			//          }
			//      }
			//  }
			// }

		/**
		 * Initilize a singleton instance
		 *
		 * @return /WPKIN_Shop_Manager
		 */
		public static function wpkin_init()
		{

			static $instance = false;

			if (! $instance) {
				$instance = new self();
			}

			return $instance;
		}

		/**
		 * Plugin Constance
		 *
		 * @return void
		 */
		public function wpkin_define_constance()
		{
			define('WPKIN_SM_VERSION', self::VERSION);
			define('WPKIN_SM_FILE', __FILE__);
			define('WPKIN_SM_PATH', plugin_dir_path(__FILE__));
			define('WPKIN_SM_URL', plugins_url('', WPKIN_SM_FILE));
			define('WPKIN_SM_ASSETS', WPKIN_SM_URL . '/assets/');
			define('WPKIN_SM_MINIMUM_PHP_VERSION', self::MIN_PHP_VERSION);
			define('WPKIN_SM_MINIMUM_WC_VERSION', self::MIN_WC_VERSION);
			define('WPKIN_SM_MINIMUM_WP_VERSION', self::MIN_WP_VERSION);
		}

		/**
		 * After Activate Plugin
		 *
		 * Fired by `register_activation_hook` hook.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 *
		 * @access public
		 */
		public function wpkin_activate()
		{
			new WPKIN_SHOP_MANAGER\Installation();
		}

		/**
		 * Load page tempate for frontend dahsboard
		 */
		public function wpkin_sm_load_page_template()
		{
			new \WPKIN_SHOP_MANAGER\Page_Template(WPKIN_SM_FILE);
			\WPKIN_SHOP_MANAGER\Cleanup\Scripts::get_instance();
		}

		/**
		 * Load Textdomain
		 *
		 * Load plugin localization files.
		 *
		 * Fired by `init` action hook.
		 *
		 * @since 1.0.0
		 *
		 * @access public
		 */
		public function wpkin_i18n() {}

		/**
		 * After Deactivate Plugin
		 *
		 * @return void
		 */
		public function wpkin_deactivate()
		{
			flush_rewrite_rules();
		}

		/**
		 * Plugins Loaded
		 *
		 * @return void
		 */
		public function wpkin_init_plugin()
		{
			new WPKIN_SHOP_MANAGER\Modules();
			new WPKIN_SHOP_MANAGER\Assets();
			new WPKIN_SHOP_MANAGER\Frontend();
			new WPKIN_SHOP_MANAGER\Api();
			if (is_admin()) {
				new WPKIN_SHOP_MANAGER\Admin();
			}
		}

		/**
		 *
		 * Redirect to settings page after activation the plugin
		 */
		public function wpkin_activation_redirect()
		{

			if (get_option('wpkin_sm_activation_redirect', false)) {
				delete_option('wpkin_sm_activation_redirect');

				wp_safe_redirect(admin_url('admin.php?page=shop-manager-settings'));
				exit();
			}
		}

		/**
		 * Plugin Page Settings menu
		 *
		 * @param mixed $links .
		 */
		public static function wpkin_sm_action_links($links)
		{

			if (! current_user_can('manage_options')) {
				return $links;
			}

			$links = array_merge(
				[
					sprintf(
						'<a href="%s">%s</a>',
						admin_url('admin.php?page=shop-manager-settings'),
						esc_html__('Settings', 'shop-manager')
					),
				],
				$links
			);

			return $links;
		}
	}
}

/**
 * Initilize the main plugin
 *
 * @return /WPKIN_Shop_Manager
 */
function wpkin_shop_manager()
{

	if (class_exists('WPKIN_Shop_Manager')) {
		return WPKIN_Shop_Manager::wpkin_init();
	}

	return false;
}

/**
 * Kick-off the plugin
 */
wpkin_shop_manager();
