Make WordPress Core


Ignore:
Timestamp:
02/11/2020 08:12:52 PM (5 years ago)
Author:
desrosj
Message:

Upgrade/Install: Enable maintenance mode when plugins are auto-updated.

When an attempt is made to update an active plugin automatically, there is the potential currently for two negative scenarios:

  • The plugin can be deactivated if the Plugins admin screen is loaded when the plugin update is incomplete, causing a PHP error.
  • The WSOD protection could be triggered, sending a false alarm email to the site administrator.

By enabling maintenance mode before an active plugin update is attempted, these scenarios can be avoided.

This change implements the same approach as the Theme_Upgrader class of using the upgrader_pre_install and upgrader_post_install hooks to toggle maintenance mode.

Props desrosj, SergeyBiryukov.
Fixes #49400.

File:
1 edited

Legend:

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

    r47122 r47275  
    168168
    169169        add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
     170        add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
    170171        add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
     172        add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
    171173        // There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
    172174        // 'source_selection' => array( $this, 'source_selection' ),
     
    193195        remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
    194196        remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
     197        remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
    195198        remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
     199        remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
    196200
    197201        if ( ! $this->result || is_wp_error( $this->result ) ) {
     
    441445
    442446    /**
     447     * Turn on maintenance mode before attempting to background update an active plugin.
     448     *
     449     * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
     450     *
     451     * @since 5.4.0
     452     *
     453     * @param bool|WP_Error  $return
     454     * @param array          $plugin
     455     * @return bool|WP_Error
     456     */
     457    public function active_before( $return, $plugin ) {
     458        if ( is_wp_error( $return ) ) {
     459            return $return;
     460        }
     461
     462        // Only enable maintenance mode when in cron (background update).
     463        if ( ! wp_doing_cron() ) {
     464            return $return;
     465        }
     466
     467        $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
     468
     469        if ( ! is_plugin_active( $plugin ) ) { // If not active.
     470            return $return;
     471        }
     472
     473        // Bulk edit handles maintenance mode separately.
     474        if ( ! $this->bulk ) {
     475            $this->maintenance_mode( true );
     476        }
     477
     478        return $return;
     479    }
     480
     481    /**
     482     * Turn off maintenance mode after upgrading an active plugin.
     483     *
     484     * Hooked to the {@see 'upgrader_post_install'} filter by Plugin_Upgrader::upgrade().
     485     *
     486     * @since 5.4.0
     487     *
     488     * @param bool|WP_Error  $return
     489     * @param array          $plugin
     490     * @return bool|WP_Error
     491     */
     492    public function active_after( $return, $plugin ) {
     493        if ( is_wp_error( $return ) ) {
     494            return $return;
     495        }
     496
     497        // Only disable maintenance mode when in cron (background update).
     498        if ( ! wp_doing_cron() ) {
     499            return $return;
     500        }
     501
     502        $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
     503
     504        if ( ! is_plugin_active( $plugin ) ) { // If not active.
     505            return $return;
     506        }
     507
     508        // Bulk edit handles maintenance mode separately.
     509        if ( ! $this->bulk ) {
     510            $this->maintenance_mode( false );
     511        }
     512
     513        return $return;
     514    }
     515
     516    /**
    443517     * Delete the old plugin during an upgrade.
    444518     *
Note: See TracChangeset for help on using the changeset viewer.