Changeset 56609
- Timestamp:
- 09/18/2023 12:39:18 PM (17 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-hook.php
r56549 r56609 28 28 29 29 /** 30 * Priorities list. 31 * 32 * @since 6.4.0 33 * @var array 34 */ 35 protected $priorities = array(); 36 37 /** 30 38 * The priority keys of actively running iterations of a hook. 31 39 * … … 87 95 } 88 96 97 $this->priorities = array_keys( $this->callbacks ); 98 89 99 if ( $this->nesting_level > 0 ) { 90 100 $this->resort_active_iterations( $priority, $priority_existed ); … … 103 113 */ 104 114 private function resort_active_iterations( $new_priority = false, $priority_existed = false ) { 105 $new_priorities = array_keys( $this->callbacks );115 $new_priorities = $this->priorities; 106 116 107 117 // If there are no remaining hooks, clear out all running iterations. … … 187 197 if ( ! $this->callbacks[ $priority ] ) { 188 198 unset( $this->callbacks[ $priority ] ); 199 200 $this->priorities = array_keys( $this->callbacks ); 189 201 190 202 if ( $this->nesting_level > 0 ) { … … 263 275 264 276 if ( false === $priority ) { 265 $this->callbacks = array(); 277 $this->callbacks = array(); 278 $this->priorities = array(); 266 279 } elseif ( isset( $this->callbacks[ $priority ] ) ) { 267 280 unset( $this->callbacks[ $priority ] ); 281 $this->priorities = array_keys( $this->callbacks ); 268 282 } 269 283 … … 290 304 $nesting_level = $this->nesting_level++; 291 305 292 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );306 $this->iterations[ $nesting_level ] = $this->priorities; 293 307 294 308 $num_args = count( $args ); … … 349 363 public function do_all_hook( &$args ) { 350 364 $nesting_level = $this->nesting_level++; 351 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );365 $this->iterations[ $nesting_level ] = $this->priorities; 352 366 353 367 do { … … 482 496 $this->callbacks[ $offset ] = $value; 483 497 } 498 499 $this->priorities = array_keys( $this->callbacks ); 484 500 } 485 501 … … 496 512 public function offsetUnset( $offset ) { 497 513 unset( $this->callbacks[ $offset ] ); 514 $this->priorities = array_keys( $this->callbacks ); 498 515 } 499 516 -
trunk/tests/phpunit/tests/hooks/addFilter.php
r55081 r56609 36 36 37 37 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 38 $this->check_priority_exists( $hook, $priority ); 38 39 39 40 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); … … 51 52 52 53 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 54 $this->check_priority_exists( $hook, $priority ); 53 55 54 56 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); … … 65 67 66 68 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 69 $this->check_priority_exists( $hook, $priority ); 67 70 68 71 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); … … 80 83 81 84 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 85 $this->check_priority_exists( $hook, $priority ); 82 86 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 83 87 … … 95 99 96 100 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 101 $this->check_priority_exists( $hook, $priority ); 97 102 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 98 103 99 104 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 105 $this->check_priority_exists( $hook, $priority + 1 ); 100 106 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 101 107 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); … … 110 116 111 117 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 118 $this->check_priority_exists( $hook, $priority ); 112 119 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 113 120 … … 124 131 125 132 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 133 $this->check_priority_exists( $hook, $priority ); 126 134 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 127 135 128 136 $hook->add_filter( $hook_name, $callback, $priority + 1, $accepted_args ); 137 $this->check_priority_exists( $hook, $priority + 1 ); 129 138 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 130 139 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); … … 142 151 $hook->add_filter( $hook_name, array( $c, 'action' ), 8, 1 ); 143 152 144 $this->assertSame( array( 5, 8, 10 ), array_keys( $hook->callbacks) );153 $this->assertSame( array( 5, 8, 10 ), $this->get_priorities( $hook ) ); 145 154 } 146 155 … … 149 158 150 159 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 ); 151 160 $this->check_priority_exists( $this->hook, 10 ); 152 161 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 ); 153 162 $this->check_priority_exists( $this->hook, 11 ); 154 163 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 ); 155 164 $this->check_priority_exists( $this->hook, 12 ); 156 165 $value = $this->hook->apply_filters( '', array() ); 157 166 167 $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' ); 168 158 169 $this->assertSame( '24', $value ); 159 170 } … … 163 174 164 175 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 ); 165 176 $this->check_priority_exists( $this->hook, 10 ); 166 177 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 ); 167 178 $this->check_priority_exists( $this->hook, 11 ); 168 179 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 ); 169 180 $this->check_priority_exists( $this->hook, 12 ); 170 181 $value = $this->hook->apply_filters( '', array() ); 182 183 $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' ); 171 184 172 185 $this->assertSame( '12', $value ); … … 184 197 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 ); 185 198 199 $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' ); 200 186 201 $value = $this->hook->apply_filters( '', array() ); 187 202 … … 196 211 $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 ); 197 212 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 ); 198 213 $this->check_priority_exists( $this->hook, 11 ); 199 214 return $value . '2'; 200 215 } … … 206 221 207 222 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 ); 208 223 $this->check_priority_exists( $this->hook, 11 ); 209 224 return $value . '2'; 210 225 } … … 292 307 $this->action_output .= '4'; 293 308 } 309 310 protected function check_priority_exists( $hook, $priority ) { 311 $priorities = $this->get_priorities( $hook ); 312 313 $this->assertContains( $priority, $priorities ); 314 } 315 316 protected function get_priorities( $hook ) { 317 $reflection = new ReflectionClass( $hook ); 318 $reflection_property = $reflection->getProperty( 'priorities' ); 319 $reflection_property->setAccessible( true ); 320 321 return $reflection_property->getValue( $hook ); 322 } 294 323 } -
trunk/tests/phpunit/tests/hooks/removeAllFilters.php
r53804 r56609 19 19 20 20 $hook->remove_all_filters(); 21 $this->check_priority_non_existent( $hook, $priority ); 21 22 22 23 $this->assertFalse( $hook->has_filters() ); … … 35 36 36 37 $hook->remove_all_filters( $priority ); 38 $this->check_priority_non_existent( $hook, $priority ); 37 39 38 40 $this->assertFalse( $hook->has_filter( $hook_name, $callback_one ) ); 39 41 $this->assertTrue( $hook->has_filters() ); 40 42 $this->assertSame( $priority + 1, $hook->has_filter( $hook_name, $callback_two ) ); 43 $this->check_priority_exists( $hook, $priority + 1 ); 44 } 45 46 protected function check_priority_non_existent( $hook, $priority ) { 47 $priorities = $this->get_priorities( $hook ); 48 49 $this->assertNotContains( $priority, $priorities ); 50 } 51 52 protected function check_priority_exists( $hook, $priority ) { 53 $priorities = $this->get_priorities( $hook ); 54 55 $this->assertContains( $priority, $priorities ); 56 } 57 protected function get_priorities( $hook ) { 58 $reflection = new ReflectionClass( $hook ); 59 $reflection_property = $reflection->getProperty( 'priorities' ); 60 $reflection_property->setAccessible( true ); 61 62 return $reflection_property->getValue( $hook ); 41 63 } 42 64 } -
trunk/tests/phpunit/tests/hooks/removeFilter.php
r53804 r56609 18 18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 19 19 $hook->remove_filter( $hook_name, $callback, $priority ); 20 $this->check_priority_non_existent( $hook, $priority ); 20 21 21 22 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 32 33 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 33 34 $hook->remove_filter( $hook_name, $callback, $priority ); 35 $this->check_priority_non_existent( $hook, $priority ); 34 36 35 37 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 45 47 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 46 48 $hook->remove_filter( $hook_name, $callback, $priority ); 49 $this->check_priority_non_existent( $hook, $priority ); 47 50 48 51 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 63 66 64 67 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 68 $this->check_priority_exists( $hook, $priority, 'Has priority of 2' ); 65 69 } 66 70 … … 77 81 78 82 $hook->remove_filter( $hook_name, $callback_one, $priority ); 83 $this->check_priority_non_existent( $hook, $priority ); 79 84 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); 80 85 $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 ); 81 107 } 82 108 }
Note: See TracChangeset
for help on using the changeset viewer.