| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately; |
| 4 |
|
| 5 |
class Loader { |
| 6 |
/** |
| 7 |
* All usable actions will add here. |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
private static $actions = []; |
| 11 |
/** |
| 12 |
* All usable filters will added here. |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $filters = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Add a new action to the collection to be registered with WordPress. |
| 19 |
* |
| 20 |
* @param string $hook The name of the WordPress action that is being registered. |
| 21 |
* @param object $component A reference to the instance of the object on which the action is defined. |
| 22 |
* @param string $callback The name of the function definition on the $component. |
| 23 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 24 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 25 |
*/ |
| 26 |
public static function add_action($tag, $component, $function_to_add, $priority = 10, $accepted_args = 1) { |
| 27 |
self::$actions = self::add(self::$actions, $tag, $component, $function_to_add, $priority, $accepted_args); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add a new filter to the collection to be registered with WordPress. |
| 32 |
* |
| 33 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 34 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 35 |
* @param string $callback The name of the function definition on the $component. |
| 36 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 37 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
| 38 |
*/ |
| 39 |
public static function add_filter($tag, $component, $function_to_add, $priority = 10, $accepted_args = 1) { |
| 40 |
self::$filters = self::add(self::$filters, $tag, $component, $function_to_add, $priority, $accepted_args); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* A utility function that is used to register the actions and hooks into a single |
| 45 |
* collection. |
| 46 |
* |
| 47 |
* @access private |
| 48 |
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
| 49 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 50 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 51 |
* @param string $callback The name of the function definition on the $component. |
| 52 |
* @param int $priority The priority at which the function should be fired. |
| 53 |
* @param int $accepted_args The number of arguments that should be passed to the $callback. |
| 54 |
* @return array The collection of actions and filters registered with WordPress. |
| 55 |
*/ |
| 56 |
private static function add($hooks, $tag, $component, $function_to_add, $priority, $accepted_args) { |
| 57 |
$hooks[] = array( |
| 58 |
'hook' => $tag, |
| 59 |
'component' => $component, |
| 60 |
'callback' => $function_to_add, |
| 61 |
'priority' => $priority, |
| 62 |
'accepted_args' => $accepted_args |
| 63 |
); |
| 64 |
return $hooks; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Register the filters and actions with WordPress. |
| 69 |
*/ |
| 70 |
public static function run() { |
| 71 |
if (!empty(self::$filters)) { |
| 72 |
foreach (self::$filters as $hook) { |
| 73 |
add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
| 74 |
} |
| 75 |
} |
| 76 |
if (!empty(self::$actions)) { |
| 77 |
foreach (self::$actions as $hook) { |
| 78 |
add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|