Make WordPress Core


Ignore:
Timestamp:
11/07/2007 04:30:11 AM (16 years ago)
Author:
ryan
Message:

has_action and has_filter. see #5231

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/plugin.php

    r6319 r6320  
    8080
    8181/**
     82 * Check if any filter has been registered for a hook.  Optionally returns the priority on that hook for the specified function.
     83 * @package WordPress
     84 * @subpackage Plugin
     85 * @since 2.4
     86 * @global array $wp_filter Stores all of the filters
     87 *
     88 * @param string $tag The name of the filter hook.
     89 * @param callback $function_to_check optional.  If specified, return the priority of that function on this hook or false if not attached.
     90 * @return int|boolean
     91 */
     92function has_filter($tag, $function_to_check = false) {
     93    global $wp_filter;
     94
     95    $has = !empty($wp_filter[$tag]);
     96    if ( false === $function_to_check || false == $has )
     97        return $has;
     98
     99    if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false, 'filter') )
     100        return false;
     101
     102    foreach ( array_keys($wp_filter[$tag]) as $priority ) {
     103        if ( isset($wp_filter[$tag][$priority][$idx]) )
     104            return $priority;
     105    }
     106
     107    return false;
     108}
     109
     110/**
    82111 * Call the functions added to a filter hook.
    83112 *
     
    181210    if ( true === $r) {
    182211        unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
     212        if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
     213            unset($GLOBALS['wp_filter'][$tag][$priority]);
    183214        unset($GLOBALS['merged_filters'][$tag]);
    184215    }
     
    326357 * the functions hooked to <tt>$tag</tt> are supplied using an array.
    327358 *
    328  * @uses merge_filters()
    329  *
    330359 * @package WordPress
    331360 * @subpackage Plugin
     
    378407
    379408/**
     409 * Check if any action has been registered for a hook.  Optionally returns the priority on that hook for the specified function.
     410 * @package WordPress
     411 * @subpackage Plugin
     412 * @since 2.4
     413 * @global array $wp_action Stores all of the actions
     414 *
     415 * @param string $tag The name of the action hook.
     416 * @param callback $function_to_check optional.  If specified, return the priority of that function on this hook or false if not attached.
     417 * @return int|boolean
     418 */
     419function has_action($tag, $function_to_check = false) {
     420    global $wp_action;
     421
     422    $has = !empty($wp_action[$tag]);
     423    if ( false === $function_to_check || false == $has )
     424        return $has;
     425
     426    if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false, 'action') )
     427        return false;
     428
     429    foreach ( array_keys($wp_action[$tag]) as $priority ) {
     430        if ( isset($wp_action[$tag][$priority][$idx]) )
     431            return $priority;
     432    }
     433
     434    return false;
     435}
     436
     437/**
    380438 * Removes a function from a specified action hook.
    381439 *
     
    383441 * method can be used to remove default functions attached to a specific filter
    384442 * hook and possibly replace them with a substitute.
    385  *
    386  * @uses remove_filter() Uses remove_filter to remove actions added.
    387443 *
    388444 * @package WordPress
     
    403459    if ( true === $r) {
    404460        unset($GLOBALS['wp_action'][$tag][$priority][$function_to_remove]);
     461        if ( empty($GLOBALS['wp_action'][$tag][$priority]) )
     462            unset($GLOBALS['wp_action'][$tag][$priority]);
    405463        unset($GLOBALS['merged_actions'][$tag]);
    406464    }
     
    514572 * @param string $tag Used in counting how many hooks were applied
    515573 * @param string|array $function Used for creating unique id
    516  * @param int $priority Used in counting how many hooks were applied
     574 * @param int|bool $priority Used in counting how many hooks were applied.  If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
    517575 * @param string $type filter or action
    518576 * @return string Unique ID for usage as array key
     
    529587        $obj_idx = get_class($function[0]).$function[1];
    530588        if ( !isset($function[0]->wp_filter_id) ) {
     589            if ( false === $priority )
     590                return false;
    531591            if ( 'filter' == $type )
    532592                $count = count((array)$wp_filter[$tag][$priority]);
Note: See TracChangeset for help on using the changeset viewer.