Changeset 61118
- Timestamp:
- 11/03/2025 11:45:40 PM (3 weeks ago)
- Location:
- trunk
- Files:
-
- 5 edited
- src/wp-includes/class-wp-hook.php (modified) (3 diffs)
- src/wp-includes/plugin.php (modified) (5 diffs)
- tests/phpunit/tests/actions.php (modified) (2 diffs)
- tests/phpunit/tests/filters.php (modified) (3 diffs)
- tests/phpunit/tests/hooks/hasFilter.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-hook.php
r60809 r61118 224 224 * 225 225 * @since 4.7.0 226 * @since 6.9.0 Added the `$priority` parameter. 226 227 * 227 228 * @param string $hook_name Optional. The name of the filter hook. Default empty. … … 229 230 * This method can be called unconditionally to speculatively check 230 231 * a callback that may or may not exist. Default false. 232 * @param int|false $priority Optional. The specific priority at which to check for the callback. 233 * Default false. 231 234 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 232 235 * anything registered. When checking a specific function, the priority 233 236 * of that hook is returned, or false if the function is not attached. 234 */ 235 public function has_filter( $hook_name = '', $callback = false ) { 237 * If `$callback` and `$priority` are both provided, a boolean is returned 238 * for whether the specific function is registered at that priority. 239 */ 240 public function has_filter( $hook_name = '', $callback = false, $priority = false ) { 236 241 if ( false === $callback ) { 237 242 return $this->has_filters(); … … 244 249 } 245 250 246 foreach ( $this->callbacks as $priority => $callbacks ) { 251 if ( is_int( $priority ) ) { 252 return isset( $this->callbacks[ $priority ][ $function_key ] ); 253 } 254 255 foreach ( $this->callbacks as $callback_priority => $callbacks ) { 247 256 if ( isset( $callbacks[ $function_key ] ) ) { 248 return $ priority;257 return $callback_priority; 249 258 } 250 259 } -
trunk/src/wp-includes/plugin.php
r60258 r61118 268 268 * 269 269 * @since 2.5.0 270 * @since 6.9.0 Added the `$priority` parameter. 270 271 * 271 272 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. … … 275 276 * This function can be called unconditionally to speculatively check 276 277 * a callback that may or may not exist. Default false. 278 * @param int|false $priority Optional. The specific priority at which to check for the callback. 279 * Default false. 277 280 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 278 281 * anything registered. When checking a specific function, the priority 279 282 * of that hook is returned, or false if the function is not attached. 280 */ 281 function has_filter( $hook_name, $callback = false ) { 283 * If `$callback` and `$priority` are both provided, a boolean is returned 284 * for whether the specific function is registered at that priority. 285 */ 286 function has_filter( $hook_name, $callback = false, $priority = false ) { 282 287 global $wp_filter; 283 288 … … 286 291 } 287 292 288 return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback );293 return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback, $priority ); 289 294 } 290 295 … … 575 580 * 576 581 * @since 2.5.0 582 * @since 6.9.0 Added the `$priority` parameter. 577 583 * 578 584 * @see has_filter() This function is an alias of has_filter(). … … 582 588 * This function can be called unconditionally to speculatively check 583 589 * a callback that may or may not exist. Default false. 590 * @param int|false $priority Optional. The specific priority at which to check for the callback. 591 * Default false. 584 592 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 585 593 * anything registered. When checking a specific function, the priority 586 594 * of that hook is returned, or false if the function is not attached. 587 */ 588 function has_action( $hook_name, $callback = false ) { 589 return has_filter( $hook_name, $callback ); 595 * If `$callback` and `$priority` are both provided, a boolean is returned 596 * for whether the specific function is registered at that priority. 597 */ 598 function has_action( $hook_name, $callback = false, $priority = false ) { 599 return has_filter( $hook_name, $callback, $priority ); 590 600 } 591 601 -
trunk/tests/phpunit/tests/actions.php
r57987 r61118 65 65 66 66 add_action( $hook_name, array( &$a, 'action' ) ); 67 add_action( $hook_name, array( &$a, 'action' ), 100 ); 67 68 do_action( $hook_name ); 68 69 69 70 // Make sure our hook was called correctly. 70 $this->assertSame( 1, $a->get_call_count() );71 $this->assertSame( array( $hook_name ), $a->get_hook_names() );71 $this->assertSame( 2, $a->get_call_count() ); 72 $this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() ); 72 73 73 74 // Now remove the action, do it again, and make sure it's not called this time. 74 75 remove_action( $hook_name, array( &$a, 'action' ) ); 76 remove_action( $hook_name, array( &$a, 'action' ), 100 ); 75 77 do_action( $hook_name ); 76 $this->assertSame( 1, $a->get_call_count() ); 77 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 78 } 79 80 /** 78 $this->assertSame( 2, $a->get_call_count() ); 79 $this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() ); 80 } 81 82 /** 83 * @ticket 64186 81 84 * @covers ::has_action 82 85 */ … … 90 93 add_action( $hook_name, $callback ); 91 94 $this->assertSame( 10, has_action( $hook_name, $callback ) ); 95 $this->assertFalse( has_action( $hook_name, $callback, 9 ) ); 92 96 $this->assertTrue( has_action( $hook_name ) ); 97 98 add_action( $hook_name, $callback, 9 ); 99 add_action( $hook_name, $callback, 11 ); 100 $this->assertSame( 9, has_action( $hook_name, $callback ) ); 101 $this->assertTrue( has_action( $hook_name, $callback, 9 ) ); 102 $this->assertTrue( has_action( $hook_name, $callback, 10 ) ); 103 $this->assertTrue( has_action( $hook_name, $callback, 11 ) ); 104 $this->assertTrue( has_action( $hook_name ) ); 105 106 remove_action( $hook_name, $callback, 9 ); 107 remove_action( $hook_name, $callback, 11 ); 108 $this->assertSame( 10, has_action( $hook_name, $callback ) ); 93 109 94 110 remove_action( $hook_name, $callback ); -
trunk/tests/phpunit/tests/filters.php
r57987 r61118 26 26 } 27 27 28 /** 29 * @covers ::remove_filter 30 */ 28 31 public function test_remove_filter() { 29 32 $a = new MockAction(); … … 32 35 33 36 add_filter( $hook_name, array( $a, 'filter' ) ); 37 add_filter( $hook_name, array( $a, 'filter' ), 100 ); 34 38 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 35 39 36 40 // Make sure our hook was called correctly. 37 $this->assertSame( 1, $a->get_call_count() );38 $this->assertSame( array( $hook_name ), $a->get_hook_names() );41 $this->assertSame( 2, $a->get_call_count() ); 42 $this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() ); 39 43 40 44 // Now remove the filter, do it again, and make sure it's not called this time. 41 45 remove_filter( $hook_name, array( $a, 'filter' ) ); 46 remove_filter( $hook_name, array( $a, 'filter' ), 100 ); 42 47 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 43 $this->assertSame( 1, $a->get_call_count() ); 44 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 45 } 46 48 $this->assertSame( 2, $a->get_call_count() ); 49 $this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() ); 50 } 51 52 /** 53 * @ticket 64186 54 * @covers ::has_filter 55 */ 47 56 public function test_has_filter() { 48 57 $hook_name = __FUNCTION__; … … 54 63 add_filter( $hook_name, $callback ); 55 64 $this->assertSame( 10, has_filter( $hook_name, $callback ) ); 65 $this->assertFalse( has_filter( $hook_name, $callback, 9 ) ); 56 66 $this->assertTrue( has_filter( $hook_name ) ); 67 68 add_filter( $hook_name, $callback, 9 ); 69 add_filter( $hook_name, $callback, 11 ); 70 $this->assertSame( 9, has_filter( $hook_name, $callback ) ); 71 $this->assertTrue( has_filter( $hook_name, $callback, 9 ) ); 72 $this->assertTrue( has_filter( $hook_name, $callback, 10 ) ); 73 $this->assertTrue( has_filter( $hook_name, $callback, 11 ) ); 74 $this->assertTrue( has_filter( $hook_name ) ); 75 76 remove_filter( $hook_name, $callback, 9 ); 77 remove_filter( $hook_name, $callback, 11 ); 78 $this->assertSame( 10, has_filter( $hook_name, $callback ) ); 57 79 58 80 remove_filter( $hook_name, $callback ); -
trunk/tests/phpunit/tests/hooks/hasFilter.php
r53804 r61118 9 9 class Tests_Hooks_HasFilter extends WP_UnitTestCase { 10 10 11 /** 12 * @ticket 64186 13 */ 11 14 public function test_has_filter_with_function() { 12 15 $callback = '__return_null'; 13 16 $hook = new WP_Hook(); 14 17 $hook_name = __FUNCTION__; 15 $priority = 1; 18 $priority_a = 1; 19 $priority_b = 10; 16 20 $accepted_args = 2; 17 21 18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 22 $hook->add_filter( $hook_name, $callback, $priority_a, $accepted_args ); 23 $hook->add_filter( $hook_name, $callback, $priority_b, $accepted_args ); 19 24 20 $this->assertSame( $priority, $hook->has_filter( $hook_name, $callback ) ); 25 $this->assertSame( $priority_a, $hook->has_filter( $hook_name, $callback ) ); 26 $this->assertTrue( $hook->has_filter( $hook_name, $callback, $priority_a ) ); 27 $this->assertTrue( $hook->has_filter( $hook_name, $callback, $priority_b ) ); 28 $hook->remove_filter( $hook_name, $callback, $priority_a ); 29 $this->assertSame( $priority_b, $hook->has_filter( $hook_name, $callback ) ); 21 30 } 22 31
Note: See TracChangeset for help on using the changeset viewer.