<?php
/**
 * Defines the core plugin class.
 *
 * This class includes attributes and functions used both on the
 * public-facing side of the site and in the admin area.
 *
 * @link       https://dcatl.diviaccessories.com/
 * @since      1.0.0
 *
 * @package    WordPress
 * @subpackage display-category-and-taxonomy-list
 */

namespace DCATL\Core;

use DCATL\Public\Action;
use DCATL\Admin\Admin;

/**
 * The core plugin class.
 *
 * Defines internationalization, admin-specific hooks, and public-facing site hooks.
 *
 * Also maintains the plugin's unique identifier and current version.
 *
 * @since      1.0.0
 * @author Nishan Mazumder <arosh019@gmail.com>
 */
class Manager {

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

	/**
	 * Defines the core functionality of the plugin.
	 *
	 * Sets the plugin name and version for use throughout the plugin.
	 * Loads dependencies, defines the locale, and sets hooks for both
	 * the admin area and the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		$this->plugin_name = DCATL_NAME;
		$this->version     = DCATL_VERSION;

		$this->load_dependencies();
		$this->plugin_action();
	}

	/**
	 * Loads the required dependencies for this plugin.
	 *
	 * Includes the following files that comprise the plugin:
	 *
	 * - Loader: Manages the hooks of the plugin.
	 * - I18n: Provides internationalization functionality.
	 *
	 * Creates 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() {
		// Traits - Singleton.
		require_once DCATL_PATH . 'includes/traits/trait-singleton.php';

		// Manage admin notcie.
		require_once DCATL_PATH . 'includes/class-notice.php';

		// The class responsible for orchestrating the actions and filters of the core plugin.
		require_once DCATL_PATH . 'includes/class-loader.php';

		// The class responsible for defining all actions that occur in the site.
		require_once DCATL_PATH . 'includes/class-action.php';

		// The class responsible for defining all actions that occur in the admin.
		require_once DCATL_PATH . 'admin/class-admin.php';

		// Instance dependency for this class.
		$this->loader = Loader::get_instance();
	}

	/**
	 * Registers all functionality related to hooks within the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function plugin_action() {
		/**
		 * ================================================
		 *                      NOTICE
		 * ================================================
		 */
		$notice = Notice::get_instance();
		// Hook to display admin notices.
		$this->loader->add_action( 'admin_notices', $notice, 'dcatl_display_notices' );
		// Transient notices on install.
		$this->loader->add_action( 'admin_notices', $notice, 'dcatl_notice' );
		// Review rescheduled.
		$this->loader->add_action( 'wp_ajax_dcatl_review_dismiss', $notice, 'dcatl_review_dismiss_action' );
		// Check screen and add footer text.
		$this->loader->add_action( 'current_screen', $notice, 'check_dcatl_page' );

		/**
		 * ================================================
		 *                      Enqueue
		 * ================================================
		 */
		$plugin_action = Action::get_instance();
		$this->loader->add_action( 'plugins_loaded', $plugin_action, 'load_textdomain' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_action, 'enqueue_admin_resources' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_action, 'enqueue_build_resources' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_action, 'register_public_resources', 5 );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_action, 'maybe_enqueue_public_resources', 20 );
		$this->loader->add_action( 'init', $plugin_action, 'register_dcatl_admin_settings', 20 );

		$this->loader->add_filter( 'dcatl_default_settings', $plugin_action, 'get_default_settings' );
		$this->loader->add_filter( 'dcatl_admin_style_settings', $plugin_action, 'admin_style_settings' );

		/**
		 * ================================================
		 *                      Admin
		 * ================================================
		 */
		$admin_action = Admin::get_instance();

		$this->loader->add_action( 'admin_menu', $admin_action, 'add_dcatl_admin_menu' );
		$this->loader->add_action( 'init', $admin_action, 'category_list_image_int', 99 );

		// shortcode.
		add_shortcode( 'dcatl_category_list', array( $admin_action, 'category_list_image_shortcode_func' ) );
		$this->loader->add_action( 'dcatl_render_item', $admin_action, 'render_list_item', 10, 2 );
	}

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

	/**
	 * Provides the plugin's name, uniquely identifying it within WordPress and defining internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

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

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