mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-12-03 19:41:39 +00:00
Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class.
In the `WP_Hook` class the function `array_keys` was called every time an array of hook priorities was needed. For sites with lots of filters or actions, this would result in thousands of calls to the `array_keys` function, which uses server resources. Instead of recomputing this array every time it is needed, only compute it when filters are added and removed, then store the result as a class property. Improve unit tests to ensure this behaviour is tested. Props spacedmonkey, bor0, flixos90, hellofromTonya, mukesh27. Fixes #58458. git-svn-id: https://develop.svn.wordpress.org/trunk@56609 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -26,6 +26,14 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
*/
|
||||
public $callbacks = array();
|
||||
|
||||
/**
|
||||
* Priorities list.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @var array
|
||||
*/
|
||||
protected $priorities = array();
|
||||
|
||||
/**
|
||||
* The priority keys of actively running iterations of a hook.
|
||||
*
|
||||
@@ -86,6 +94,8 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
ksort( $this->callbacks, SORT_NUMERIC );
|
||||
}
|
||||
|
||||
$this->priorities = array_keys( $this->callbacks );
|
||||
|
||||
if ( $this->nesting_level > 0 ) {
|
||||
$this->resort_active_iterations( $priority, $priority_existed );
|
||||
}
|
||||
@@ -102,7 +112,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
* filter was added. Default false.
|
||||
*/
|
||||
private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
|
||||
$new_priorities = array_keys( $this->callbacks );
|
||||
$new_priorities = $this->priorities;
|
||||
|
||||
// If there are no remaining hooks, clear out all running iterations.
|
||||
if ( ! $new_priorities ) {
|
||||
@@ -187,6 +197,8 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
if ( ! $this->callbacks[ $priority ] ) {
|
||||
unset( $this->callbacks[ $priority ] );
|
||||
|
||||
$this->priorities = array_keys( $this->callbacks );
|
||||
|
||||
if ( $this->nesting_level > 0 ) {
|
||||
$this->resort_active_iterations();
|
||||
}
|
||||
@@ -262,9 +274,11 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
}
|
||||
|
||||
if ( false === $priority ) {
|
||||
$this->callbacks = array();
|
||||
$this->callbacks = array();
|
||||
$this->priorities = array();
|
||||
} elseif ( isset( $this->callbacks[ $priority ] ) ) {
|
||||
unset( $this->callbacks[ $priority ] );
|
||||
$this->priorities = array_keys( $this->callbacks );
|
||||
}
|
||||
|
||||
if ( $this->nesting_level > 0 ) {
|
||||
@@ -289,7 +303,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
|
||||
$nesting_level = $this->nesting_level++;
|
||||
|
||||
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
|
||||
$this->iterations[ $nesting_level ] = $this->priorities;
|
||||
|
||||
$num_args = count( $args );
|
||||
|
||||
@@ -348,7 +362,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
*/
|
||||
public function do_all_hook( &$args ) {
|
||||
$nesting_level = $this->nesting_level++;
|
||||
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
|
||||
$this->iterations[ $nesting_level ] = $this->priorities;
|
||||
|
||||
do {
|
||||
$priority = current( $this->iterations[ $nesting_level ] );
|
||||
@@ -481,6 +495,8 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
} else {
|
||||
$this->callbacks[ $offset ] = $value;
|
||||
}
|
||||
|
||||
$this->priorities = array_keys( $this->callbacks );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -495,6 +511,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
#[ReturnTypeWillChange]
|
||||
public function offsetUnset( $offset ) {
|
||||
unset( $this->callbacks[ $offset ] );
|
||||
$this->priorities = array_keys( $this->callbacks );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,6 +35,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
|
||||
$function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
|
||||
$this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
|
||||
@@ -50,6 +51,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
|
||||
$function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
|
||||
$this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
|
||||
@@ -64,6 +66,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
|
||||
$function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
|
||||
$this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
|
||||
@@ -79,6 +82,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
|
||||
$hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args );
|
||||
@@ -94,9 +98,11 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
|
||||
$hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority + 1 );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
|
||||
}
|
||||
@@ -109,6 +115,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
@@ -123,9 +130,11 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$accepted_args = 2;
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority + 1, $accepted_args );
|
||||
$this->check_priority_exists( $hook, $priority + 1 );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
|
||||
}
|
||||
@@ -141,20 +150,22 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$hook->add_filter( $hook_name, array( $b, 'action' ), 5, 1 );
|
||||
$hook->add_filter( $hook_name, array( $c, 'action' ), 8, 1 );
|
||||
|
||||
$this->assertSame( array( 5, 8, 10 ), array_keys( $hook->callbacks ) );
|
||||
$this->assertSame( array( 5, 8, 10 ), $this->get_priorities( $hook ) );
|
||||
}
|
||||
|
||||
public function test_remove_and_add() {
|
||||
$this->hook = new WP_Hook();
|
||||
|
||||
$this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 10 );
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 11 );
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 12 );
|
||||
$value = $this->hook->apply_filters( '', array() );
|
||||
|
||||
$this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
|
||||
|
||||
$this->assertSame( '24', $value );
|
||||
}
|
||||
|
||||
@@ -162,13 +173,15 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$this->hook = new WP_Hook();
|
||||
|
||||
$this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 10 );
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 11 );
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 12 );
|
||||
$value = $this->hook->apply_filters( '', array() );
|
||||
|
||||
$this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
|
||||
|
||||
$this->assertSame( '12', $value );
|
||||
}
|
||||
|
||||
@@ -183,6 +196,8 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
|
||||
|
||||
$this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
|
||||
|
||||
$value = $this->hook->apply_filters( '', array() );
|
||||
|
||||
$this->assertSame( '1-134-234', $value );
|
||||
@@ -195,7 +210,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
public function _filter_remove_and_add2( $value ) {
|
||||
$this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 );
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 11 );
|
||||
return $value . '2';
|
||||
}
|
||||
|
||||
@@ -205,7 +220,7 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
$value .= '-' . $this->hook->apply_filters( '', array() ) . '-';
|
||||
|
||||
$this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
|
||||
|
||||
$this->check_priority_exists( $this->hook, 11 );
|
||||
return $value . '2';
|
||||
}
|
||||
|
||||
@@ -291,4 +306,18 @@ class Tests_Hooks_AddFilter extends WP_UnitTestCase {
|
||||
public function _action_remove_and_add4() {
|
||||
$this->action_output .= '4';
|
||||
}
|
||||
|
||||
protected function check_priority_exists( $hook, $priority ) {
|
||||
$priorities = $this->get_priorities( $hook );
|
||||
|
||||
$this->assertContains( $priority, $priorities );
|
||||
}
|
||||
|
||||
protected function get_priorities( $hook ) {
|
||||
$reflection = new ReflectionClass( $hook );
|
||||
$reflection_property = $reflection->getProperty( 'priorities' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
return $reflection_property->getValue( $hook );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ class Tests_Hooks_RemoveAllFilters extends WP_UnitTestCase {
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
|
||||
$hook->remove_all_filters();
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
|
||||
$this->assertFalse( $hook->has_filters() );
|
||||
}
|
||||
@@ -34,9 +35,30 @@ class Tests_Hooks_RemoveAllFilters extends WP_UnitTestCase {
|
||||
$hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
|
||||
|
||||
$hook->remove_all_filters( $priority );
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
|
||||
$this->assertFalse( $hook->has_filter( $hook_name, $callback_one ) );
|
||||
$this->assertTrue( $hook->has_filters() );
|
||||
$this->assertSame( $priority + 1, $hook->has_filter( $hook_name, $callback_two ) );
|
||||
$this->check_priority_exists( $hook, $priority + 1 );
|
||||
}
|
||||
|
||||
protected function check_priority_non_existent( $hook, $priority ) {
|
||||
$priorities = $this->get_priorities( $hook );
|
||||
|
||||
$this->assertNotContains( $priority, $priorities );
|
||||
}
|
||||
|
||||
protected function check_priority_exists( $hook, $priority ) {
|
||||
$priorities = $this->get_priorities( $hook );
|
||||
|
||||
$this->assertContains( $priority, $priorities );
|
||||
}
|
||||
protected function get_priorities( $hook ) {
|
||||
$reflection = new ReflectionClass( $hook );
|
||||
$reflection_property = $reflection->getProperty( 'priorities' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
return $reflection_property->getValue( $hook );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class Tests_Hooks_RemoveFilter extends WP_UnitTestCase {
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$hook->remove_filter( $hook_name, $callback, $priority );
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
|
||||
$this->assertArrayNotHasKey( $priority, $hook->callbacks );
|
||||
}
|
||||
@@ -31,6 +32,7 @@ class Tests_Hooks_RemoveFilter extends WP_UnitTestCase {
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$hook->remove_filter( $hook_name, $callback, $priority );
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
|
||||
$this->assertArrayNotHasKey( $priority, $hook->callbacks );
|
||||
}
|
||||
@@ -44,6 +46,7 @@ class Tests_Hooks_RemoveFilter extends WP_UnitTestCase {
|
||||
|
||||
$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
$hook->remove_filter( $hook_name, $callback, $priority );
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
|
||||
$this->assertArrayNotHasKey( $priority, $hook->callbacks );
|
||||
}
|
||||
@@ -62,6 +65,7 @@ class Tests_Hooks_RemoveFilter extends WP_UnitTestCase {
|
||||
$hook->remove_filter( $hook_name, $callback_one, $priority );
|
||||
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority ] );
|
||||
$this->check_priority_exists( $hook, $priority, 'Has priority of 2' );
|
||||
}
|
||||
|
||||
public function test_remove_filter_with_another_at_different_priority() {
|
||||
@@ -76,7 +80,29 @@ class Tests_Hooks_RemoveFilter extends WP_UnitTestCase {
|
||||
$hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
|
||||
|
||||
$hook->remove_filter( $hook_name, $callback_one, $priority );
|
||||
$this->check_priority_non_existent( $hook, $priority );
|
||||
$this->assertArrayNotHasKey( $priority, $hook->callbacks );
|
||||
$this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
|
||||
$this->check_priority_exists( $hook, $priority + 1, 'Should priority of 3' );
|
||||
}
|
||||
|
||||
protected function check_priority_non_existent( $hook, $priority ) {
|
||||
$priorities = $this->get_priorities( $hook );
|
||||
|
||||
$this->assertNotContains( $priority, $priorities );
|
||||
}
|
||||
|
||||
protected function check_priority_exists( $hook, $priority ) {
|
||||
$priorities = $this->get_priorities( $hook );
|
||||
|
||||
$this->assertContains( $priority, $priorities );
|
||||
}
|
||||
|
||||
protected function get_priorities( $hook ) {
|
||||
$reflection = new ReflectionClass( $hook );
|
||||
$reflection_property = $reflection->getProperty( 'priorities' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
return $reflection_property->getValue( $hook );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user