Source of file class-loader.php

Size: 3,205 Bytes - Last Modified: 2016-01-06T12:33:17-05:00

../src/lib/class-loader.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_add_action
50
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_add_action
515253545556575859
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_get_actions
60616263646566676869707172
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_add_filter
73
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_add_filter
747576777879808182
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_get_filters
8384858687888990919293949596
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
97
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
98
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
99
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
100
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
101
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_componentify with data set #0
102103104105106107108109110
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
111
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
112113
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
114
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
115
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
116117
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
118
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
119
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
120121
Covered by 1 test(s):
  • WpWodify\LoaderTest::test_run
122123124
<?php
/**
 * Loader Class
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Loader
 * @since       File available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace WpWodify;

/**
 * Loader Class
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Loader
 * @since       Class available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Loader {

    /**
     * A list of actions to add.
     *
     * @var array
     */
    protected $actions = array();

    /**
     * A list of filters to add.
     *
     * @var array
     */
    protected $filters = array();

    /**
     * Adds an action to the list of actions.
     *
     * @param string $hook The name of the hook to implement
     * @param mixed $component The class instance which a callback will be called on.
     * @param string $callback Name of the method to call on the component.
     *
     * @return WpWodify\Overlord Returns $this, for object-chaining.
     */
    public function add_action( $hook, $component, $callback ) {
        $this->actions[] = $this->componentify( $hook, $component, $callback );
        return $this;
    }

    /**
     * Gets the actions,
     *
     * @return array The array of actions.
     */
    public function get_actions() {
        return $this->actions;
    }

    /**
     * Adds an filter to the list of filters.
     *
     * @param string $hook The name of the hook to implement
     * @param mixed $component The class instance which a callback will be called on.
     * @param string $callback Name of the method to call on the component.
     *
     * @return WpWodify\Overlord Returns $this, for object-chaining.
     */
    public function add_filter( $hook, $component, $callback ) {
        $this->filters[] = $this->componentify( $hook, $component, $callback );
        return $this;
    }

    /**
     * Gets the filters,
     *
     * @return array The array of filters.
     */
    public function get_filters() {
        return $this->filters;
    }

    /**
     * Takes string arguments, and turns them into an standardized array.
     *
     * @param string $hook the name of a wordpress hook
     * @param mixed $component A class instance.
     * @param string $callback The name of a method to call on the component.
     *
     * @return array The resulting array.
     */
    protected function componentify( $hook, $component, $callback ) {
        $result = array(
            'hook'          => $hook,
            'function'      => array( $component, $callback ),
            'priority'      => 10,
            'accepted_args' => 1,
        );
        return $result;
    }

    /**
     * Runs the loader.
     *
     * @return WpWodify\Loader Returns $this, for object-chaining.
     */
    public function run() {
        $filters = $this->get_filters();
        $actions = $this->get_actions();

        foreach ($filters as $filter) {
           \add_filter($filter['hook'], $filter['function']);
        }

        foreach ($actions as $action) {
            \add_filter($action['hook'], $action['function']);
        }

        return $this;
    }

}