WordPress.org

Make WordPress Core

Changeset 20525


Ignore:
Timestamp:
04/19/2012 03:41:29 AM (6 years ago)
Author:
nacin
Message:

Clean up plugins.php with regards to recently edited files, deactivations, and the network admin. see #20468, #20104.

  • Limit recently_activated to the site dashboard, and properly remove bulk-activated plugins from the array.
  • Remove code used from before the network admin, such as the unused 'network' plugin_status.
  • Don't try to deactivate a plugin already deactivated.
  • Use more specific caps (manage_network_plugins) rather than is_super_admin().
Location:
trunk/wp-admin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-plugins-list-table.php

    r20236 r20525  
    1414
    1515        $status = 'all';
    16         if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'active', 'inactive', 'recently_activated', 'upgrade', 'network', 'mustuse', 'dropins', 'search' ) ) )
     16        if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search' ) ) )
    1717            $status = $_REQUEST['plugin_status'];
    1818
     
    7979        set_transient( 'plugin_slugs', array_keys( $plugins['all'] ), 86400 );
    8080
    81         $recently_activated = get_option( 'recently_activated', array() );
    82 
    83         $one_week = 7*24*60*60;
    84         foreach ( $recently_activated as $key => $time )
    85             if ( $time + $one_week < time() )
    86                 unset( $recently_activated[$key] );
    87         update_option( 'recently_activated', $recently_activated );
     81        if ( ! $screen->is_network ) {
     82            $recently_activated = get_option( 'recently_activated', array() );
     83
     84            $one_week = 7*24*60*60;
     85            foreach ( $recently_activated as $key => $time )
     86                if ( $time + $one_week < time() )
     87                    unset( $recently_activated[$key] );
     88            update_option( 'recently_activated', $recently_activated );
     89        }
    8890
    8991        foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
    9092            // Filter into individual sections
    91             if ( is_multisite() && is_network_only_plugin( $plugin_file ) && !$screen->is_network ) {
    92                 unset( $plugins['all'][ $plugin_file] );
    93             } elseif ( is_plugin_active_for_network($plugin_file) && !$screen->is_network ) {
     93            if ( ! $screen->is_network && is_plugin_active_for_network( $plugin_file ) ) {
    9494                unset( $plugins['all'][ $plugin_file ] );
    95             } elseif ( is_multisite() && is_network_only_plugin( $plugin_file ) && !current_user_can( 'manage_network_plugins' ) ) {
    96                 $plugins['network'][ $plugin_file ] = $plugin_data;
    97             } elseif ( ( !$screen->is_network && is_plugin_active( $plugin_file ) )
     95            } elseif ( ( ! $screen->is_network && is_plugin_active( $plugin_file ) )
    9896                || ( $screen->is_network && is_plugin_active_for_network( $plugin_file ) ) ) {
    9997                $plugins['active'][ $plugin_file ] = $plugin_data;
     
    216214                    $text = _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count );
    217215                    break;
    218                 case 'network':
    219                     $text = _n( 'Network <span class="count">(%s)</span>', 'Network <span class="count">(%s)</span>', $count );
    220                     break;
    221216                case 'mustuse':
    222217                    $text = _n( 'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', $count );
     
    249244        $screen = get_current_screen();
    250245
    251         if ( 'active' != $status ) {
    252             $action = $screen->is_network ? 'network-activate-selected' : 'activate-selected';
    253             $actions[ $action ] = $screen->is_network ? __( 'Network Activate' ) : __( 'Activate' );
    254         }
     246        if ( 'active' != $status )
     247            $actions['activate-selected'] = $screen->is_network ? __( 'Network Activate' ) : __( 'Activate' );
    255248
    256249        if ( 'inactive' != $status && 'recent' != $status )
     
    284277        echo '<div class="alignleft actions">';
    285278
    286         if ( 'recently_activated' == $status )
     279        $screen = get_current_screen();
     280
     281        if ( ! $screen->is_network && 'recently_activated' == $status )
    287282            submit_button( __( 'Clear List' ), 'secondary', 'clear-recent-list', false );
    288283        elseif ( 'top' == $which && 'mustuse' == $status )
     
    322317        // preorder
    323318        $actions = array(
    324             'network_deactivate' => '', 'deactivate' => '',
    325             'network_only' => '', 'activate' => '',
    326             'network_activate' => '',
     319            'deactivate' => '',
     320            'activate' => '',
    327321            'edit' => '',
    328322            'delete' => '',
     
    349343                $description .= '<p>' . $plugin_data['Description'] . '</p>';
    350344        } else {
    351             $is_active_for_network = is_plugin_active_for_network($plugin_file);
    352345            if ( $screen->is_network )
    353                 $is_active = $is_active_for_network;
     346                $is_active = is_plugin_active_for_network( $plugin_file );
    354347            else
    355348                $is_active = is_plugin_active( $plugin_file );
    356349
    357             if ( $is_active_for_network && !is_super_admin() && !$screen->is_network )
    358                 return;
    359 
    360350            if ( $screen->is_network ) {
    361                 if ( $is_active_for_network ) {
     351                if ( $is_active ) {
    362352                    if ( current_user_can( 'manage_network_plugins' ) )
    363                         $actions['network_deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Network Deactivate') . '</a>';
     353                        $actions['deactivate'] = '<a href="' . wp_nonce_url('plugins.php?action=deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'deactivate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Deactivate this plugin') . '">' . __('Network Deactivate') . '</a>';
    364354                } else {
    365355                    if ( current_user_can( 'manage_network_plugins' ) )
    366                         $actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" class="edit">' . __('Network Activate') . '</a>';
     356                        $actions['activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin for all sites in this network') . '" class="edit">' . __('Network Activate') . '</a>';
    367357                    if ( current_user_can( 'delete_plugins' ) && ! is_plugin_active( $plugin_file ) )
    368358                        $actions['delete'] = '<a href="' . wp_nonce_url('plugins.php?action=delete-selected&amp;checked[]=' . $plugin_file . '&amp;plugin_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bulk-plugins') . '" title="' . esc_attr__('Delete this plugin') . '" class="delete">' . __('Delete') . '</a>';
  • trunk/wp-admin/includes/plugin.php

    r20229 r20525  
    573573 *
    574574 * @param string|array $plugins Single plugin or list of plugins to deactivate.
     575 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
     576 *  A value of null (the default) will deactivate plugins for both the site and the network.
    575577 * @param bool $silent Prevent calling deactivation hooks. Default is false.
    576578 */
    577 function deactivate_plugins( $plugins, $silent = false ) {
     579function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
    578580    if ( is_multisite() )
    579581        $network_current = get_site_option( 'active_sitewide_plugins', array() );
     
    586588            continue;
    587589
    588         $network_wide = is_plugin_active_for_network( $plugin );
     590        $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
    589591
    590592        if ( ! $silent )
    591             do_action( 'deactivate_plugin', $plugin, $network_wide );
    592 
    593         if ( $network_wide ) {
     593            do_action( 'deactivate_plugin', $plugin, $network_deactivating );
     594
     595        if ( false !== $network_wide ) {
     596            if ( ! is_plugin_active_for_network( $plugin ) )
     597                continue;
    594598            $do_network = true;
    595599            unset( $network_current[ $plugin ] );
    596         } else {
     600        }
     601
     602        if ( true !== $network_wide ) {
    597603            $key = array_search( $plugin, $current );
    598604            if ( false !== $key ) {
     
    603609
    604610        if ( ! $silent ) {
    605             do_action( 'deactivate_' . $plugin, $network_wide );
    606             do_action( 'deactivated_plugin', $plugin, $network_wide );
     611            do_action( 'deactivate_' . $plugin, $network_deactivating );
     612            do_action( 'deactivated_plugin', $plugin, $network_deactivating );
    607613        }
    608614    }
  • trunk/wp-admin/plugin-editor.php

    r19593 r20525  
    6666                deactivate_plugins($file, true);
    6767
    68             update_option('recently_activated', array($file => time()) + (array)get_option('recently_activated'));
     68            if ( ! is_network_admin() )
     69                update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
    6970
    7071            wp_redirect(add_query_arg('_wpnonce', wp_create_nonce('edit-plugin-test_' . $file), "plugin-editor.php?file=$file&liveupdate=1&scrollto=$scrollto&networkwide=" . $network_wide));
  • trunk/wp-admin/plugins.php

    r19684 r20525  
    1313    $menu_perms = get_site_option( 'menu_items', array() );
    1414
    15     if ( empty( $menu_perms['plugins'] ) && ! is_super_admin() )
     15    if ( empty( $menu_perms['plugins'] ) && ! current_user_can( 'manage_network_plugins' ) )
    1616        wp_die( __( 'Cheatin&#8217; uh?' ) );
    1717}
     
    3232
    3333if ( $action ) {
    34     $network_wide = false;
    35     if ( ( isset( $_GET['networkwide'] ) || 'network-activate-selected' == $action ) && is_multisite() && current_user_can( 'manage_network_plugins' ) )
    36         $network_wide = true;
    3734
    3835    switch ( $action ) {
     
    4340            check_admin_referer('activate-plugin_' . $plugin);
    4441
    45             $result = activate_plugin($plugin, self_admin_url('plugins.php?error=true&plugin=' . $plugin), $network_wide);
     42            $result = activate_plugin($plugin, self_admin_url('plugins.php?error=true&plugin=' . $plugin), is_network_admin() );
    4643            if ( is_wp_error( $result ) ) {
    4744                if ( 'unexpected_output' == $result->get_error_code() ) {
     
    5451            }
    5552
    56             $recent = (array)get_option('recently_activated');
    57             if ( isset($recent[ $plugin ]) ) {
    58                 unset($recent[ $plugin ]);
    59                 update_option('recently_activated', $recent);
    60             }
     53            if ( ! is_network_admin() ) {
     54                $recent = (array) get_option( 'recently_activated' );
     55                unset( $recent[ $plugin ] );
     56                update_option( 'recently_activated', $recent );
     57            }
     58
    6159            if ( isset($_GET['from']) && 'import' == $_GET['from'] ) {
    6260                wp_redirect( self_admin_url("import.php?import=" . str_replace('-importer', '', dirname($plugin))) ); // overrides the ?error=true one above and redirects to the Imports page, stripping the -importer suffix
     
    6765            break;
    6866        case 'activate-selected':
    69         case 'network-activate-selected':
    7067            if ( ! current_user_can('activate_plugins') )
    7168                wp_die(__('You do not have sufficient permissions to activate plugins for this site.'));
     
    7673
    7774            // Only activate plugins which are not already active.
    78             $check = $network_wide ? 'is_plugin_active_for_network' : 'is_plugin_active';
     75            $check = is_network_admin() ? 'is_plugin_active_for_network' : 'is_plugin_active';
    7976            foreach ( $plugins as $i => $plugin )
    8077                if ( $check( $plugin ) )
     
    8683            }
    8784
    88             activate_plugins($plugins, self_admin_url('plugins.php?error=true'), $network_wide);
    89 
    90             $recent = (array)get_option('recently_activated');
    91             foreach ( $plugins as $plugin => $time)
    92                 if ( isset($recent[ $plugin ]) )
    93                     unset($recent[ $plugin ]);
    94 
    95             update_option('recently_activated', $recent);
     85            activate_plugins($plugins, self_admin_url('plugins.php?error=true'), is_network_admin() );
     86
     87            if ( ! is_network_admin() ) {
     88                $recent = (array) get_option('recently_activated' );
     89                foreach ( $plugins as $plugin )
     90                    unset( $recent[ $plugin ] );
     91                update_option( 'recently_activated', $recent );
     92            }
    9693
    9794            wp_redirect( self_admin_url("plugins.php?activate-multi=true&plugin_status=$status&paged=$page&s=$s") );
     
    154151
    155152            check_admin_referer('deactivate-plugin_' . $plugin);
    156             deactivate_plugins($plugin);
    157             update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated'));
     153
     154            if ( ! is_network_admin() && is_plugin_active_for_network() ) {
     155                wp_redirect( self_admin_url("plugins.php?plugin_status=$status&paged=$page&s=$s") );
     156                exit;
     157            }
     158
     159            deactivate_plugins( $plugin, false, is_network_admin() );
     160            if ( ! is_network_admin() )
     161                update_option( 'recently_activated', array( $plugin => time() ) + (array) get_option( 'recently_activated' ) );
    158162            if ( headers_sent() )
    159163                echo "<meta http-equiv='refresh' content='" . esc_attr( "0;url=plugins.php?deactivate=true&plugin_status=$status&paged=$page&s=$s" ) . "' />";
     
    169173
    170174            $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
    171             $plugins = array_filter($plugins, 'is_plugin_active'); //Do not deactivate plugins which are already deactivated.
     175            // Do not deactivate plugins which are already deactivated.
     176            if ( is_network_admin() ) {
     177                $plugins = array_filter( $plugins, 'is_plugin_active_for_network' );
     178            } else {
     179                $plugins = array_filter( $plugins, 'is_plugin_active' );
     180                $plugins = array_diff( $plugins, array_filter( $plugins, 'is_plugin_active_for_network' ) );
     181            }
    172182            if ( empty($plugins) ) {
    173183                wp_redirect( self_admin_url("plugins.php?plugin_status=$status&paged=$page&s=$s") );
     
    175185            }
    176186
    177             deactivate_plugins($plugins);
    178 
    179             $deactivated = array();
    180             foreach ( $plugins as $plugin )
    181                 $deactivated[ $plugin ] = time();
    182 
    183             update_option('recently_activated', $deactivated + (array)get_option('recently_activated'));
     187            deactivate_plugins( $plugins, false, is_network_admin() );
     188
     189            if ( ! is_network_admin() ) {
     190                $deactivated = array();
     191                foreach ( $plugins as $plugin )
     192                    $deactivated[ $plugin ] = time();
     193                update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
     194            }
     195
    184196            wp_redirect( self_admin_url("plugins.php?deactivate-multi=true&plugin_status=$status&paged=$page&s=$s") );
    185197            exit;
     
    306318            break;
    307319        case 'clear-recent-list':
    308             update_option('recently_activated', array());
     320            if ( ! is_network_admin() )
     321                update_option( 'recently_activated', array() );
    309322            break;
    310323    }
Note: See TracChangeset for help on using the changeset viewer.