我的知识库

知识等于力量

« Building Web Services the REST WayEJB3 再战 Spring »

Dissect WordPress Plugin(深入剖析WordPress插件机制)

一直对软件的组件、插件架构非常感兴趣,我认为现在开发任何应用程序,一定要有一个架构良好的插件机制,这样可以吸引其他人来开发插件,极大的扩充系统的功能。Eclipse就是一个非常棒的例子,几乎你想什么功能,都可以从社区中找到相应的第三方插件。Firefox也同样如此,它的插件机制非常灵活(尤其是相对IE而言),因此Firefox社区中可以找到各种各样的插件。

就像Nat在Open Source Summit中的一句话

You have to give people work to do.
Create an “architecture of participation” as Tim O’Reilly says.

自从开始使用WordPress,就想学习学习WordPress强大的插件机制。以前曾经研究过的COM、XPCOM、Firefox Extensions、Eclipse Plugin都是桌面应用级别的组件结构,而对Web应用中的插件确实知之甚少,于是,Dissect了一下WordPress(1.5.1.3)。虽然对PHP不是很熟悉,不过看看源码还是问题不大。

1)WordPress读取所有可用的插件

在文件“admin-functions.php”中,函数

  1. <?php function get_plugins() ?> ?>

用来从文件系统得到所有的插件。原理很简单,就是读取’wp-content/plugins’目录下的所有PHP文件。这个函数允许一级的子文件夹,也就是说在’wp-content/plugins’下面的PHP文件,以及所以在此目录下的一级子文件夹内部的PHP文件被列作插件的候选,用下面的函数去进一步提取插件信息。这样的好处是方便用户利用文件夹来对插件进行管理和组织。

而函数

  1. <?php function get_plugin_data() ?>

用来得到插件的描述(Plugin Descriptor),主要包括插件的版本、名称、作者,等信息,而这些其实是以注释的方式存在的。用WordPress中自带的Hello插件来举例:

  1. <?php 
  2. Plugin Name: Hello Dolly
  3. Plugin URI: http://wordpress.org/#
  4. Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire ...
  5. Author: Matt Mullenweg
  6. Version: 1.0
  7. Author URI: http://photomatt.net/
  8.  ?>

这样,在get_plugin_data函数中,就可以来得到插件的详细信息。

  1. <?php 
  2. function get_plugin_data($plugin_file) {
  3.     $plugin_data = implode('', file($plugin_file));
  4.     preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name);
  5.     preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri);
  6.     preg_match("|Description:(.*)|i", $plugin_data, $description);
  7.     preg_match("|Author:(.*)|i", $plugin_data, $author_name);
  8.     preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri);
  9.  ?>

2)Active & Deactive Plugin

Active(Deactive)插件的操作都在Plugins.php中,比如我要Deactive “Hello”这个插件,最后的URL其实是这个样子:

  1. <?php http://localhost/blog/wp-admin/plugins.php?action=deactivate&amp;plugin=hello.php ?>

其中,“Action”表示动作,值为“active”或者“deactivate”,而“Plugin”表示动作的对象插件,此处为“hello.php”。得到动作指令后,首先从数据库中取出当前已经激活的插件。

  1. <?php $current = get_settings('active_plugins'); ?>

然后根据动作,重新生成已激活插件数组,存入数据库,并重新加载此页。加载的时候就需要考虑这些已经激活的插件是怎么工作的了。

BTW:附上数据库的Options表中0插件和只有1个插件的值:

没有插件:’a:1:{i:0;s:0:"";}’

只有Hello插件:’a:2:{i:0;s:0:"";i:1;s:9:"hello.php";}’

3)插件(Active)如何加载到系统中

WordPress中的每页都会包含“wp-config.php”文件,而在“wp-config.php”的最后有这样一句:

  1. <?php require_once(ABSPATH.'wp-settings.php'); ?>

在“wp-settings.php”文件中,可以找到以下与插件相关的代码片断:

  1. <?php 
  2. if ( get_settings('active_plugins') ) {
  3.     $current_plugins = get_settings('active_plugins');
  4.     if ( is_array($current_plugins) ) {
  5.         foreach ($current_plugins as $plugin) {
  6.             if ('' != $plugin && file_exists(ABSPATH . 'wp-content/plugins/' . $plugin))
  7.                 include_once(ABSPATH . 'wp-content/plugins/' . $plugin);
  8.         }
  9.     }
  10. }
  11.  ?>

可见,这段代码会取出系统中所有Active的插件,并Include进来。所以在每页加载的时候,都会首先Include这些插件代码。那么,这些插件自己在加载的时候都做了什么呢?

4)插件的加载

插件的加载其实最重要的一个部分就是插件的事件注册机制,WordPress插件中的事件注册其实和Eclipse中的扩展点(Extension-Point)机制非常相像,而这种类似“插销”、“插销座”的软件插拔方式也成为了最近软件组件架构方面应用最多的实践。

事件注册过程中比较重要的几个函数分别是:do_action、add_action、add_filter。

WordPress中默认定义了很多扩展点(也可以叫做“钩子”),或者说注册了很多系统事件(WP中的正规叫法应该是“Action Tag”),比如“admin_head”表示Admin页面的Head输出事件,“publish_post”表示发布一篇帖子的事件等等。而插件要做的就是扩展这些扩展点,或者说挂接这些钩子,从而实现系统的扩展功能。add_action就是通常插件扩展某个扩展点用到的函数,而do_action是扩展点本身开始执行的函数。

刚才说过WordPress中的每一页执行前都会Include所有Active的插件代码,而这些代码通常都会用“add_action”来将自己的函数注册到系统的扩展点中。这样,在扩展点执行的时候,就会找到系统中所有已经挂接到这个扩展点上的插件的函数来执行之,从而扩充系统的功能。

WordPress中的很多功能也都是通过这种插件结构来实现的,默认注册了很多系统事件,都在’default-filter.php’中。比如:

  1. <?php add_action('publish_post', 'generic_ping'); ?>

这个是用来在发布每篇帖子的时候发送XML-RPC Ping的。再比如:

  1. <?php add_filter('the_content', 'convert_smilies'); ?>

用来将正文(content)中的笑脸符号转换为图像。

还是举“Hello”插件来说。Hello插件会随机的在Admin Page的右上角显示一段话,它的工作原理是这样的:

在每一个Admin page的前面都有

  1. <?php require_once('admin-header.php'); ?>

而在“admin-head.php”中将会执行扩展点“admin_head”的所有扩展:

  1. <?php 
  2. do_action('admin_head', '');
  3.  ?>

这样,就会执行所有挂接到admin_head的函数

Admin Page的Footer部分同样如此,

  1. <?php 
  2. do_action('admin_footer', '');
  3.  ?>

这样,就会执行所有挂接到admin_footer的函数

再来看看Hello插件的初始化过程中:

  1. <?php 
  2.  
  3. // This just echoes the chosen line, we'll position it later
  4. function hello_dolly() {
  5.     global $chosen;
  6.     echo "<p id='dolly'>$chosen</p>";
  7. }
  8.  
  9. // Now we set that function up to execute when the admin_footer action is called
  10. add_action('admin_footer', 'hello_dolly');
  11.  
  12. // We need some CSS to position the paragraph
  13. function dolly_css() {
  14.     echo "
  15.     <style type='text/css'>
  16.     #dolly {
  17.         position: absolute;
  18.         top: 5px;
  19. margin: 0; padding: 0;
  20.         right: 3em;
  21.         font-size: 20px;
  22.         color: #f66;
  23.     }
  24.     </style>
  25.     ";
  26. }
  27.  
  28. add_action('admin_head', 'dolly_css');
  29.  
  30. ?>

可见,将dolly的CSS部分插到Header部分,而在footer部分插入显示代码,这样,在AdminPage的右上角就会随机显示一段话。

再来看看曾经说过Google Sitemap插件,插件中有一项功能是在发布、修改或者删除文章的时候重新Build Sitemap文件,这个功能就是通过注册系统的三个事件(“publish_post”,“edit_post”,“delete_post”)来完成的。

  1. <?php 
  2. //Register to various events... @WordPress Dev Team: I wish me a 'public_content_changed' action :)
  3. if(defined("SM_ACTIVE") && SM_ACTIVE===true) {
  4.     //If a new post gets published
  5.     add_action('publish_post', 'sm_buildSitemap');
  6.  
  7.     //Existing post gets edited (published or not)
  8.     add_action('edit_post', 'sm_buildSitemap');
  9.  
  10.     //Existing posts gets deleted (published or not)
  11.     add_action('delete_post', 'sm_buildSitemap');
  12. }
  13. #endregion
  14.  ?>

BTW:其中的注释也很有意思,我也觉得确实需要有“public_content_changed”这样一个事件,就不必分别订阅三个单独的事件了。

对了,就在我们刚才说的wp-settings.php中的插件启动后,有

  1. <?php do_action('plugins_loaded'); ?>

这样一个系统事件,可以注册这个事件来做一些希望在所有插件Load完毕而做的事儿。

5)如果插件中涉及UI

其实是一样的。以WordPress FeedBurner Plugin中添加菜单为例:

如果想添加一个菜单,就需要注册“admin_menu”这个Action Tag(系统事件):

  1. <?php add_action('admin_menu', 'ol_add_feedburner_options_page')?>

即可,插件中的这个函数为:

  1. <?php function ol_add_feedburner_options_page() {
  2.   if (function_exists('add_options_page')) {
  3.       add_options_page('FeedBurner', 'FeedBurner', 8, basename(__FILE__), 'ol_feedburner_options_subpanel');
  4.   }<br>}
  5.  ?>

“add_options_page”这个函数就会在系统的“Options”菜单中添加“FeedBurner”这样一个子菜单。

而这个函数其实就是增加 Menu 或者 SubMenu,

  1. <?php 
  2. function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = '') {
  3.     global $submenu;
  4.     global $menu;
  5.  
  6.     $parent = plugin_basename($parent);
  7.     $file = plugin_basename($file);
  8.  
  9.     // If the parent doesn't already have a submenu, add a link to the parent
  10.     // as the first item in the submenu.  If the submenu file is the same as the
  11.     // parent file someone is trying to link back to the parent manually.  In
  12.     // this case, don't automatically add a link back to avoid duplication.
  13.     if (! isset($submenu[$parent]) && $file != $parent) {
  14.         foreach ($menu as $parent_menu) {
  15.             if ($parent_menu[2] == $parent) {
  16.                 $submenu[$parent][] = $parent_menu;
  17.             }
  18.         }
  19.     }
  20.    
  21.     $submenu[$parent][] = array($menu_title, $access_level, $file, $page_title);
  22.  
  23.     $hookname = get_plugin_page_hookname($file, $parent);
  24.     if ( !empty($function) && !empty($hookname) )
  25.         add_action($hookname, $function);
  26.  
  27.     return $hookname;
  28. }
  29.  
  30. function add_options_page($page_title, $menu_title, $access_level, $file, $function = '') {
  31.     return add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function);
  32. }
  33.  ?>

6)其它

还有一些简单的插件就是只提供一些API函数。比如Most_Commented Plugin,它提供一个API “mdv_most_commented”:通过数据库查询得到评论最多的文章,并加以显示。因为这个插件已经被Include过,所以可以用这个API来进行显示

Search

导航

热门文章

最新文章

Powered By duduwolf's wiki 1.0

Copyright 1999-2007 duduwolf.com Some Rights Reserved.