我完成了“浏览所有博客”的计划。我的代码很粗糙,但很有效。
首先,我从Plugin Activation Status 插件。因为该插件只列出哪个插件处于活动状态,在哪里可以方便地获取功能。
我的函数版本在范围上有所缩减,只返回一个以blog ID为键的关联数组和一个以插件基名称为值的数组。网络范围的插件包含在“网络”键下。
// Note: Since the source plugin is licensed under GPLv2 or later, so is this snippet.
function parse_plugins() {
global $wpdb;
$blogs = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} ORDER BY blog_id" );
$network_plugins = $wpdb->get_results( $wpdb->prepare( "SELECT site_id, meta_value FROM {$wpdb->sitemeta} WHERE meta_key=%s", \'active_sitewide_plugins\' ) );
$active = array(\'network\' => array());
foreach ( $network_plugins as $k => $val ) {
$v = maybe_unserialize( $val->meta_value );
if ( ! is_array( $v ) )
continue;
if ( count( v ) <= 0 )
continue;
$tmp = array_values( $v );
/**
* Some records are stored with the plugin name as the key & the timestamp
* of activation as the value; others are stored with just the plugin
* name as the value, with numeric keys
*/
$v = is_numeric( $tmp[0] ) ? array_keys( $v ) : array_values( $v );
$active[\'network\'] = array_merge( $active[\'network\'], $v );
}
/**
* Retrieve all of the plugins active on individual sites
*/
foreach ( $blogs as $b ) {
$wpdb->set_blog_id( $b );
$plugins = maybe_unserialize( $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name=%s", \'active_plugins\' ) ) );
if ( ! is_array( $plugins ) )
continue;
$tmp = array_values( $plugins );
if ( count( $tmp ) <= 0 )
continue;
/**
* Some records are stored with the plugin name as the key & the timestamp
* of activate as the value; others are stored with just the plugin
* name as the value, with numeric keys
*/
$plugins = is_numeric( $tmp[0] ) ? array_keys( $plugins ) : array_values( $plugins );
$active[$b] = $plugins;
}
return $active;
}
现在我们知道了哪个插件处于活动状态,我们可以在网络中漫游并停用它们。
function deactivate_all_the_plugins()
{
$active_plugins = parse_plugins();
// Add the basenames of all unwanted plugins here
$target_plugins = array(
\'hello.php\',
\'akismet/akismet.php\',
\'some-other-plugin/other_plugin.php\'
);
foreach ($active_plugins as $blog_id => $blog_plugins) {
$plugins = array_intersect($target_plugins, $blog_plugins);
if (!empty($plugins)) {
if ($blog_id == \'network\') {
deactivate_plugins($plugins, false, true);
}
else {
switch_to_blog($blog_id);
deactivate_plugins($plugins, false, false);
restore_current_blog();
}
}
echo \'<h1>\'.$blog_id.\'</h1>\'; var_dump($plugins);
}
}
我不会在这里触发卸载挂钩;我们的假设是,该插件在停用时将得到充分清理。此外,我不确定如何在没有实际运行“删除插件”代码的情况下触发它们,WordPress也没有FTP凭据。不过,总比什么都没有好。