<?php
/**
 * The core plugin class.
 *
 * @package    MaplePress
 * @subpackage MaplePress/includes
 */

class MaplePress {

	/**
	 * The loader that's responsible for maintaining and registering all hooks.
	 *
	 * @var MaplePress_Loader
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @var string
	 */
	protected $plugin_name;

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

	/**
	 * Initialize the class and set its properties.
	 */
	public function __construct() {
		$this->version     = MAPLEPRESS_VERSION;
		$this->plugin_name = 'maplepress';

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

	/**
	 * Load the required dependencies for this plugin.
	 */
	private function load_dependencies() {
		require_once MAPLEPRESS_PLUGIN_DIR . 'includes/class-maplepress-loader.php';
		require_once MAPLEPRESS_PLUGIN_DIR . 'includes/class-maplepress-admin.php';
		require_once MAPLEPRESS_PLUGIN_DIR . 'includes/class-maplepress-public.php';
		require_once MAPLEPRESS_PLUGIN_DIR . 'includes/class-maplepress-api-client.php';

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

	/**
	 * Register all hooks related to the admin area functionality.
	 */
	private function define_admin_hooks() {
		$plugin_admin = new MaplePress_Admin( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'register_settings' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
	}

	/**
	 * Register all hooks related to the public-facing functionality.
	 */
	private function define_public_hooks() {
		$plugin_public = new MaplePress_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' );

		// Search integration
		$this->loader->add_action( 'pre_get_posts', $plugin_public, 'intercept_search' );
		$this->loader->add_filter( 'get_search_query', $plugin_public, 'restore_search_query' );
	}

	/**
	 * Run the loader to execute all hooks.
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin.
	 *
	 * @return string
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks.
	 *
	 * @return MaplePress_Loader
	 */
	public function get_loader() {
		return $this->loader;
	}

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