Skip to content
Closed
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 ) {
Comment on lines +153 to +154
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
69 changes: 69 additions & 0 deletions tests/phpunit/tests/hooks/doAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,75 @@ 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 +329 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() {
$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.' );
}



Comment on lines +361 to +362
/**
* Use this rather than MockAction so we can test callbacks with no args
*
Expand Down
Loading