Make WordPress Core


Ignore:
Timestamp:
09/18/2023 12:39:18 PM (18 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/hooks/removeFilter.php

    r53804 r56609  
    1818        $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
    1919        $hook->remove_filter( $hook_name, $callback, $priority );
     20        $this->check_priority_non_existent( $hook, $priority );
    2021
    2122        $this->assertArrayNotHasKey( $priority, $hook->callbacks );
     
    3233        $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
    3334        $hook->remove_filter( $hook_name, $callback, $priority );
     35        $this->check_priority_non_existent( $hook, $priority );
    3436
    3537        $this->assertArrayNotHasKey( $priority, $hook->callbacks );
     
    4547        $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
    4648        $hook->remove_filter( $hook_name, $callback, $priority );
     49        $this->check_priority_non_existent( $hook, $priority );
    4750
    4851        $this->assertArrayNotHasKey( $priority, $hook->callbacks );
     
    6366
    6467        $this->assertCount( 1, $hook->callbacks[ $priority ] );
     68        $this->check_priority_exists( $hook, $priority, 'Has priority of 2' );
    6569    }
    6670
     
    7781
    7882        $hook->remove_filter( $hook_name, $callback_one, $priority );
     83        $this->check_priority_non_existent( $hook, $priority );
    7984        $this->assertArrayNotHasKey( $priority, $hook->callbacks );
    8085        $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
     86        $this->check_priority_exists( $hook, $priority + 1, 'Should priority of 3' );
     87    }
     88
     89    protected function check_priority_non_existent( $hook, $priority ) {
     90        $priorities = $this->get_priorities( $hook );
     91
     92        $this->assertNotContains( $priority, $priorities );
     93    }
     94
     95    protected function check_priority_exists( $hook, $priority ) {
     96        $priorities = $this->get_priorities( $hook );
     97
     98        $this->assertContains( $priority, $priorities );
     99    }
     100
     101    protected function get_priorities( $hook ) {
     102        $reflection          = new ReflectionClass( $hook );
     103        $reflection_property = $reflection->getProperty( 'priorities' );
     104        $reflection_property->setAccessible( true );
     105
     106        return $reflection_property->getValue( $hook );
    81107    }
    82108}
Note: See TracChangeset for help on using the changeset viewer.