<?php
/**
 * Hook Loader
 *
 * Centralized registration for WordPress actions and filters.
 *
 * @package DonorCore\Utils
 */

namespace DonorCore\Utils;

defined( 'ABSPATH' ) || exit;

/**
 * Hook Loader Class
 */
class Hook_Loader {

	/**
	 * Array of registered actions.
	 *
	 * @var array
	 */
	private $actions = array();

	/**
	 * Array of registered filters.
	 *
	 * @var array
	 */
	private $filters = array();

	/**
	 * Register an action.
	 *
	 * @param string   $hook          The WordPress action hook.
	 * @param object   $component     The object instance.
	 * @param string   $callback      The method name.
	 * @param int      $priority      Hook priority (default 10).
	 * @param int      $accepted_args Number of arguments (default 1).
	 */
	public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
		$this->actions[] = array(
			'hook'          => $hook,
			'component'     => $component,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);
	}

	/**
	 * Register a filter.
	 *
	 * @param string   $hook          The WordPress filter hook.
	 * @param object   $component     The object instance.
	 * @param string   $callback      The method name.
	 * @param int      $priority      Hook priority (default 10).
	 * @param int      $accepted_args Number of arguments (default 1).
	 */
	public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
		$this->filters[] = array(
			'hook'          => $hook,
			'component'     => $component,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);
	}

	/**
	 * Execute all registered hooks with WordPress.
	 */
	public function run() {
		// Register all actions.
		foreach ( $this->actions as $hook ) {
			add_action(
				$hook['hook'],
				array( $hook['component'], $hook['callback'] ),
				$hook['priority'],
				$hook['accepted_args']
			);
		}

		// Register all filters.
		foreach ( $this->filters as $hook ) {
			add_filter(
				$hook['hook'],
				array( $hook['component'], $hook['callback'] ),
				$hook['priority'],
				$hook['accepted_args']
			);
		}
	}

	/**
	 * Get all registered actions (for debugging).
	 *
	 * @return array
	 */
	public function get_actions() {
		return $this->actions;
	}

	/**
	 * Get all registered filters (for debugging).
	 *
	 * @return array
	 */
	public function get_filters() {
		return $this->filters;
	}
}
