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