33 |
33 |
34 if ( ! isset( $wp_current_filter ) ) |
34 if ( ! isset( $wp_current_filter ) ) |
35 $wp_current_filter = array(); |
35 $wp_current_filter = array(); |
36 |
36 |
37 /** |
37 /** |
38 * Hooks a function or method to a specific filter action. |
38 * Hook a function or method to a specific filter action. |
39 * |
39 * |
40 * WordPress offers filter hooks to allow plugins to modify |
40 * WordPress offers filter hooks to allow plugins to modify |
41 * various types of internal data at runtime. |
41 * various types of internal data at runtime. |
42 * |
42 * |
43 * A plugin can modify data by binding a callback to a filter hook. When the filter |
43 * A plugin can modify data by binding a callback to a filter hook. When the filter |
44 * is later applied, each bound callback is run in order of priority, and given |
44 * is later applied, each bound callback is run in order of priority, and given |
45 * the opportunity to modify a value by returning a new value. |
45 * the opportunity to modify a value by returning a new value. |
46 * |
46 * |
47 * The following example shows how a callback function is bound to a filter hook. |
47 * The following example shows how a callback function is bound to a filter hook. |
48 * Note that $example is passed to the callback, (maybe) modified, then returned: |
48 * |
49 * |
49 * Note that `$example` is passed to the callback, (maybe) modified, then returned: |
50 * <code> |
50 * |
51 * function example_callback( $example ) { |
51 * function example_callback( $example ) { |
52 * // Maybe modify $example in some way |
52 * // Maybe modify $example in some way. |
53 * return $example; |
53 * return $example; |
54 * } |
54 * } |
55 * add_filter( 'example_filter', 'example_callback' ); |
55 * add_filter( 'example_filter', 'example_callback' ); |
56 * </code> |
|
57 * |
56 * |
58 * Since WordPress 1.5.1, bound callbacks can take as many arguments as are |
57 * Since WordPress 1.5.1, bound callbacks can take as many arguments as are |
59 * passed as parameters in the corresponding apply_filters() call. The $accepted_args |
58 * passed as parameters in the corresponding apply_filters() call. The `$accepted_args` |
60 * parameter allows for calling functions only when the number of args match. |
59 * parameter allows for calling functions only when the number of args match. |
61 * |
60 * |
62 * <strong>Note:</strong> the function will return true whether or not the callback |
61 * *Note:* the function will return true whether or not the callback is valid. |
63 * is valid. It is up to you to take care. This is done for optimization purposes, |
62 * It is up to you to take care. This is done for optimization purposes, |
64 * so everything is as quick as possible. |
63 * so everything is as quick as possible. |
65 * |
64 * |
66 * @package WordPress |
65 * @since 0.71 |
67 * @subpackage Plugin |
|
68 * |
66 * |
69 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
67 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
70 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process. |
68 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, |
71 * |
69 * it doesn't need to run through that process. |
72 * @since 0.71 |
|
73 * |
70 * |
74 * @param string $tag The name of the filter to hook the $function_to_add callback to. |
71 * @param string $tag The name of the filter to hook the $function_to_add callback to. |
75 * @param callback $function_to_add The callback to be run when the filter is applied. |
72 * @param callback $function_to_add The callback to be run when the filter is applied. |
76 * @param int $priority (optional) The order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. |
73 * @param int $priority Optional. Used to specify the order in which the functions |
77 * Default 10. |
74 * associated with a particular action are executed. Default 10. |
78 * @param int $accepted_args (optional) The number of arguments the function accepts. |
75 * Lower numbers correspond with earlier execution, |
79 * Default 1. |
76 * and functions with the same priority are executed |
|
77 * in the order in which they were added to the action. |
|
78 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
80 * @return boolean true |
79 * @return boolean true |
81 */ |
80 */ |
82 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
81 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
83 global $wp_filter, $merged_filters; |
82 global $wp_filter, $merged_filters; |
84 |
83 |
89 } |
88 } |
90 |
89 |
91 /** |
90 /** |
92 * Check if any filter has been registered for a hook. |
91 * Check if any filter has been registered for a hook. |
93 * |
92 * |
94 * @package WordPress |
93 * @since 2.5.0 |
95 * @subpackage Plugin |
94 * |
96 * @since 2.5 |
95 * @global array $wp_filter Stores all of the filters. |
97 * @global array $wp_filter Stores all of the filters |
96 * |
98 * |
97 * @param string $tag The name of the filter hook. |
99 * @param string $tag The name of the filter hook. |
98 * @param callback|bool $function_to_check Optional. The callback to check for. Default false. |
100 * @param callback $function_to_check optional. |
99 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has |
101 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered. |
100 * anything registered. When checking a specific function, the priority of that |
102 * When checking a specific function, the priority of that hook is returned, or false if the function is not attached. |
101 * hook is returned, or false if the function is not attached. When using the |
103 * When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false |
102 * $function_to_check argument, this function may return a non-boolean value |
104 * (e.g.) 0, so use the === operator for testing the return value. |
103 * that evaluates to false (e.g.) 0, so use the === operator for testing the |
|
104 * return value. |
105 */ |
105 */ |
106 function has_filter($tag, $function_to_check = false) { |
106 function has_filter($tag, $function_to_check = false) { |
107 global $wp_filter; |
107 // Don't reset the internal array pointer |
108 |
108 $wp_filter = $GLOBALS['wp_filter']; |
109 $has = !empty($wp_filter[$tag]); |
109 |
|
110 $has = ! empty( $wp_filter[ $tag ] ); |
|
111 |
|
112 // Make sure at least one priority has a filter callback |
|
113 if ( $has ) { |
|
114 $exists = false; |
|
115 foreach ( $wp_filter[ $tag ] as $callbacks ) { |
|
116 if ( ! empty( $callbacks ) ) { |
|
117 $exists = true; |
|
118 break; |
|
119 } |
|
120 } |
|
121 |
|
122 if ( ! $exists ) { |
|
123 $has = false; |
|
124 } |
|
125 } |
|
126 |
110 if ( false === $function_to_check || false == $has ) |
127 if ( false === $function_to_check || false == $has ) |
111 return $has; |
128 return $has; |
112 |
129 |
113 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) ) |
130 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) ) |
114 return false; |
131 return false; |
128 * this function. This function can be used to create a new filter hook by |
145 * this function. This function can be used to create a new filter hook by |
129 * simply calling this function with the name of the new hook specified using |
146 * simply calling this function with the name of the new hook specified using |
130 * the $tag parameter. |
147 * the $tag parameter. |
131 * |
148 * |
132 * The function allows for additional arguments to be added and passed to hooks. |
149 * The function allows for additional arguments to be added and passed to hooks. |
133 * <code> |
150 * |
134 * // Our filter callback function |
151 * // Our filter callback function |
135 * function example_callback( $string, $arg1, $arg2 ) { |
152 * function example_callback( $string, $arg1, $arg2 ) { |
136 * // (maybe) modify $string |
153 * // (maybe) modify $string |
137 * return $string; |
154 * return $string; |
138 * } |
155 * } |
139 * add_filter( 'example_filter', 'example_callback', 10, 3 ); |
156 * add_filter( 'example_filter', 'example_callback', 10, 3 ); |
140 * |
157 * |
141 * // Apply the filters by calling the 'example_callback' function we |
158 * /* |
142 * // "hooked" to 'example_filter' using the add_filter() function above. |
159 * * Apply the filters by calling the 'example_callback' function we |
143 * // - 'example_filter' is the filter hook $tag |
160 * * "hooked" to 'example_filter' using the add_filter() function above. |
144 * // - 'filter me' is the value being filtered |
161 * * - 'example_filter' is the filter hook $tag |
145 * // - $arg1 and $arg2 are the additional arguments passed to the callback. |
162 * * - 'filter me' is the value being filtered |
146 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
163 * * - $arg1 and $arg2 are the additional arguments passed to the callback. |
147 * </code> |
164 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
148 * |
165 * |
149 * @package WordPress |
166 * @since 0.71 |
150 * @subpackage Plugin |
167 * |
151 * |
168 * @global array $wp_filter Stores all of the filters. |
152 * @global array $wp_filter Stores all of the filters |
|
153 * @global array $merged_filters Merges the filter hooks using this function. |
169 * @global array $merged_filters Merges the filter hooks using this function. |
154 * @global array $wp_current_filter stores the list of current filters with the current one last |
170 * @global array $wp_current_filter Stores the list of current filters with the current one last. |
155 * |
171 * |
156 * @since 0.71 |
172 * @param string $tag The name of the filter hook. |
157 * |
173 * @param mixed $value The value on which the filters hooked to `$tag` are applied on. |
158 * @param string $tag The name of the filter hook. |
174 * @param mixed $var Additional variables passed to the functions hooked to `$tag`. |
159 * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on. |
|
160 * @param mixed $var Additional variables passed to the functions hooked to <tt>$tag</tt>. |
|
161 * @return mixed The filtered value after all hooked functions are applied to it. |
175 * @return mixed The filtered value after all hooked functions are applied to it. |
162 */ |
176 */ |
163 function apply_filters( $tag, $value ) { |
177 function apply_filters( $tag, $value ) { |
164 global $wp_filter, $merged_filters, $wp_current_filter; |
178 global $wp_filter, $merged_filters, $wp_current_filter; |
165 |
179 |
166 $args = array(); |
180 $args = array(); |
167 |
181 |
168 // Do 'all' actions first |
182 // Do 'all' actions first. |
169 if ( isset($wp_filter['all']) ) { |
183 if ( isset($wp_filter['all']) ) { |
170 $wp_current_filter[] = $tag; |
184 $wp_current_filter[] = $tag; |
171 $args = func_get_args(); |
185 $args = func_get_args(); |
172 _wp_call_all_hook($args); |
186 _wp_call_all_hook($args); |
173 } |
187 } |
271 * |
284 * |
272 * To remove a hook, the $function_to_remove and $priority arguments must match |
285 * To remove a hook, the $function_to_remove and $priority arguments must match |
273 * when the hook was added. This goes for both filters and actions. No warning |
286 * when the hook was added. This goes for both filters and actions. No warning |
274 * will be given on removal failure. |
287 * will be given on removal failure. |
275 * |
288 * |
276 * @package WordPress |
289 * @since 1.2.0 |
277 * @subpackage Plugin |
290 * |
278 * @since 1.2 |
291 * @param string $tag The filter hook to which the function to be removed is hooked. |
279 * |
|
280 * @param string $tag The filter hook to which the function to be removed is hooked. |
|
281 * @param callback $function_to_remove The name of the function which should be removed. |
292 * @param callback $function_to_remove The name of the function which should be removed. |
282 * @param int $priority optional. The priority of the function (default: 10). |
293 * @param int $priority Optional. The priority of the function. Default 10. |
283 * @param int $accepted_args optional. The number of arguments the function accepts (default: 1). |
|
284 * @return boolean Whether the function existed before it was removed. |
294 * @return boolean Whether the function existed before it was removed. |
285 */ |
295 */ |
286 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { |
296 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { |
287 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority); |
297 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority ); |
288 |
298 |
289 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]); |
299 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); |
290 |
300 |
291 if ( true === $r) { |
301 if ( true === $r ) { |
292 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]); |
302 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); |
293 if ( empty($GLOBALS['wp_filter'][$tag][$priority]) ) |
303 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) { |
294 unset($GLOBALS['wp_filter'][$tag][$priority]); |
304 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] ); |
295 unset($GLOBALS['merged_filters'][$tag]); |
305 } |
|
306 if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) { |
|
307 $GLOBALS['wp_filter'][ $tag ] = array(); |
|
308 } |
|
309 unset( $GLOBALS['merged_filters'][ $tag ] ); |
296 } |
310 } |
297 |
311 |
298 return $r; |
312 return $r; |
299 } |
313 } |
300 |
314 |
301 /** |
315 /** |
302 * Remove all of the hooks from a filter. |
316 * Remove all of the hooks from a filter. |
303 * |
317 * |
304 * @since 2.7 |
318 * @since 2.7.0 |
305 * |
319 * |
306 * @param string $tag The filter to remove hooks from. |
320 * @param string $tag The filter to remove hooks from. |
307 * @param int $priority The priority number to remove. |
321 * @param int|bool $priority Optional. The priority number to remove. Default false. |
308 * @return bool True when finished. |
322 * @return bool True when finished. |
309 */ |
323 */ |
310 function remove_all_filters($tag, $priority = false) { |
324 function remove_all_filters( $tag, $priority = false ) { |
311 global $wp_filter, $merged_filters; |
325 global $wp_filter, $merged_filters; |
312 |
326 |
313 if( isset($wp_filter[$tag]) ) { |
327 if ( isset( $wp_filter[ $tag ]) ) { |
314 if( false !== $priority && isset($wp_filter[$tag][$priority]) ) |
328 if ( false === $priority ) { |
315 unset($wp_filter[$tag][$priority]); |
329 $wp_filter[ $tag ] = array(); |
316 else |
330 } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) { |
317 unset($wp_filter[$tag]); |
331 $wp_filter[ $tag ][ $priority ] = array(); |
318 } |
332 } |
319 |
333 } |
320 if( isset($merged_filters[$tag]) ) |
334 |
321 unset($merged_filters[$tag]); |
335 if ( isset( $merged_filters[ $tag ] ) ) { |
|
336 unset( $merged_filters[ $tag ] ); |
|
337 } |
322 |
338 |
323 return true; |
339 return true; |
324 } |
340 } |
325 |
341 |
326 /** |
342 /** |
327 * Retrieve the name of the current filter or action. |
343 * Retrieve the name of the current filter or action. |
328 * |
344 * |
329 * @package WordPress |
345 * @since 2.5.0 |
330 * @subpackage Plugin |
|
331 * @since 2.5 |
|
332 * |
346 * |
333 * @return string Hook name of the current filter or action. |
347 * @return string Hook name of the current filter or action. |
334 */ |
348 */ |
335 function current_filter() { |
349 function current_filter() { |
336 global $wp_current_filter; |
350 global $wp_current_filter; |
337 return end( $wp_current_filter ); |
351 return end( $wp_current_filter ); |
|
352 } |
|
353 |
|
354 /** |
|
355 * Retrieve the name of the current action. |
|
356 * |
|
357 * @since 3.9.0 |
|
358 * |
|
359 * @return string Hook name of the current action. |
|
360 */ |
|
361 function current_action() { |
|
362 return current_filter(); |
|
363 } |
|
364 |
|
365 /** |
|
366 * Retrieve the name of a filter currently being processed. |
|
367 * |
|
368 * The function current_filter() only returns the most recent filter or action |
|
369 * being executed. did_action() returns true once the action is initially |
|
370 * processed. |
|
371 * |
|
372 * This function allows detection for any filter currently being |
|
373 * executed (despite not being the most recent filter to fire, in the case of |
|
374 * hooks called from hook callbacks) to be verified. |
|
375 * |
|
376 * @since 3.9.0 |
|
377 * |
|
378 * @see current_filter() |
|
379 * @see did_action() |
|
380 * @global array $wp_current_filter Current filter. |
|
381 * |
|
382 * @param null|string $filter Optional. Filter to check. Defaults to null, which |
|
383 * checks if any filter is currently being run. |
|
384 * @return bool Whether the filter is currently in the stack. |
|
385 */ |
|
386 function doing_filter( $filter = null ) { |
|
387 global $wp_current_filter; |
|
388 |
|
389 if ( null === $filter ) { |
|
390 return ! empty( $wp_current_filter ); |
|
391 } |
|
392 |
|
393 return in_array( $filter, $wp_current_filter ); |
|
394 } |
|
395 |
|
396 /** |
|
397 * Retrieve the name of an action currently being processed. |
|
398 * |
|
399 * @since 3.9.0 |
|
400 * |
|
401 * @param string|null $action Optional. Action to check. Defaults to null, which checks |
|
402 * if any action is currently being run. |
|
403 * @return bool Whether the action is currently in the stack. |
|
404 */ |
|
405 function doing_action( $action = null ) { |
|
406 return doing_filter( $action ); |
338 } |
407 } |
339 |
408 |
340 /** |
409 /** |
341 * Hooks a function on to a specific action. |
410 * Hooks a function on to a specific action. |
342 * |
411 * |
343 * Actions are the hooks that the WordPress core launches at specific points |
412 * Actions are the hooks that the WordPress core launches at specific points |
344 * during execution, or when specific events occur. Plugins can specify that |
413 * during execution, or when specific events occur. Plugins can specify that |
345 * one or more of its PHP functions are executed at these points, using the |
414 * one or more of its PHP functions are executed at these points, using the |
346 * Action API. |
415 * Action API. |
347 * |
416 * |
348 * @uses add_filter() Adds an action. Parameter list and functionality are the same. |
417 * @since 1.2.0 |
349 * |
418 * |
350 * @package WordPress |
419 * @param string $tag The name of the action to which the $function_to_add is hooked. |
351 * @subpackage Plugin |
|
352 * @since 1.2 |
|
353 * |
|
354 * @param string $tag The name of the action to which the $function_to_add is hooked. |
|
355 * @param callback $function_to_add The name of the function you wish to be called. |
420 * @param callback $function_to_add The name of the function you wish to be called. |
356 * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. |
421 * @param int $priority Optional. Used to specify the order in which the functions |
357 * @param int $accepted_args optional. The number of arguments the function accept (default 1). |
422 * associated with a particular action are executed. Default 10. |
|
423 * Lower numbers correspond with earlier execution, |
|
424 * and functions with the same priority are executed |
|
425 * in the order in which they were added to the action. |
|
426 * @param int $accepted_args Optional. The number of arguments the function accept. Default 1. |
|
427 * @return bool Will always return true. |
358 */ |
428 */ |
359 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
429 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
360 return add_filter($tag, $function_to_add, $priority, $accepted_args); |
430 return add_filter($tag, $function_to_add, $priority, $accepted_args); |
361 } |
431 } |
362 |
432 |
363 /** |
433 /** |
364 * Execute functions hooked on a specific action hook. |
434 * Execute functions hooked on a specific action hook. |
365 * |
435 * |
366 * This function invokes all functions attached to action hook $tag. It is |
436 * This function invokes all functions attached to action hook `$tag`. It is |
367 * possible to create new action hooks by simply calling this function, |
437 * possible to create new action hooks by simply calling this function, |
368 * specifying the name of the new hook using the <tt>$tag</tt> parameter. |
438 * specifying the name of the new hook using the `$tag` parameter. |
369 * |
439 * |
370 * You can pass extra arguments to the hooks, much like you can with |
440 * You can pass extra arguments to the hooks, much like you can with |
371 * apply_filters(). |
441 * {@see apply_filters()}. |
372 * |
442 * |
373 * @see apply_filters() This function works similar with the exception that |
443 * @since 1.2.0 |
374 * nothing is returned and only the functions or methods are called. |
444 * |
375 * |
445 * @global array $wp_filter Stores all of the filters |
376 * @package WordPress |
|
377 * @subpackage Plugin |
|
378 * @since 1.2 |
|
379 * @global array $wp_filter Stores all of the filters |
|
380 * @global array $wp_actions Increments the amount of times action was triggered. |
446 * @global array $wp_actions Increments the amount of times action was triggered. |
381 * |
447 * |
382 * @param string $tag The name of the action to be executed. |
448 * @param string $tag The name of the action to be executed. |
383 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action. |
449 * @param mixed $arg Optional. Additional arguments which are passed on to the |
384 * @return null Will return null if $tag does not exist in $wp_filter array |
450 * functions hooked to the action. Default empty. |
|
451 * @return null Will return null if $tag does not exist in $wp_filter array. |
385 */ |
452 */ |
386 function do_action($tag, $arg = '') { |
453 function do_action($tag, $arg = '') { |
387 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; |
454 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; |
388 |
455 |
389 if ( ! isset($wp_actions[$tag]) ) |
456 if ( ! isset($wp_actions[$tag]) ) |
535 * |
600 * |
536 * This function removes a function attached to a specified action hook. This |
601 * This function removes a function attached to a specified action hook. This |
537 * method can be used to remove default functions attached to a specific filter |
602 * method can be used to remove default functions attached to a specific filter |
538 * hook and possibly replace them with a substitute. |
603 * hook and possibly replace them with a substitute. |
539 * |
604 * |
540 * @package WordPress |
605 * @since 1.2.0 |
541 * @subpackage Plugin |
606 * |
542 * @since 1.2 |
607 * @param string $tag The action hook to which the function to be removed is hooked. |
543 * |
|
544 * @param string $tag The action hook to which the function to be removed is hooked. |
|
545 * @param callback $function_to_remove The name of the function which should be removed. |
608 * @param callback $function_to_remove The name of the function which should be removed. |
546 * @param int $priority optional The priority of the function (default: 10). |
609 * @param int $priority Optional. The priority of the function. Default 10. |
547 * @return boolean Whether the function is removed. |
610 * @return boolean Whether the function is removed. |
548 */ |
611 */ |
549 function remove_action( $tag, $function_to_remove, $priority = 10 ) { |
612 function remove_action( $tag, $function_to_remove, $priority = 10 ) { |
550 return remove_filter( $tag, $function_to_remove, $priority ); |
613 return remove_filter( $tag, $function_to_remove, $priority ); |
551 } |
614 } |
552 |
615 |
553 /** |
616 /** |
554 * Remove all of the hooks from an action. |
617 * Remove all of the hooks from an action. |
555 * |
618 * |
556 * @since 2.7 |
619 * @since 2.7.0 |
557 * |
620 * |
558 * @param string $tag The action to remove hooks from. |
621 * @param string $tag The action to remove hooks from. |
559 * @param int $priority The priority number to remove them from. |
622 * @param int|bool $priority The priority number to remove them from. Default false. |
560 * @return bool True when finished. |
623 * @return bool True when finished. |
561 */ |
624 */ |
562 function remove_all_actions($tag, $priority = false) { |
625 function remove_all_actions($tag, $priority = false) { |
563 return remove_all_filters($tag, $priority); |
626 return remove_all_filters($tag, $priority); |
564 } |
627 } |
570 /** |
633 /** |
571 * Gets the basename of a plugin. |
634 * Gets the basename of a plugin. |
572 * |
635 * |
573 * This method extracts the name of a plugin from its filename. |
636 * This method extracts the name of a plugin from its filename. |
574 * |
637 * |
575 * @package WordPress |
638 * @since 1.5.0 |
576 * @subpackage Plugin |
|
577 * @since 1.5 |
|
578 * |
|
579 * @access private |
|
580 * |
639 * |
581 * @param string $file The filename of plugin. |
640 * @param string $file The filename of plugin. |
582 * @return string The name of a plugin. |
641 * @return string The name of a plugin. |
583 * @uses WP_PLUGIN_DIR |
642 */ |
584 */ |
643 function plugin_basename( $file ) { |
585 function plugin_basename($file) { |
644 global $wp_plugin_paths; |
586 $file = str_replace('\\','/',$file); // sanitize for Win32 installs |
645 |
587 $file = preg_replace('|/+|','/', $file); // remove any duplicate slash |
646 foreach ( $wp_plugin_paths as $dir => $realdir ) { |
588 $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs |
647 if ( strpos( $file, $realdir ) === 0 ) { |
589 $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash |
648 $file = $dir . substr( $file, strlen( $realdir ) ); |
590 $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs |
649 } |
591 $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash |
650 } |
|
651 |
|
652 $file = wp_normalize_path( $file ); |
|
653 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); |
|
654 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); |
|
655 |
592 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir |
656 $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir |
593 $file = trim($file, '/'); |
657 $file = trim($file, '/'); |
594 return $file; |
658 return $file; |
595 } |
659 } |
596 |
660 |
597 /** |
661 /** |
598 * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in |
662 * Register a plugin's real path. |
599 * @package WordPress |
663 * |
600 * @subpackage Plugin |
664 * This is used in plugin_basename() to resolve symlinked paths. |
601 * @since 2.8 |
665 * |
602 * |
666 * @since 3.9.0 |
603 * @param string $file The filename of the plugin (__FILE__) |
667 * |
604 * @return string the filesystem path of the directory that contains the plugin |
668 * @see plugin_basename() |
|
669 * |
|
670 * @param string $file Known path to the file. |
|
671 * @return bool Whether the path was able to be registered. |
|
672 */ |
|
673 function wp_register_plugin_realpath( $file ) { |
|
674 global $wp_plugin_paths; |
|
675 |
|
676 // Normalize, but store as static to avoid recalculation of a constant value |
|
677 static $wp_plugin_path, $wpmu_plugin_path; |
|
678 if ( ! isset( $wp_plugin_path ) ) { |
|
679 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR ); |
|
680 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR ); |
|
681 } |
|
682 |
|
683 $plugin_path = wp_normalize_path( dirname( $file ) ); |
|
684 $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) ); |
|
685 |
|
686 if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) { |
|
687 return false; |
|
688 } |
|
689 |
|
690 if ( $plugin_path !== $plugin_realpath ) { |
|
691 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath; |
|
692 } |
|
693 |
|
694 return true; |
|
695 } |
|
696 |
|
697 /** |
|
698 * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in. |
|
699 * |
|
700 * @since 2.8.0 |
|
701 * |
|
702 * @param string $file The filename of the plugin (__FILE__). |
|
703 * @return string the filesystem path of the directory that contains the plugin. |
605 */ |
704 */ |
606 function plugin_dir_path( $file ) { |
705 function plugin_dir_path( $file ) { |
607 return trailingslashit( dirname( $file ) ); |
706 return trailingslashit( dirname( $file ) ); |
608 } |
707 } |
609 |
708 |
610 /** |
709 /** |
611 * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in |
710 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. |
612 * @package WordPress |
711 * |
613 * @subpackage Plugin |
712 * @since 2.8.0 |
614 * @since 2.8 |
713 * |
615 * |
714 * @param string $file The filename of the plugin (__FILE__). |
616 * @param string $file The filename of the plugin (__FILE__) |
715 * @return string the URL path of the directory that contains the plugin. |
617 * @return string the URL path of the directory that contains the plugin |
|
618 */ |
716 */ |
619 function plugin_dir_url( $file ) { |
717 function plugin_dir_url( $file ) { |
620 return trailingslashit( plugins_url( '', $file ) ); |
718 return trailingslashit( plugins_url( '', $file ) ); |
621 } |
719 } |
622 |
720 |
688 * folder. This file will be called, if it exists, during the uninstall process |
782 * folder. This file will be called, if it exists, during the uninstall process |
689 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php' |
783 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php' |
690 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before |
784 * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before |
691 * executing. |
785 * executing. |
692 * |
786 * |
693 * @since 2.7 |
787 * @since 2.7.0 |
694 * |
788 * |
695 * @param string $file |
789 * @param string $file Plugin file. |
696 * @param callback $callback The callback to run when the hook is called. Must be a static method or function. |
790 * @param callback $callback The callback to run when the hook is called. Must be |
|
791 * a static method or function. |
697 */ |
792 */ |
698 function register_uninstall_hook( $file, $callback ) { |
793 function register_uninstall_hook( $file, $callback ) { |
699 if ( is_array( $callback ) && is_object( $callback[0] ) ) { |
794 if ( is_array( $callback ) && is_object( $callback[0] ) ) { |
700 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' ); |
795 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' ); |
701 return; |
796 return; |
702 } |
797 } |
703 |
798 |
704 // The option should not be autoloaded, because it is not needed in most |
799 /* |
705 // cases. Emphasis should be put on using the 'uninstall.php' way of |
800 * The option should not be autoloaded, because it is not needed in most |
706 // uninstalling the plugin. |
801 * cases. Emphasis should be put on using the 'uninstall.php' way of |
|
802 * uninstalling the plugin. |
|
803 */ |
707 $uninstallable_plugins = (array) get_option('uninstall_plugins'); |
804 $uninstallable_plugins = (array) get_option('uninstall_plugins'); |
708 $uninstallable_plugins[plugin_basename($file)] = $callback; |
805 $uninstallable_plugins[plugin_basename($file)] = $callback; |
|
806 |
709 update_option('uninstall_plugins', $uninstallable_plugins); |
807 update_option('uninstall_plugins', $uninstallable_plugins); |
710 } |
808 } |
711 |
809 |
712 /** |
810 /** |
713 * Calls the 'all' hook, which will process the functions hooked into it. |
811 * Call the 'all' hook, which will process the functions hooked into it. |
714 * |
812 * |
715 * The 'all' hook passes all of the arguments or parameters that were used for |
813 * The 'all' hook passes all of the arguments or parameters that were used for |
716 * the hook, which this function was called for. |
814 * the hook, which this function was called for. |
717 * |
815 * |
718 * This function is used internally for apply_filters(), do_action(), and |
816 * This function is used internally for apply_filters(), do_action(), and |
719 * do_action_ref_array() and is not meant to be used from outside those |
817 * do_action_ref_array() and is not meant to be used from outside those |
720 * functions. This function does not check for the existence of the all hook, so |
818 * functions. This function does not check for the existence of the all hook, so |
721 * it will fail unless the all hook exists prior to this function call. |
819 * it will fail unless the all hook exists prior to this function call. |
722 * |
820 * |
723 * @package WordPress |
821 * @since 2.5.0 |
724 * @subpackage Plugin |
|
725 * @since 2.5 |
|
726 * @access private |
822 * @access private |
727 * |
|
728 * @uses $wp_filter Used to process all of the functions in the 'all' hook |
|
729 * |
823 * |
730 * @param array $args The collected parameters from the hook that was called. |
824 * @param array $args The collected parameters from the hook that was called. |
731 */ |
825 */ |
732 function _wp_call_all_hook($args) { |
826 function _wp_call_all_hook($args) { |
733 global $wp_filter; |
827 global $wp_filter; |