[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-plugin-upgrader.php (source)

   1  <?php
   2  /**
   3   * Upgrade API: Plugin_Upgrader class
   4   *
   5   * @package WordPress
   6   * @subpackage Upgrader
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Core class used for upgrading/installing plugins.
  12   *
  13   * It is designed to upgrade/install plugins from a local zip, remote zip URL,
  14   * or uploaded zip file.
  15   *
  16   * @since 2.8.0
  17   * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  18   *
  19   * @see WP_Upgrader
  20   */
  21  class Plugin_Upgrader extends WP_Upgrader {
  22  
  23      /**
  24       * Plugin upgrade result.
  25       *
  26       * @since 2.8.0
  27       * @var array|WP_Error|null $result
  28       * @phpstan-var non-empty-array<string, mixed>|WP_Error|null
  29       *
  30       * @see WP_Upgrader::$result
  31       */
  32      public $result;
  33  
  34      /**
  35       * Whether a bulk upgrade/installation is being performed.
  36       *
  37       * @since 2.9.0
  38       * @var bool $bulk
  39       */
  40      public $bulk = false;
  41  
  42      /**
  43       * New plugin info.
  44       *
  45       * @since 5.5.0
  46       * @var array $new_plugin_data
  47       *
  48       * @see check_package()
  49       */
  50      public $new_plugin_data = array();
  51  
  52      /**
  53       * Initializes the upgrade strings.
  54       *
  55       * @since 2.8.0
  56       */
  57  	public function upgrade_strings() {
  58          $this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
  59          $this->strings['no_package'] = __( 'Update package not available.' );
  60          /* translators: %s: Package URL. */
  61          $this->strings['downloading_package']  = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code pre">%s</span>' );
  62          $this->strings['unpack_package']       = __( 'Unpacking the update&#8230;' );
  63          $this->strings['remove_old']           = __( 'Removing the old version of the plugin&#8230;' );
  64          $this->strings['remove_old_failed']    = __( 'Could not remove the old plugin.' );
  65          $this->strings['process_failed']       = __( 'Plugin update failed.' );
  66          $this->strings['process_success']      = __( 'Plugin updated successfully.' );
  67          $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
  68      }
  69  
  70      /**
  71       * Initializes the installation strings.
  72       *
  73       * @since 2.8.0
  74       */
  75  	public function install_strings() {
  76          $this->strings['no_package'] = __( 'Installation package not available.' );
  77          /* translators: %s: Package URL. */
  78          $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code pre">%s</span>' );
  79          $this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
  80          $this->strings['installing_package']  = __( 'Installing the plugin&#8230;' );
  81          $this->strings['remove_old']          = __( 'Removing the current plugin&#8230;' );
  82          $this->strings['remove_old_failed']   = __( 'Could not remove the current plugin.' );
  83          $this->strings['no_files']            = __( 'The plugin contains no files.' );
  84          $this->strings['process_failed']      = __( 'Plugin installation failed.' );
  85          $this->strings['process_success']     = __( 'Plugin installed successfully.' );
  86          /* translators: 1: Plugin name, 2: Plugin version. */
  87          $this->strings['process_success_specific'] = __( 'Successfully installed the plugin <strong>%1$s %2$s</strong>.' );
  88  
  89          if ( ! empty( $this->skin->overwrite ) ) {
  90              if ( 'update-plugin' === $this->skin->overwrite ) {
  91                  $this->strings['installing_package'] = __( 'Updating the plugin&#8230;' );
  92                  $this->strings['process_failed']     = __( 'Plugin update failed.' );
  93                  $this->strings['process_success']    = __( 'Plugin updated successfully.' );
  94              }
  95  
  96              if ( 'downgrade-plugin' === $this->skin->overwrite ) {
  97                  $this->strings['installing_package'] = __( 'Downgrading the plugin&#8230;' );
  98                  $this->strings['process_failed']     = __( 'Plugin downgrade failed.' );
  99                  $this->strings['process_success']    = __( 'Plugin downgraded successfully.' );
 100              }
 101          }
 102      }
 103  
 104      /**
 105       * Install a plugin package.
 106       *
 107       * @since 2.8.0
 108       * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
 109       *
 110       * @param string $package The full local path or URI of the package.
 111       * @param array  $args {
 112       *     Optional. Other arguments for installing a plugin package. Default empty array.
 113       *
 114       *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
 115       *                                    Default true.
 116       * }
 117       * @return bool|WP_Error|null True if the installation was successful, false or a WP_Error otherwise.
 118       *                            Null if the installation failed before the package was installed, for
 119       *                            example when the package could not be downloaded.
 120       */
 121  	public function install( $package, $args = array() ) {
 122          $defaults    = array(
 123              'clear_update_cache' => true,
 124              'overwrite_package'  => false, // Do not overwrite files.
 125          );
 126          $parsed_args = wp_parse_args( $args, $defaults );
 127  
 128          $this->init();
 129          $this->install_strings();
 130  
 131          add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 132  
 133          if ( $parsed_args['clear_update_cache'] ) {
 134              // Clear cache so wp_update_plugins() knows about the new plugin.
 135              add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
 136          }
 137  
 138          $this->run(
 139              array(
 140                  'package'           => $package,
 141                  'destination'       => WP_PLUGIN_DIR,
 142                  'clear_destination' => $parsed_args['overwrite_package'],
 143                  'clear_working'     => true,
 144                  'hook_extra'        => array(
 145                      'type'   => 'plugin',
 146                      'action' => 'install',
 147                  ),
 148              )
 149          );
 150  
 151          remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
 152          remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 153  
 154          if ( ! $this->result || is_wp_error( $this->result ) ) {
 155              return $this->result;
 156          }
 157  
 158          // Force refresh of plugin update information.
 159          wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
 160  
 161          if ( $parsed_args['overwrite_package'] ) {
 162              /**
 163               * Fires when the upgrader has successfully overwritten a currently installed
 164               * plugin or theme with an uploaded zip package.
 165               *
 166               * @since 5.5.0
 167               *
 168               * @param string  $package      The package file.
 169               * @param array   $data         The new plugin or theme data.
 170               * @param string  $package_type The package type ('plugin' or 'theme').
 171               */
 172              do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
 173          }
 174  
 175          return true;
 176      }
 177  
 178      /**
 179       * Upgrades a plugin.
 180       *
 181       * @since 2.8.0
 182       * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
 183       *
 184       * @param string $plugin Path to the plugin file relative to the plugins directory.
 185       * @param array  $args {
 186       *     Optional. Other arguments for upgrading a plugin package. Default empty array.
 187       *
 188       *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
 189       *                                    Default true.
 190       * }
 191       * @return bool|WP_Error|null True if the upgrade was successful, false or a WP_Error object otherwise.
 192       *                            Null if the upgrade failed before the package was installed, for example
 193       *                            when the package could not be downloaded.
 194       */
 195  	public function upgrade( $plugin, $args = array() ) {
 196          $defaults    = array(
 197              'clear_update_cache' => true,
 198          );
 199          $parsed_args = wp_parse_args( $args, $defaults );
 200  
 201          $this->init();
 202          $this->upgrade_strings();
 203  
 204          $current = get_site_transient( 'update_plugins' );
 205          if ( ! isset( $current->response[ $plugin ] ) ) {
 206              $this->skin->before();
 207              $this->skin->set_result( false );
 208              $this->skin->error( 'up_to_date' );
 209              $this->skin->after();
 210              return false;
 211          }
 212  
 213          // Get the URL to the zip file.
 214          $upgrade_data = $current->response[ $plugin ];
 215  
 216          add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
 217          add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
 218          add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
 219          add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
 220          /*
 221           * There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
 222           * 'source_selection' => array( $this, 'source_selection' ),
 223           */
 224          if ( $parsed_args['clear_update_cache'] ) {
 225              // Clear cache so wp_update_plugins() knows about the new plugin.
 226              add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
 227          }
 228  
 229          $this->run(
 230              array(
 231                  'package'           => $upgrade_data->package,
 232                  'destination'       => WP_PLUGIN_DIR,
 233                  'clear_destination' => true,
 234                  'clear_working'     => true,
 235                  'hook_extra'        => array(
 236                      'plugin'      => $plugin,
 237                      'type'        => 'plugin',
 238                      'action'      => 'update',
 239                      'temp_backup' => array(
 240                          'slug' => dirname( $plugin ),
 241                          'src'  => WP_PLUGIN_DIR,
 242                          'dir'  => 'plugins',
 243                      ),
 244                  ),
 245              )
 246          );
 247  
 248          // Cleanup our hooks, in case something else does an upgrade on this connection.
 249          remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
 250          remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
 251          remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
 252          remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
 253          remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
 254  
 255          if ( ! $this->result || is_wp_error( $this->result ) ) {
 256              return $this->result;
 257          }
 258  
 259          // Force refresh of plugin update information.
 260          wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
 261  
 262          /*
 263           * Ensure any future auto-update failures trigger a failure email by removing
 264           * the last failure notification from the list when plugins update successfully.
 265           */
 266          $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
 267  
 268          if ( isset( $past_failure_emails[ $plugin ] ) ) {
 269              unset( $past_failure_emails[ $plugin ] );
 270              update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
 271          }
 272  
 273          return true;
 274      }
 275  
 276      /**
 277       * Upgrades several plugins at once.
 278       *
 279       * @since 2.8.0
 280       * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
 281       *
 282       * @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
 283       * @param array    $args {
 284       *     Optional. Other arguments for upgrading several plugins at once.
 285       *
 286       *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
 287       * }
 288       * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
 289       */
 290  	public function bulk_upgrade( $plugins, $args = array() ) {
 291          $wp_version = wp_get_wp_version();
 292  
 293          $defaults    = array(
 294              'clear_update_cache' => true,
 295          );
 296          $parsed_args = wp_parse_args( $args, $defaults );
 297  
 298          $this->init();
 299          $this->bulk = true;
 300          $this->upgrade_strings();
 301  
 302          $current = get_site_transient( 'update_plugins' );
 303  
 304          add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
 305  
 306          $this->skin->header();
 307  
 308          // Connect to the filesystem first.
 309          $connected = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
 310          if ( ! $connected ) {
 311              $this->skin->footer();
 312              return false;
 313          }
 314  
 315          $this->skin->bulk_header();
 316  
 317          /*
 318           * Only start maintenance mode if:
 319           * - running Multisite and there are one or more plugins specified, OR
 320           * - a plugin with an update available is currently active.
 321           * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
 322           */
 323          $maintenance = ( is_multisite() && ! empty( $plugins ) );
 324          foreach ( $plugins as $plugin ) {
 325              $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
 326          }
 327          if ( $maintenance ) {
 328              $this->maintenance_mode( true );
 329          }
 330  
 331          $results = array();
 332  
 333          $this->update_count   = count( $plugins );
 334          $this->update_current = 0;
 335          foreach ( $plugins as $plugin ) {
 336              ++$this->update_current;
 337              $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
 338  
 339              if ( ! isset( $current->response[ $plugin ] ) ) {
 340                  $this->skin->set_result( 'up_to_date' );
 341                  $this->skin->before();
 342                  $this->skin->feedback( 'up_to_date' );
 343                  $this->skin->after();
 344                  $results[ $plugin ] = true;
 345                  continue;
 346              }
 347  
 348              // Get the URL to the zip file.
 349              $upgrade_data = $current->response[ $plugin ];
 350  
 351              $this->skin->plugin_active = is_plugin_active( $plugin );
 352  
 353              if ( isset( $upgrade_data->requires ) && ! is_wp_version_compatible( $upgrade_data->requires ) ) {
 354                  $result = new WP_Error(
 355                      'incompatible_wp_required_version',
 356                      sprintf(
 357                          /* translators: 1: Current WordPress version, 2: WordPress version required by the new plugin version. */
 358                          __( 'Your WordPress version is %1$s, however the new plugin version requires %2$s.' ),
 359                          $wp_version,
 360                          $upgrade_data->requires
 361                      )
 362                  );
 363  
 364                  $this->skin->before();
 365                  $this->skin->error( $result );
 366                  $this->skin->after();
 367              } elseif ( isset( $upgrade_data->requires_php ) && ! is_php_version_compatible( $upgrade_data->requires_php ) ) {
 368                  $result = new WP_Error(
 369                      'incompatible_php_required_version',
 370                      sprintf(
 371                          /* translators: 1: Current PHP version, 2: PHP version required by the new plugin version. */
 372                          __( 'The PHP version on your server is %1$s, however the new plugin version requires %2$s.' ),
 373                          PHP_VERSION,
 374                          $upgrade_data->requires_php
 375                      )
 376                  );
 377  
 378                  $this->skin->before();
 379                  $this->skin->error( $result );
 380                  $this->skin->after();
 381              } else {
 382                  add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 383                  $result = $this->run(
 384                      array(
 385                          'package'           => $upgrade_data->package,
 386                          'destination'       => WP_PLUGIN_DIR,
 387                          'clear_destination' => true,
 388                          'clear_working'     => true,
 389                          'is_multi'          => true,
 390                          'hook_extra'        => array(
 391                              'plugin'      => $plugin,
 392                              'temp_backup' => array(
 393                                  'slug' => dirname( $plugin ),
 394                                  'src'  => WP_PLUGIN_DIR,
 395                                  'dir'  => 'plugins',
 396                              ),
 397                          ),
 398                      )
 399                  );
 400                  remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 401              }
 402  
 403              $results[ $plugin ] = $result;
 404  
 405              // Prevent credentials auth screen from displaying multiple times.
 406              if ( false === $result ) {
 407                  break;
 408              }
 409          } // End foreach $plugins.
 410  
 411          $this->maintenance_mode( false );
 412  
 413          // Force refresh of plugin update information.
 414          wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
 415  
 416          /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
 417          do_action(
 418              'upgrader_process_complete',
 419              $this,
 420              array(
 421                  'action'  => 'update',
 422                  'type'    => 'plugin',
 423                  'bulk'    => true,
 424                  'plugins' => $plugins,
 425              )
 426          );
 427  
 428          $this->skin->bulk_footer();
 429  
 430          $this->skin->footer();
 431  
 432          // Cleanup our hooks, in case something else does an upgrade on this connection.
 433          remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
 434  
 435          /*
 436           * Ensure any future auto-update failures trigger a failure email by removing
 437           * the last failure notification from the list when plugins update successfully.
 438           */
 439          $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
 440  
 441          foreach ( $results as $plugin => $result ) {
 442              // Maintain last failure notification when plugins failed to update manually.
 443              if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $plugin ] ) ) {
 444                  continue;
 445              }
 446  
 447              unset( $past_failure_emails[ $plugin ] );
 448          }
 449  
 450          update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
 451  
 452          return $results;
 453      }
 454  
 455      /**
 456       * Checks that the source package contains a valid plugin.
 457       *
 458       * Hooked to the {@see 'upgrader_source_selection'} filter by Plugin_Upgrader::install().
 459       *
 460       * @since 3.3.0
 461       *
 462       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 463       *
 464       * @param string $source The path to the downloaded package source.
 465       * @return string|WP_Error The source as passed, or a WP_Error object on failure.
 466       */
 467  	public function check_package( $source ) {
 468          global $wp_filesystem;
 469  
 470          $wp_version            = wp_get_wp_version();
 471          $this->new_plugin_data = array();
 472  
 473          if ( is_wp_error( $source ) ) {
 474              return $source;
 475          }
 476  
 477          $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
 478          if ( ! is_dir( $working_directory ) ) { // Confidence check, if the above fails, let's not prevent installation.
 479              return $source;
 480          }
 481  
 482          // Check that the folder contains at least 1 valid plugin.
 483          $files = glob( $working_directory . '*.php' );
 484          if ( $files ) {
 485              foreach ( $files as $file ) {
 486                  $new_plugin_data = get_plugin_data( $file, false, false );
 487                  if ( ! empty( $new_plugin_data['Name'] ) ) {
 488                      $this->new_plugin_data = $new_plugin_data;
 489                      break;
 490                  }
 491              }
 492          }
 493  
 494          if ( empty( $this->new_plugin_data ) ) {
 495              return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
 496          }
 497  
 498          $requires_php = $new_plugin_data['RequiresPHP'] ?? null;
 499          $requires_wp  = $new_plugin_data['RequiresWP'] ?? null;
 500  
 501          if ( ! is_php_version_compatible( $requires_php ) ) {
 502              $error = sprintf(
 503                  /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
 504                  __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
 505                  PHP_VERSION,
 506                  $requires_php
 507              );
 508  
 509              return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
 510          }
 511  
 512          if ( ! is_wp_version_compatible( $requires_wp ) ) {
 513              $error = sprintf(
 514                  /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
 515                  __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
 516                  $wp_version,
 517                  $requires_wp
 518              );
 519  
 520              return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
 521          }
 522  
 523          return $source;
 524      }
 525  
 526      /**
 527       * Retrieves the path to the file that contains the plugin info.
 528       *
 529       * This isn't used internally in the class, but is called by the skins.
 530       *
 531       * @since 2.8.0
 532       *
 533       * @return string|false The full path to the main plugin file, or false.
 534       */
 535  	public function plugin_info() {
 536          if ( ! is_array( $this->result ) ) {
 537              return false;
 538          }
 539          if ( empty( $this->result['destination_name'] ) ) {
 540              return false;
 541          }
 542  
 543          // Ensure to pass with leading slash.
 544          $plugin = get_plugins( '/' . $this->result['destination_name'] );
 545          if ( empty( $plugin ) ) {
 546              return false;
 547          }
 548  
 549          // Assume the requested plugin is the first in the list.
 550          return $this->result['destination_name'] . '/' . array_key_first( $plugin );
 551      }
 552  
 553      /**
 554       * Deactivates a plugin before it is upgraded.
 555       *
 556       * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
 557       *
 558       * @since 2.8.0
 559       * @since 4.1.0 Added a return value.
 560       *
 561       * @param bool|WP_Error $response The installation response before the installation has started.
 562       * @param array         $plugin   Plugin package arguments.
 563       * @return bool|WP_Error The original `$response` parameter or WP_Error.
 564       */
 565  	public function deactivate_plugin_before_upgrade( $response, $plugin ) {
 566  
 567          if ( is_wp_error( $response ) ) { // Bypass.
 568              return $response;
 569          }
 570  
 571          // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it.
 572          if ( wp_doing_cron() ) {
 573              return $response;
 574          }
 575  
 576          $plugin = $plugin['plugin'] ?? '';
 577          if ( empty( $plugin ) ) {
 578              return new WP_Error( 'bad_request', $this->strings['bad_request'] );
 579          }
 580  
 581          if ( is_plugin_active( $plugin ) ) {
 582              // Deactivate the plugin silently, Prevent deactivation hooks from running.
 583              deactivate_plugins( $plugin, true );
 584          }
 585  
 586          return $response;
 587      }
 588  
 589      /**
 590       * Turns on maintenance mode before attempting to background update an active plugin.
 591       *
 592       * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
 593       *
 594       * @since 5.4.0
 595       *
 596       * @param bool|WP_Error $response The installation response before the installation has started.
 597       * @param array         $plugin   Plugin package arguments.
 598       * @return bool|WP_Error The original `$response` parameter or WP_Error.
 599       */
 600  	public function active_before( $response, $plugin ) {
 601          if ( is_wp_error( $response ) ) {
 602              return $response;
 603          }
 604  
 605          // Only enable maintenance mode when in cron (background update).
 606          if ( ! wp_doing_cron() ) {
 607              return $response;
 608          }
 609  
 610          $plugin = $plugin['plugin'] ?? '';
 611  
 612          // Only run if plugin is active.
 613          if ( ! is_plugin_active( $plugin ) ) {
 614              return $response;
 615          }
 616  
 617          // Change to maintenance mode. Bulk edit handles this separately.
 618          if ( ! $this->bulk ) {
 619              $this->maintenance_mode( true );
 620          }
 621  
 622          return $response;
 623      }
 624  
 625      /**
 626       * Turns off maintenance mode after upgrading an active plugin.
 627       *
 628       * Hooked to the {@see 'upgrader_post_install'} filter by Plugin_Upgrader::upgrade().
 629       *
 630       * @since 5.4.0
 631       *
 632       * @param bool|WP_Error $response The installation response after the installation has finished.
 633       * @param array         $plugin   Plugin package arguments.
 634       * @return bool|WP_Error The original `$response` parameter or WP_Error.
 635       */
 636  	public function active_after( $response, $plugin ) {
 637          if ( is_wp_error( $response ) ) {
 638              return $response;
 639          }
 640  
 641          // Only disable maintenance mode when in cron (background update).
 642          if ( ! wp_doing_cron() ) {
 643              return $response;
 644          }
 645  
 646          $plugin = $plugin['plugin'] ?? '';
 647  
 648          // Only run if plugin is active.
 649          if ( ! is_plugin_active( $plugin ) ) {
 650              return $response;
 651          }
 652  
 653          // Time to remove maintenance mode. Bulk edit handles this separately.
 654          if ( ! $this->bulk ) {
 655              $this->maintenance_mode( false );
 656          }
 657  
 658          return $response;
 659      }
 660  
 661      /**
 662       * Deletes the old plugin during an upgrade.
 663       *
 664       * Hooked to the {@see 'upgrader_clear_destination'} filter by
 665       * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
 666       *
 667       * @since 2.8.0
 668       *
 669       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 670       *
 671       * @param bool|WP_Error $removed            Whether the destination was cleared.
 672       *                                          True on success, WP_Error on failure.
 673       * @param string        $local_destination  The local package destination.
 674       * @param string        $remote_destination The remote package destination.
 675       * @param array         $plugin             Extra arguments passed to hooked filters.
 676       * @return bool|WP_Error
 677       */
 678  	public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
 679          global $wp_filesystem;
 680  
 681          if ( is_wp_error( $removed ) ) {
 682              return $removed; // Pass errors through.
 683          }
 684  
 685          $plugin = $plugin['plugin'] ?? '';
 686          if ( empty( $plugin ) ) {
 687              return new WP_Error( 'bad_request', $this->strings['bad_request'] );
 688          }
 689  
 690          $plugins_dir     = $wp_filesystem->wp_plugins_dir();
 691          $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
 692  
 693          if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { // If it's already vanished.
 694              return $removed;
 695          }
 696  
 697          /*
 698           * If plugin is in its own directory, recursively delete the directory.
 699           * Base check on if plugin includes directory separator AND that it's not the root plugin folder.
 700           */
 701          if ( strpos( $plugin, '/' ) && $this_plugin_dir !== $plugins_dir ) {
 702              $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
 703          } else {
 704              $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
 705          }
 706  
 707          if ( ! $deleted ) {
 708              return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
 709          }
 710  
 711          return true;
 712      }
 713  }


Generated : Fri Oct 30 08:20:34 2026 Cross-referenced by PHPXref