wp/wp-admin/includes/class-plugin-upgrader.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    42 	 * Initialize the upgrade strings.
    42 	 * Initialize the upgrade strings.
    43 	 *
    43 	 *
    44 	 * @since 2.8.0
    44 	 * @since 2.8.0
    45 	 */
    45 	 */
    46 	public function upgrade_strings() {
    46 	public function upgrade_strings() {
    47 		$this->strings['up_to_date'] = __('The plugin is at the latest version.');
    47 		$this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
    48 		$this->strings['no_package'] = __('Update package not available.');
    48 		$this->strings['no_package'] = __( 'Update package not available.' );
    49 		/* translators: %s: package URL */
    49 		/* translators: %s: package URL */
    50 		$this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
    50 		$this->strings['downloading_package']  = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
    51 		$this->strings['unpack_package'] = __('Unpacking the update&#8230;');
    51 		$this->strings['unpack_package']       = __( 'Unpacking the update&#8230;' );
    52 		$this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
    52 		$this->strings['remove_old']           = __( 'Removing the old version of the plugin&#8230;' );
    53 		$this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
    53 		$this->strings['remove_old_failed']    = __( 'Could not remove the old plugin.' );
    54 		$this->strings['process_failed'] = __('Plugin update failed.');
    54 		$this->strings['process_failed']       = __( 'Plugin update failed.' );
    55 		$this->strings['process_success'] = __('Plugin updated successfully.');
    55 		$this->strings['process_success']      = __( 'Plugin updated successfully.' );
    56 		$this->strings['process_bulk_success'] = __('Plugins updated successfully.');
    56 		$this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
    57 	}
    57 	}
    58 
    58 
    59 	/**
    59 	/**
    60 	 * Initialize the installation strings.
    60 	 * Initialize the installation strings.
    61 	 *
    61 	 *
    62 	 * @since 2.8.0
    62 	 * @since 2.8.0
    63 	 */
    63 	 */
    64 	public function install_strings() {
    64 	public function install_strings() {
    65 		$this->strings['no_package'] = __('Installation package not available.');
    65 		$this->strings['no_package'] = __( 'Installation package not available.' );
    66 		/* translators: %s: package URL */
    66 		/* translators: %s: package URL */
    67 		$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
    67 		$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
    68 		$this->strings['unpack_package'] = __('Unpacking the package&#8230;');
    68 		$this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
    69 		$this->strings['installing_package'] = __('Installing the plugin&#8230;');
    69 		$this->strings['installing_package']  = __( 'Installing the plugin&#8230;' );
    70 		$this->strings['no_files'] = __('The plugin contains no files.');
    70 		$this->strings['no_files']            = __( 'The plugin contains no files.' );
    71 		$this->strings['process_failed'] = __('Plugin installation failed.');
    71 		$this->strings['process_failed']      = __( 'Plugin installation failed.' );
    72 		$this->strings['process_success'] = __('Plugin installed successfully.');
    72 		$this->strings['process_success']     = __( 'Plugin installed successfully.' );
    73 	}
    73 	}
    74 
    74 
    75 	/**
    75 	/**
    76 	 * Install a plugin package.
    76 	 * Install a plugin package.
    77 	 *
    77 	 *
    87 	 * }
    87 	 * }
    88 	 * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
    88 	 * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
    89 	 */
    89 	 */
    90 	public function install( $package, $args = array() ) {
    90 	public function install( $package, $args = array() ) {
    91 
    91 
    92 		$defaults = array(
    92 		$defaults    = array(
    93 			'clear_update_cache' => true,
    93 			'clear_update_cache' => true,
    94 		);
    94 		);
    95 		$parsed_args = wp_parse_args( $args, $defaults );
    95 		$parsed_args = wp_parse_args( $args, $defaults );
    96 
    96 
    97 		$this->init();
    97 		$this->init();
    98 		$this->install_strings();
    98 		$this->install_strings();
    99 
    99 
   100 		add_filter('upgrader_source_selection', array($this, 'check_package') );
   100 		add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
   101 		if ( $parsed_args['clear_update_cache'] ) {
   101 		if ( $parsed_args['clear_update_cache'] ) {
   102 			// Clear cache so wp_update_plugins() knows about the new plugin.
   102 			// Clear cache so wp_update_plugins() knows about the new plugin.
   103 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
   103 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
   104 		}
   104 		}
   105 
   105 
   106 		$this->run( array(
   106 		$this->run(
   107 			'package' => $package,
   107 			array(
   108 			'destination' => WP_PLUGIN_DIR,
   108 				'package'           => $package,
   109 			'clear_destination' => false, // Do not overwrite files.
   109 				'destination'       => WP_PLUGIN_DIR,
   110 			'clear_working' => true,
   110 				'clear_destination' => false, // Do not overwrite files.
   111 			'hook_extra' => array(
   111 				'clear_working'     => true,
   112 				'type' => 'plugin',
   112 				'hook_extra'        => array(
   113 				'action' => 'install',
   113 					'type'   => 'plugin',
       
   114 					'action' => 'install',
       
   115 				),
   114 			)
   116 			)
   115 		) );
   117 		);
   116 
   118 
   117 		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
   119 		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
   118 		remove_filter('upgrader_source_selection', array($this, 'check_package') );
   120 		remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
   119 
   121 
   120 		if ( ! $this->result || is_wp_error($this->result) )
   122 		if ( ! $this->result || is_wp_error( $this->result ) ) {
   121 			return $this->result;
   123 			return $this->result;
       
   124 		}
   122 
   125 
   123 		// Force refresh of plugin update information
   126 		// Force refresh of plugin update information
   124 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   127 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   125 
   128 
   126 		return true;
   129 		return true;
   130 	 * Upgrade a plugin.
   133 	 * Upgrade a plugin.
   131 	 *
   134 	 *
   132 	 * @since 2.8.0
   135 	 * @since 2.8.0
   133 	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
   136 	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
   134 	 *
   137 	 *
   135 	 * @param string $plugin The basename path to the main plugin file.
   138 	 * @param string $plugin Path to the plugin file relative to the plugins directory.
   136 	 * @param array  $args {
   139 	 * @param array  $args {
   137 	 *     Optional. Other arguments for upgrading a plugin package. Default empty array.
   140 	 *     Optional. Other arguments for upgrading a plugin package. Default empty array.
   138 	 *
   141 	 *
   139 	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
   142 	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
   140 	 *                                    Default true.
   143 	 *                                    Default true.
   141 	 * }
   144 	 * }
   142 	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
   145 	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
   143 	 */
   146 	 */
   144 	public function upgrade( $plugin, $args = array() ) {
   147 	public function upgrade( $plugin, $args = array() ) {
   145 
   148 
   146 		$defaults = array(
   149 		$defaults    = array(
   147 			'clear_update_cache' => true,
   150 			'clear_update_cache' => true,
   148 		);
   151 		);
   149 		$parsed_args = wp_parse_args( $args, $defaults );
   152 		$parsed_args = wp_parse_args( $args, $defaults );
   150 
   153 
   151 		$this->init();
   154 		$this->init();
   152 		$this->upgrade_strings();
   155 		$this->upgrade_strings();
   153 
   156 
   154 		$current = get_site_transient( 'update_plugins' );
   157 		$current = get_site_transient( 'update_plugins' );
   155 		if ( !isset( $current->response[ $plugin ] ) ) {
   158 		if ( ! isset( $current->response[ $plugin ] ) ) {
   156 			$this->skin->before();
   159 			$this->skin->before();
   157 			$this->skin->set_result(false);
   160 			$this->skin->set_result( false );
   158 			$this->skin->error('up_to_date');
   161 			$this->skin->error( 'up_to_date' );
   159 			$this->skin->after();
   162 			$this->skin->after();
   160 			return false;
   163 			return false;
   161 		}
   164 		}
   162 
   165 
   163 		// Get the URL to the zip file
   166 		// Get the URL to the zip file
   164 		$r = $current->response[ $plugin ];
   167 		$r = $current->response[ $plugin ];
   165 
   168 
   166 		add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
   169 		add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
   167 		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
   170 		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
   168 		//'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.
   171 		//'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.
   169 		if ( $parsed_args['clear_update_cache'] ) {
   172 		if ( $parsed_args['clear_update_cache'] ) {
   170 			// Clear cache so wp_update_plugins() knows about the new plugin.
   173 			// Clear cache so wp_update_plugins() knows about the new plugin.
   171 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
   174 			add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
   172 		}
   175 		}
   173 
   176 
   174 		$this->run( array(
   177 		$this->run(
   175 			'package' => $r->package,
   178 			array(
   176 			'destination' => WP_PLUGIN_DIR,
   179 				'package'           => $r->package,
   177 			'clear_destination' => true,
   180 				'destination'       => WP_PLUGIN_DIR,
   178 			'clear_working' => true,
   181 				'clear_destination' => true,
   179 			'hook_extra' => array(
   182 				'clear_working'     => true,
   180 				'plugin' => $plugin,
   183 				'hook_extra'        => array(
   181 				'type' => 'plugin',
   184 					'plugin' => $plugin,
   182 				'action' => 'update',
   185 					'type'   => 'plugin',
   183 			),
   186 					'action' => 'update',
   184 		) );
   187 				),
       
   188 			)
       
   189 		);
   185 
   190 
   186 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   191 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   187 		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
   192 		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
   188 		remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
   193 		remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
   189 		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
   194 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
   190 
   195 
   191 		if ( ! $this->result || is_wp_error($this->result) )
   196 		if ( ! $this->result || is_wp_error( $this->result ) ) {
   192 			return $this->result;
   197 			return $this->result;
       
   198 		}
   193 
   199 
   194 		// Force refresh of plugin update information
   200 		// Force refresh of plugin update information
   195 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   201 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   196 
   202 
   197 		return true;
   203 		return true;
   201 	 * Bulk upgrade several plugins at once.
   207 	 * Bulk upgrade several plugins at once.
   202 	 *
   208 	 *
   203 	 * @since 2.8.0
   209 	 * @since 2.8.0
   204 	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
   210 	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
   205 	 *
   211 	 *
   206 	 * @param array $plugins Array of the basename paths of the plugins' main files.
   212 	 * @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
   207 	 * @param array $args {
   213 	 * @param array    $args {
   208 	 *     Optional. Other arguments for upgrading several plugins at once. Default empty array.
   214 	 *     Optional. Other arguments for upgrading several plugins at once.
   209 	 *
   215 	 *
   210 	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
   216 	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
   211 	 *                                    Default true.
       
   212 	 * }
   217 	 * }
   213 	 * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
   218 	 * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
   214 	 */
   219 	 */
   215 	public function bulk_upgrade( $plugins, $args = array() ) {
   220 	public function bulk_upgrade( $plugins, $args = array() ) {
   216 
   221 
   217 		$defaults = array(
   222 		$defaults    = array(
   218 			'clear_update_cache' => true,
   223 			'clear_update_cache' => true,
   219 		);
   224 		);
   220 		$parsed_args = wp_parse_args( $args, $defaults );
   225 		$parsed_args = wp_parse_args( $args, $defaults );
   221 
   226 
   222 		$this->init();
   227 		$this->init();
   223 		$this->bulk = true;
   228 		$this->bulk = true;
   224 		$this->upgrade_strings();
   229 		$this->upgrade_strings();
   225 
   230 
   226 		$current = get_site_transient( 'update_plugins' );
   231 		$current = get_site_transient( 'update_plugins' );
   227 
   232 
   228 		add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
   233 		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
   229 
   234 
   230 		$this->skin->header();
   235 		$this->skin->header();
   231 
   236 
   232 		// Connect to the Filesystem first.
   237 		// Connect to the Filesystem first.
   233 		$res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
   238 		$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
   234 		if ( ! $res ) {
   239 		if ( ! $res ) {
   235 			$this->skin->footer();
   240 			$this->skin->footer();
   236 			return false;
   241 			return false;
   237 		}
   242 		}
   238 
   243 
   243 		 * - running Multisite and there are one or more plugins specified, OR
   248 		 * - running Multisite and there are one or more plugins specified, OR
   244 		 * - a plugin with an update available is currently active.
   249 		 * - a plugin with an update available is currently active.
   245 		 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
   250 		 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
   246 		 */
   251 		 */
   247 		$maintenance = ( is_multisite() && ! empty( $plugins ) );
   252 		$maintenance = ( is_multisite() && ! empty( $plugins ) );
   248 		foreach ( $plugins as $plugin )
   253 		foreach ( $plugins as $plugin ) {
   249 			$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
   254 			$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
   250 		if ( $maintenance )
   255 		}
   251 			$this->maintenance_mode(true);
   256 		if ( $maintenance ) {
       
   257 			$this->maintenance_mode( true );
       
   258 		}
   252 
   259 
   253 		$results = array();
   260 		$results = array();
   254 
   261 
   255 		$this->update_count = count($plugins);
   262 		$this->update_count   = count( $plugins );
   256 		$this->update_current = 0;
   263 		$this->update_current = 0;
   257 		foreach ( $plugins as $plugin ) {
   264 		foreach ( $plugins as $plugin ) {
   258 			$this->update_current++;
   265 			$this->update_current++;
   259 			$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
   266 			$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
   260 
   267 
   261 			if ( !isset( $current->response[ $plugin ] ) ) {
   268 			if ( ! isset( $current->response[ $plugin ] ) ) {
   262 				$this->skin->set_result('up_to_date');
   269 				$this->skin->set_result( 'up_to_date' );
   263 				$this->skin->before();
   270 				$this->skin->before();
   264 				$this->skin->feedback('up_to_date');
   271 				$this->skin->feedback( 'up_to_date' );
   265 				$this->skin->after();
   272 				$this->skin->after();
   266 				$results[$plugin] = true;
   273 				$results[ $plugin ] = true;
   267 				continue;
   274 				continue;
   268 			}
   275 			}
   269 
   276 
   270 			// Get the URL to the zip file.
   277 			// Get the URL to the zip file.
   271 			$r = $current->response[ $plugin ];
   278 			$r = $current->response[ $plugin ];
   272 
   279 
   273 			$this->skin->plugin_active = is_plugin_active($plugin);
   280 			$this->skin->plugin_active = is_plugin_active( $plugin );
   274 
   281 
   275 			$result = $this->run( array(
   282 			$result = $this->run(
   276 				'package' => $r->package,
   283 				array(
   277 				'destination' => WP_PLUGIN_DIR,
   284 					'package'           => $r->package,
   278 				'clear_destination' => true,
   285 					'destination'       => WP_PLUGIN_DIR,
   279 				'clear_working' => true,
   286 					'clear_destination' => true,
   280 				'is_multi' => true,
   287 					'clear_working'     => true,
   281 				'hook_extra' => array(
   288 					'is_multi'          => true,
   282 					'plugin' => $plugin
   289 					'hook_extra'        => array(
       
   290 						'plugin' => $plugin,
       
   291 					),
   283 				)
   292 				)
   284 			) );
   293 			);
   285 
   294 
   286 			$results[$plugin] = $this->result;
   295 			$results[ $plugin ] = $this->result;
   287 
   296 
   288 			// Prevent credentials auth screen from displaying multiple times
   297 			// Prevent credentials auth screen from displaying multiple times
   289 			if ( false === $result )
   298 			if ( false === $result ) {
   290 				break;
   299 				break;
       
   300 			}
   291 		} //end foreach $plugins
   301 		} //end foreach $plugins
   292 
   302 
   293 		$this->maintenance_mode(false);
   303 		$this->maintenance_mode( false );
   294 
   304 
   295 		// Force refresh of plugin update information.
   305 		// Force refresh of plugin update information.
   296 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   306 		wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
   297 
   307 
   298 		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
   308 		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
   299 		do_action( 'upgrader_process_complete', $this, array(
   309 		do_action(
   300 			'action' => 'update',
   310 			'upgrader_process_complete',
   301 			'type' => 'plugin',
   311 			$this,
   302 			'bulk' => true,
   312 			array(
   303 			'plugins' => $plugins,
   313 				'action'  => 'update',
   304 		) );
   314 				'type'    => 'plugin',
       
   315 				'bulk'    => true,
       
   316 				'plugins' => $plugins,
       
   317 			)
       
   318 		);
   305 
   319 
   306 		$this->skin->bulk_footer();
   320 		$this->skin->bulk_footer();
   307 
   321 
   308 		$this->skin->footer();
   322 		$this->skin->footer();
   309 
   323 
   310 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   324 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   311 		remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
   325 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
   312 
   326 
   313 		return $results;
   327 		return $results;
   314 	}
   328 	}
   315 
   329 
   316 	/**
   330 	/**
   319 	 * This function is added to the {@see 'upgrader_source_selection'} filter by
   333 	 * This function is added to the {@see 'upgrader_source_selection'} filter by
   320 	 * Plugin_Upgrader::install().
   334 	 * Plugin_Upgrader::install().
   321 	 *
   335 	 *
   322 	 * @since 3.3.0
   336 	 * @since 3.3.0
   323 	 *
   337 	 *
   324 	 * @global WP_Filesystem_Base $wp_filesystem Subclass
   338 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   325 	 *
   339 	 *
   326 	 * @param string $source The path to the downloaded package source.
   340 	 * @param string $source The path to the downloaded package source.
   327 	 * @return string|WP_Error The source as passed, or a WP_Error object
   341 	 * @return string|WP_Error The source as passed, or a WP_Error object
   328 	 *                         if no plugins were found.
   342 	 *                         if no plugins were found.
   329 	 */
   343 	 */
   330 	public function check_package($source) {
   344 	public function check_package( $source ) {
   331 		global $wp_filesystem;
   345 		global $wp_filesystem;
   332 
   346 
   333 		if ( is_wp_error($source) )
   347 		if ( is_wp_error( $source ) ) {
   334 			return $source;
   348 			return $source;
   335 
   349 		}
   336 		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
   350 
   337 		if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
   351 		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
       
   352 		if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
   338 			return $source;
   353 			return $source;
       
   354 		}
   339 
   355 
   340 		// Check the folder contains at least 1 valid plugin.
   356 		// Check the folder contains at least 1 valid plugin.
   341 		$plugins_found = false;
   357 		$plugins_found = false;
   342 		$files = glob( $working_directory . '*.php' );
   358 		$files         = glob( $working_directory . '*.php' );
   343 		if ( $files ) {
   359 		if ( $files ) {
   344 			foreach ( $files as $file ) {
   360 			foreach ( $files as $file ) {
   345 				$info = get_plugin_data( $file, false, false );
   361 				$info = get_plugin_data( $file, false, false );
   346 				if ( ! empty( $info['Name'] ) ) {
   362 				if ( ! empty( $info['Name'] ) ) {
   347 					$plugins_found = true;
   363 					$plugins_found = true;
   348 					break;
   364 					break;
   349 				}
   365 				}
   350 			}
   366 			}
   351 		}
   367 		}
   352 
   368 
   353 		if ( ! $plugins_found )
   369 		if ( ! $plugins_found ) {
   354 			return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
   370 			return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
       
   371 		}
   355 
   372 
   356 		return $source;
   373 		return $source;
   357 	}
   374 	}
   358 
   375 
   359 	/**
   376 	/**
   364 	 * @since 2.8.0
   381 	 * @since 2.8.0
   365 	 *
   382 	 *
   366 	 * @return string|false The full path to the main plugin file, or false.
   383 	 * @return string|false The full path to the main plugin file, or false.
   367 	 */
   384 	 */
   368 	public function plugin_info() {
   385 	public function plugin_info() {
   369 		if ( ! is_array($this->result) )
   386 		if ( ! is_array( $this->result ) ) {
   370 			return false;
   387 			return false;
   371 		if ( empty($this->result['destination_name']) )
   388 		}
       
   389 		if ( empty( $this->result['destination_name'] ) ) {
   372 			return false;
   390 			return false;
   373 
   391 		}
   374 		$plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
   392 
   375 		if ( empty($plugin) )
   393 		$plugin = get_plugins( '/' . $this->result['destination_name'] ); //Ensure to pass with leading slash
       
   394 		if ( empty( $plugin ) ) {
   376 			return false;
   395 			return false;
   377 
   396 		}
   378 		$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
   397 
       
   398 		$pluginfiles = array_keys( $plugin ); //Assume the requested plugin is the first in the list
   379 
   399 
   380 		return $this->result['destination_name'] . '/' . $pluginfiles[0];
   400 		return $this->result['destination_name'] . '/' . $pluginfiles[0];
   381 	}
   401 	}
   382 
   402 
   383 	/**
   403 	/**
   390 	 *
   410 	 *
   391 	 * @param bool|WP_Error  $return Upgrade offer return.
   411 	 * @param bool|WP_Error  $return Upgrade offer return.
   392 	 * @param array          $plugin Plugin package arguments.
   412 	 * @param array          $plugin Plugin package arguments.
   393 	 * @return bool|WP_Error The passed in $return param or WP_Error.
   413 	 * @return bool|WP_Error The passed in $return param or WP_Error.
   394 	 */
   414 	 */
   395 	public function deactivate_plugin_before_upgrade($return, $plugin) {
   415 	public function deactivate_plugin_before_upgrade( $return, $plugin ) {
   396 
   416 
   397 		if ( is_wp_error($return) ) //Bypass.
   417 		if ( is_wp_error( $return ) ) { //Bypass.
   398 			return $return;
   418 			return $return;
       
   419 		}
   399 
   420 
   400 		// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
   421 		// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
   401 		if ( wp_doing_cron() )
   422 		if ( wp_doing_cron() ) {
   402 			return $return;
   423 			return $return;
   403 
   424 		}
   404 		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
   425 
   405 		if ( empty($plugin) )
   426 		$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
   406 			return new WP_Error('bad_request', $this->strings['bad_request']);
   427 		if ( empty( $plugin ) ) {
   407 
   428 			return new WP_Error( 'bad_request', $this->strings['bad_request'] );
   408 		if ( is_plugin_active($plugin) ) {
   429 		}
       
   430 
       
   431 		if ( is_plugin_active( $plugin ) ) {
   409 			//Deactivate the plugin silently, Prevent deactivation hooks from running.
   432 			//Deactivate the plugin silently, Prevent deactivation hooks from running.
   410 			deactivate_plugins($plugin, true);
   433 			deactivate_plugins( $plugin, true );
   411 		}
   434 		}
   412 
   435 
   413 		return $return;
   436 		return $return;
   414 	}
   437 	}
   415 
   438 
   419 	 * Hooked to the {@see 'upgrader_clear_destination'} filter by
   442 	 * Hooked to the {@see 'upgrader_clear_destination'} filter by
   420 	 * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
   443 	 * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
   421 	 *
   444 	 *
   422 	 * @since 2.8.0
   445 	 * @since 2.8.0
   423 	 *
   446 	 *
   424 	 * @global WP_Filesystem_Base $wp_filesystem Subclass
   447 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   425      *
   448 	 *
   426 	 * @param bool|WP_Error $removed
   449 	 * @param bool|WP_Error $removed
   427 	 * @param string        $local_destination
   450 	 * @param string        $local_destination
   428 	 * @param string        $remote_destination
   451 	 * @param string        $remote_destination
   429 	 * @param array         $plugin
   452 	 * @param array         $plugin
   430 	 * @return WP_Error|bool
   453 	 * @return WP_Error|bool
   431 	 */
   454 	 */
   432 	public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
   455 	public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
   433 		global $wp_filesystem;
   456 		global $wp_filesystem;
   434 
   457 
   435 		if ( is_wp_error($removed) )
   458 		if ( is_wp_error( $removed ) ) {
   436 			return $removed; //Pass errors through.
   459 			return $removed; //Pass errors through.
   437 
   460 		}
   438 		$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
   461 
   439 		if ( empty($plugin) )
   462 		$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
   440 			return new WP_Error('bad_request', $this->strings['bad_request']);
   463 		if ( empty( $plugin ) ) {
   441 
   464 			return new WP_Error( 'bad_request', $this->strings['bad_request'] );
   442 		$plugins_dir = $wp_filesystem->wp_plugins_dir();
   465 		}
   443 		$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
   466 
   444 
   467 		$plugins_dir     = $wp_filesystem->wp_plugins_dir();
   445 		if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
   468 		$this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
       
   469 
       
   470 		if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { //If it's already vanished.
   446 			return $removed;
   471 			return $removed;
       
   472 		}
   447 
   473 
   448 		// If plugin is in its own directory, recursively delete the directory.
   474 		// If plugin is in its own directory, recursively delete the directory.
   449 		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
   475 		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
   450 			$deleted = $wp_filesystem->delete($this_plugin_dir, true);
   476 			$deleted = $wp_filesystem->delete( $this_plugin_dir, true );
   451 		else
   477 		} else {
   452 			$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
   478 			$deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
   453 
   479 		}
   454 		if ( ! $deleted )
   480 
   455 			return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
   481 		if ( ! $deleted ) {
       
   482 			return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
       
   483 		}
   456 
   484 
   457 		return true;
   485 		return true;
   458 	}
   486 	}
   459 }
   487 }