#64186 closed defect (bug) (fixed)
has_filter()/has_action() do not support an expected $priority param
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | 2.5 |
| Component: | Plugins | Keywords: | has-patch has-unit-tests |
| Focuses: | Cc: |
Description
This is split out from #64178 for visibility. Initially explained in PR.
The has_action() and has_filter() functions are lacking an expected $priority parameter to be able to check whether a given callback is added at a specific priority. This is something I've missed in the past. It also "feels right" given that add_action()/remove_action()/add_filter()/remove_filter() all have an optional $priority parameter. See examples on GitHub and on WP Directory where ecosystem code assumed there was such a $priority parameter. This is important because the same callback can be added multiple times on the same hook with different priorities.
This is needed for #64178 to be able to move up the priority of wp_oembed_add_discovery_links() from 10 to 4. For back-compt, it cannot be unhooked at priority 10 for the sake of the many plugins that remove the action via:
<?php remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
See GitHub Search (1.7k files) and WPDirectory (145 plugins).
So the action needs to be hooked twice:
<?php add_action( 'wp_head', 'wp_oembed_add_discovery_links', 4 ); // Printed after feed_links() and feed_links_extra(). add_action( 'wp_head', 'wp_oembed_add_discovery_links' ); // Unhooked the first time that wp_oembed_add_discovery_links() runs for back-compat.
The wp_oembed_add_discovery_links() function can then make use of the new $priority parameter for has_action() to check whether a plugin has unhooked the function at the original priority:
<?php // For back-compat, short-circuit if a plugin has removed the action at the original priority. if ( ! has_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ) ) { return; } // Prevent running again at the original priority. remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
This was accounting for back-compat was similarly in r60910 with the difference there being a new function was introduced as opposed to reusing the same function.
Change History (4)
This ticket was mentioned in PR #10456 on WordPress/wordpress-develop by @westonruter.
3 months ago
#1
- Keywords has-patch has-unit-tests added
@westonruter commented on PR #10456:
3 months ago
#2
Review from Gemini:
The changes introduce a new $priority parameter to the has_filter method in WP_Hook class and the has_filter and has_action functions in plugin.php. This allows checking for the existence of a filter or action at a specific priority.
Review Summary:
-
src/wp-includes/class-wp-hook.php:- The
has_filtermethod now accepts an optional$priorityparameter. - The docblock for
has_filterhas been updated with@since 6.9.0 Added the $priority parameter.and the@paramand@returndescriptions are correctly updated to reflect the new parameter and its behavior. - The internal logic correctly handles the new
$priorityparameter, including a check foris_int( $priority )and changing theforeachloop variable to$callback_priorityto avoid conflicts.
- The
-
src/wp-includes/plugin.php:- The
has_filterandhas_actionfunctions now accept an optional$priorityparameter and pass it to theWP_Hookclass'shas_filtermethod. - Docblocks for both functions are updated with
@since 6.9.0 Added the $priority parameter.and the@paramand@returndescriptions are correctly updated.
- The
- Test Files (
tests/phpunit/tests/actions.php,tests/phpunit/tests/filters.php,tests/phpunit/tests/hooks/hasFilter.php):- New tests and modifications to existing tests have been added to cover the new
$priorityparameter functionality forhas_actionandhas_filter. -
@ticket 64186has been added to the relevant test methods, which is appropriate for test files.
- New tests and modifications to existing tests have been added to cover the new
Overall Assessment:
The changes are well-implemented and adhere to WordPress coding standards. The docblocks are correctly updated, and the new functionality is covered by unit tests. The code remains compatible with PHP 7.2.
This cherry picks the
has_filter()/has_action()changes from https://github.com/WordPress/wordpress-develop/pull/10449 to isolate them from the changes towp_oembed_add_discovery_links().Trac ticket: https://core.trac.wordpress.org/ticket/64186