[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Oct 21 01:00:03 2021 Cross-referenced by PHPXref 0.7.1