since.2006  

(一)中讲到了wordpress是如何读取系统中所有插件的。

现在我们来看看她是如何激活及停用插件的。

激活及停用插件

wordpress的当前使用的插件列表存放在数据库中,wp_options表,字段option_name值为active_plugins的列存放的就是当前系统中使用的插件。
a:2:{i:0;s:9:"hello.php";i:1;s:16:"wp-db-backup.php";}
就是当前系统中使用的两个插件Hello Dolly、WordPress Database Backup。
这个字段原有类型是array,经过serialize后存放在数据库中,读出时unserialize。

取得当前系统中使用的插件 functions.php | 273行 | function get_settings($setting) {}

  1.     // 这个方法是通用方法,不光是用来读取当前使用插件   
  2.     // 当 $setting = active_plugins 时读取当前使用的插件   
  3.     $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");   
  4.        
  5.     // ......   
  6.   
  7.     ifis_object$row) ) {   
  8.         $value = $row->option_value;   
  9.         // 存入缓存中,下次直接从缓存中取值不用再查询数据库   
  10.         wp_cache_set($setting$value'options');   
  11.     }   
  12.        
  13.     // 对于active_plugins,只是将值unserialize返回   
  14.     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );   
  15.        
  16.     // unserialize前的值   
  17.     // a:2:{i:0;s:9:"hello.php";i:1;s:16:"wp-db-backup.php";}   
  18.   
  19.     // unserialize后的值   
  20.     /*  
  21.     Array  
  22.     (  
  23.         [0] => hello.php  
  24.         [1] => wp-db-backup.php  
  25.     )  
  26.     */  

激活插件

通过链接plugins.php?action=activate&plugin=hello.php
wp-admin/plugins.php | 5行左右 |

  1. // 取得当前系统中使用的插件   
  2. $current = get_settings('active_plugins');   
  3. // 待激活插件不在已激活插件列表中   
  4. if (!in_array($_GET['plugin'], $current)) {   
  5.     // 将新插件添加到已激活插件列表中   
  6.     $current[] = trim( $_GET['plugin'] );   
  7.     sort($current);   
  8.     // 将当前正在使用的插件存放到数据库中   
  9.     // 对于active_plugins,只是将当前使用的插件列表serialize后存放进数据库   
  10.     update_option('active_plugins'$current);   
  11.        
  12.     // ......   
  13. }  

停用插件

通过链接plugins.php?action=deactivate&plugin=hello.php
wp-admin/plugins.php | 16行左右 |

  1. // 取得当前系统中使用的插件   
  2. $current = get_settings('active_plugins');   
  3. // 先搜索待停用插件在使用插件中的位置,再将插件列表这部分内容去掉   
  4. array_splice($currentarray_search$_GET['plugin'], $current), 1 );   
  5. // 将删除待停用过后的插件列表存放到数据库中   
  6. // 对于active_plugins,只是将当前使用的插件列表serialize后存放进数据库   
  7. update_option('active_plugins'$current);   
  8.   
  9. // ......  
Posted by hee at 12:04 PM | Permalink | 评论(0)

wordpress目前非常流行一款开放源代码基于php,mysql的blog系统,尤其是她的插件机制更让人着迷。有很多wordpress插件的相关网站。
让我们来一起剖析她的插件机制的实现吧。

wordpress是怎么读取当前所有插件的?
wordpress的插件存放在wp-content\plugins目录下,可以直接存放在plugins或plugins的二级目录下。
登录后台进入Plugins栏目可以查看当前所有存在的插件相关信息。

以Hello Dolly插件为例。

wp的插件相关描述存放在php文件内容开始的地方

  1. /*  
  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 generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <CITE>Hello, Dolly</CITE> in the upper right of your admin screen on every page.  
  5. Author: Matt Mullenweg  
  6. Version: 1.5  
  7. Author URI: http://photomatt.net/  
  8. */   
读取当前系统中插件文件夹中的文件 admin-functions.php | 1516行 | function get_plugins() {}
  1. // Files in wp-content/plugins directory   
  2.     // 读取系统插件目录中所有文件及文件夹   
  3.     $plugins_dir = @ dir($plugin_root);   
  4.     if ($plugins_dir) {   
  5.         while (($file = $plugins_dir->read()) !== false) {   
  6.             // 跳过当前目录 . 和上级目录 ..   
  7.             if (preg_match('|^\.+$|'$file))   
  8.                 continue;   
  9.             // 如果当前句柄是目录,再循环读取当前目录下所有.php的文件   
  10.             if (is_dir($plugin_root.'/'.$file)) {   
  11.                 $plugins_subdir = @ dir($plugin_root.'/'.$file);   
  12.                 if ($plugins_subdir) {   
  13.                     while (($subfile = $plugins_subdir->read()) !== false) {   
  14.                         // 跳过当前目录 . 和上级目录 ..   
  15.                         if (preg_match('|^\.+$|'$subfile))   
  16.                             continue;   
  17.                         // 如果是php文件则保存到$plugins_files数组中   
  18.                         if (preg_match('|\.php$|'$subfile))   
  19.                             $plugin_files[] = "$file/$subfile";   
  20.                     }   
  21.                 }   
  22.             } else {   
  23.                 // 如果是php文件则保存到$plugins_files数组中   
  24.                 if (preg_match('|\.php$|'$file))   
  25.                     $plugin_files[] = $file;   
  26.             }   
  27.         }   
  28.     }   
  29.        
  30.     foreach ($plugin_files as $plugin_file) {   
  31.         if ( !is_readable("$plugin_root/$plugin_file"))   
  32.             continue;   
  33.            
  34.         // 读取每个插件的相关描述信息,见下   
  35.         $plugin_data = get_plugin_data("$plugin_root/$plugin_file");   
  36.   
  37.         if (emptyempty ($plugin_data['Name'])) {   
  38.             continue;   
  39.         }   
  40.   
  41.         $wp_plugins[plugin_basename($plugin_file)] = $plugin_data;   
  42.     }   
  43.        
  44.     // ......   
  45.        
  46.     // 所有插件相关描述信息就保存在这个数组里了   
  47.     return $wp_plugins;  
分析每个插件的相关描述信息 admin-functions.php | 1486行 | function get_plugin_data($plugin_file) {}
  1. // 将每个php文件中内容读取到$plugin_data中,PHP4.3.0开始可用file_get_contents()来将文件读入到一个字符串返回。   
  2.     $plugin_data = implode('', file($plugin_file));   
  3.     // 用正则读出每个插件的名称、URL、描述、作者、版本等信息   
  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.     if (preg_match("|Version:(.*)|i"$plugin_data$version))   
  10.         $version = trim($version[1]);   
  11.     else  
  12.         $version = '';   
  13.        
  14.     // ......   
  15.        
  16.     return array ('Name' => $name'Title' => $plugin'Description' => $description'Author' => $author'Version' => $version'Template' => $template[1]);  

wp-admin/plugins.php 调用 get_plugins() {} 方法显示所有插件,读取完毕。

 

Posted by hee at 21:03 PM | Permalink | 评论(0)