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