<?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 dashboard.
 *
 * @link       https://nextgenthemes.com
 * @since      1.0.0
 *
 * @package    Advanced_Responsive_Video_Embedder
 * @subpackage Advanced_Responsive_Video_Embedder/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, dashboard-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    Advanced_Responsive_Video_Embedder
 * @subpackage Advanced_Responsive_Video_Embedder/includes
 * @author     Nicolas Jonas
 */
class Advanced_Responsive_Video_Embedder {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Advanced_Responsive_Video_Embedder_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_slug    The string used to uniquely identify this plugin.
	 */
	protected $plugin_slug;

	/**
	 * 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 Dashboard and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {

		$this->plugin_slug = 'advanced-responsive-video-embedder';
		$this->version = '7.0.6';

		$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:
	 *
	 * - Advanced_Responsive_Video_Embedder_Loader. Orchestrates the hooks of the plugin.
	 * - Advanced_Responsive_Video_Embedder_i18n. Defines internationalization functionality.
	 * - Advanced_Responsive_Video_Embedder_Admin. Defines all hooks for the dashboard.
	 * - Advanced_Responsive_Video_Embedder_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( dirname( __FILE__ ) ) . 'includes/class-arve-loader.php';

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

		/**
		 * The class for admin and public.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-arve-shared.php';

		/**
		 * The class responsible for defining all actions that occur in the Dashboard.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-arve-admin.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-arve-admin-notice-factory.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-arve-public.php';

		$this->loader = new Advanced_Responsive_Video_Embedder_Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Advanced_Responsive_Video_Embedder_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 Advanced_Responsive_Video_Embedder_i18n();
		$plugin_i18n->set_domain( $this->get_plugin_slug() );

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

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

		$plugin_admin = new Advanced_Responsive_Video_Embedder_Admin( $this->get_plugin_slug(), $this->get_version() );

		// Only loaded on admin pages with editor
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles', 99 );
		$this->loader->add_filter( 'mce_css', $plugin_admin, 'mce_css' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		$this->loader->add_action( 'enqueue_shortcode_ui', $plugin_admin, 'enqueue_shortcode_ui_scripts' );

		$this->loader->add_action( 'admin_footer', $plugin_admin, 'print_dialog' );

		// Add the options page and menu item.
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );

		$this->loader->add_action( 'admin_notices', $plugin_admin, 'pro_notice' );
		$this->loader->add_action( 'wp_ajax_arve_ajax_dismiss_pro_notice', $plugin_admin, 'arve_ajax_dismiss_pro_notice' );

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

		// Add an action link pointing to the options page.
		$plugin_basename = plugin_basename( plugin_dir_path( realpath( dirname( __FILE__ ) ) ) . $this->plugin_slug . '.php' );

		$this->loader->add_filter( 'plugin_action_links_' . $plugin_basename, $plugin_admin, 'add_action_links' );

		$this->loader->add_action( 'wp_dashboard_setup', $plugin_admin, 'add_dashboard_widget' );

		$this->loader->add_action( 'admin_init', $plugin_admin, 'register_settings' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'register_settings_debug', 99 );

		$this->loader->add_action( 'media_buttons', $plugin_admin, 'add_media_button', 11 );

		$msg = sprintf(
			__( 'WTF %s', $this->plugin_slug ),
			'https://nextgenthemes.com/plugins/advanced-responsive-video-embedder-pro/documentation/installing-and-license-management/'
		);
		new Advanced_Responsive_Video_Embedder_Admin_Notice_Factory( 'wtf', $msg );

		if( is_file( WP_PLUGIN_DIR . '/arve-pro/arve-pro.php' ) && ( ! defined( 'ARVE_PRO_VERSION' ) || version_compare( ARVE_PRO_VERSION, ARVE_PRO_VERSION_REQUIRED, '<' ) ) ) {

			$msg = sprintf(
				__( 'Your ARVE Pro Addon is outdated, please <a href="%s">look here</a> for manual updates if your auto-updates do not work or are disabled.', $this->plugin_slug ),
				'https://nextgenthemes.com/plugins/advanced-responsive-video-embedder-pro/documentation/installing-and-license-management/'
			);
			new Advanced_Responsive_Video_Embedder_Admin_Notice_Factory(	'arve-pro-outdated', $msg, false );
		}
	}

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

		$this->loader->add_action( 'init', $plugin_public, 'oembed_remove_providers', 99 );
		$this->loader->add_action( 'init', $plugin_public, 'create_shortcodes', 99 );
		$this->loader->add_action( 'init', $plugin_public, 'create_url_handlers', 99 );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		#$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'register_scripts', 0 );

		$this->loader->add_action( 'wp_head', $plugin_public, 'print_styles' );

		$this->loader->add_action( 'arve_output', $plugin_public, 'normal_output', 10, 2 );

		$this->loader->add_action( 'wp_video_shortcode_override', $plugin_public, 'wp_video_shortcode_override', 10, 4 );

		add_filter( 'widget_text', 'do_shortcode' );
	}

	/**
	 * 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_slug() {
		return $this->plugin_slug;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Advanced_Responsive_Video_Embedder_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;
	}
}
