| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce; |
| 4 |
|
| 5 |
/** |
| 6 |
* Register all actions and filters for the plugin |
| 7 |
* |
| 8 |
* @link https://makecommerce.net/ |
| 9 |
* @since 3.0.0 |
| 10 |
* |
| 11 |
* @package Makecommerce |
| 12 |
* @subpackage Makecommerce/includes |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Register all actions and filters for the plugin. |
| 17 |
* |
| 18 |
* Maintain a list of all hooks that are registered throughout |
| 19 |
* the plugin, and register them with the WordPress API. Call the |
| 20 |
* run function to execute the list of actions and filters. |
| 21 |
* |
| 22 |
* @package Makecommerce |
| 23 |
* @subpackage Makecommerce/includes |
| 24 |
* @author Maksekeskus AS <support@maksekeskus.ee> |
| 25 |
*/ |
| 26 |
class Loader { |
| 27 |
|
| 28 |
/** |
| 29 |
* The array of actions registered with WordPress. |
| 30 |
* |
| 31 |
* @since 3.0.0 |
| 32 |
* @access protected |
| 33 |
* @var array $actions The actions registered with WordPress to fire when the plugin loads. |
| 34 |
*/ |
| 35 |
protected $actions; |
| 36 |
|
| 37 |
/** |
| 38 |
* The array of filters registered with WordPress. |
| 39 |
* |
| 40 |
* @since 3.0.0 |
| 41 |
* @access protected |
| 42 |
* @var array $filters The filters registered with WordPress to fire when the plugin loads. |
| 43 |
*/ |
| 44 |
protected $filters; |
| 45 |
|
| 46 |
/** |
| 47 |
* Initialize the collections used to maintain the actions and filters. |
| 48 |
* |
| 49 |
* @since 3.0.0 |
| 50 |
*/ |
| 51 |
public function __construct() { |
| 52 |
|
| 53 |
$this->actions = array(); |
| 54 |
$this->filters = array(); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add a new action to the collection to be registered with WordPress. |
| 60 |
* |
| 61 |
* @since 3.0.0 |
| 62 |
* @param string $hook The name of the WordPress action that is being registered. |
| 63 |
* @param object $component A reference to the instance of the object on which the action is defined. |
| 64 |
* @param string $callback The name of the function definition on the $component. |
| 65 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 66 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 67 |
*/ |
| 68 |
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 69 |
|
| 70 |
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add a new filter to the collection to be registered with WordPress. |
| 75 |
* |
| 76 |
* @since 3.0.0 |
| 77 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 78 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 79 |
* @param string $callback The name of the function definition on the $component. |
| 80 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 81 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
| 82 |
*/ |
| 83 |
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 84 |
|
| 85 |
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* A utility function that is used to register the actions and hooks into a single |
| 90 |
* collection. |
| 91 |
* |
| 92 |
* @since 3.0.0 |
| 93 |
* @access private |
| 94 |
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
| 95 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 96 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 97 |
* @param string $callback The name of the function definition on the $component. |
| 98 |
* @param int $priority The priority at which the function should be fired. |
| 99 |
* @param int $accepted_args The number of arguments that should be passed to the $callback. |
| 100 |
* @return array The collection of actions and filters registered with WordPress. |
| 101 |
*/ |
| 102 |
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
| 103 |
|
| 104 |
$hooks[] = array( |
| 105 |
'hook' => $hook, |
| 106 |
'component' => $component, |
| 107 |
'callback' => $callback, |
| 108 |
'priority' => $priority, |
| 109 |
'accepted_args' => $accepted_args |
| 110 |
); |
| 111 |
|
| 112 |
return $hooks; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Register the filters and actions with WordPress. |
| 117 |
* |
| 118 |
* @since 3.0.0 |
| 119 |
*/ |
| 120 |
public function run() { |
| 121 |
|
| 122 |
foreach ( $this->filters as $hook ) { |
| 123 |
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
| 124 |
} |
| 125 |
|
| 126 |
foreach ( $this->actions as $hook ) { |
| 127 |
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |