WordPress.org

Make WordPress Core

Ticket #22704: 22704-reactivate-plugins.2.patch.diff

File 22704-reactivate-plugins.2.patch.diff, 1.9 KB (added by kurtpayne, 4 years ago)

Reactivate plugins after updating ... but only if they were active before updating

  • wp-admin/includes/class-wp-upgrader.php

     
    482482 
    483483                add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2); 
    484484                add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); 
     485                if ( is_plugin_active( $plugin ) ) 
     486                        add_filter( 'upgrader_post_install', array( $this, 'activate_plugin_after_upgrade' ), 10, 3 ); 
    485487                //'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins. 
    486488 
    487489                $this->run( array( 
     
    497499                // Cleanup our hooks, in case something else does a upgrade on this connection. 
    498500                remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade')); 
    499501                remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); 
     502                if ( is_plugin_active( $plugin ) ) 
     503                        remove_filter( 'upgrader_post_install', array( $this, 'activate_plugin_after_upgrade' ) ); 
    500504 
    501505                if ( ! $this->result || is_wp_error($this->result) ) 
    502506                        return $this->result; 
     
    660664                } 
    661665        } 
    662666 
     667        /** 
     668         * Re-activate a plugin after it's been upgraded 
     669         * @param bool $return 
     670         * @param array $plugin 
     671         * @return null|WP_Error 
     672         */ 
     673        function activate_plugin_after_upgrade( $return, $plugin ) { 
     674                $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : ''; 
     675                if ( empty( $plugin ) ) 
     676                        return new WP_Error( 'bad_request', $this->strings['bad_request'] ); 
     677 
     678                if ( is_plugin_active( $plugin ) ) 
     679                        return $return; 
     680 
     681                return activate_plugin( $plugin, '', false, true ); 
     682        } 
     683         
    663684        //Hooked to upgrade_clear_destination 
    664685        function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) { 
    665686                global $wp_filesystem;