<?php
/**
 * フック登録ローダー
 *
 * @package CF7MasterPro\Core
 */

namespace CF7MasterPro\Core;

defined( 'ABSPATH' ) || exit;

/**
 * actions/filtersを一元管理
 */
class Loader {

	/**
	 * actions配列
	 *
	 * @var array
	 */
	protected $actions = array();

	/**
	 * filters配列
	 *
	 * @var array
	 */
	protected $filters = array();

	/**
	 * action追加
	 *
	 * @param string $hook hook名.
	 * @param object $component インスタンス.
	 * @param string $callback メソッド名.
	 * @param int    $priority 優先度.
	 * @param int    $args 引数数.
	 */
	public function add_action( $hook, $component, $callback, $priority = 10, $args = 1 ) {
		$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $args );
	}

	/**
	 * filter追加
	 *
	 * @param string $hook hook名.
	 * @param object $component インスタンス.
	 * @param string $callback メソッド名.
	 * @param int    $priority 優先度.
	 * @param int    $args 引数数.
	 */
	public function add_filter( $hook, $component, $callback, $priority = 10, $args = 1 ) {
		$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $args );
	}

	/**
	 * 内部追加処理
	 */
	private function add( $hooks, $hook, $component, $callback, $priority, $args ) {
		$hooks[] = compact( 'hook', 'component', 'callback', 'priority', 'args' );
		return $hooks;
	}

	/**
	 * 全フック登録実行
	 */
	public function run() {
		foreach ( $this->filters as $h ) {
			add_filter( $h['hook'], array( $h['component'], $h['callback'] ), $h['priority'], $h['args'] );
		}
		foreach ( $this->actions as $h ) {
			add_action( $h['hook'], array( $h['component'], $h['callback'] ), $h['priority'], $h['args'] );
		}
	}
}
