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