<?php
/**
 * Hook loader for VMPlayer.
 *
 * Collects all action and filter registrations from across the plugin
 * and registers them with WordPress in a single batch via run().
 *
 * @package VMPlayer
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class VMPlayer_Loader
 *
 * Hook registration helper.
 */
class VMPlayer_Loader {

    /**
     * Registered actions.
     *
     * @var array
     */
    protected $actions = array();

    /**
     * Registered filters.
     *
     * @var array
     */
    protected $filters = array();

    /**
     * Add a WordPress action.
     *
     * @param string $hook          Hook name.
     * @param object $component     Object instance containing the callback.
     * @param string $callback      Method name on the component.
     * @param int    $priority      Optional. Default 10.
     * @param int    $accepted_args Optional. 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 WordPress filter.
     *
     * @param string $hook          Hook name.
     * @param object $component     Object instance containing the callback.
     * @param string $callback      Method name on the component.
     * @param int    $priority      Optional. Default 10.
     * @param int    $accepted_args Optional. 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 );
    }

    /**
     * Internal helper. Push a hook record onto a collection.
     *
     * @param array  $hooks         Collection.
     * @param string $hook          Hook name.
     * @param object $component     Component instance.
     * @param string $callback      Callback method.
     * @param int    $priority      Priority.
     * @param int    $accepted_args Accepted args.
     * @return array
     */
    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 all queued hooks 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']
            );
        }
    }
}
