Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/wp-includes/class-wp-hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ private function resort_active_iterations( $new_priority = false, $priority_exis
}
}

// If the current priority was removed, step back so the next() call in the main loop lands correctly.
if ( false !== current( $iteration ) && current( $iteration ) !== $current ) {
prev( $iteration );
}

// 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 ) {
/*
Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/tests/hooks/doAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Member

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:

1) Tests_Hooks_DoAction::test_self_removing_callback_does_not_skip_next_priority
Priority 100 should not be skipped when priority 50 removes itself during iteration.
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
     0 => 10
     1 => 50
-    2 => 100
 )

/var/www/tests/phpunit/tests/hooks/doAction.php:331

$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() {
Copy link
Member

Choose a reason for hiding this comment

The 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
*
Expand Down
Loading