登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

820716的博客-知识分享 向上的心!

知识分享 开阔向上的心胸! http://www.820716.net/blog

 
 
 

日志

 
 

[WordPress]理解wordpress的插件机制 转载  

2009-06-17 18:59:39|  分类: WordPress WPMU |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
< DOCTYPE HTML PUBLIC -WCDTD HTML TransitionalEN>

Wordpress的插件机制使得开发者可以方便地向系统添加自己需要的功能,而这是使得Wordpress得以在全世界流行的重要原因。插件机制的实现主要依靠wp-includes目录下的plugin.php文件,该文件中包含了与插件机制相关的几个函数。

基本原理:和任何官方文档一样,wordpress对插件原理的说明还是显得太过学术。因此用通俗的语言来说就是:在wordpress内核运行时设立了一些标记(tag),当遇到这些标记时,wordpess会自动调用挂载到(hook to)这个标记上的所有函数,该功能是通过数组来实现的,其过程可以直观的表示如下:

[WordPress]理解wordpress的插件机制 转载 - ggmmchou - 820716的博客

这样,用户可以通过plugin API方便的将自定义的功能添加到系统相应的位置。值得注意的是:wordpress定义了两种类型的插件API,行为(actions)和过滤器(filters)。这两种类型的API从代码上看没有任何区别,主要的不同反映在实际应用中。行为(actions)即是在内核执行到某个标记点时所要执行的一系列函数;而过滤器则是内核执行到此标记时,将一些数据(通常是文本)传递给相应的函数,这些函数在数据库和浏览器之间对数据进行相应的修改,因此起到了“过滤器”的作用。

下面对plugin.php中的相关函数作下解释(为了方便理解,没有选择该文件默认的排列顺序):

1.merge_filters

 

function merge_filters($tag) {
global $wp_filter, $merged_filters;

if ( isset($wp_filter['all']) && is_array($wp_filter['all']) ) //由于PHP中的arrray_merge函数要求参数必须是数组,因而要作此判定
$wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]);

if ( isset($wp_filter[$tag]) ){
reset($wp_filter[$tag]);
uksort($wp_filter[$tag], “strnatcasecmp”);
}
$merged_filters[ $tag ] = true; //将$merged_filters[ $tag ] 置为true,表明已经将需要执行的通用函数合并到了该标记($tag)下的$wp_filter队列中
}

解释:这里首先要知道wordpress为了实现插件机制所定义的两个全局数组$wp_filter和$merged_filters,$wp_filter的作用是通过数组来保存某个标记下挂载的函数队列,而$merged_filters的作用则主要体现在本函数中。在wordpress中有一些通用函数是需要被所有过滤器所调用的,merge_filters函数的作用就是将这些函数合并到所有标记所对应的函数队列中,并以$wp_filter数组下的键名”all”来记录。

2.add_filter

 

function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
global $wp_filter, $merged_filters;

$wp_filter[$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_add, $priority)] = array(’function’ => $function_to_add, ‘accepted_args’ => $accepted_args);
unset( $merged_filters[ $tag ] );
return true;
}

解释:该函数的目的是将用户自定义函数$function_to_add,添加到$tag对应的过滤器队列中,并记录相应的优先级$priority和自定义函数的参数个数$accepted_args。

3.apply_filters

 

function apply_filters($tag, $string) {
global $wp_filter, $merged_filters;

if ( !isset( $merged_filters[ $tag ] ) ) //判断$tag所对应的过滤器是否已合并到$wp_filter中
merge_filters($tag);

if ( !isset($wp_filter[$tag]) ) //这步主要出于容错考虑,如:万一插件作者笔误,系统中无此过滤器,那么直接返回$string,不会造成系统错误
return $string;

reset( $wp_filter[ $tag ] );

$args = func_get_args();

do{
foreach( (array) current($wp_filter[$tag]) as $the_ ) //通过循环调用执行每个过滤器队列中的函数,匹配相应的函数参数
if ( !is_null($the_['function']) ){
$args[1] = $string;
$string = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
}

} while ( next($wp_filter[$tag]) );

return $string;
}

解释:该函数将字符$string应用到过滤器标记$tag处,此外,该函数可以接受额外的参数。

4.remove_filter

 

function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
global $wp_filter, $merged_filters;

unset($GLOBALS['wp_filter'][$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_remove, $priority)]);
unset( $merged_filters[ $tag ] );

return true;
}

解释:这个函数相当简单,就是将某个函数从过滤器中移除,记得同时要将$merged_filters数组中对应的$tag键销毁。

5.add_action

 

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
add_filter($tag, $function_to_add, $priority, $accepted_args);
}

解释:向特定的行为添加自定义函数,前面说过,代码上看跟向过滤器添加是完全一样,区别在于应用。

6.do_action

 

function do_action($tag, $arg = ”) {
 global $wp_filter, $wp_actions;

 if ( is_array($wp_actions) )
  $wp_actions[] = $tag; 
 else
  $wp_actions = array($tag);

 $args = array();
 if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) //此步判断是将do_action函数的参数$arg加入参数数组$args中
  $args[] =& $arg[0];
 else
  $args[] = $arg;
 for ( $a = 2; $a < func_num_args(); $a++ ) //若do_action函数还有额外的参数,则继续加入到参数数组$args中
  $args[] = func_get_arg($a);

 merge_filters($tag); //将$tag所对应的过滤器合并到$wp_filter中

 if ( !isset($wp_filter[$tag]) )
  return;

 do{
  foreach( (array) current($wp_filter[$tag]) as $the_ ) //通过循环调用执行每个行为队列中的函数,匹配相应的函数参数
   if ( !is_null($the_['function']) )
    call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

 } while ( next($wp_filter[$tag]) );

}

解释:这个函数跟前面的apply_filters类似,执行某行为标记$tag下的函数队列。

7.remove_action

 

function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
remove_filter($tag, $function_to_remove, $priority, $accepted_args);
}

解释:将某函数从其所属行为的函数队列中清除。

 

 

原文地址:http://blog.sina.com.cn/s/blog_5ed1dcc70100d4pi.html~type=v5_one&label=rela_prevarticle

 

------------------

陈龙
QQ: 84047848
Qzone: http://84047848.qzone.qq.com
MSN: dragonchen82@hotmail.com
Blog: http://ggmmchou.blog.163.com

  评论这张
 
阅读(562)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018