Make WordPress Core


Ignore:
Timestamp:
01/29/2010 09:45:32 PM (16 years ago)
Author:
ryan
Message:

Integrate sitewide plugin handling into activate_plugins(), deactivate_plugins(), and plugins.php. fixes #11767 see #11644

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/plugin.php

    r12891 r12903  
    264264
    265265/**
     266 * Check whether the plugin is active for the entire network.
     267 *
     268 * @since 3.0.0
     269 *
     270 * @param string $plugin Base plugin path from plugins directory.
     271 * @return bool True, if active for the network, otherwise false.
     272 */
     273function is_plugin_active_for_network( $plugin ){
     274    if ( !is_multisite() )
     275        return false;
     276
     277    $plugins = get_site_option( 'active_sitewide_plugins');
     278    if ( isset($plugins[$plugin]) )
     279        return true;
     280
     281    return false;
     282}
     283
     284/**
     285 * Checks for "Site Wide Only: true" in the plugin header to see if this should
     286 * be activated as a network wide MU plugin.
     287 *
     288 * @since 3.0.0
     289 *
     290 * @todo Use API for getting arbitrary plugin headers.
     291 *
     292 * @param $file Plugin to check
     293 * $return bool True if plugin is network only, false otherwise.
     294 */
     295function is_network_only_plugin( $file ) {
     296    /* Open the plugin file for reading to check if this is a ms-plugin. */
     297    $fp = @fopen( WP_PLUGIN_DIR . '/' . $file, 'r' );
     298
     299    /* Pull only the first 8kiB of the file in. */
     300    $plugin_data = @fread( $fp, 8192 );
     301
     302    /* PHP will close file handle, but we are good citizens. */
     303    @fclose($fp);
     304
     305    if ( preg_match( '|Site Wide Only:(.*)true$|mi', $plugin_data ) )
     306        return true;
     307
     308    return false;
     309}
     310
     311/**
    266312 * Attempts activation of plugin in a "sandbox" and redirects on success.
    267313 *
     
    285331 * @param string $plugin Plugin path to main plugin file with plugin data.
    286332 * @param string $redirect Optional. URL to redirect to.
     333 * @param bool $network_wide Whether to enable the plugin for all sites in the network or just the current site.  Multisite only. Default is false.
    287334 * @return WP_Error|null WP_Error on invalid file or null on success.
    288335 */
    289 function activate_plugin( $plugin, $redirect = '' ) {
    290     $current = get_option( 'active_plugins', array() );
     336function activate_plugin( $plugin, $redirect = '', $network_wide = false) {
    291337    $plugin  = plugin_basename( trim( $plugin ) );
     338
     339    if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
     340        $network_wide = true;
     341        $current = get_site_option( 'active_sitewide_plugins', array() );
     342    } else {
     343        $current = get_option( 'active_plugins', array() );
     344    }
    292345
    293346    $valid = validate_plugin($plugin);
     
    300353        ob_start();
    301354        @include(WP_PLUGIN_DIR . '/' . $plugin);
    302         $current[] = $plugin;
    303         sort($current);
    304355        do_action( 'activate_plugin', trim( $plugin) );
    305         update_option('active_plugins', $current);
     356        if ( $network_wide ) {
     357            $current[$plugin] = time();
     358            update_site_option( 'active_sitewide_plugins', $current );
     359        } else {
     360            $current[] = $plugin;
     361            sort($current);
     362            update_option('active_plugins', $current);
     363        }
    306364        do_action( 'activate_' . trim( $plugin ) );
    307365        do_action( 'activated_plugin', trim( $plugin) );
     
    324382 */
    325383function deactivate_plugins( $plugins, $silent = false ) {
     384    $network_current = get_site_option( 'active_sitewide_plugins', array() );
    326385    $current = get_option( 'active_plugins', array() );
     386    $do_blog = $do_network = false;
    327387
    328388    foreach ( (array) $plugins as $plugin ) {
     
    333393            do_action( 'deactivate_plugin', trim( $plugin ) );
    334394
    335         $key = array_search( $plugin, (array) $current );
    336 
    337         if ( false !== $key )
    338             array_splice( $current, $key, 1 );
     395        if ( is_plugin_active_for_network($plugin) ) {
     396            // Deactivate network wide
     397            $do_network = true;
     398            unset($network_current[$plugin]);
     399        } else {
     400            // Deactivate for this blog only
     401            $do_blog = true;
     402            $key = array_search( $plugin, (array) $current );
     403
     404            if ( false !== $key )
     405                array_splice( $current, $key, 1 );
     406        }
    339407
    340408        //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
     
    345413    }
    346414
    347     update_option('active_plugins', $current);
     415    if ( $do_blog )
     416        update_option('active_plugins', $current);
     417    if ( $do_network )
     418        update_site_option( 'active_sitewide_plugins', $network_current );
    348419}
    349420
Note: See TracChangeset for help on using the changeset viewer.