| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Register all actions and filters for the plugin improved by ThemeAtelier. |
| 5 |
* |
| 6 |
* @link https://themeatelier.net |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package darkify |
| 10 |
* @subpackage darkify/src |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace ThemeAtelier\Darkify; |
| 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 darkify |
| 23 |
* @subpackage darkify/src |
| 24 |
* @author ThemeAtelier <themeatelierbd@gmail.com> |
| 25 |
*/ |
| 26 |
class Loader |
| 27 |
{ |
| 28 |
/** |
| 29 |
* The array of actions registered with WordPress. |
| 30 |
* |
| 31 |
* @since 1.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 1.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 |
* The array of shortcode registered with WordPress. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @access protected |
| 51 |
* @var array $shortcodes The Shortcodes registered with WordPress to add shortcode to the plugin. |
| 52 |
*/ |
| 53 |
protected $shortcodes; |
| 54 |
|
| 55 |
/** |
| 56 |
* Initialize the collections used to maintain the actions and filters. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
*/ |
| 60 |
public function __construct() |
| 61 |
{ |
| 62 |
$this->actions = array(); |
| 63 |
$this->filters = array(); |
| 64 |
$this->shortcodes = array(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add a new action to the collection to be registered with WordPress. |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* @param string $hook The name of the WordPress action that is being registered. |
| 72 |
* @param object $component A reference to the instance of the object on which the action is defined. |
| 73 |
* @param string $callback The name of the function definition on the $component. |
| 74 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 75 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 76 |
*/ |
| 77 |
public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) |
| 78 |
{ |
| 79 |
$this->actions = $this->add($this->actions, $hook, $component, $callback, $priority, $accepted_args); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add a new filter to the collection to be registered with WordPress. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 87 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 88 |
* @param string $callback The name of the function definition on the $component. |
| 89 |
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
| 90 |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 91 |
*/ |
| 92 |
public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) |
| 93 |
{ |
| 94 |
$this->filters = $this->add($this->filters, $hook, $component, $callback, $priority, $accepted_args); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Add new shortcode to the collection to be registered with the WordPress. |
| 99 |
* |
| 100 |
* @since 3.0.0 |
| 101 |
* @param string $tag The name of the shortcode that is being registered. |
| 102 |
* @param object $component The reference to the instance of the object on which the shortcode is defined. |
| 103 |
* @param string $callback The name of the function definition on the $component. |
| 104 |
* @param integer $priority Optional. The priority at which the function should be fired. Default is 10. |
| 105 |
* @param integer $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function add_shortcode($tag, $component, $callback, $priority = 10, $accepted_args = 1) |
| 109 |
{ |
| 110 |
$this->shortcodes = $this->add($this->shortcodes, $tag, $component, $callback, $priority, $accepted_args); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* A utility function that is used to register the actions and hooks into a single |
| 115 |
* collection. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* @access private |
| 119 |
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
| 120 |
* @param string $hook The name of the WordPress filter that is being registered. |
| 121 |
* @param object $component A reference to the instance of the object on which the filter is defined. |
| 122 |
* @param string $callback The name of the function definition on the $component. |
| 123 |
* @param int $priority The priority at which the function should be fired. |
| 124 |
* @param int $accepted_args The number of arguments that should be passed to the $callback. |
| 125 |
* @return array The collection of actions and filters registered with WordPress. |
| 126 |
*/ |
| 127 |
private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) |
| 128 |
{ |
| 129 |
|
| 130 |
$hooks[] = array( |
| 131 |
'hook' => $hook, |
| 132 |
'component' => $component, |
| 133 |
'callback' => $callback, |
| 134 |
'priority' => $priority, |
| 135 |
'accepted_args' => $accepted_args, |
| 136 |
); |
| 137 |
|
| 138 |
return $hooks; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Register the filters and actions with WordPress. |
| 143 |
* |
| 144 |
* @since 1.0.0 |
| 145 |
*/ |
| 146 |
public function run() |
| 147 |
{ |
| 148 |
foreach ($this->filters as $hook) { |
| 149 |
add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
| 150 |
} |
| 151 |
|
| 152 |
foreach ($this->actions as $hook) { |
| 153 |
add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
| 154 |
} |
| 155 |
|
| 156 |
foreach ($this->shortcodes as $hook) { |
| 157 |
add_shortcode($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|