Skip to content

Commit 2bf4822

Browse files
committed
Add priority parameter to has_filter()/has_action()
1 parent bafd51b commit 2bf4822

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/wp-includes/class-wp-hook.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,20 @@ public function remove_filter( $hook_name, $callback, $priority ) {
223223
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
224224
*
225225
* @since 4.7.0
226+
* @since 6.9.0 Added the `$priority` parameter.
226227
*
227228
* @param string $hook_name Optional. The name of the filter hook. Default empty.
228229
* @param callable|string|array|false $callback Optional. The callback to check for.
229230
* This method can be called unconditionally to speculatively check
230231
* a callback that may or may not exist. Default false.
232+
* @param int|false $priority Whether to check for a filter at a specific priority.
231233
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
232234
* anything registered. When checking a specific function, the priority
233235
* of that hook is returned, or false if the function is not attached.
236+
* If `$callback` and `$priority` are both provided, a boolean is returned
237+
* for whether the specific function is registered at that priority.
234238
*/
235-
public function has_filter( $hook_name = '', $callback = false ) {
239+
public function has_filter( $hook_name = '', $callback = false, $priority = false ) {
236240
if ( false === $callback ) {
237241
return $this->has_filters();
238242
}
@@ -243,9 +247,15 @@ public function has_filter( $hook_name = '', $callback = false ) {
243247
return false;
244248
}
245249

246-
foreach ( $this->callbacks as $priority => $callbacks ) {
250+
foreach ( $this->callbacks as $callback_priority => $callbacks ) {
247251
if ( isset( $callbacks[ $function_key ] ) ) {
248-
return $priority;
252+
if ( is_int( $priority ) ) {
253+
if ( $priority === (int) $callback_priority ) {
254+
return true;
255+
}
256+
} else {
257+
return $callback_priority;
258+
}
249259
}
250260
}
251261

src/wp-includes/plugin.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,25 +267,29 @@ function apply_filters_ref_array( $hook_name, $args ) {
267267
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
268268
*
269269
* @since 2.5.0
270+
* @since 6.9.0 Added the `$priority` parameter.
270271
*
271272
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
272273
*
273274
* @param string $hook_name The name of the filter hook.
274275
* @param callable|string|array|false $callback Optional. The callback to check for.
275276
* This function can be called unconditionally to speculatively check
276277
* a callback that may or may not exist. Default false.
278+
* @param int|false $priority Whether to check for a filter at a specific priority.
277279
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
278280
* anything registered. When checking a specific function, the priority
279281
* of that hook is returned, or false if the function is not attached.
282+
* If `$callback` and `$priority` are both provided, a boolean is returned
283+
* for whether the specific function is registered at that priority.
280284
*/
281-
function has_filter( $hook_name, $callback = false ) {
285+
function has_filter( $hook_name, $callback = false, $priority = false ) {
282286
global $wp_filter;
283287

284288
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
285289
return false;
286290
}
287291

288-
return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback );
292+
return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback, $priority );
289293
}
290294

291295
/**
@@ -574,19 +578,23 @@ function do_action_ref_array( $hook_name, $args ) {
574578
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
575579
*
576580
* @since 2.5.0
581+
* @since 6.9.0 Added the `$priority` parameter.
577582
*
578583
* @see has_filter() This function is an alias of has_filter().
579584
*
580585
* @param string $hook_name The name of the action hook.
581586
* @param callable|string|array|false $callback Optional. The callback to check for.
582587
* This function can be called unconditionally to speculatively check
583588
* a callback that may or may not exist. Default false.
589+
* @param int|false $priority Whether to check for a filter at a specific priority.
584590
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
585591
* anything registered. When checking a specific function, the priority
586592
* of that hook is returned, or false if the function is not attached.
593+
* If `$callback` and `$priority` are both provided, a boolean is returned
594+
* for whether the specific function is registered at that priority.
587595
*/
588-
function has_action( $hook_name, $callback = false ) {
589-
return has_filter( $hook_name, $callback );
596+
function has_action( $hook_name, $callback = false, $priority = false ) {
597+
return has_filter( $hook_name, $callback, $priority );
590598
}
591599

592600
/**

0 commit comments

Comments
 (0)