Make WordPress Core

Ticket #5231: has_filter_with_arg.diff

File has_filter_with_arg.diff, 5.8 KB (added by mdawaffe, 14 years ago)
  • wp-includes/plugin.php

     
    7979}
    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 *
    84113 * The callback functions attached to filter hook <tt>$tag</tt> are invoked by
     
    180209
    181210        if ( true === $r) {
    182211                unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
     212                if ( !count($GLOBALS['wp_filter'][$tag][$priority]) )
     213                        unset($GLOBALS['wp_filter'][$tag][$priority]);
    183214                unset($GLOBALS['merged_filters'][$tag]);
    184215        }
    185216
     
    325356 * @see do_action() This function is identical, but the arguments passed to
    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
    332361 * @since 2.1
     
    377406}
    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 *
    382440 * This function removes a function attached to a specified action hook. This
    383441 * method can be used to remove default functions attached to a specific filter
    384442 * hook and possibly replace them with a substitute.
    385443 *
    386  * @uses remove_filter() Uses remove_filter to remove actions added.
    387  *
    388444 * @package WordPress
    389445 * @subpackage Plugin
    390446 * @since 1.5
     
    402458
    403459        if ( true === $r) {
    404460                unset($GLOBALS['wp_action'][$tag][$priority][$function_to_remove]);
     461                if ( !count($GLOBALS['wp_action'][$tag][$priority]) )
     462                        unset($GLOBALS['wp_action'][$tag][$priority]);
    405463                unset($GLOBALS['merged_actions'][$tag]);
    406464        }
    407465
     
    513571 * @global array $wp_filter Storage for all of the filters and actions
    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
    519577 */
     
    528586        else if (is_object($function[0]) ) {
    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]);
    533593                        else
  • wp-includes/classes.php

     
    238238                }
    239239
    240240                // query_string filter deprecated.  Use request filter instead.
    241                 global $wp_filter;
    242                 if ( isset($wp_filter['query_string']) ) {  // Don't bother filtering and parsing if no plugins are hooked in.
     241                if ( has_filter('query_string') ) {  // Don't bother filtering and parsing if no plugins are hooked in.
    243242                        $this->query_string = apply_filters('query_string', $this->query_string);
    244243                        parse_str($this->query_string, $this->query_vars);
    245244                }
  • wp-settings.php

     
    2020
    2121wp_unregister_GLOBALS();
    2222
    23 unset( $wp_filter, $cache_lastcommentmodified, $cache_lastpostdate );
     23unset( $wp_filter, $wp_action, $cache_lastcommentmodified, $cache_lastpostdate );
    2424
    2525if ( ! isset($blog_id) )
    2626        $blog_id = 1;
  • wp-admin/includes/plugin.php

     
    329329}
    330330
    331331function get_plugin_page_hook( $plugin_page, $parent_page ) {
    332         global $wp_filter;
    333 
    334332        $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
    335         if ( isset( $wp_filter[$hook] ))
     333        if ( has_action($hook) )
    336334                return $hook;
    337335        else
    338336                return null;