<?php
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://wpgenie.org
 * @since      1.0.0
 *
 * @package    Mysterx
 * @subpackage Mysterx/includes
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}
/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Mysterx
 * @subpackage Mysterx/includes
 * @author     wpgenie <info@wpgenie.org>
 */
class Mysterx {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Mysterx_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Shortcodes instance
	 *
	 * @var mixed
	 */
	public $shortcodes;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {

		if ( defined( 'MYSTERX_VERSION' ) ) {

			$this->version = MYSTERX_VERSION;

		} else {

			$this->version = '1.0.0';
		}

		$this->plugin_name = 'mysterx';

		$this->load_dependencies();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Mysterx_Loader. Orchestrates the hooks of the plugin.
	 * - Mysterx_i18n. Defines internationalization functionality.
	 * - Mysterx_Admin. Defines all hooks for the admin area.
	 * - Mysterx_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'includes/class-mysterx-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */

		require_once plugin_dir_path( __DIR__ ) . 'includes/mysterx-functions.php';

		require_once plugin_dir_path( __DIR__ ) . 'includes/mysterx-template-functions.php';

		require_once plugin_dir_path( __DIR__ ) . 'includes/class-mysterx-product.php';

		require_once plugin_dir_path( __DIR__ ) . 'public/class-mysterx-shortcodes.php';
		require_once plugin_dir_path( __DIR__ ) . 'public/class-mysterx-shortcode.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'admin/class-mysterx-admin.php';
		/**
		 * The class responsible for defining all actions that occur in the settings.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'admin/class-mysterx-settings.php';
		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'public/class-mysterx-public.php';

		$this->loader = new Mysterx_Loader();

		$this->shortcodes = new Mysterx_Shortcodes();
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {

		$plugin_admin = new Mysterx_Admin( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'init', $plugin_admin, 'register_reward_order_status' );
		$this->loader->add_action( 'wc_order_statuses', $plugin_admin, 'add_reward_to_order_statuses' );

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		$this->loader->add_filter( 'woocommerce_product_class', $plugin_admin, 'mysterx_product_class', 10, 2 );

		$this->loader->add_action( 'product_type_selector', $plugin_admin, 'add_to_product_type_selector' );
		$this->loader->add_filter( 'woocommerce_product_data_tabs', $plugin_admin, 'product_write_panel_tab', 99 );
		$this->loader->add_action( 'woocommerce_product_data_panels', $plugin_admin, 'product_write_panel' );

		$this->loader->add_action( 'woocommerce_process_product_meta', $plugin_admin, 'product_save_data', 80, 2 );
		$this->loader->add_action( 'woocommerce_order_status_processing', $plugin_admin, 'order', 10, 1 );
		$this->loader->add_action( 'woocommerce_order_status_completed', $plugin_admin, 'order', 10, 1 );
		$this->loader->add_action( 'woocommerce_order_status_cancelled', $plugin_admin, 'order_canceled', 10, 1 );
		$this->loader->add_action( 'woocommerce_order_status_refunded', $plugin_admin, 'order_canceled', 10, 1 );
		$this->loader->add_action( 'woocommerce_order_status_cancelled', $plugin_admin, 'order_failed', 10, 1 );
		$this->loader->add_action( 'woocommerce_order_status_failed', $plugin_admin, 'order_failed', 10, 1 );

		$this->loader->add_action( 'admin_menu', $plugin_admin, 'mysterx_admin_menu' );
		$this->loader->add_filter( 'parent_file', $plugin_admin, 'mysterx_parent_file', 999 );
		$this->loader->add_filter( 'submenu_file', $plugin_admin, 'mysterx_submenu_file', 999 );
		$this->loader->add_filter( 'get_edit_post_link', $plugin_admin, 'mysterx_add_menu_param', 10, 3 );

		$this->loader->add_filter( 'woocommerce_product_get_virtual', $plugin_admin, 'mysterx_force_virtual_property', 10, 2 );
		$this->loader->add_filter( 'woocommerce_product_get_downloadable', $plugin_admin, 'mysterx_force_downloadable_property', 10, 2 );

		$this->loader->add_action( 'current_screen', $plugin_admin, 'mysterx_change_admin_title_contextually' );
		$this->loader->add_filter( 'get_edit_post_link', $plugin_admin, 'mysterx_add_menu_param', 10, 3 );
		$this->loader->add_filter( 'woocommerce_payment_complete_reduce_order_stock_statuses', $plugin_admin, 'add_reward_status_to_stock_reduction' );

		$this->loader->add_filter( 'woocommerce_email_classes', $plugin_admin, 'add_reward_email_to_woocommerce' );
		$this->loader->add_action( 'woocommerce_order_status_reward', $plugin_admin, 'trigger_reward_status_email', 10, 2 );
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks() {

		$plugin_public = new Mysterx_Public( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

		$this->loader->add_filter( 'woocommerce_locate_template', $plugin_public, 'woocommerce_locate_template', 10, 3 );

		$this->loader->add_filter( 'init', $plugin_public, 'add_my_account_endpoint', 20 );
		$this->loader->add_filter( 'woocommerce_account_menu_items', $plugin_public, 'add_my_account_tab', 12, 5 );
		$this->loader->add_filter( 'woocommerce_account_mysterx-wins_endpoint', $plugin_public, 'my_account_mysterx_win_content', 12, 5 );
		$this->loader->add_filter( 'woocommerce_account_mysterx_endpoint', $plugin_public, 'my_account_mysterx_content', 12, 5 );
		$this->loader->add_filter( 'woocommerce_loop_add_to_cart_args', $plugin_public, 'woocommerce_loop_add_to_cart_args', 12, 2 );
		$this->loader->add_filter( 'wp_ajax_wc_open_box', $plugin_public, 'wc_open_box_ajax' );
		$this->loader->add_filter( 'woocommerce_product_add_to_cart_url', $plugin_public, 'woocommerce_product_add_to_cart_url', 12, 2 );
		$this->loader->add_filter( 'woocommerce_is_purchasable', $plugin_public, 'woocommerce_is_purchasable', 12, 2 );
		$this->loader->add_filter( 'woocommerce_loop_add_to_cart_link', $plugin_public, 'woocommerce_loop_add_to_cart_link', 12, 2 );
		$this->loader->add_filter( 'woocommerce_loop_product_link', $plugin_public, 'woocommerce_loop_product_link', 12, 2 );
		$this->loader->add_action( 'woocommerce_email_after_order_table', $plugin_public, 'add_open_box_button_to_order', 10, 4 );
		$this->loader->add_action( 'woocommerce_order_details_after_order_table', $plugin_public, 'add_open_box_button_to_order', 10, 1 );

		add_action( 'init', array( 'Mysterx_Shortcodes', 'init' ) );
		add_action( 'woocommerce_after_shop_loop_item', 'mysterx_print_rewards_dataset', 20 );
		add_action( 'woocommerce_single_product_summary', 'mysterx_print_rewards_dataset', 20 );
		add_action( 'woocommerce_after_shop_loop_item', 'mysterx_box_opened_at', 20 );
		remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
		add_action( 'woocommerce_shop_loop_item_title', 'mysterx_win_loop_product_title', 10 );
		add_action( 'woocommerce_mysterx_add_to_cart', 'woocommerce_mysterx_add_to_cart_template' );
		add_action( 'woocommerce_single_product_summary', 'woocommerce_mysterx_single_product_info' );

		// Rewards List Position.
		$rewards_position = Mysterx_Settings::get_option( 'rewards_list_position', 'woocommerce_single_product_summary' );
		add_action( $rewards_position, 'woocommerce_mysterx_single_product_rewards', 35 );

		add_action( 'wp_footer', 'woocommerce_mysterx_opening_modal' );
		add_action( 'wp_footer', 'woocommerce_mysterx_win_modal' );
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Mysterx_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}
}
