<?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://www.eurostocks.com
 * @since      1.0.0
 *
 * @package    Eurostocks
 * @subpackage Eurostocks/includes
 */

/**
 * 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    Eurostocks
 * @subpackage Eurostocks/includes
 * @author     EuroStocks <info@eurostocks.com>
 */
class Eurostocks {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Eurostocks_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;

	/**
	 * 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( 'EUROSTOCKS_VERSION' ) ) {
			$this->version = EUROSTOCKS_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'eurostocks';

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

	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Eurostocks_Loader. Orchestrates the hooks of the plugin.
	 * - Eurostocks_i18n. Defines internationalization functionality.
	 * - Eurostocks_Admin. Defines all hooks for the admin area.
	 * - Eurostocks_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() {
		/**
		 * Language helper functions.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/language-helper.php';

		/**
		 * Anti spam settings helper.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-anti-spam-settings.php';

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

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-i18n.php';


		if (version_compare(PHP_VERSION, '7.2.0') >= 0) {
			/**
			 * Import PDF functionality.
			 */
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dompdf/autoload.inc.php';
		}
		/**
		 * The class responsible for doing requests to the EuroStocks API.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-request.php';

		/**
		 * The class responsible for checking the robots.txt file.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-robot-txt-parser.php';

		/**
		 * The class responsible for checking if a visitor is a scraper.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-botchecker.php';

		/**
		 * Email verification helper.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-email-verification.php';

		/**
		 * The class responsible for EuroStocks Analytics.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-eurostocks-analytics.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-eurostocks-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-eurostocks-public.php';


		$this->loader = new Eurostocks_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Eurostocks_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {

		$plugin_i18n = new Eurostocks_i18n();

		$this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' );

	}

	/**
	 * 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 Eurostocks_Admin( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_filter( 'cron_schedules', $plugin_admin, 'eurostocks_add_cron_interval' );

		$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_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'options_update');

		$this->loader->add_action( 'eurostocks_clean_ip_history', $plugin_admin, 'eurostocks_clean_ip_history_now' );


		$this->loader->add_action('wp_ajax_validate_eurostocks_key', $plugin_admin, 'validate_eurostocks_key');
		$this->loader->add_action('wp_ajax_get_models', $plugin_admin, 'get_models');
		$this->loader->add_action('wp_ajax_get_eurostocks_models_ajax', $plugin_admin, 'get_eurostocks_models_ajax');
		$this->loader->add_action('wp_ajax_eurostocks_block_ip', $plugin_admin, 'block_ip_address_admin');
		$this->loader->add_action('wp_ajax_eurostocks_remove_ip_block', $plugin_admin, 'remove_ip_block');
		$this->loader->add_action('wp_ajax_eurostocks_remove_verification_entry', $plugin_admin, 'remove_verification_entry');
		$this->loader->add_action('wp_ajax_eurostocks_fetch_api_statistics', $plugin_admin, 'fetch_api_statistics');
		$this->loader->add_action('admin_post_eurostocks_export_analytics', $plugin_admin, 'export_analytics_csv');

		$this->loader->add_action('eurostocks_update_model_scheduler', $plugin_admin, 'eurostocks_update_model_scheduler_handle');
		$this->loader->add_action('eurostocks_update_model_job', $plugin_admin, 'eurostocks_update_model_job_handle', 10, 1);

		$this->loader->add_action( 'init', $plugin_admin, 'eurostocks_update_db_check' );

		$this->loader->add_action( 'wp_ajax_check_robots_txt', $plugin_admin, 'check_robots_txt' );

		

	}

	/**
	 * 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 Eurostocks_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_action( 'init', $plugin_public, 'maybe_handle_email_verification' );

		$this->loader->add_shortcode( 'eurostocks', $plugin_public, 'eurostocks_shortcode' );
		$this->loader->add_shortcode( 'eurostocks_details', $plugin_public, 'eurostocks_details_shortcode');
		$this->loader->add_shortcode( 'eurostocks_search', $plugin_public, 'eurostocks_search_shortcode');

		$this->loader->add_action( 'wp', $plugin_public, 'eurostocks_details_pdf', 10, 1);

		$this->loader->add_action('wp_ajax_get_vehicles_details', $plugin_public, 'get_vehicles_details');
		$this->loader->add_action('wp_ajax_nopriv_get_vehicles_details', $plugin_public, 'get_vehicles_details');

		$this->loader->add_action( 'wp_ajax_eurostocks_send_form', $plugin_public, 'eurostocks_send_form');
		$this->loader->add_action( 'wp_ajax_nopriv_eurostocks_send_form', $plugin_public, 'eurostocks_send_form');

		$this->loader->add_action( 'wp_ajax_eurostocks_submit_verification_email', $plugin_public, 'submit_verification_email' );
		$this->loader->add_action( 'wp_ajax_nopriv_eurostocks_submit_verification_email', $plugin_public, 'submit_verification_email' );

		$this->loader->add_action( 'wp_ajax_eurostocks_get_models_for_make', $plugin_public, 'eurostocks_get_models_for_make');
		$this->loader->add_action( 'wp_ajax_nopriv_eurostocks_get_models_for_make', $plugin_public, 'eurostocks_get_models_for_make');

		$this->loader->add_action( 'init', $plugin_public, 'custom_rewrite_tag', 10, 0);
		$this->loader->add_action( 'init', $plugin_public, 'custom_rewrite_rule', 10, 0);

	}

	/**
	 * 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    Eurostocks_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;
	}

}
