Changeset 42343 for trunk/src/wp-admin/includes/class-plugin-upgrader.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-plugin-upgrader.php
r41908 r42343 45 45 */ 46 46 public function upgrade_strings() { 47 $this->strings['up_to_date'] = __( 'The plugin is at the latest version.');48 $this->strings['no_package'] = __( 'Update package not available.');47 $this->strings['up_to_date'] = __( 'The plugin is at the latest version.' ); 48 $this->strings['no_package'] = __( 'Update package not available.' ); 49 49 /* translators: %s: package URL */ 50 $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '<span class="code">%s</span>' );51 $this->strings['unpack_package'] = __('Unpacking the update…');52 $this->strings['remove_old'] = __('Removing the old version of the plugin…');53 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');54 $this->strings['process_failed'] = __('Plugin update failed.');55 $this->strings['process_success'] = __('Plugin updated successfully.');56 $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.');50 $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '<span class="code">%s</span>' ); 51 $this->strings['unpack_package'] = __( 'Unpacking the update…' ); 52 $this->strings['remove_old'] = __( 'Removing the old version of the plugin…' ); 53 $this->strings['remove_old_failed'] = __( 'Could not remove the old plugin.' ); 54 $this->strings['process_failed'] = __( 'Plugin update failed.' ); 55 $this->strings['process_success'] = __( 'Plugin updated successfully.' ); 56 $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' ); 57 57 } 58 58 … … 63 63 */ 64 64 public function install_strings() { 65 $this->strings['no_package'] = __( 'Installation package not available.');65 $this->strings['no_package'] = __( 'Installation package not available.' ); 66 66 /* translators: %s: package URL */ 67 67 $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s…' ), '<span class="code">%s</span>' ); 68 $this->strings['unpack_package'] = __('Unpacking the package…');69 $this->strings['installing_package'] = __('Installing the plugin…');70 $this->strings['no_files'] = __('The plugin contains no files.');71 $this->strings['process_failed'] = __('Plugin installation failed.');72 $this->strings['process_success'] = __('Plugin installed successfully.');68 $this->strings['unpack_package'] = __( 'Unpacking the package…' ); 69 $this->strings['installing_package'] = __( 'Installing the plugin…' ); 70 $this->strings['no_files'] = __( 'The plugin contains no files.' ); 71 $this->strings['process_failed'] = __( 'Plugin installation failed.' ); 72 $this->strings['process_success'] = __( 'Plugin installed successfully.' ); 73 73 } 74 74 … … 90 90 public function install( $package, $args = array() ) { 91 91 92 $defaults = array(92 $defaults = array( 93 93 'clear_update_cache' => true, 94 94 ); … … 98 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 101 if ( $parsed_args['clear_update_cache'] ) { 102 102 // Clear cache so wp_update_plugins() knows about the new plugin. … … 104 104 } 105 105 106 $this->run( array( 107 'package' => $package, 108 'destination' => WP_PLUGIN_DIR, 109 'clear_destination' => false, // Do not overwrite files. 110 'clear_working' => true, 111 'hook_extra' => array( 112 'type' => 'plugin', 113 'action' => 'install', 106 $this->run( 107 array( 108 'package' => $package, 109 'destination' => WP_PLUGIN_DIR, 110 'clear_destination' => false, // Do not overwrite files. 111 'clear_working' => true, 112 'hook_extra' => array( 113 'type' => 'plugin', 114 'action' => 'install', 115 ), 114 116 ) 115 ) );117 ); 116 118 117 119 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 ); 118 remove_filter( 'upgrader_source_selection', array($this, 'check_package') );119 120 if ( ! $this->result || is_wp_error( $this->result) )120 remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) ); 121 122 if ( ! $this->result || is_wp_error( $this->result ) ) { 121 123 return $this->result; 124 } 122 125 123 126 // Force refresh of plugin update information … … 144 147 public function upgrade( $plugin, $args = array() ) { 145 148 146 $defaults = array(149 $defaults = array( 147 150 'clear_update_cache' => true, 148 151 ); … … 153 156 154 157 $current = get_site_transient( 'update_plugins' ); 155 if ( ! isset( $current->response[ $plugin ] ) ) {158 if ( ! isset( $current->response[ $plugin ] ) ) { 156 159 $this->skin->before(); 157 $this->skin->set_result( false);158 $this->skin->error( 'up_to_date');160 $this->skin->set_result( false ); 161 $this->skin->error( 'up_to_date' ); 159 162 $this->skin->after(); 160 163 return false; … … 164 167 $r = $current->response[ $plugin ]; 165 168 166 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);169 add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 ); 170 add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 ); 168 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 172 if ( $parsed_args['clear_update_cache'] ) { … … 172 175 } 173 176 174 $this->run( array( 175 'package' => $r->package, 176 'destination' => WP_PLUGIN_DIR, 177 'clear_destination' => true, 178 'clear_working' => true, 179 'hook_extra' => array( 180 'plugin' => $plugin, 181 'type' => 'plugin', 182 'action' => 'update', 183 ), 184 ) ); 177 $this->run( 178 array( 179 'package' => $r->package, 180 'destination' => WP_PLUGIN_DIR, 181 'clear_destination' => true, 182 'clear_working' => true, 183 'hook_extra' => array( 184 'plugin' => $plugin, 185 'type' => 'plugin', 186 'action' => 'update', 187 ), 188 ) 189 ); 185 190 186 191 // Cleanup our hooks, in case something else does a upgrade on this connection. 187 192 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 ); 188 remove_filter( 'upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));189 remove_filter( 'upgrader_clear_destination', array($this, 'delete_old_plugin'));190 191 if ( ! $this->result || is_wp_error( $this->result) )193 remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) ); 194 remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) ); 195 196 if ( ! $this->result || is_wp_error( $this->result ) ) { 192 197 return $this->result; 198 } 193 199 194 200 // Force refresh of plugin update information … … 215 221 public function bulk_upgrade( $plugins, $args = array() ) { 216 222 217 $defaults = array(223 $defaults = array( 218 224 'clear_update_cache' => true, 219 225 ); … … 226 232 $current = get_site_transient( 'update_plugins' ); 227 233 228 add_filter( 'upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);234 add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 ); 229 235 230 236 $this->skin->header(); 231 237 232 238 // Connect to the Filesystem first. 233 $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR) );239 $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); 234 240 if ( ! $res ) { 235 241 $this->skin->footer(); … … 246 252 */ 247 253 $maintenance = ( is_multisite() && ! empty( $plugins ) ); 248 foreach ( $plugins as $plugin ) 249 $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) ); 250 if ( $maintenance ) 251 $this->maintenance_mode(true); 254 foreach ( $plugins as $plugin ) { 255 $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) ); 256 } 257 if ( $maintenance ) { 258 $this->maintenance_mode( true ); 259 } 252 260 253 261 $results = array(); 254 262 255 $this->update_count = count($plugins);263 $this->update_count = count( $plugins ); 256 264 $this->update_current = 0; 257 265 foreach ( $plugins as $plugin ) { 258 266 $this->update_current++; 259 $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );260 261 if ( ! isset( $current->response[ $plugin ] ) ) {262 $this->skin->set_result( 'up_to_date');267 $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true ); 268 269 if ( ! isset( $current->response[ $plugin ] ) ) { 270 $this->skin->set_result( 'up_to_date' ); 263 271 $this->skin->before(); 264 $this->skin->feedback( 'up_to_date');272 $this->skin->feedback( 'up_to_date' ); 265 273 $this->skin->after(); 266 $results[ $plugin] = true;274 $results[ $plugin ] = true; 267 275 continue; 268 276 } … … 271 279 $r = $current->response[ $plugin ]; 272 280 273 $this->skin->plugin_active = is_plugin_active($plugin); 274 275 $result = $this->run( array( 276 'package' => $r->package, 277 'destination' => WP_PLUGIN_DIR, 278 'clear_destination' => true, 279 'clear_working' => true, 280 'is_multi' => true, 281 'hook_extra' => array( 282 'plugin' => $plugin 281 $this->skin->plugin_active = is_plugin_active( $plugin ); 282 283 $result = $this->run( 284 array( 285 'package' => $r->package, 286 'destination' => WP_PLUGIN_DIR, 287 'clear_destination' => true, 288 'clear_working' => true, 289 'is_multi' => true, 290 'hook_extra' => array( 291 'plugin' => $plugin, 292 ), 283 293 ) 284 ) );285 286 $results[ $plugin] = $this->result;294 ); 295 296 $results[ $plugin ] = $this->result; 287 297 288 298 // Prevent credentials auth screen from displaying multiple times 289 if ( false === $result ) 299 if ( false === $result ) { 290 300 break; 301 } 291 302 } //end foreach $plugins 292 303 293 $this->maintenance_mode( false);304 $this->maintenance_mode( false ); 294 305 295 306 // Force refresh of plugin update information. … … 297 308 298 309 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */ 299 do_action( 'upgrader_process_complete', $this, array( 300 'action' => 'update', 301 'type' => 'plugin', 302 'bulk' => true, 303 'plugins' => $plugins, 304 ) ); 310 do_action( 311 'upgrader_process_complete', $this, array( 312 'action' => 'update', 313 'type' => 'plugin', 314 'bulk' => true, 315 'plugins' => $plugins, 316 ) 317 ); 305 318 306 319 $this->skin->bulk_footer(); … … 309 322 310 323 // Cleanup our hooks, in case something else does a upgrade on this connection. 311 remove_filter( 'upgrader_clear_destination', array($this, 'delete_old_plugin'));324 remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) ); 312 325 313 326 return $results; … … 328 341 * if no plugins were found. 329 342 */ 330 public function check_package( $source) {343 public function check_package( $source ) { 331 344 global $wp_filesystem; 332 345 333 if ( is_wp_error( $source) )346 if ( is_wp_error( $source ) ) { 334 347 return $source; 335 336 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source); 337 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation. 348 } 349 350 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source ); 351 if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation. 338 352 return $source; 353 } 339 354 340 355 // Check the folder contains at least 1 valid plugin. 341 356 $plugins_found = false; 342 $files = glob( $working_directory . '*.php' );357 $files = glob( $working_directory . '*.php' ); 343 358 if ( $files ) { 344 359 foreach ( $files as $file ) { … … 351 366 } 352 367 353 if ( ! $plugins_found ) 368 if ( ! $plugins_found ) { 354 369 return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) ); 370 } 355 371 356 372 return $source; … … 367 383 */ 368 384 public function plugin_info() { 369 if ( ! is_array( $this->result) )385 if ( ! is_array( $this->result ) ) { 370 386 return false; 371 if ( empty($this->result['destination_name']) ) 387 } 388 if ( empty( $this->result['destination_name'] ) ) { 372 389 return false; 373 374 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash 375 if ( empty($plugin) ) 390 } 391 392 $plugin = get_plugins( '/' . $this->result['destination_name'] ); //Ensure to pass with leading slash 393 if ( empty( $plugin ) ) { 376 394 return false; 377 378 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list 395 } 396 397 $pluginfiles = array_keys( $plugin ); //Assume the requested plugin is the first in the list 379 398 380 399 return $this->result['destination_name'] . '/' . $pluginfiles[0]; … … 393 412 * @return bool|WP_Error The passed in $return param or WP_Error. 394 413 */ 395 public function deactivate_plugin_before_upgrade( $return, $plugin) {396 397 if ( is_wp_error( $return) )//Bypass.414 public function deactivate_plugin_before_upgrade( $return, $plugin ) { 415 416 if ( is_wp_error( $return ) ) { //Bypass. 398 417 return $return; 418 } 399 419 400 420 // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it 401 if ( wp_doing_cron() ) 421 if ( wp_doing_cron() ) { 402 422 return $return; 403 404 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : ''; 405 if ( empty($plugin) ) 406 return new WP_Error('bad_request', $this->strings['bad_request']); 407 408 if ( is_plugin_active($plugin) ) { 423 } 424 425 $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : ''; 426 if ( empty( $plugin ) ) { 427 return new WP_Error( 'bad_request', $this->strings['bad_request'] ); 428 } 429 430 if ( is_plugin_active( $plugin ) ) { 409 431 //Deactivate the plugin silently, Prevent deactivation hooks from running. 410 deactivate_plugins( $plugin, true);432 deactivate_plugins( $plugin, true ); 411 433 } 412 434 … … 423 445 * 424 446 * @global WP_Filesystem_Base $wp_filesystem Subclass 425 *447 * 426 448 * @param bool|WP_Error $removed 427 449 * @param string $local_destination … … 430 452 * @return WP_Error|bool 431 453 */ 432 public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin) {454 public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) { 433 455 global $wp_filesystem; 434 456 435 if ( is_wp_error( $removed) )457 if ( is_wp_error( $removed ) ) { 436 458 return $removed; //Pass errors through. 437 438 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : ''; 439 if ( empty($plugin) ) 440 return new WP_Error('bad_request', $this->strings['bad_request']); 441 442 $plugins_dir = $wp_filesystem->wp_plugins_dir(); 443 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) ); 444 445 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished. 459 } 460 461 $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : ''; 462 if ( empty( $plugin ) ) { 463 return new WP_Error( 'bad_request', $this->strings['bad_request'] ); 464 } 465 466 $plugins_dir = $wp_filesystem->wp_plugins_dir(); 467 $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) ); 468 469 if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { //If it's already vanished. 446 470 return $removed; 471 } 447 472 448 473 // 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 450 $deleted = $wp_filesystem->delete($this_plugin_dir, true); 451 else 452 $deleted = $wp_filesystem->delete($plugins_dir . $plugin); 453 454 if ( ! $deleted ) 455 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']); 474 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 $deleted = $wp_filesystem->delete( $this_plugin_dir, true ); 476 } else { 477 $deleted = $wp_filesystem->delete( $plugins_dir . $plugin ); 478 } 479 480 if ( ! $deleted ) { 481 return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] ); 482 } 456 483 457 484 return true;
Note: See TracChangeset
for help on using the changeset viewer.