<?php
/**
 * Register all actions and filters for the plugin.
 *
 * @package BoostBot
 * @since   1.0.0
 */

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

/**
 * Class BoostBot_Loader
 *
 * Maintains a list of all hooks that are registered throughout
 * the plugin, and registers them with the WordPress API.
 *
 * @since 1.0.0
 */
class BoostBot_Loader {

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

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

    /**
     * Add a new action to the collection.
     *
     * @param string $hook          The name of the WordPress action.
     * @param object $component     A reference to the instance of the object.
     * @param string $callback      The name of the function to call.
     * @param int    $priority      Optional. The priority. Default 10.
     * @param int    $accepted_args Optional. Number of accepted arguments. 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.
     *
     * @param string $hook          The name of the WordPress filter.
     * @param object $component     A reference to the instance of the object.
     * @param string $callback      The name of the function to call.
     * @param int    $priority      Optional. The priority. Default 10.
     * @param int    $accepted_args Optional. Number of accepted arguments. 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 hook to the collection.
     *
     * @param array  $hooks         The collection of hooks.
     * @param string $hook          The name of the hook.
     * @param object $component     A reference to the instance of the object.
     * @param string $callback      The name of the function to call.
     * @param int    $priority      The priority.
     * @param int    $accepted_args Number of accepted arguments.
     * @return array The modified 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;
    }

    /**
     * Remove an action from the collection.
     *
     * @param string $hook      The name of the WordPress action.
     * @param object $component A reference to the instance of the object.
     * @param string $callback  The name of the function.
     * @param int    $priority  The priority. Default 10.
     * @return bool Whether the action was removed.
     */
    public function remove_action( $hook, $component, $callback, $priority = 10 ) {
        return $this->remove( 'actions', $hook, $component, $callback, $priority );
    }

    /**
     * Remove a filter from the collection.
     *
     * @param string $hook      The name of the WordPress filter.
     * @param object $component A reference to the instance of the object.
     * @param string $callback  The name of the function.
     * @param int    $priority  The priority. Default 10.
     * @return bool Whether the filter was removed.
     */
    public function remove_filter( $hook, $component, $callback, $priority = 10 ) {
        return $this->remove( 'filters', $hook, $component, $callback, $priority );
    }

    /**
     * Remove a hook from the collection.
     *
     * @param string $type      Either 'actions' or 'filters'.
     * @param string $hook      The name of the hook.
     * @param object $component A reference to the instance of the object.
     * @param string $callback  The name of the function.
     * @param int    $priority  The priority.
     * @return bool Whether the hook was removed.
     */
    private function remove( $type, $hook, $component, $callback, $priority ) {
        $hooks = &$this->$type;

        foreach ( $hooks as $index => $registered_hook ) {
            if (
                $registered_hook['hook'] === $hook &&
                $registered_hook['component'] === $component &&
                $registered_hook['callback'] === $callback &&
                $registered_hook['priority'] === $priority
            ) {
                unset( $hooks[ $index ] );
                return true;
            }
        }

        return false;
    }

    /**
     * Register the actions and filters with WordPress.
     *
     * @return void
     */
    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.
     *
     * @return array
     */
    public function get_actions() {
        return $this->actions;
    }

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

    /**
     * Get count of all registered hooks.
     *
     * @return array
     */
    public function get_hook_counts() {
        return array(
            'actions' => count( $this->actions ),
            'filters' => count( $this->filters ),
            'total'   => count( $this->actions ) + count( $this->filters ),
        );
    }
}










