wp/wp-admin/includes/class-plugin-upgrader.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-admin/includes/class-plugin-upgrader.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-admin/includes/class-plugin-upgrader.php	Tue Dec 15 13:49:49 2020 +0100
@@ -39,6 +39,16 @@
 	public $bulk = false;
 
 	/**
+	 * New plugin info.
+	 *
+	 * @since 5.5.0
+	 * @var array $new_plugin_data
+	 *
+	 * @see check_package()
+	 */
+	public $new_plugin_data = array();
+
+	/**
 	 * Initialize the upgrade strings.
 	 *
 	 * @since 2.8.0
@@ -46,7 +56,7 @@
 	public function upgrade_strings() {
 		$this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
 		$this->strings['no_package'] = __( 'Update package not available.' );
-		/* translators: %s: package URL */
+		/* translators: %s: Package URL. */
 		$this->strings['downloading_package']  = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
 		$this->strings['unpack_package']       = __( 'Unpacking the update&#8230;' );
 		$this->strings['remove_old']           = __( 'Removing the old version of the plugin&#8230;' );
@@ -63,13 +73,31 @@
 	 */
 	public function install_strings() {
 		$this->strings['no_package'] = __( 'Installation package not available.' );
-		/* translators: %s: package URL */
+		/* translators: %s: Package URL. */
 		$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
 		$this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
 		$this->strings['installing_package']  = __( 'Installing the plugin&#8230;' );
+		$this->strings['remove_old']          = __( 'Removing the current plugin&#8230;' );
+		$this->strings['remove_old_failed']   = __( 'Could not remove the current plugin.' );
 		$this->strings['no_files']            = __( 'The plugin contains no files.' );
 		$this->strings['process_failed']      = __( 'Plugin installation failed.' );
 		$this->strings['process_success']     = __( 'Plugin installed successfully.' );
+		/* translators: 1: Plugin name, 2: Plugin version. */
+		$this->strings['process_success_specific'] = __( 'Successfully installed the plugin <strong>%1$s %2$s</strong>.' );
+
+		if ( ! empty( $this->skin->overwrite ) ) {
+			if ( 'update-plugin' === $this->skin->overwrite ) {
+				$this->strings['installing_package'] = __( 'Updating the plugin&#8230;' );
+				$this->strings['process_failed']     = __( 'Plugin update failed.' );
+				$this->strings['process_success']    = __( 'Plugin updated successfully.' );
+			}
+
+			if ( 'downgrade-plugin' === $this->skin->overwrite ) {
+				$this->strings['installing_package'] = __( 'Downgrading the plugin&#8230;' );
+				$this->strings['process_failed']     = __( 'Plugin downgrade failed.' );
+				$this->strings['process_success']    = __( 'Plugin downgraded successfully.' );
+			}
+		}
 	}
 
 	/**
@@ -88,9 +116,9 @@
 	 * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
 	 */
 	public function install( $package, $args = array() ) {
-
 		$defaults    = array(
 			'clear_update_cache' => true,
+			'overwrite_package'  => false, // Do not overwrite files.
 		);
 		$parsed_args = wp_parse_args( $args, $defaults );
 
@@ -98,6 +126,7 @@
 		$this->install_strings();
 
 		add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+
 		if ( $parsed_args['clear_update_cache'] ) {
 			// Clear cache so wp_update_plugins() knows about the new plugin.
 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
@@ -107,7 +136,7 @@
 			array(
 				'package'           => $package,
 				'destination'       => WP_PLUGIN_DIR,
-				'clear_destination' => false, // Do not overwrite files.
+				'clear_destination' => $parsed_args['overwrite_package'],
 				'clear_working'     => true,
 				'hook_extra'        => array(
 					'type'   => 'plugin',
@@ -123,9 +152,23 @@
 			return $this->result;
 		}
 
-		// Force refresh of plugin update information
+		// Force refresh of plugin update information.
 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
 
+		if ( $parsed_args['overwrite_package'] ) {
+			/**
+			 * Fires when the upgrader has successfully overwritten a currently installed
+			 * plugin or theme with an uploaded zip package.
+			 *
+			 * @since 5.5.0
+			 *
+			 * @param string  $package          The package file.
+			 * @param array   $new_plugin_data  The new plugin data.
+			 * @param string  $package_type     The package type (plugin or theme).
+			 */
+			do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
+		}
+
 		return true;
 	}
 
@@ -145,7 +188,6 @@
 	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
 	 */
 	public function upgrade( $plugin, $args = array() ) {
-
 		$defaults    = array(
 			'clear_update_cache' => true,
 		);
@@ -163,12 +205,15 @@
 			return false;
 		}
 
-		// Get the URL to the zip file
+		// Get the URL to the zip file.
 		$r = $current->response[ $plugin ];
 
 		add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
+		add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
 		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
-		//'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.
+		add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
+		// There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
+		// 'source_selection' => array( $this, 'source_selection' ),
 		if ( $parsed_args['clear_update_cache'] ) {
 			// Clear cache so wp_update_plugins() knows about the new plugin.
 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
@@ -191,15 +236,26 @@
 		// Cleanup our hooks, in case something else does a upgrade on this connection.
 		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
 		remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
+		remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
+		remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
 
 		if ( ! $this->result || is_wp_error( $this->result ) ) {
 			return $this->result;
 		}
 
-		// Force refresh of plugin update information
+		// Force refresh of plugin update information.
 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
 
+		// Ensure any future auto-update failures trigger a failure email by removing
+		// the last failure notification from the list when plugins update successfully.
+		$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+		if ( isset( $past_failure_emails[ $plugin ] ) ) {
+			unset( $past_failure_emails[ $plugin ] );
+			update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+		}
+
 		return true;
 	}
 
@@ -218,7 +274,6 @@
 	 * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
 	 */
 	public function bulk_upgrade( $plugins, $args = array() ) {
-
 		$defaults    = array(
 			'clear_update_cache' => true,
 		);
@@ -234,7 +289,7 @@
 
 		$this->skin->header();
 
-		// Connect to the Filesystem first.
+		// Connect to the filesystem first.
 		$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
 		if ( ! $res ) {
 			$this->skin->footer();
@@ -247,7 +302,7 @@
 		 * Only start maintenance mode if:
 		 * - running Multisite and there are one or more plugins specified, OR
 		 * - a plugin with an update available is currently active.
-		 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
+		 * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
 		 */
 		$maintenance = ( is_multisite() && ! empty( $plugins ) );
 		foreach ( $plugins as $plugin ) {
@@ -294,11 +349,11 @@
 
 			$results[ $plugin ] = $this->result;
 
-			// Prevent credentials auth screen from displaying multiple times
+			// Prevent credentials auth screen from displaying multiple times.
 			if ( false === $result ) {
 				break;
 			}
-		} //end foreach $plugins
+		} // End foreach $plugins.
 
 		$this->maintenance_mode( false );
 
@@ -324,6 +379,21 @@
 		// Cleanup our hooks, in case something else does a upgrade on this connection.
 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
 
+		// Ensure any future auto-update failures trigger a failure email by removing
+		// the last failure notification from the list when plugins update successfully.
+		$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+		foreach ( $results as $plugin => $result ) {
+			// Maintain last failure notification when plugins failed to update manually.
+			if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $plugin ] ) ) {
+				continue;
+			}
+
+			unset( $past_failure_emails[ $plugin ] );
+		}
+
+		update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+
 		return $results;
 	}
 
@@ -344,6 +414,8 @@
 	public function check_package( $source ) {
 		global $wp_filesystem;
 
+		$this->new_plugin_data = array();
+
 		if ( is_wp_error( $source ) ) {
 			return $source;
 		}
@@ -353,23 +425,47 @@
 			return $source;
 		}
 
-		// Check the folder contains at least 1 valid plugin.
-		$plugins_found = false;
-		$files         = glob( $working_directory . '*.php' );
+		// Check that the folder contains at least 1 valid plugin.
+		$files = glob( $working_directory . '*.php' );
 		if ( $files ) {
 			foreach ( $files as $file ) {
 				$info = get_plugin_data( $file, false, false );
 				if ( ! empty( $info['Name'] ) ) {
-					$plugins_found = true;
+					$this->new_plugin_data = $info;
 					break;
 				}
 			}
 		}
 
-		if ( ! $plugins_found ) {
+		if ( empty( $this->new_plugin_data ) ) {
 			return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
 		}
 
+		$requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
+		$requires_wp  = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
+
+		if ( ! is_php_version_compatible( $requires_php ) ) {
+			$error = sprintf(
+				/* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
+				__( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
+				phpversion(),
+				$requires_php
+			);
+
+			return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
+		}
+
+		if ( ! is_wp_version_compatible( $requires_wp ) ) {
+			$error = sprintf(
+				/* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
+				__( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
+				$GLOBALS['wp_version'],
+				$requires_wp
+			);
+
+			return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
+		}
+
 		return $source;
 	}
 
@@ -390,12 +486,14 @@
 			return false;
 		}
 
-		$plugin = get_plugins( '/' . $this->result['destination_name'] ); //Ensure to pass with leading slash
+		// Ensure to pass with leading slash.
+		$plugin = get_plugins( '/' . $this->result['destination_name'] );
 		if ( empty( $plugin ) ) {
 			return false;
 		}
 
-		$pluginfiles = array_keys( $plugin ); //Assume the requested plugin is the first in the list
+		// Assume the requested plugin is the first in the list.
+		$pluginfiles = array_keys( $plugin );
 
 		return $this->result['destination_name'] . '/' . $pluginfiles[0];
 	}
@@ -408,17 +506,17 @@
 	 * @since 2.8.0
 	 * @since 4.1.0 Added a return value.
 	 *
-	 * @param bool|WP_Error  $return Upgrade offer return.
-	 * @param array          $plugin Plugin package arguments.
+	 * @param bool|WP_Error $return Upgrade offer return.
+	 * @param array         $plugin Plugin package arguments.
 	 * @return bool|WP_Error The passed in $return param or WP_Error.
 	 */
 	public function deactivate_plugin_before_upgrade( $return, $plugin ) {
 
-		if ( is_wp_error( $return ) ) { //Bypass.
+		if ( is_wp_error( $return ) ) { // Bypass.
 			return $return;
 		}
 
-		// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
+		// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it.
 		if ( wp_doing_cron() ) {
 			return $return;
 		}
@@ -429,7 +527,7 @@
 		}
 
 		if ( is_plugin_active( $plugin ) ) {
-			//Deactivate the plugin silently, Prevent deactivation hooks from running.
+			// Deactivate the plugin silently, Prevent deactivation hooks from running.
 			deactivate_plugins( $plugin, true );
 		}
 
@@ -437,7 +535,79 @@
 	}
 
 	/**
-	 * Delete the old plugin during an upgrade.
+	 * Turns on maintenance mode before attempting to background update an active plugin.
+	 *
+	 * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
+	 *
+	 * @since 5.4.0
+	 *
+	 * @param bool|WP_Error $return Upgrade offer return.
+	 * @param array         $plugin Plugin package arguments.
+	 * @return bool|WP_Error The passed in $return param or WP_Error.
+	 */
+	public function active_before( $return, $plugin ) {
+		if ( is_wp_error( $return ) ) {
+			return $return;
+		}
+
+		// Only enable maintenance mode when in cron (background update).
+		if ( ! wp_doing_cron() ) {
+			return $return;
+		}
+
+		$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+
+		// Only run if plugin is active.
+		if ( ! is_plugin_active( $plugin ) ) {
+			return $return;
+		}
+
+		// Change to maintenance mode. Bulk edit handles this separately.
+		if ( ! $this->bulk ) {
+			$this->maintenance_mode( true );
+		}
+
+		return $return;
+	}
+
+	/**
+	 * Turns off maintenance mode after upgrading an active plugin.
+	 *
+	 * Hooked to the {@see 'upgrader_post_install'} filter by Plugin_Upgrader::upgrade().
+	 *
+	 * @since 5.4.0
+	 *
+	 * @param bool|WP_Error $return Upgrade offer return.
+	 * @param array         $plugin Plugin package arguments.
+	 * @return bool|WP_Error The passed in $return param or WP_Error.
+	 */
+	public function active_after( $return, $plugin ) {
+		if ( is_wp_error( $return ) ) {
+			return $return;
+		}
+
+		// Only disable maintenance mode when in cron (background update).
+		if ( ! wp_doing_cron() ) {
+			return $return;
+		}
+
+		$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+
+		// Only run if plugin is active
+		if ( ! is_plugin_active( $plugin ) ) {
+			return $return;
+		}
+
+		// Time to remove maintenance mode. Bulk edit handles this separately.
+		if ( ! $this->bulk ) {
+			$this->maintenance_mode( false );
+		}
+
+		return $return;
+	}
+
+	/**
+	 * Deletes the old plugin during an upgrade.
 	 *
 	 * Hooked to the {@see 'upgrader_clear_destination'} filter by
 	 * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
@@ -446,17 +616,18 @@
 	 *
 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 	 *
-	 * @param bool|WP_Error $removed
-	 * @param string        $local_destination
-	 * @param string        $remote_destination
-	 * @param array         $plugin
-	 * @return WP_Error|bool
+	 * @param bool|WP_Error $removed            Whether the destination was cleared.
+	 *                                          True on success, WP_Error on failure.
+	 * @param string        $local_destination  The local package destination.
+	 * @param string        $remote_destination The remote package destination.
+	 * @param array         $plugin             Extra arguments passed to hooked filters.
+	 * @return bool|WP_Error
 	 */
 	public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
 		global $wp_filesystem;
 
 		if ( is_wp_error( $removed ) ) {
-			return $removed; //Pass errors through.
+			return $removed; // Pass errors through.
 		}
 
 		$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
@@ -467,12 +638,13 @@
 		$plugins_dir     = $wp_filesystem->wp_plugins_dir();
 		$this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
 
-		if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { //If it's already vanished.
+		if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { // If it's already vanished.
 			return $removed;
 		}
 
 		// If plugin is in its own directory, recursively delete the directory.
-		if ( strpos( $plugin, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
+		// Base check on if plugin includes directory separator AND that it's not the root plugin folder.
+		if ( strpos( $plugin, '/' ) && $this_plugin_dir !== $plugins_dir ) {
 			$deleted = $wp_filesystem->delete( $this_plugin_dir, true );
 		} else {
 			$deleted = $wp_filesystem->delete( $plugins_dir . $plugin );