-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
190 lines (175 loc) · 6.04 KB
/
Loader.php
File metadata and controls
190 lines (175 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
* This file is part of WBF Framework: https://github.com/wagaweb/wbf
*
* @author WAGA Team <dev@waga.it>
*/
namespace WBF\components\pluginsframework;
/**
* Register all actions and filters for the plugin.
*
* Maintain a list of all hooks that are registered throughout
* the plugin, and register them with the WordPress API. Call the
* run function to execute the list of actions and filters.
*/
class Loader {
const TYPE_ACTION = 'action';
const TYPE_FILTER = 'filter';
/**
* @var array $actions The actions registered with WordPress to fire when the plugin loads.
*/
protected $actions = [];
/**
* @var array $filters The filters registered with WordPress to fire when the plugin loads.
*/
protected $filters = [];
/**
* @var object
*/
public $public_plugin;
/**
* @var object
*/
public $admin_plugin;
/**
* @var array
*/
public $classes;
/**
* Initialize the collections used to maintain the actions and filters.
*
* @param Plugin $caller
*/
public function __construct($caller = null) {
if(isset($caller)){
if($caller->get_public_class_name()){
$class_name = $caller->get_public_class_name();
$this->public_plugin = new $class_name($caller->get_plugin_name(), $caller->get_version(), $caller);
}
if($caller->get_admin_class_name()){
$class_name = $caller->get_admin_class_name();
$this->admin_plugin = new $class_name($caller->get_plugin_name(), $caller->get_version(), $caller);
}
}
}
/**
* A utility function that is used to register the actions and hooks into a single
* collection.
*
* @param string $type The collection of hooks that is being registered (that is, actions or filters).
* @param string $hook The name of the WordPress filter that is being registered.
* @param callable $callback The name of the function definition on the $component.
* @param int $priority The priority at which the function should be fired.
* @param int $accepted_args The number of arguments that should be passed to the $callback.
*
* @return Loader
*/
private function add( $type, $hook, $callback, $priority, $accepted_args ) {
switch ($type){
case self::TYPE_ACTION:
$this->actions[] = [
'hook' => $hook,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args
];
break;
case self::TYPE_FILTER:
$this->filters[] = [
'hook' => $hook,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args
];
break;
}
return $this;
}
/**
* Add a new action to the collection to be registered with WordPress.
*
* @var string $hook The name of the WordPress action that is being registered.
* @var object|callable $component A reference to the instance of the object on which the action is defined.
* @var string $callback The name of the function definition on the $component.
* @var int $priority The priority at which the function should be fired.
* @var int $accepted_args The number of arguments that should be passed to the $callback.
*/
public function add_action( $hook, $callable_or_component, $function_name = null, $priority = 10, $accepted_args = 1 ) {
if(!is_callable($callable_or_component)){
if(!isset($function_name)){
_doing_it_wrong(__FUNCTION__,'You cannot call the function without a $function_name paramater',"1.0.9");
}
$callback = [$callable_or_component,$function_name];
}else{
$callback = $callable_or_component;
}
$this->add( self::TYPE_ACTION, $hook, $callback, $priority, $accepted_args );
}
/**
* Add a new filter to the collection to be registered with WordPress.
*
* @var string $hook The name of the WordPress filter that is being registered.
* @var object|callable $callable_or_component A reference to the instance of the object on which the filter is defined.
* @var string Optional $function_name The name of the function definition on the $component.
* @var int $priority The priority at which the function should be fired.
* @var int $accepted_args The number of arguments that should be passed to the $callback.
*/
public function add_filter( $hook, $callable_or_component, $function_name = null, $priority = 10, $accepted_args = 1 ) {
if(!is_callable($callable_or_component)){
if(!isset($function_name)){
_doing_it_wrong(__FUNCTION__,'You cannot call the function without a $function_name paramater',"1.0.9");
}
$callback = [$callable_or_component,$function_name];
}else{
$callback = $callable_or_component;
}
$this->add( self::TYPE_FILTER, $hook, $callback, $priority, $accepted_args );
}
/**
* Add a new ajax action
*
* @since 1.0.5
*
* @param string $action
* @param $callable_or_component
* @param null $function_name
* @param bool $public if TRUE wp_ajax_nopriv will be added as well
* @param int $priority
* @param int $accepted_args
*/
public function add_ajax_action($action, $callable_or_component, $function_name = null, $public = true, $priority = 10, $accepted_args = 1){
if(!is_callable($callable_or_component)){
if(!isset($function_name)){
_doing_it_wrong(__FUNCTION__,'You cannot call the function without a $function_name paramater',"1.0.9");
}
$callback = [$callable_or_component,$function_name];
}else{
$callback = $callable_or_component;
}
$this->add( self::TYPE_ACTION, "wp_ajax_".$action, $callback, $priority, $accepted_args);
if($public){
$this->add( self::TYPE_ACTION, "wp_ajax_nopriv_".$action, $callback, $priority, $accepted_args);
}
}
/**
* Append a new class (Not used at the moment)
*
* @param $class_obj
*/
public function add_class($class_obj){
if(is_object($class_obj)){
$this->classes[] = $class_obj;
}
}
/**
* Register the filters and actions with WordPress.
*/
public function run() {
foreach ( $this->filters as $hook ) {
add_filter( $hook['hook'], $hook['callback'], $hook['priority'], $hook['accepted_args'] );
}
foreach ( $this->actions as $hook ) {
add_action( $hook['hook'], $hook['callback'], $hook['priority'], $hook['accepted_args'] );
}
}
}