<?php
/**
 * Register all actions and filters for the plugin.
 *
 * Maintains a list of all hooks that are registered throughout the plugin,
 * and registers them with the WordPress API. Call the run() method to
 * execute the list of actions and filters.
 *
 * @package    Cloudi7_ID_Card_Generator
 * @subpackage Cloudi7_ID_Card_Generator/includes
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Cloudi7_ICG_Loader
 *
 * Collects and registers WordPress actions and filters.
 * Components (Admin, Public, etc.) add their hooks via this loader
 * rather than calling add_action / add_filter directly.
 */
class Cloudi7_ICG_Loader {

	/**
	 * The array of actions registered with WordPress.
	 *
	 * @var array $actions
	 */
	protected $actions = array();

	/**
	 * The array of filters registered with WordPress.
	 *
	 * @var array $filters
	 */
	protected $filters = array();

	/**
	 * The array of shortcodes registered with WordPress.
	 *
	 * @var array $shortcodes
	 */
	protected $shortcodes = array();

	/**
	 * Add a new action to the collection to be registered with WordPress.
	 *
	 * @param string $hook          The name of the WordPress action.
	 * @param object $component     A reference to the instance of the class.
	 * @param string $callback      The name of the method on the $component.
	 * @param int    $priority      Optional. Priority. Default 10.
	 * @param int    $accepted_args Optional. Number of args. Default 1.
	 * @return void
	 */
	public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
		$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
	}

	/**
	 * Add a new filter to the collection to be registered with WordPress.
	 *
	 * @param string $hook          The name of the WordPress filter.
	 * @param object $component     A reference to the instance of the class.
	 * @param string $callback      The name of the method on the $component.
	 * @param int    $priority      Optional. Priority. Default 10.
	 * @param int    $accepted_args Optional. Number of args. Default 1.
	 * @return void
	 */
	public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
		$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
	}

	/**
	 * Add a new shortcode to the collection to be registered with WordPress.
	 *
	 * @param string $tag       The name of the shortcode.
	 * @param object $component A reference to the instance of the class.
	 * @param string $callback  The name of the method on the $component.
	 * @return void
	 */
	public function add_shortcode( $tag, $component, $callback ) {
		$this->shortcodes[] = array(
			'tag'       => $tag,
			'component' => $component,
			'callback'  => $callback,
		);
	}

	/**
	 * A utility function to register actions and filters into a single collection.
	 *
	 * @param array  $hooks         The collection of hooks (actions or filters).
	 * @param string $hook          The name of the WordPress hook.
	 * @param object $component     A reference to the instance of the class.
	 * @param string $callback      The name of the method on the $component.
	 * @param int    $priority      Priority value.
	 * @param int    $accepted_args Number of accepted arguments.
	 * @return array The updated collection of hooks.
	 */
	private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
		$hooks[] = array(
			'hook'          => $hook,
			'component'     => $component,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);

		return $hooks;
	}

	/**
	 * Register the filters and actions with WordPress.
	 *
	 * @return void
	 */
	public function run() {
		foreach ( $this->filters as $hook ) {
			add_filter(
				$hook['hook'],
				array( $hook['component'], $hook['callback'] ),
				$hook['priority'],
				$hook['accepted_args']
			);
		}

		foreach ( $this->actions as $hook ) {
			add_action(
				$hook['hook'],
				array( $hook['component'], $hook['callback'] ),
				$hook['priority'],
				$hook['accepted_args']
			);
		}

		foreach ( $this->shortcodes as $shortcode ) {
			\add_shortcode( $shortcode['tag'], array( $shortcode['component'], $shortcode['callback'] ) );
		}
	}
}
