Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Plugin API: WP_Hook class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Plugin
|
|
|
|
* @since 4.7.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to implement action and filter hook functionality.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @see Iterator
|
|
|
|
* @see ArrayAccess
|
|
|
|
*/
|
|
|
|
final class WP_Hook implements Iterator, ArrayAccess {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook callbacks.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $callbacks = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The priority keys of actively running iterations of a hook.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $iterations = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current priority of actively running iterations of a hook.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $current_priority = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of levels this hook can be recursively called.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $nesting_level = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag for if we're current doing an action, rather than a filter.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
2016-09-08 06:17:30 +02:00
|
|
|
* @var bool
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
private $doing_action = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks a function or method to a specific filter action.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param string $tag The name of the filter to hook the $function_to_add callback to.
|
|
|
|
* @param callable $function_to_add The callback to be run when the filter is applied.
|
2019-11-29 22:42:01 +01:00
|
|
|
* @param int $priority 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.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @param int $accepted_args The number of arguments the function accepts.
|
|
|
|
*/
|
|
|
|
public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
|
2019-11-29 22:42:01 +01:00
|
|
|
$idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
|
|
|
|
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
$priority_existed = isset( $this->callbacks[ $priority ] );
|
|
|
|
|
|
|
|
$this->callbacks[ $priority ][ $idx ] = array(
|
2017-12-01 00:11:00 +01:00
|
|
|
'function' => $function_to_add,
|
|
|
|
'accepted_args' => $accepted_args,
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
);
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If we're adding a new priority to the list, put them back in sorted order.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
|
|
|
|
ksort( $this->callbacks, SORT_NUMERIC );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->nesting_level > 0 ) {
|
|
|
|
$this->resort_active_iterations( $priority, $priority_existed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-08 15:16:55 +02:00
|
|
|
* Handles resetting callback priority keys mid-iteration.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @param false|int $new_priority Optional. The priority of the new filter being added. Default false,
|
|
|
|
* for no priority being added.
|
|
|
|
* @param bool $priority_existed Optional. Flag for whether the priority already existed before the new
|
|
|
|
* filter was added. Default false.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
|
|
|
|
$new_priorities = array_keys( $this->callbacks );
|
|
|
|
|
|
|
|
// If there are no remaining hooks, clear out all running iterations.
|
|
|
|
if ( ! $new_priorities ) {
|
|
|
|
foreach ( $this->iterations as $index => $iteration ) {
|
|
|
|
$this->iterations[ $index ] = $new_priorities;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$min = min( $new_priorities );
|
|
|
|
foreach ( $this->iterations as $index => &$iteration ) {
|
|
|
|
$current = current( $iteration );
|
|
|
|
// If we're already at the end of this iteration, just leave the array pointer where it is.
|
|
|
|
if ( false === $current ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iteration = $new_priorities;
|
|
|
|
|
|
|
|
if ( $current < $min ) {
|
|
|
|
array_unshift( $iteration, $current );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( current( $iteration ) < $current ) {
|
|
|
|
if ( false === next( $iteration ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority...
|
|
|
|
if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) {
|
2016-09-08 06:17:30 +02:00
|
|
|
/*
|
2020-01-29 01:45:18 +01:00
|
|
|
* ...and the new priority is the same as what $this->iterations thinks is the previous
|
2016-09-08 06:17:30 +02:00
|
|
|
* priority, we need to move back to it.
|
|
|
|
*/
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
|
|
|
|
if ( false === current( $iteration ) ) {
|
|
|
|
// If we've already moved off the end of the array, go back to the last element.
|
|
|
|
$prev = end( $iteration );
|
|
|
|
} else {
|
|
|
|
// Otherwise, just go back to the previous element.
|
|
|
|
$prev = prev( $iteration );
|
|
|
|
}
|
|
|
|
if ( false === $prev ) {
|
|
|
|
// Start of the array. Reset, and go about our day.
|
|
|
|
reset( $iteration );
|
|
|
|
} elseif ( $new_priority !== $prev ) {
|
|
|
|
// Previous wasn't the same. Move forward again.
|
|
|
|
next( $iteration );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset( $iteration );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unhooks a function or method from a specific filter action.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2019-11-29 22:42:01 +01:00
|
|
|
* @param string $tag The filter hook to which the function to be removed is hooked.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @param callable $function_to_remove The callback to be removed from running when the filter is applied.
|
|
|
|
* @param int $priority The exact priority used when adding the original filter callback.
|
|
|
|
* @return bool Whether the callback existed before it was removed.
|
|
|
|
*/
|
|
|
|
public function remove_filter( $tag, $function_to_remove, $priority ) {
|
|
|
|
$function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
|
|
|
|
|
|
|
|
$exists = isset( $this->callbacks[ $priority ][ $function_key ] );
|
|
|
|
if ( $exists ) {
|
|
|
|
unset( $this->callbacks[ $priority ][ $function_key ] );
|
|
|
|
if ( ! $this->callbacks[ $priority ] ) {
|
|
|
|
unset( $this->callbacks[ $priority ] );
|
|
|
|
if ( $this->nesting_level > 0 ) {
|
|
|
|
$this->resort_active_iterations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a specific action has been registered for this hook.
|
|
|
|
*
|
2021-01-04 17:58:08 +01:00
|
|
|
* When using the `$function_to_check` argument, this function may return a non-boolean value
|
|
|
|
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
|
2021-01-03 22:57:09 +01:00
|
|
|
*
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @param string $tag Optional. The name of the filter hook. Default empty.
|
|
|
|
* @param callable|false $function_to_check Optional. The callback to check for. Default false.
|
2021-01-04 18:18:04 +01:00
|
|
|
* @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has
|
2021-01-04 17:58:08 +01:00
|
|
|
* anything registered. When checking a specific function, the priority of that
|
|
|
|
* hook is returned, or false if the function is not attached.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function has_filter( $tag = '', $function_to_check = false ) {
|
|
|
|
if ( false === $function_to_check ) {
|
|
|
|
return $this->has_filters();
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
|
|
|
|
if ( ! $function_key ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $this->callbacks as $priority => $callbacks ) {
|
|
|
|
if ( isset( $callbacks[ $function_key ] ) ) {
|
|
|
|
return $priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any callbacks have been registered for this hook.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2016-09-08 06:17:30 +02:00
|
|
|
* @return bool True if callbacks have been registered for the current hook, otherwise false.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function has_filters() {
|
|
|
|
foreach ( $this->callbacks as $callbacks ) {
|
|
|
|
if ( $callbacks ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all callbacks from the current filter.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @param int|false $priority Optional. The priority number to remove. Default false.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function remove_all_filters( $priority = false ) {
|
|
|
|
if ( ! $this->callbacks ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( false === $priority ) {
|
|
|
|
$this->callbacks = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
} elseif ( isset( $this->callbacks[ $priority ] ) ) {
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
unset( $this->callbacks[ $priority ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->nesting_level > 0 ) {
|
|
|
|
$this->resort_active_iterations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-26 00:52:54 +02:00
|
|
|
* Calls the callback functions that have been added to a filter hook.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to filter.
|
2019-05-26 00:52:54 +02:00
|
|
|
* @param array $args Additional parameters to pass to the callback functions.
|
2019-09-15 13:45:56 +02:00
|
|
|
* This array is expected to include $value at index 0.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @return mixed The filtered value after all hooked functions are applied to it.
|
|
|
|
*/
|
|
|
|
public function apply_filters( $value, $args ) {
|
|
|
|
if ( ! $this->callbacks ) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nesting_level = $this->nesting_level++;
|
|
|
|
|
|
|
|
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
|
2017-12-01 00:11:00 +01:00
|
|
|
$num_args = count( $args );
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
|
|
|
|
do {
|
2019-07-03 01:42:58 +02:00
|
|
|
$this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
|
|
|
|
$priority = $this->current_priority[ $nesting_level ];
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
|
|
|
|
foreach ( $this->callbacks[ $priority ] as $the_ ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $this->doing_action ) {
|
|
|
|
$args[0] = $value;
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Avoid the array_slice() if possible.
|
2020-02-09 17:55:09 +01:00
|
|
|
if ( 0 == $the_['accepted_args'] ) {
|
2019-09-15 13:45:56 +02:00
|
|
|
$value = call_user_func( $the_['function'] );
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
} elseif ( $the_['accepted_args'] >= $num_args ) {
|
|
|
|
$value = call_user_func_array( $the_['function'], $args );
|
|
|
|
} else {
|
2017-12-01 00:11:00 +01:00
|
|
|
$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ( false !== next( $this->iterations[ $nesting_level ] ) );
|
|
|
|
|
|
|
|
unset( $this->iterations[ $nesting_level ] );
|
|
|
|
unset( $this->current_priority[ $nesting_level ] );
|
|
|
|
|
|
|
|
$this->nesting_level--;
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-26 00:52:54 +02:00
|
|
|
* Calls the callback functions that have been added to an action hook.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2019-05-26 00:52:54 +02:00
|
|
|
* @param array $args Parameters to pass to the callback functions.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function do_action( $args ) {
|
|
|
|
$this->doing_action = true;
|
|
|
|
$this->apply_filters( '', $args );
|
|
|
|
|
|
|
|
// If there are recursive calls to the current action, we haven't finished it until we get to the last one.
|
|
|
|
if ( ! $this->nesting_level ) {
|
|
|
|
$this->doing_action = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the functions hooked into the 'all' hook.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param array $args Arguments to pass to the hook callbacks. Passed by reference.
|
|
|
|
*/
|
|
|
|
public function do_all_hook( &$args ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$nesting_level = $this->nesting_level++;
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
|
|
|
|
|
|
|
|
do {
|
|
|
|
$priority = current( $this->iterations[ $nesting_level ] );
|
|
|
|
foreach ( $this->callbacks[ $priority ] as $the_ ) {
|
|
|
|
call_user_func_array( $the_['function'], $args );
|
|
|
|
}
|
|
|
|
} while ( false !== next( $this->iterations[ $nesting_level ] ) );
|
|
|
|
|
|
|
|
unset( $this->iterations[ $nesting_level ] );
|
|
|
|
$this->nesting_level--;
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:10:43 +01:00
|
|
|
/**
|
|
|
|
* Return the current priority level of the currently running iteration of the hook.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
|
|
|
|
*/
|
|
|
|
public function current_priority() {
|
|
|
|
if ( false === current( $this->iterations ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current( current( $this->iterations ) );
|
|
|
|
}
|
|
|
|
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Normalizes filters set up before WordPress has initialized to WP_Hook objects.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
2020-11-03 18:36:09 +01:00
|
|
|
* The `$filters` parameter should be an array keyed by hook name, with values
|
|
|
|
* containing either:
|
|
|
|
*
|
|
|
|
* - A `WP_Hook` instance
|
|
|
|
* - An array of callbacks keyed by their priorities
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* $filters = array(
|
|
|
|
* 'wp_fatal_error_handler_enabled' => array(
|
|
|
|
* 10 => array(
|
|
|
|
* array(
|
|
|
|
* 'accepted_args' => 0,
|
|
|
|
* 'function' => function() {
|
|
|
|
* return false;
|
|
|
|
* },
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* );
|
|
|
|
*
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-11-03 18:36:09 +01:00
|
|
|
* @param array $filters Filters to normalize. See documentation above for details.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
* @return WP_Hook[] Array of normalized filters.
|
|
|
|
*/
|
|
|
|
public static function build_preinitialized_hooks( $filters ) {
|
|
|
|
/** @var WP_Hook[] $normalized */
|
|
|
|
$normalized = array();
|
|
|
|
|
|
|
|
foreach ( $filters as $tag => $callback_groups ) {
|
|
|
|
if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
|
|
|
|
$normalized[ $tag ] = $callback_groups;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hook = new WP_Hook();
|
|
|
|
|
|
|
|
// Loop through callback groups.
|
|
|
|
foreach ( $callback_groups as $priority => $callbacks ) {
|
|
|
|
|
|
|
|
// Loop through callbacks.
|
|
|
|
foreach ( $callbacks as $cb ) {
|
|
|
|
$hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$normalized[ $tag ] = $hook;
|
|
|
|
}
|
|
|
|
return $normalized;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether an offset value exists.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @param mixed $offset An offset to check for.
|
|
|
|
* @return bool True if the offset exists, false otherwise.
|
|
|
|
*/
|
|
|
|
public function offsetExists( $offset ) {
|
|
|
|
return isset( $this->callbacks[ $offset ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a value at a specified offset.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/arrayaccess.offsetget.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @param mixed $offset The offset to retrieve.
|
|
|
|
* @return mixed If set, the value at the specified offset, null otherwise.
|
|
|
|
*/
|
|
|
|
public function offsetGet( $offset ) {
|
|
|
|
return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value at a specified offset.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/arrayaccess.offsetset.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @param mixed $offset The offset to assign the value to.
|
|
|
|
* @param mixed $value The value to set.
|
|
|
|
*/
|
|
|
|
public function offsetSet( $offset, $value ) {
|
|
|
|
if ( is_null( $offset ) ) {
|
|
|
|
$this->callbacks[] = $value;
|
|
|
|
} else {
|
|
|
|
$this->callbacks[ $offset ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets a specified offset.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @param mixed $offset The offset to unset.
|
|
|
|
*/
|
|
|
|
public function offsetUnset( $offset ) {
|
|
|
|
unset( $this->callbacks[ $offset ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Returns the current element.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/iterator.current.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @return array Of callbacks at current priority.
|
|
|
|
*/
|
|
|
|
public function current() {
|
|
|
|
return current( $this->callbacks );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Moves forward to the next element.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/iterator.next.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @return array Of callbacks at next priority.
|
|
|
|
*/
|
|
|
|
public function next() {
|
|
|
|
return next( $this->callbacks );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Returns the key of the current element.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/iterator.key.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @return mixed Returns current priority on success, or NULL on failure
|
|
|
|
*/
|
|
|
|
public function key() {
|
|
|
|
return key( $this->callbacks );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Checks if current position is valid.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/iterator.valid.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
2020-10-10 22:02:05 +02:00
|
|
|
* @return bool Whether the current position is valid.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function valid() {
|
|
|
|
return key( $this->callbacks ) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 06:17:30 +02:00
|
|
|
* Rewinds the Iterator to the first element.
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2020-01-20 04:14:06 +01:00
|
|
|
* @link https://www.php.net/manual/en/iterator.rewind.php
|
Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.
Built from https://develop.svn.wordpress.org/trunk@38571
git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 05:55:31 +02:00
|
|
|
*/
|
|
|
|
public function rewind() {
|
|
|
|
reset( $this->callbacks );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|