author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* The plugin API is located in this file, which allows for creating actions |
|
4 |
* and filters and hooking functions, and methods. The functions or methods will |
|
5 |
* then be run when the action or filter is called. |
|
6 |
* |
|
7 |
* The API callback examples reference functions, but can be methods of classes. |
|
8 |
* To hook methods, you'll need to pass an array one of two ways. |
|
9 |
* |
|
10 |
* Any of the syntaxes explained in the PHP documentation for the |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* {@link https://secure.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'} |
0 | 12 |
* type are valid. |
13 |
* |
|
5 | 14 |
* Also see the {@link https://codex.wordpress.org/Plugin_API Plugin API} for |
0 | 15 |
* more information and examples on how to use a lot of these functions. |
16 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* This file should have no external dependencies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* |
0 | 19 |
* @package WordPress |
20 |
* @subpackage Plugin |
|
5 | 21 |
* @since 1.5.0 |
0 | 22 |
*/ |
23 |
||
24 |
// Initialize the filter globals. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
require( dirname( __FILE__ ) . '/class-wp-hook.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
/** @var WP_Hook[] $wp_filter */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
global $wp_filter, $wp_actions, $wp_current_filter; |
0 | 29 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
if ( $wp_filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
$wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
} else { |
0 | 33 |
$wp_filter = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
} |
0 | 35 |
|
36 |
if ( ! isset( $wp_actions ) ) |
|
37 |
$wp_actions = array(); |
|
38 |
||
39 |
if ( ! isset( $wp_current_filter ) ) |
|
40 |
$wp_current_filter = array(); |
|
41 |
||
42 |
/** |
|
5 | 43 |
* Hook a function or method to a specific filter action. |
0 | 44 |
* |
45 |
* WordPress offers filter hooks to allow plugins to modify |
|
46 |
* various types of internal data at runtime. |
|
47 |
* |
|
48 |
* A plugin can modify data by binding a callback to a filter hook. When the filter |
|
49 |
* is later applied, each bound callback is run in order of priority, and given |
|
50 |
* the opportunity to modify a value by returning a new value. |
|
51 |
* |
|
52 |
* The following example shows how a callback function is bound to a filter hook. |
|
5 | 53 |
* |
54 |
* Note that `$example` is passed to the callback, (maybe) modified, then returned: |
|
0 | 55 |
* |
5 | 56 |
* function example_callback( $example ) { |
57 |
* // Maybe modify $example in some way. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* return $example; |
5 | 59 |
* } |
60 |
* add_filter( 'example_filter', 'example_callback' ); |
|
0 | 61 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* Bound callbacks can accept from none to the total number of arguments passed as parameters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* in the corresponding apply_filters() call. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* In other words, if an apply_filters() call passes four total arguments, callbacks bound to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* it can accept none (the same as 1) of the arguments or up to four. The important part is that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* the `$accepted_args` value must reflect the number of arguments the bound callback *actually* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* opted to accept. If no arguments were accepted by the callback that is considered to be the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* same as accepting 1 argument. For example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* // Filter call. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* $value = apply_filters( 'hook', $value, $arg2, $arg3 ); |
0 | 73 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* // Accepting zero/one arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* function example_callback() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* return 'some value'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* // Accepting two arguments (three possible). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* function example_callback( $value, $arg2 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* return $maybe_modified_value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
* *Note:* The function will return true whether or not the callback is valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
* It is up to you to take care. This is done for optimization purposes, so |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* everything is as quick as possible. |
0 | 91 |
* |
92 |
* @since 0.71 |
|
93 |
* |
|
5 | 94 |
* @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
95 |
* |
|
0 | 96 |
* @param string $tag The name of the filter to hook the $function_to_add callback to. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
* @param callable $function_to_add The callback to be run when the filter is applied. |
5 | 98 |
* @param int $priority Optional. Used to specify the order in which the functions |
99 |
* associated with a particular action are executed. Default 10. |
|
100 |
* Lower numbers correspond with earlier execution, |
|
101 |
* and functions with the same priority are executed |
|
102 |
* in the order in which they were added to the action. |
|
103 |
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* @return true |
0 | 105 |
*/ |
106 |
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
global $wp_filter; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
if ( ! isset( $wp_filter[ $tag ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
$wp_filter[ $tag ] = new WP_Hook(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args ); |
0 | 112 |
return true; |
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Check if any filter has been registered for a hook. |
|
117 |
* |
|
5 | 118 |
* @since 2.5.0 |
119 |
* |
|
120 |
* @global array $wp_filter Stores all of the filters. |
|
0 | 121 |
* |
5 | 122 |
* @param string $tag The name of the filter hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @return false|int If $function_to_check is omitted, returns boolean for whether the hook has |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* anything registered. When checking a specific function, the priority of that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* hook is returned, or false if the function is not attached. When using the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* $function_to_check argument, this function may return a non-boolean value |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* that evaluates to false (e.g.) 0, so use the === operator for testing the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
* return value. |
0 | 130 |
*/ |
131 |
function has_filter($tag, $function_to_check = false) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
global $wp_filter; |
0 | 133 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
if ( ! isset( $wp_filter[ $tag ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
return false; |
5 | 136 |
} |
137 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check ); |
0 | 139 |
} |
140 |
||
141 |
/** |
|
142 |
* Call the functions added to a filter hook. |
|
143 |
* |
|
144 |
* The callback functions attached to filter hook $tag are invoked by calling |
|
145 |
* this function. This function can be used to create a new filter hook by |
|
146 |
* simply calling this function with the name of the new hook specified using |
|
147 |
* the $tag parameter. |
|
148 |
* |
|
149 |
* The function allows for additional arguments to be added and passed to hooks. |
|
5 | 150 |
* |
151 |
* // Our filter callback function |
|
152 |
* function example_callback( $string, $arg1, $arg2 ) { |
|
153 |
* // (maybe) modify $string |
|
154 |
* return $string; |
|
155 |
* } |
|
156 |
* add_filter( 'example_filter', 'example_callback', 10, 3 ); |
|
0 | 157 |
* |
5 | 158 |
* /* |
159 |
* * Apply the filters by calling the 'example_callback' function we |
|
160 |
* * "hooked" to 'example_filter' using the add_filter() function above. |
|
161 |
* * - 'example_filter' is the filter hook $tag |
|
162 |
* * - 'filter me' is the value being filtered |
|
163 |
* * - $arg1 and $arg2 are the additional arguments passed to the callback. |
|
164 |
* $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
|
0 | 165 |
* |
166 |
* @since 0.71 |
|
167 |
* |
|
5 | 168 |
* @global array $wp_filter Stores all of the filters. |
169 |
* @global array $wp_current_filter Stores the list of current filters with the current one last. |
|
170 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
* @param string $tag The name of the filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
* @param mixed $value The value on which the filters hooked to `$tag` are applied on. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* @param mixed $var,... Additional variables passed to the functions hooked to `$tag`. |
0 | 174 |
* @return mixed The filtered value after all hooked functions are applied to it. |
175 |
*/ |
|
176 |
function apply_filters( $tag, $value ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
global $wp_filter, $wp_current_filter; |
0 | 178 |
|
179 |
$args = array(); |
|
180 |
||
5 | 181 |
// Do 'all' actions first. |
0 | 182 |
if ( isset($wp_filter['all']) ) { |
183 |
$wp_current_filter[] = $tag; |
|
184 |
$args = func_get_args(); |
|
185 |
_wp_call_all_hook($args); |
|
186 |
} |
|
187 |
||
188 |
if ( !isset($wp_filter[$tag]) ) { |
|
189 |
if ( isset($wp_filter['all']) ) |
|
190 |
array_pop($wp_current_filter); |
|
191 |
return $value; |
|
192 |
} |
|
193 |
||
194 |
if ( !isset($wp_filter['all']) ) |
|
195 |
$wp_current_filter[] = $tag; |
|
196 |
||
197 |
if ( empty($args) ) |
|
198 |
$args = func_get_args(); |
|
199 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
// don't pass the tag name to WP_Hook |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
array_shift( $args ); |
0 | 202 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
$filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); |
0 | 204 |
|
205 |
array_pop( $wp_current_filter ); |
|
206 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
return $filtered; |
0 | 208 |
} |
209 |
||
210 |
/** |
|
211 |
* Execute functions hooked on a specific filter hook, specifying arguments in an array. |
|
212 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* @since 3.0.0 |
5 | 214 |
* |
0 | 215 |
* @see apply_filters() This function is identical, but the arguments passed to the |
5 | 216 |
* functions hooked to `$tag` are supplied using an array. |
0 | 217 |
* |
5 | 218 |
* @global array $wp_filter Stores all of the filters |
219 |
* @global array $wp_current_filter Stores the list of current filters with the current one last |
|
0 | 220 |
* |
5 | 221 |
* @param string $tag The name of the filter hook. |
222 |
* @param array $args The arguments supplied to the functions hooked to $tag. |
|
0 | 223 |
* @return mixed The filtered value after all hooked functions are applied to it. |
224 |
*/ |
|
225 |
function apply_filters_ref_array($tag, $args) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
global $wp_filter, $wp_current_filter; |
0 | 227 |
|
228 |
// Do 'all' actions first |
|
229 |
if ( isset($wp_filter['all']) ) { |
|
230 |
$wp_current_filter[] = $tag; |
|
231 |
$all_args = func_get_args(); |
|
232 |
_wp_call_all_hook($all_args); |
|
233 |
} |
|
234 |
||
235 |
if ( !isset($wp_filter[$tag]) ) { |
|
236 |
if ( isset($wp_filter['all']) ) |
|
237 |
array_pop($wp_current_filter); |
|
238 |
return $args[0]; |
|
239 |
} |
|
240 |
||
241 |
if ( !isset($wp_filter['all']) ) |
|
242 |
$wp_current_filter[] = $tag; |
|
243 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
$filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args ); |
0 | 245 |
|
246 |
array_pop( $wp_current_filter ); |
|
247 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
return $filtered; |
0 | 249 |
} |
250 |
||
251 |
/** |
|
252 |
* Removes a function from a specified filter hook. |
|
253 |
* |
|
254 |
* This function removes a function attached to a specified filter hook. This |
|
255 |
* method can be used to remove default functions attached to a specific filter |
|
256 |
* hook and possibly replace them with a substitute. |
|
257 |
* |
|
258 |
* To remove a hook, the $function_to_remove and $priority arguments must match |
|
259 |
* when the hook was added. This goes for both filters and actions. No warning |
|
260 |
* will be given on removal failure. |
|
261 |
* |
|
5 | 262 |
* @since 1.2.0 |
0 | 263 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* @global array $wp_filter Stores all of the filters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* |
5 | 266 |
* @param string $tag The filter hook to which the function to be removed is hooked. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
* @param callable $function_to_remove The name of the function which should be removed. |
5 | 268 |
* @param int $priority Optional. The priority of the function. Default 10. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* @return bool Whether the function existed before it was removed. |
0 | 270 |
*/ |
271 |
function remove_filter( $tag, $function_to_remove, $priority = 10 ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
global $wp_filter; |
0 | 273 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
$r = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
if ( isset( $wp_filter[ $tag ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
$r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
if ( ! $wp_filter[ $tag ]->callbacks ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
unset( $wp_filter[ $tag ] ); |
5 | 279 |
} |
0 | 280 |
} |
281 |
||
282 |
return $r; |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Remove all of the hooks from a filter. |
|
287 |
* |
|
5 | 288 |
* @since 2.7.0 |
0 | 289 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* @global array $wp_filter Stores all of the filters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
* |
5 | 292 |
* @param string $tag The filter to remove hooks from. |
293 |
* @param int|bool $priority Optional. The priority number to remove. Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
* @return true True when finished. |
0 | 295 |
*/ |
5 | 296 |
function remove_all_filters( $tag, $priority = false ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
global $wp_filter; |
0 | 298 |
|
5 | 299 |
if ( isset( $wp_filter[ $tag ]) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
$wp_filter[ $tag ]->remove_all_filters( $priority ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
if ( ! $wp_filter[ $tag ]->has_filters() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
unset( $wp_filter[ $tag ] ); |
5 | 303 |
} |
0 | 304 |
} |
305 |
||
306 |
return true; |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Retrieve the name of the current filter or action. |
|
311 |
* |
|
5 | 312 |
* @since 2.5.0 |
0 | 313 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
* @global array $wp_current_filter Stores the list of current filters with the current one last |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
* |
0 | 316 |
* @return string Hook name of the current filter or action. |
317 |
*/ |
|
318 |
function current_filter() { |
|
319 |
global $wp_current_filter; |
|
320 |
return end( $wp_current_filter ); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
5 | 324 |
* Retrieve the name of the current action. |
325 |
* |
|
326 |
* @since 3.9.0 |
|
327 |
* |
|
328 |
* @return string Hook name of the current action. |
|
329 |
*/ |
|
330 |
function current_action() { |
|
331 |
return current_filter(); |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Retrieve the name of a filter currently being processed. |
|
336 |
* |
|
337 |
* The function current_filter() only returns the most recent filter or action |
|
338 |
* being executed. did_action() returns true once the action is initially |
|
339 |
* processed. |
|
340 |
* |
|
341 |
* This function allows detection for any filter currently being |
|
342 |
* executed (despite not being the most recent filter to fire, in the case of |
|
343 |
* hooks called from hook callbacks) to be verified. |
|
344 |
* |
|
345 |
* @since 3.9.0 |
|
346 |
* |
|
347 |
* @see current_filter() |
|
348 |
* @see did_action() |
|
349 |
* @global array $wp_current_filter Current filter. |
|
350 |
* |
|
351 |
* @param null|string $filter Optional. Filter to check. Defaults to null, which |
|
352 |
* checks if any filter is currently being run. |
|
353 |
* @return bool Whether the filter is currently in the stack. |
|
354 |
*/ |
|
355 |
function doing_filter( $filter = null ) { |
|
356 |
global $wp_current_filter; |
|
357 |
||
358 |
if ( null === $filter ) { |
|
359 |
return ! empty( $wp_current_filter ); |
|
360 |
} |
|
361 |
||
362 |
return in_array( $filter, $wp_current_filter ); |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Retrieve the name of an action currently being processed. |
|
367 |
* |
|
368 |
* @since 3.9.0 |
|
369 |
* |
|
370 |
* @param string|null $action Optional. Action to check. Defaults to null, which checks |
|
371 |
* if any action is currently being run. |
|
372 |
* @return bool Whether the action is currently in the stack. |
|
373 |
*/ |
|
374 |
function doing_action( $action = null ) { |
|
375 |
return doing_filter( $action ); |
|
376 |
} |
|
377 |
||
378 |
/** |
|
0 | 379 |
* Hooks a function on to a specific action. |
380 |
* |
|
381 |
* Actions are the hooks that the WordPress core launches at specific points |
|
382 |
* during execution, or when specific events occur. Plugins can specify that |
|
383 |
* one or more of its PHP functions are executed at these points, using the |
|
384 |
* Action API. |
|
385 |
* |
|
5 | 386 |
* @since 1.2.0 |
0 | 387 |
* |
5 | 388 |
* @param string $tag The name of the action to which the $function_to_add is hooked. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
* @param callable $function_to_add The name of the function you wish to be called. |
5 | 390 |
* @param int $priority Optional. Used to specify the order in which the functions |
391 |
* associated with a particular action are executed. Default 10. |
|
392 |
* Lower numbers correspond with earlier execution, |
|
393 |
* and functions with the same priority are executed |
|
394 |
* in the order in which they were added to the action. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* @return true Will always return true. |
0 | 397 |
*/ |
398 |
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
|
399 |
return add_filter($tag, $function_to_add, $priority, $accepted_args); |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Execute functions hooked on a specific action hook. |
|
404 |
* |
|
5 | 405 |
* This function invokes all functions attached to action hook `$tag`. It is |
0 | 406 |
* possible to create new action hooks by simply calling this function, |
5 | 407 |
* specifying the name of the new hook using the `$tag` parameter. |
0 | 408 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* You can pass extra arguments to the hooks, much like you can with apply_filters(). |
0 | 410 |
* |
5 | 411 |
* @since 1.2.0 |
412 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* @global array $wp_filter Stores all of the filters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* @global array $wp_actions Increments the amount of times action was triggered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* @global array $wp_current_filter Stores the list of current filters with the current one last |
0 | 416 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
* @param string $tag The name of the action to be executed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
* @param mixed $arg,... Optional. Additional arguments which are passed on to the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* functions hooked to the action. Default empty. |
0 | 420 |
*/ |
421 |
function do_action($tag, $arg = '') { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
global $wp_filter, $wp_actions, $wp_current_filter; |
0 | 423 |
|
424 |
if ( ! isset($wp_actions[$tag]) ) |
|
425 |
$wp_actions[$tag] = 1; |
|
426 |
else |
|
427 |
++$wp_actions[$tag]; |
|
428 |
||
429 |
// Do 'all' actions first |
|
430 |
if ( isset($wp_filter['all']) ) { |
|
431 |
$wp_current_filter[] = $tag; |
|
432 |
$all_args = func_get_args(); |
|
433 |
_wp_call_all_hook($all_args); |
|
434 |
} |
|
435 |
||
436 |
if ( !isset($wp_filter[$tag]) ) { |
|
437 |
if ( isset($wp_filter['all']) ) |
|
438 |
array_pop($wp_current_filter); |
|
439 |
return; |
|
440 |
} |
|
441 |
||
442 |
if ( !isset($wp_filter['all']) ) |
|
443 |
$wp_current_filter[] = $tag; |
|
444 |
||
445 |
$args = array(); |
|
446 |
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this) |
|
447 |
$args[] =& $arg[0]; |
|
448 |
else |
|
449 |
$args[] = $arg; |
|
5 | 450 |
for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) |
0 | 451 |
$args[] = func_get_arg($a); |
452 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
$wp_filter[ $tag ]->do_action( $args ); |
0 | 454 |
|
455 |
array_pop($wp_current_filter); |
|
456 |
} |
|
457 |
||
458 |
/** |
|
459 |
* Retrieve the number of times an action is fired. |
|
460 |
* |
|
5 | 461 |
* @since 2.1.0 |
462 |
* |
|
0 | 463 |
* @global array $wp_actions Increments the amount of times action was triggered. |
464 |
* |
|
465 |
* @param string $tag The name of the action hook. |
|
5 | 466 |
* @return int The number of times action hook $tag is fired. |
0 | 467 |
*/ |
468 |
function did_action($tag) { |
|
469 |
global $wp_actions; |
|
470 |
||
471 |
if ( ! isset( $wp_actions[ $tag ] ) ) |
|
472 |
return 0; |
|
473 |
||
474 |
return $wp_actions[$tag]; |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Execute functions hooked on a specific action hook, specifying arguments in an array. |
|
479 |
* |
|
5 | 480 |
* @since 2.1.0 |
0 | 481 |
* |
5 | 482 |
* @see do_action() This function is identical, but the arguments passed to the |
483 |
* functions hooked to $tag< are supplied using an array. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* @global array $wp_filter Stores all of the filters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* @global array $wp_actions Increments the amount of times action was triggered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* @global array $wp_current_filter Stores the list of current filters with the current one last |
0 | 487 |
* |
5 | 488 |
* @param string $tag The name of the action to be executed. |
489 |
* @param array $args The arguments supplied to the functions hooked to `$tag`. |
|
0 | 490 |
*/ |
491 |
function do_action_ref_array($tag, $args) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
global $wp_filter, $wp_actions, $wp_current_filter; |
0 | 493 |
|
494 |
if ( ! isset($wp_actions[$tag]) ) |
|
495 |
$wp_actions[$tag] = 1; |
|
496 |
else |
|
497 |
++$wp_actions[$tag]; |
|
498 |
||
499 |
// Do 'all' actions first |
|
500 |
if ( isset($wp_filter['all']) ) { |
|
501 |
$wp_current_filter[] = $tag; |
|
502 |
$all_args = func_get_args(); |
|
503 |
_wp_call_all_hook($all_args); |
|
504 |
} |
|
505 |
||
506 |
if ( !isset($wp_filter[$tag]) ) { |
|
507 |
if ( isset($wp_filter['all']) ) |
|
508 |
array_pop($wp_current_filter); |
|
509 |
return; |
|
510 |
} |
|
511 |
||
512 |
if ( !isset($wp_filter['all']) ) |
|
513 |
$wp_current_filter[] = $tag; |
|
514 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
$wp_filter[ $tag ]->do_action( $args ); |
0 | 516 |
|
517 |
array_pop($wp_current_filter); |
|
518 |
} |
|
519 |
||
520 |
/** |
|
521 |
* Check if any action has been registered for a hook. |
|
522 |
* |
|
5 | 523 |
* @since 2.5.0 |
524 |
* |
|
0 | 525 |
* @see has_filter() has_action() is an alias of has_filter(). |
526 |
* |
|
5 | 527 |
* @param string $tag The name of the action hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
* @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
5 | 529 |
* @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has |
530 |
* anything registered. When checking a specific function, the priority of that |
|
531 |
* hook is returned, or false if the function is not attached. When using the |
|
532 |
* $function_to_check argument, this function may return a non-boolean value |
|
533 |
* that evaluates to false (e.g.) 0, so use the === operator for testing the |
|
534 |
* return value. |
|
0 | 535 |
*/ |
536 |
function has_action($tag, $function_to_check = false) { |
|
537 |
return has_filter($tag, $function_to_check); |
|
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* Removes a function from a specified action hook. |
|
542 |
* |
|
543 |
* This function removes a function attached to a specified action hook. This |
|
544 |
* method can be used to remove default functions attached to a specific filter |
|
545 |
* hook and possibly replace them with a substitute. |
|
546 |
* |
|
5 | 547 |
* @since 1.2.0 |
0 | 548 |
* |
5 | 549 |
* @param string $tag The action hook to which the function to be removed is hooked. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
* @param callable $function_to_remove The name of the function which should be removed. |
5 | 551 |
* @param int $priority Optional. The priority of the function. Default 10. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
* @return bool Whether the function is removed. |
0 | 553 |
*/ |
554 |
function remove_action( $tag, $function_to_remove, $priority = 10 ) { |
|
555 |
return remove_filter( $tag, $function_to_remove, $priority ); |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Remove all of the hooks from an action. |
|
560 |
* |
|
5 | 561 |
* @since 2.7.0 |
0 | 562 |
* |
5 | 563 |
* @param string $tag The action to remove hooks from. |
564 |
* @param int|bool $priority The priority number to remove them from. Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* @return true True when finished. |
0 | 566 |
*/ |
567 |
function remove_all_actions($tag, $priority = false) { |
|
568 |
return remove_all_filters($tag, $priority); |
|
569 |
} |
|
570 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* Fires functions attached to a deprecated filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* When a filter hook is deprecated, the apply_filters() call is replaced with |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
* apply_filters_deprecated(), which triggers a deprecation notice and then fires |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
* the original filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
* Note: the value and extra arguments passed to the original apply_filters() call |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
* must be passed here to `$args` as an array. For example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* // Old filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* return apply_filters( 'wpdocs_filter', $value, $extra_arg ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
* // Deprecated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9', 'wpdocs_new_filter' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
* @see _deprecated_hook() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
* @param string $tag The name of the filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
* @param array $args Array of additional function arguments to be passed to apply_filters(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* @param string $version The version of WordPress that deprecated the hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
* @param string $replacement Optional. The hook that should have been used. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
* @param string $message Optional. A message regarding the change. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
function apply_filters_deprecated( $tag, $args, $version, $replacement = false, $message = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
if ( ! has_filter( $tag ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
return $args[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
_deprecated_hook( $tag, $version, $replacement, $message ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
return apply_filters_ref_array( $tag, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
605 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
* Fires functions attached to a deprecated action hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
* When an action hook is deprecated, the do_action() call is replaced with |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
* do_action_deprecated(), which triggers a deprecation notice and then fires |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
* the original hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
* @see _deprecated_hook() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
* @param string $tag The name of the action hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
* @param array $args Array of additional function arguments to be passed to do_action(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
* @param string $version The version of WordPress that deprecated the hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* @param string $replacement Optional. The hook that should have been used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
* @param string $message Optional. A message regarding the change. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
function do_action_deprecated( $tag, $args, $version, $replacement = false, $message = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
if ( ! has_action( $tag ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
_deprecated_hook( $tag, $version, $replacement, $message ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
do_action_ref_array( $tag, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
|
0 | 634 |
// |
635 |
// Functions for handling plugins. |
|
636 |
// |
|
637 |
||
638 |
/** |
|
639 |
* Gets the basename of a plugin. |
|
640 |
* |
|
641 |
* This method extracts the name of a plugin from its filename. |
|
642 |
* |
|
5 | 643 |
* @since 1.5.0 |
0 | 644 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* @global array $wp_plugin_paths |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* |
0 | 647 |
* @param string $file The filename of plugin. |
648 |
* @return string The name of a plugin. |
|
649 |
*/ |
|
5 | 650 |
function plugin_basename( $file ) { |
651 |
global $wp_plugin_paths; |
|
652 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
// $wp_plugin_paths contains normalized paths. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
$file = wp_normalize_path( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
arsort( $wp_plugin_paths ); |
5 | 657 |
foreach ( $wp_plugin_paths as $dir => $realdir ) { |
658 |
if ( strpos( $file, $realdir ) === 0 ) { |
|
659 |
$file = $dir . substr( $file, strlen( $realdir ) ); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); |
|
664 |
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); |
|
665 |
||
0 | 666 |
$file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir |
667 |
$file = trim($file, '/'); |
|
668 |
return $file; |
|
669 |
} |
|
670 |
||
671 |
/** |
|
5 | 672 |
* Register a plugin's real path. |
673 |
* |
|
674 |
* This is used in plugin_basename() to resolve symlinked paths. |
|
675 |
* |
|
676 |
* @since 3.9.0 |
|
677 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
* @see wp_normalize_path() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
* @global array $wp_plugin_paths |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
* @staticvar string $wp_plugin_path |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
* @staticvar string $wpmu_plugin_path |
0 | 684 |
* |
5 | 685 |
* @param string $file Known path to the file. |
686 |
* @return bool Whether the path was able to be registered. |
|
687 |
*/ |
|
688 |
function wp_register_plugin_realpath( $file ) { |
|
689 |
global $wp_plugin_paths; |
|
690 |
||
691 |
// Normalize, but store as static to avoid recalculation of a constant value |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
static $wp_plugin_path = null, $wpmu_plugin_path = null; |
5 | 693 |
if ( ! isset( $wp_plugin_path ) ) { |
694 |
$wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR ); |
|
695 |
$wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR ); |
|
696 |
} |
|
697 |
||
698 |
$plugin_path = wp_normalize_path( dirname( $file ) ); |
|
699 |
$plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) ); |
|
700 |
||
701 |
if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) { |
|
702 |
return false; |
|
703 |
} |
|
704 |
||
705 |
if ( $plugin_path !== $plugin_realpath ) { |
|
706 |
$wp_plugin_paths[ $plugin_path ] = $plugin_realpath; |
|
707 |
} |
|
708 |
||
709 |
return true; |
|
710 |
} |
|
711 |
||
712 |
/** |
|
713 |
* Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in. |
|
714 |
* |
|
715 |
* @since 2.8.0 |
|
716 |
* |
|
717 |
* @param string $file The filename of the plugin (__FILE__). |
|
718 |
* @return string the filesystem path of the directory that contains the plugin. |
|
0 | 719 |
*/ |
720 |
function plugin_dir_path( $file ) { |
|
721 |
return trailingslashit( dirname( $file ) ); |
|
722 |
} |
|
723 |
||
724 |
/** |
|
5 | 725 |
* Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. |
0 | 726 |
* |
5 | 727 |
* @since 2.8.0 |
728 |
* |
|
729 |
* @param string $file The filename of the plugin (__FILE__). |
|
730 |
* @return string the URL path of the directory that contains the plugin. |
|
0 | 731 |
*/ |
732 |
function plugin_dir_url( $file ) { |
|
733 |
return trailingslashit( plugins_url( '', $file ) ); |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Set the activation hook for a plugin. |
|
738 |
* |
|
739 |
* When a plugin is activated, the action 'activate_PLUGINNAME' hook is |
|
740 |
* called. In the name of this hook, PLUGINNAME is replaced with the name |
|
741 |
* of the plugin, including the optional subdirectory. For example, when the |
|
742 |
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then |
|
743 |
* the name of this hook will become 'activate_sampleplugin/sample.php'. |
|
744 |
* |
|
745 |
* When the plugin consists of only one file and is (as by default) located at |
|
746 |
* wp-content/plugins/sample.php the name of this hook will be |
|
747 |
* 'activate_sample.php'. |
|
748 |
* |
|
5 | 749 |
* @since 2.0.0 |
0 | 750 |
* |
5 | 751 |
* @param string $file The filename of the plugin including the path. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
* @param callable $function The function hooked to the 'activate_PLUGIN' action. |
0 | 753 |
*/ |
754 |
function register_activation_hook($file, $function) { |
|
755 |
$file = plugin_basename($file); |
|
756 |
add_action('activate_' . $file, $function); |
|
757 |
} |
|
758 |
||
759 |
/** |
|
760 |
* Set the deactivation hook for a plugin. |
|
761 |
* |
|
762 |
* When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is |
|
763 |
* called. In the name of this hook, PLUGINNAME is replaced with the name |
|
764 |
* of the plugin, including the optional subdirectory. For example, when the |
|
765 |
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then |
|
766 |
* the name of this hook will become 'deactivate_sampleplugin/sample.php'. |
|
767 |
* |
|
768 |
* When the plugin consists of only one file and is (as by default) located at |
|
769 |
* wp-content/plugins/sample.php the name of this hook will be |
|
770 |
* 'deactivate_sample.php'. |
|
771 |
* |
|
5 | 772 |
* @since 2.0.0 |
0 | 773 |
* |
5 | 774 |
* @param string $file The filename of the plugin including the path. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
* @param callable $function The function hooked to the 'deactivate_PLUGIN' action. |
0 | 776 |
*/ |
777 |
function register_deactivation_hook($file, $function) { |
|
778 |
$file = plugin_basename($file); |
|
779 |
add_action('deactivate_' . $file, $function); |
|
780 |
} |
|
781 |
||
782 |
/** |
|
783 |
* Set the uninstallation hook for a plugin. |
|
784 |
* |
|
785 |
* Registers the uninstall hook that will be called when the user clicks on the |
|
786 |
* uninstall link that calls for the plugin to uninstall itself. The link won't |
|
787 |
* be active unless the plugin hooks into the action. |
|
788 |
* |
|
789 |
* The plugin should not run arbitrary code outside of functions, when |
|
790 |
* registering the uninstall hook. In order to run using the hook, the plugin |
|
791 |
* will have to be included, which means that any code laying outside of a |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
* function will be run during the uninstallation process. The plugin should not |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
* hinder the uninstallation process. |
0 | 794 |
* |
795 |
* If the plugin can not be written without running code within the plugin, then |
|
796 |
* the plugin should create a file named 'uninstall.php' in the base plugin |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
* folder. This file will be called, if it exists, during the uninstallation process |
0 | 798 |
* bypassing the uninstall hook. The plugin, when using the 'uninstall.php' |
799 |
* should always check for the 'WP_UNINSTALL_PLUGIN' constant, before |
|
800 |
* executing. |
|
801 |
* |
|
5 | 802 |
* @since 2.7.0 |
0 | 803 |
* |
5 | 804 |
* @param string $file Plugin file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
* @param callable $callback The callback to run when the hook is called. Must be |
5 | 806 |
* a static method or function. |
0 | 807 |
*/ |
808 |
function register_uninstall_hook( $file, $callback ) { |
|
809 |
if ( is_array( $callback ) && is_object( $callback[0] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' ); |
0 | 811 |
return; |
812 |
} |
|
813 |
||
5 | 814 |
/* |
815 |
* The option should not be autoloaded, because it is not needed in most |
|
816 |
* cases. Emphasis should be put on using the 'uninstall.php' way of |
|
817 |
* uninstalling the plugin. |
|
818 |
*/ |
|
0 | 819 |
$uninstallable_plugins = (array) get_option('uninstall_plugins'); |
820 |
$uninstallable_plugins[plugin_basename($file)] = $callback; |
|
5 | 821 |
|
0 | 822 |
update_option('uninstall_plugins', $uninstallable_plugins); |
823 |
} |
|
824 |
||
825 |
/** |
|
5 | 826 |
* Call the 'all' hook, which will process the functions hooked into it. |
0 | 827 |
* |
828 |
* The 'all' hook passes all of the arguments or parameters that were used for |
|
829 |
* the hook, which this function was called for. |
|
830 |
* |
|
831 |
* This function is used internally for apply_filters(), do_action(), and |
|
832 |
* do_action_ref_array() and is not meant to be used from outside those |
|
833 |
* functions. This function does not check for the existence of the all hook, so |
|
834 |
* it will fail unless the all hook exists prior to this function call. |
|
835 |
* |
|
5 | 836 |
* @since 2.5.0 |
0 | 837 |
* @access private |
838 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
839 |
* @global array $wp_filter Stores all of the filters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
840 |
* |
0 | 841 |
* @param array $args The collected parameters from the hook that was called. |
842 |
*/ |
|
843 |
function _wp_call_all_hook($args) { |
|
844 |
global $wp_filter; |
|
845 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
$wp_filter['all']->do_all_hook( $args ); |
0 | 847 |
} |
848 |
||
849 |
/** |
|
850 |
* Build Unique ID for storage and retrieval. |
|
851 |
* |
|
852 |
* The old way to serialize the callback caused issues and this function is the |
|
5 | 853 |
* solution. It works by checking for objects and creating a new property in |
0 | 854 |
* the class to keep track of the object and new objects of the same class that |
855 |
* need to be added. |
|
856 |
* |
|
857 |
* It also allows for the removal of actions and filters for objects after they |
|
858 |
* change class properties. It is possible to include the property $wp_filter_id |
|
859 |
* in your class and set it to "null" or a number to bypass the workaround. |
|
860 |
* However this will prevent you from adding new classes and any new classes |
|
861 |
* will overwrite the previous hook by the same class. |
|
862 |
* |
|
863 |
* Functions and static method callbacks are just returned as strings and |
|
864 |
* shouldn't have any speed penalty. |
|
865 |
* |
|
5 | 866 |
* @link https://core.trac.wordpress.org/ticket/3875 |
867 |
* |
|
868 |
* @since 2.2.3 |
|
0 | 869 |
* @access private |
5 | 870 |
* |
871 |
* @global array $wp_filter Storage for all of the filters and actions. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
* @staticvar int $filter_id_count |
0 | 873 |
* |
5 | 874 |
* @param string $tag Used in counting how many hooks were applied |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
* @param callable $function Used for creating unique id |
5 | 876 |
* @param int|bool $priority Used in counting how many hooks were applied. If === false |
877 |
* and $function is an object reference, we return the unique |
|
878 |
* id only if it already has one, false otherwise. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
* @return string|false Unique ID for usage as array key or false if $priority === false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
* and $function is an object reference, and it does not already have |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
* a unique id. |
0 | 882 |
*/ |
883 |
function _wp_filter_build_unique_id($tag, $function, $priority) { |
|
884 |
global $wp_filter; |
|
885 |
static $filter_id_count = 0; |
|
886 |
||
887 |
if ( is_string($function) ) |
|
888 |
return $function; |
|
889 |
||
890 |
if ( is_object($function) ) { |
|
891 |
// Closures are currently implemented as objects |
|
892 |
$function = array( $function, '' ); |
|
893 |
} else { |
|
894 |
$function = (array) $function; |
|
895 |
} |
|
896 |
||
897 |
if (is_object($function[0]) ) { |
|
898 |
// Object Class Calling |
|
899 |
if ( function_exists('spl_object_hash') ) { |
|
900 |
return spl_object_hash($function[0]) . $function[1]; |
|
901 |
} else { |
|
902 |
$obj_idx = get_class($function[0]).$function[1]; |
|
903 |
if ( !isset($function[0]->wp_filter_id) ) { |
|
904 |
if ( false === $priority ) |
|
905 |
return false; |
|
906 |
$obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count; |
|
907 |
$function[0]->wp_filter_id = $filter_id_count; |
|
908 |
++$filter_id_count; |
|
909 |
} else { |
|
910 |
$obj_idx .= $function[0]->wp_filter_id; |
|
911 |
} |
|
912 |
||
913 |
return $obj_idx; |
|
914 |
} |
|
5 | 915 |
} elseif ( is_string( $function[0] ) ) { |
0 | 916 |
// Static Calling |
917 |
return $function[0] . '::' . $function[1]; |
|
918 |
} |
|
919 |
} |