-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Hooks: Fix resort_active_iterations() skipping next priority on self-removal #10949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrcasual
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
mrcasual:fix/wp-hook-resort-active-iterations-skip
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -291,6 +291,73 @@ public function _filter_do_action_doesnt_change_value3( $value ) { | |
| return 'x3'; | ||
| } | ||
|
|
||
| /** | ||
| * Verify that a callback removing itself during execution does not cause | ||
| * the next priority to be skipped. | ||
| * | ||
| * When a callback is the sole entry at its priority and removes itself | ||
| * mid-iteration, resort_active_iterations() repositions the internal | ||
| * array pointer. Before the fix, the pointer ended up one position too | ||
| * far, causing apply_filters()'s next() call to skip the following | ||
| * priority entirely. | ||
| * | ||
| * @ticket 64653 | ||
| */ | ||
| public function test_self_removing_callback_does_not_skip_next_priority() { | ||
| $hook = new WP_Hook(); | ||
| $hook_name = __FUNCTION__; | ||
| $log = array(); | ||
|
|
||
| $callback_10 = function () use ( &$log ) { | ||
| $log[] = 10; | ||
| }; | ||
|
|
||
| // Callback that removes itself -- the only callback at priority 50. | ||
| $self_removing = function () use ( &$log, &$self_removing, $hook, $hook_name ) { | ||
| $hook->remove_filter( $hook_name, $self_removing, 50 ); | ||
| $log[] = 50; | ||
| }; | ||
|
|
||
| $callback_100 = function () use ( &$log ) { | ||
| $log[] = 100; | ||
| }; | ||
|
|
||
| $hook->add_filter( $hook_name, $callback_10, 10, 0 ); | ||
| $hook->add_filter( $hook_name, $self_removing, 50, 0 ); | ||
| $hook->add_filter( $hook_name, $callback_100, 100, 0 ); | ||
|
|
||
| $hook->do_action( array() ); | ||
|
|
||
| $this->assertSame( array( 10, 50, 100 ), $log, 'Priority 100 should not be skipped when priority 50 removes itself during iteration.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Verify the fix when the self-removing callback is at the first priority. | ||
| * | ||
| * @ticket 64653 | ||
| */ | ||
| public function test_self_removing_callback_at_lowest_priority() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test does not fail without the fix applied. |
||
| $hook = new WP_Hook(); | ||
| $hook_name = __FUNCTION__; | ||
| $log = array(); | ||
|
|
||
| $self_removing = function () use ( &$log, &$self_removing, $hook, $hook_name ) { | ||
| $hook->remove_filter( $hook_name, $self_removing, 10 ); | ||
| $log[] = 10; | ||
| }; | ||
|
|
||
| $callback_50 = function () use ( &$log ) { | ||
| $log[] = 50; | ||
| }; | ||
|
|
||
| $hook->add_filter( $hook_name, $self_removing, 10, 0 ); | ||
| $hook->add_filter( $hook_name, $callback_50, 50, 0 ); | ||
|
|
||
| $hook->do_action( array() ); | ||
|
|
||
| $this->assertSame( array( 10, 50 ), $log, 'Priority 50 should execute when priority 10 removes itself.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Use this rather than MockAction so we can test callbacks with no args | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can confirm this test fails without the fix applied: