-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.' ); | ||
| } | ||
|
Comment on lines
+306
to
+332
|
||
|
|
||
| /** | ||
| * 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.
Author
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.
Yup, that's expected. This test covers the edge case where the self-removing callback is at the lowest priority, which takes a different code path in |
||
| $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 | ||
| * | ||
|
|
||
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: