<?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.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/includes
 */

namespace Popper;

/**
 * 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    Popper
 * @subpackage Popper/includes
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class Plugin {

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

	/**
	 * The main plugin file.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $entry_point The entry point of the plugin.
	 */
	protected $entry_point;

	/**
	 * 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.
	 *
	 * @param string $entry_point The entry point of this plugin.
	 * @since    1.0.0
	 */
	public function __construct( $entry_point ) {
		$this->version = get_file_data( $entry_point, array( 'Version' ) )[0];
		$this->entry_point = $entry_point;
		$this->plugin_name = 'popper';

		$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:
	 *
	 * - Loader. Orchestrates the hooks of the plugin.
	 * - I18n. Defines internationalization functionality.
	 * - Admin. Defines all hooks for the admin area.
	 * - 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() {
		$this->loader = new Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Popper_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() {

		$i18n = new I18n();

		$this->loader->add_action( 'plugins_loaded', $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() {

		$register = new Register( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'init', $register, 'register_cpt' );
		$this->loader->add_action( 'init', $register, 'register_meta' );
		$this->loader->add_action( 'init', $register, 'register_settings' );
		$this->loader->add_filter( 'block_categories_all', $register, 'register_block_category' );

		$admin = new Admin( $this->get_plugin_name(), $this->get_version(), $this->entry_point );

		$this->loader->add_action( 'admin_init', $admin, 'register_scripts' );
		$this->loader->add_action( 'admin_menu', $admin, 'menu' );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'popper_positions' );
		$this->loader->add_action( 'load-edit.php', $admin, 'redirect_post_type' );
		$this->loader->add_filter( 'popper_logging_should_we_prune', $admin, 'activate_pruning' );
		$this->loader->add_filter( 'wp_link_query_args', $admin, 'wp_link_query_args' );
		$this->loader->add_filter( 'rest_post_search_query', $admin, 'wp_link_query_args', 10, 2 );
		$this->loader->add_filter( 'post_type_link', $admin, 'post_type_link', 10, 2 );
		$this->loader->add_filter( 'wp_theme_json_data_default', $admin, 'remove_classics' );

		$rest = new Rest();

		$this->loader->add_action( 'rest_api_init', $rest, 'register_routes' );

		$log = new Logging();

		$this->loader->add_action( 'init', $log, 'register_post_type' );
		$this->loader->add_action( 'init', $log, 'register_taxonomy' );
		$this->loader->add_action( 'popper_logging_prune_routine', $log, 'prune_logs' );
	}

	/**
	 * 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() {
		$frontend = new Frontend( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'template_redirect', $frontend, 'popper_matcher' );
		$this->loader->add_action( 'wp_body_open', $frontend, 'get_popups' );
		$this->loader->add_filter( 'render_block', $frontend, 'add_data_attribute', 10, 2 );

		$submission = new Submission( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'wp_ajax_popper', $submission, 'handle_form' );
		$this->loader->add_action( 'wp_ajax_nopriv_popper', $submission, 'handle_form' );

		$email = new Actions\Email();
		$subscribe = new Actions\Subscribe();
	}

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

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