wordpress 中的 pluging.php 中的函数 has_filter() 源码的变化及疑惑:
1.版本 3.9.8 中为:global $wp_filter 
 版本 4.3.1 中为:$wp_filter = $GLOBALS['wp_filter']
   这样有什么区别吗?
2.返回值一定为 false 吗?
 版本 4.3.1 的源码 中为 false,
   官方reference 说明中为 bool,可以为 true.
   不知为什么,请高手指点。
3.版本 3.9.8 /版本 4.3.1
   两个版本源码中最后都是 return false ,它和前面的 if 分支 是什么关系?
 ----请高手指点,谢谢。
  
/**版本 3.9.8
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters
 *
 * @param string $tag The name of the filter hook.
 * @param callback $function_to_check optional.
 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
 *  When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
 *  When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
 *  (e.g.) 0, so use the === operator for testing the return value.
 */版本 3.9.8
function has_filter($tag, $function_to_check = false) {
global $wp_filter; $has = !empty($wp_filter[$tag]);
if ( false === $function_to_check || false == $has )
return $has; if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
return false; foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
if ( isset($wp_filter[$tag][$priority][$idx]) )
return $priority;
} return false;
}/**版本 4.3.1
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check is omitted, returns boolean for whether the hook has
 *                   anything registered. When checking a specific function, the priority of that
 *                   hook is returned, or false if the function is not attached. When using the
 *                   $function_to_check argument, this function may return a non-boolean value
 *                   that evaluates to false (e.g.) 0, so use the === operator for testing the
 *                   return value.
 */版本 4.3.1
function has_filter($tag, $function_to_check = false) {
// Don't reset the internal array pointer
$wp_filter = $GLOBALS['wp_filter']; $has = ! empty( $wp_filter[ $tag ] ); // Make sure at least one priority has a filter callback
if ( $has ) {
$exists = false;
foreach ( $wp_filter[ $tag ] as $callbacks ) {
if ( ! empty( $callbacks ) ) {
$exists = true;
break;
}
} if ( ! $exists ) {
$has = false;
}
} if ( false === $function_to_check || false === $has )
return $has; if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
return false; foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
if ( isset($wp_filter[$tag][$priority][$idx]) )
return $priority;
} return false;
}
以下为官方的说明,版本为 4.3.1
Description--http://codex.wordpress.org/Function_Reference/has_filterCheck if any filter has been registered for a hook.Usage <?php has_filter( $tag, $function_to_check ); ?> Parameters$tag
(string) (required) The name of the filter hook.
Default: None
$function_to_check
(callback) (optional) If specified, return the priority of that function on this hook. If the specified function is not attached to this hook, returns false.
Default: False
Returnint | boolean 
If no function is specified: returns true if any function is registered for this hook, or false otherwise. If a function is specified (as the second parameter): returns an integer for the priority of the hook if the function is registered, or false otherwise.
Examples<?php
if ( ! has_filter( 'the_content', 'example_alter_the_content' ) )
    add_filter( 'the_content', 'prefix_alter_the_content' );
?>NotesUses: global array $wp_filter that stores all of the filters
Change Log Since: 2.5.0
Source File has_filter() is located in wp-includes/plugin.php.

解决方案 »

  1.   

    1、没有区别
    2、不是还有 return $priority; 吗?按示例:
    if ( ! has_filter( 'the_content', 'example_alter_the_content' ) ) //如果钩子 example_alter_the_content 不存在
        add_filter( 'the_content', 'prefix_alter_the_content' ); //则添加钩子 prefix_alter_the_content
      

  2.   

    版本 4.3.1 源码 的注释:
    return false|int If $function_to_check is omitted, returns boolean for whether the hook has
     *                   anything registered. When checking a specific function, the priority of that
     *                   hook is returned, or false if the function is not attached. When using the
     *                   $function_to_check argument, this function may return a non-boolean value
     *                   that evaluates to false (e.g.) 0, so use the === operator for testing the
     *                   return value.
    ----我以为,是笔误。应当是:return baloon|int 理由:官方文档可作佐证。
    版本 4.3.1 源码 中的一部分,我想,有问题吧?请看 $exists , 是不是有矛盾?
    // Make sure at least one priority has a filter callback
    if ( $has ) {
    $exists = false;
    foreach ( $wp_filter[ $tag ] as $callbacks ) {
    if ( ! empty( $callbacks ) ) {
    $exists = true;
    break;
    }
    }if ( ! $exists ) {
    $has = false;
    }
    }
      

  3.   

    函数 has_filter($tag, $function_to_check = false)
    传入两个参数:
     $tag 节点
     $function_to_check 钩子函数,默认为空
    返回
    如果 $tag, $function_to_check 为空,返回真
    如果 $tag, $function_to_check 已存在,返回其优先级
    否则返回假您认为毛病在哪里?
      

  4.   

    “$function_to_check 为空”还好理解,就是不指定所挂函数的意思。
    $tag 为空,就不理解了。$wp_filter[ $tag ],难道数组下标会为空,为 null ? 为 0 ?为 None ?
    官方说明中:$tag (string) (required) The name of the filter hook.
          Default: None
    这些牛角尖,真让人费解。
      

  5.   

    你是对这一句不理解?
    $has = ! empty( $wp_filter[ $tag ] );1、$tag 不会为空,否则就不会有执行这个函数的需求
          任何人都不可能对空标记去检查是否有钩子(钩子总是要钩住什么的,无论是鱼嘴,还是尊驾的臀)
    2、$wp_filter 中没有名为 $tag 的项,这是很正常那个的事情!因为可能这项从来就没有登记过(漏网之鱼) 
          如果确实不存在,那么  ! empty( $wp_filter[ $tag ] ) 否定之否定,$has 为假就是说 $wp_filter[ $tag ]  (数组单元)有可能为空
    当然,如果你非要让 $tag = null,那么 $wp_filter[ null ] 也一定是空的
      

  6.   

    领教。$tag default none ,是官方说的。如图:
    再冒昧的问一个问题:
    最后一句:return false  这一句会实际执行吗? 好像永远也轮不到它吧?前面各种情况好像都考虑到了。
      

  7.   

    是否还有没考虑道德情况呢?
    即使没有,留着也没有坏处题外话,如果就写那几行代码,都想你那样推敲好几周,我想他早就饿死了
    当开始计划加这个函数的时候,就只是
    function has_filter($tag, $function_to_check = false) {
      return false;
    }
    这样,并不会影响整个主流程的写作
    其他的代码都是后期一点一点加进去的