[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Upgrade API: Theme_Upgrader class
   4   *
   5   * @package WordPress
   6   * @subpackage Upgrader
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Core class used for upgrading/installing themes.
  12   *
  13   * It is designed to upgrade/install themes 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 Theme_Upgrader extends WP_Upgrader {
  22  
  23      /**
  24       * Result of the theme upgrade offer.
  25       *
  26       * @since 2.8.0
  27       * @var array|WP_Error $result
  28       * @see WP_Upgrader::$result
  29       */
  30      public $result;
  31  
  32      /**
  33       * Whether multiple themes are being upgraded/installed in bulk.
  34       *
  35       * @since 2.9.0
  36       * @var bool $bulk
  37       */
  38      public $bulk = false;
  39  
  40      /**
  41       * New theme info.
  42       *
  43       * @since 5.5.0
  44       * @var array $new_theme_data
  45       *
  46       * @see check_package()
  47       */
  48      public $new_theme_data = array();
  49  
  50      /**
  51       * Initialize the upgrade strings.
  52       *
  53       * @since 2.8.0
  54       */
  55  	public function upgrade_strings() {
  56          $this->strings['up_to_date'] = __( 'The theme is at the latest version.' );
  57          $this->strings['no_package'] = __( 'Update package not available.' );
  58          /* translators: %s: Package URL. */
  59          $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  60          $this->strings['unpack_package']      = __( 'Unpacking the update&#8230;' );
  61          $this->strings['remove_old']          = __( 'Removing the old version of the theme&#8230;' );
  62          $this->strings['remove_old_failed']   = __( 'Could not remove the old theme.' );
  63          $this->strings['process_failed']      = __( 'Theme update failed.' );
  64          $this->strings['process_success']     = __( 'Theme updated successfully.' );
  65      }
  66  
  67      /**
  68       * Initialize the installation strings.
  69       *
  70       * @since 2.8.0
  71       */
  72  	public function install_strings() {
  73          $this->strings['no_package'] = __( 'Installation package not available.' );
  74          /* translators: %s: Package URL. */
  75          $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
  76          $this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
  77          $this->strings['installing_package']  = __( 'Installing the theme&#8230;' );
  78          $this->strings['remove_old']          = __( 'Removing the old version of the theme&#8230;' );
  79          $this->strings['remove_old_failed']   = __( 'Could not remove the old theme.' );
  80          $this->strings['no_files']            = __( 'The theme contains no files.' );
  81          $this->strings['process_failed']      = __( 'Theme installation failed.' );
  82          $this->strings['process_success']     = __( 'Theme installed successfully.' );
  83          /* translators: 1: Theme name, 2: Theme version. */
  84          $this->strings['process_success_specific'] = __( 'Successfully installed the theme <strong>%1$s %2$s</strong>.' );
  85          $this->strings['parent_theme_search']      = __( 'This theme requires a parent theme. Checking if it is installed&#8230;' );
  86          /* translators: 1: Theme name, 2: Theme version. */
  87          $this->strings['parent_theme_prepare_install'] = __( 'Preparing to install <strong>%1$s %2$s</strong>&#8230;' );
  88          /* translators: 1: Theme name, 2: Theme version. */
  89          $this->strings['parent_theme_currently_installed'] = __( 'The parent theme, <strong>%1$s %2$s</strong>, is currently installed.' );
  90          /* translators: 1: Theme name, 2: Theme version. */
  91          $this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, <strong>%1$s %2$s</strong>.' );
  92          /* translators: %s: Theme name. */
  93          $this->strings['parent_theme_not_found'] = sprintf( __( '<strong>The parent theme could not be found.</strong> You will need to install the parent theme, %s, before you can use this child theme.' ), '<strong>%s</strong>' );
  94          /* translators: %s: Theme error. */
  95          $this->strings['current_theme_has_errors'] = __( 'The current theme has the following error: "%s".' );
  96  
  97          if ( ! empty( $this->skin->overwrite ) ) {
  98              if ( 'update-theme' === $this->skin->overwrite ) {
  99                  $this->strings['installing_package'] = __( 'Updating the theme&#8230;' );
 100                  $this->strings['process_failed']     = __( 'Theme update failed.' );
 101                  $this->strings['process_success']    = __( 'Theme updated successfully.' );
 102              }
 103  
 104              if ( 'downgrade-theme' === $this->skin->overwrite ) {
 105                  $this->strings['installing_package'] = __( 'Downgrading the theme&#8230;' );
 106                  $this->strings['process_failed']     = __( 'Theme downgrade failed.' );
 107                  $this->strings['process_success']    = __( 'Theme downgraded successfully.' );
 108              }
 109          }
 110      }
 111  
 112      /**
 113       * Check if a child theme is being installed and we need to install its parent.
 114       *
 115       * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
 116       *
 117       * @since 3.4.0
 118       *
 119       * @param bool  $install_result
 120       * @param array $hook_extra
 121       * @param array $child_result
 122       * @return bool
 123       */
 124  	public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
 125          // Check to see if we need to install a parent theme.
 126          $theme_info = $this->theme_info();
 127  
 128          if ( ! $theme_info->parent() ) {
 129              return $install_result;
 130          }
 131  
 132          $this->skin->feedback( 'parent_theme_search' );
 133  
 134          if ( ! $theme_info->parent()->errors() ) {
 135              $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display( 'Name' ), $theme_info->parent()->display( 'Version' ) );
 136              // We already have the theme, fall through.
 137              return $install_result;
 138          }
 139  
 140          // We don't have the parent theme, let's install it.
 141          $api = themes_api(
 142              'theme_information',
 143              array(
 144                  'slug'   => $theme_info->get( 'Template' ),
 145                  'fields' => array(
 146                      'sections' => false,
 147                      'tags'     => false,
 148                  ),
 149              )
 150          ); // Save on a bit of bandwidth.
 151  
 152          if ( ! $api || is_wp_error( $api ) ) {
 153              $this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
 154              // Don't show activate or preview actions after installation.
 155              add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
 156              return $install_result;
 157          }
 158  
 159          // Backup required data we're going to override:
 160          $child_api             = $this->skin->api;
 161          $child_success_message = $this->strings['process_success'];
 162  
 163          // Override them.
 164          $this->skin->api = $api;
 165  
 166          $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];
 167  
 168          $this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
 169  
 170          add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
 171  
 172          // Install the parent theme.
 173          $parent_result = $this->run(
 174              array(
 175                  'package'           => $api->download_link,
 176                  'destination'       => get_theme_root(),
 177                  'clear_destination' => false, // Do not overwrite files.
 178                  'clear_working'     => true,
 179              )
 180          );
 181  
 182          if ( is_wp_error( $parent_result ) ) {
 183              add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
 184          }
 185  
 186          // Start cleaning up after the parent's installation.
 187          remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
 188  
 189          // Reset child's result and data.
 190          $this->result                     = $child_result;
 191          $this->skin->api                  = $child_api;
 192          $this->strings['process_success'] = $child_success_message;
 193  
 194          return $install_result;
 195      }
 196  
 197      /**
 198       * Don't display the activate and preview actions to the user.
 199       *
 200       * Hooked to the {@see 'install_theme_complete_actions'} filter by
 201       * Theme_Upgrader::check_parent_theme_filter() when installing
 202       * a child theme and installing the parent theme fails.
 203       *
 204       * @since 3.4.0
 205       *
 206       * @param array $actions Preview actions.
 207       * @return array
 208       */
 209  	public function hide_activate_preview_actions( $actions ) {
 210          unset( $actions['activate'], $actions['preview'] );
 211          return $actions;
 212      }
 213  
 214      /**
 215       * Install a theme package.
 216       *
 217       * @since 2.8.0
 218       * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
 219       *
 220       * @param string $package The full local path or URI of the package.
 221       * @param array  $args {
 222       *     Optional. Other arguments for installing a theme package. Default empty array.
 223       *
 224       *     @type bool $clear_update_cache Whether to clear the updates cache if successful.
 225       *                                    Default true.
 226       * }
 227       *
 228       * @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
 229       */
 230  	public function install( $package, $args = array() ) {
 231          $defaults    = array(
 232              'clear_update_cache' => true,
 233              'overwrite_package'  => false, // Do not overwrite files.
 234          );
 235          $parsed_args = wp_parse_args( $args, $defaults );
 236  
 237          $this->init();
 238          $this->install_strings();
 239  
 240          add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 241          add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
 242  
 243          if ( $parsed_args['clear_update_cache'] ) {
 244              // Clear cache so wp_update_themes() knows about the new theme.
 245              add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
 246          }
 247  
 248          $this->run(
 249              array(
 250                  'package'           => $package,
 251                  'destination'       => get_theme_root(),
 252                  'clear_destination' => $parsed_args['overwrite_package'],
 253                  'clear_working'     => true,
 254                  'hook_extra'        => array(
 255                      'type'   => 'theme',
 256                      'action' => 'install',
 257                  ),
 258              )
 259          );
 260  
 261          remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
 262          remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
 263          remove_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ) );
 264  
 265          if ( ! $this->result || is_wp_error( $this->result ) ) {
 266              return $this->result;
 267          }
 268  
 269          // Refresh the Theme Update information.
 270          wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
 271  
 272          if ( $parsed_args['overwrite_package'] ) {
 273              /**
 274               * Fires when the upgrader has successfully overwritten a currently installed
 275               * plugin or theme with an uploaded zip package.
 276               *
 277               * @since 5.5.0
 278               *
 279               * @param string  $package        The package file.
 280               * @param array   $new_theme_data The new theme data.
 281               * @param string  $package_type   The package type (plugin or theme).
 282               */
 283              do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
 284          }
 285  
 286          return true;
 287      }
 288  
 289      /**
 290       * Upgrade a theme.
 291       *
 292       * @since 2.8.0
 293       * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
 294       *
 295       * @param string $theme The theme slug.
 296       * @param array  $args {
 297       *     Optional. Other arguments for upgrading a theme. Default empty array.
 298       *
 299       *     @type bool $clear_update_cache Whether to clear the update cache if successful.
 300       *                                    Default true.
 301       * }
 302       * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
 303       */
 304  	public function upgrade( $theme, $args = array() ) {
 305          $defaults    = array(
 306              'clear_update_cache' => true,
 307          );
 308          $parsed_args = wp_parse_args( $args, $defaults );
 309  
 310          $this->init();
 311          $this->upgrade_strings();
 312  
 313          // Is an update available?
 314          $current = get_site_transient( 'update_themes' );
 315          if ( ! isset( $current->response[ $theme ] ) ) {
 316              $this->skin->before();
 317              $this->skin->set_result( false );
 318              $this->skin->error( 'up_to_date' );
 319              $this->skin->after();
 320              return false;
 321          }
 322  
 323          $r = $current->response[ $theme ];
 324  
 325          add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
 326          add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
 327          add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
 328          if ( $parsed_args['clear_update_cache'] ) {
 329              // Clear cache so wp_update_themes() knows about the new theme.
 330              add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
 331          }
 332  
 333          $this->run(
 334              array(
 335                  'package'           => $r['package'],
 336                  'destination'       => get_theme_root( $theme ),
 337                  'clear_destination' => true,
 338                  'clear_working'     => true,
 339                  'hook_extra'        => array(
 340                      'theme'  => $theme,
 341                      'type'   => 'theme',
 342                      'action' => 'update',
 343                  ),
 344              )
 345          );
 346  
 347          remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
 348          remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
 349          remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
 350          remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
 351  
 352          if ( ! $this->result || is_wp_error( $this->result ) ) {
 353              return $this->result;
 354          }
 355  
 356          wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
 357  
 358          // Ensure any future auto-update failures trigger a failure email by removing
 359          // the last failure notification from the list when themes update successfully.
 360          $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
 361  
 362          if ( isset( $past_failure_emails[ $theme ] ) ) {
 363              unset( $past_failure_emails[ $theme ] );
 364              update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
 365          }
 366  
 367          return true;
 368      }
 369  
 370      /**
 371       * Upgrade several themes at once.
 372       *
 373       * @since 3.0.0
 374       * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
 375       *
 376       * @param string[] $themes Array of the theme slugs.
 377       * @param array    $args {
 378       *     Optional. Other arguments for upgrading several themes at once. Default empty array.
 379       *
 380       *     @type bool $clear_update_cache Whether to clear the update cache if successful.
 381       *                                    Default true.
 382       * }
 383       * @return array[]|false An array of results, or false if unable to connect to the filesystem.
 384       */
 385  	public function bulk_upgrade( $themes, $args = array() ) {
 386          $defaults    = array(
 387              'clear_update_cache' => true,
 388          );
 389          $parsed_args = wp_parse_args( $args, $defaults );
 390  
 391          $this->init();
 392          $this->bulk = true;
 393          $this->upgrade_strings();
 394  
 395          $current = get_site_transient( 'update_themes' );
 396  
 397          add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
 398          add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
 399          add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
 400  
 401          $this->skin->header();
 402  
 403          // Connect to the filesystem first.
 404          $res = $this->fs_connect( array( WP_CONTENT_DIR ) );
 405          if ( ! $res ) {
 406              $this->skin->footer();
 407              return false;
 408          }
 409  
 410          $this->skin->bulk_header();
 411  
 412          /*
 413           * Only start maintenance mode if:
 414           * - running Multisite and there are one or more themes specified, OR
 415           * - a theme with an update available is currently in use.
 416           * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
 417           */
 418          $maintenance = ( is_multisite() && ! empty( $themes ) );
 419          foreach ( $themes as $theme ) {
 420              $maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
 421          }
 422          if ( $maintenance ) {
 423              $this->maintenance_mode( true );
 424          }
 425  
 426          $results = array();
 427  
 428          $this->update_count   = count( $themes );
 429          $this->update_current = 0;
 430          foreach ( $themes as $theme ) {
 431              $this->update_current++;
 432  
 433              $this->skin->theme_info = $this->theme_info( $theme );
 434  
 435              if ( ! isset( $current->response[ $theme ] ) ) {
 436                  $this->skin->set_result( true );
 437                  $this->skin->before();
 438                  $this->skin->feedback( 'up_to_date' );
 439                  $this->skin->after();
 440                  $results[ $theme ] = true;
 441                  continue;
 442              }
 443  
 444              // Get the URL to the zip file.
 445              $r = $current->response[ $theme ];
 446  
 447              $result = $this->run(
 448                  array(
 449                      'package'           => $r['package'],
 450                      'destination'       => get_theme_root( $theme ),
 451                      'clear_destination' => true,
 452                      'clear_working'     => true,
 453                      'is_multi'          => true,
 454                      'hook_extra'        => array(
 455                          'theme' => $theme,
 456                      ),
 457                  )
 458              );
 459  
 460              $results[ $theme ] = $this->result;
 461  
 462              // Prevent credentials auth screen from displaying multiple times.
 463              if ( false === $result ) {
 464                  break;
 465              }
 466          } // End foreach $themes.
 467  
 468          $this->maintenance_mode( false );
 469  
 470          // Refresh the Theme Update information.
 471          wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
 472  
 473          /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
 474          do_action(
 475              'upgrader_process_complete',
 476              $this,
 477              array(
 478                  'action' => 'update',
 479                  'type'   => 'theme',
 480                  'bulk'   => true,
 481                  'themes' => $themes,
 482              )
 483          );
 484  
 485          $this->skin->bulk_footer();
 486  
 487          $this->skin->footer();
 488  
 489          // Cleanup our hooks, in case something else does a upgrade on this connection.
 490          remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
 491          remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
 492          remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
 493  
 494          // Ensure any future auto-update failures trigger a failure email by removing
 495          // the last failure notification from the list when themes update successfully.
 496          $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
 497  
 498          foreach ( $results as $theme => $result ) {
 499              // Maintain last failure notification when themes failed to update manually.
 500              if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $theme ] ) ) {
 501                  continue;
 502              }
 503  
 504              unset( $past_failure_emails[ $theme ] );
 505          }
 506  
 507          update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
 508  
 509          return $results;
 510      }
 511  
 512      /**
 513       * Check that the package source contains a valid theme.
 514       *
 515       * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
 516       * It will return an error if the theme doesn't have style.css or index.php
 517       * files.
 518       *
 519       * @since 3.3.0
 520       *
 521       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 522       *
 523       * @param string $source The full path to the package source.
 524       * @return string|WP_Error The source or a WP_Error.
 525       */
 526  	public function check_package( $source ) {
 527          global $wp_filesystem;
 528  
 529          $this->new_theme_data = array();
 530  
 531          if ( is_wp_error( $source ) ) {
 532              return $source;
 533          }
 534  
 535          // Check that the folder contains a valid theme.
 536          $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
 537          if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
 538              return $source;
 539          }
 540  
 541          // A proper archive should have a style.css file in the single subdirectory.
 542          if ( ! file_exists( $working_directory . 'style.css' ) ) {
 543              return new WP_Error(
 544                  'incompatible_archive_theme_no_style',
 545                  $this->strings['incompatible_archive'],
 546                  sprintf(
 547                      /* translators: %s: style.css */
 548                      __( 'The theme is missing the %s stylesheet.' ),
 549                      '<code>style.css</code>'
 550                  )
 551              );
 552          }
 553  
 554          // All these headers are needed on Theme_Installer_Skin::do_overwrite().
 555          $info = get_file_data(
 556              $working_directory . 'style.css',
 557              array(
 558                  'Name'        => 'Theme Name',
 559                  'Version'     => 'Version',
 560                  'Author'      => 'Author',
 561                  'Template'    => 'Template',
 562                  'RequiresWP'  => 'Requires at least',
 563                  'RequiresPHP' => 'Requires PHP',
 564              )
 565          );
 566  
 567          if ( empty( $info['Name'] ) ) {
 568              return new WP_Error(
 569                  'incompatible_archive_theme_no_name',
 570                  $this->strings['incompatible_archive'],
 571                  sprintf(
 572                      /* translators: %s: style.css */
 573                      __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
 574                      '<code>style.css</code>'
 575                  )
 576              );
 577          }
 578  
 579          // If it's not a child theme, it must have at least an index.php to be legit.
 580          if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
 581              return new WP_Error(
 582                  'incompatible_archive_theme_no_index',
 583                  $this->strings['incompatible_archive'],
 584                  sprintf(
 585                      /* translators: %s: index.php */
 586                      __( 'The theme is missing the %s file.' ),
 587                      '<code>index.php</code>'
 588                  )
 589              );
 590          }
 591  
 592          $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
 593          $requires_wp  = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
 594  
 595          if ( ! is_php_version_compatible( $requires_php ) ) {
 596              $error = sprintf(
 597                  /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
 598                  __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
 599                  phpversion(),
 600                  $requires_php
 601              );
 602  
 603              return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
 604          }
 605          if ( ! is_wp_version_compatible( $requires_wp ) ) {
 606              $error = sprintf(
 607                  /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
 608                  __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
 609                  $GLOBALS['wp_version'],
 610                  $requires_wp
 611              );
 612  
 613              return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
 614          }
 615  
 616          $this->new_theme_data = $info;
 617  
 618          return $source;
 619      }
 620  
 621      /**
 622       * Turn on maintenance mode before attempting to upgrade the current theme.
 623       *
 624       * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
 625       * Theme_Upgrader::bulk_upgrade().
 626       *
 627       * @since 2.8.0
 628       *
 629       * @param bool|WP_Error $return Upgrade offer return.
 630       * @param array         $theme  Theme arguments.
 631       * @return bool|WP_Error The passed in $return param or WP_Error.
 632       */
 633  	public function current_before( $return, $theme ) {
 634          if ( is_wp_error( $return ) ) {
 635              return $return;
 636          }
 637  
 638          $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
 639  
 640          // Only run if current theme
 641          if ( get_stylesheet() !== $theme ) {
 642              return $return;
 643          }
 644  
 645          // Change to maintenance mode. Bulk edit handles this separately.
 646          if ( ! $this->bulk ) {
 647              $this->maintenance_mode( true );
 648          }
 649  
 650          return $return;
 651      }
 652  
 653      /**
 654       * Turn off maintenance mode after upgrading the current theme.
 655       *
 656       * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
 657       * and Theme_Upgrader::bulk_upgrade().
 658       *
 659       * @since 2.8.0
 660       *
 661       * @param bool|WP_Error $return Upgrade offer return.
 662       * @param array         $theme  Theme arguments.
 663       * @return bool|WP_Error The passed in $return param or WP_Error.
 664       */
 665  	public function current_after( $return, $theme ) {
 666          if ( is_wp_error( $return ) ) {
 667              return $return;
 668          }
 669  
 670          $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
 671  
 672          // Only run if current theme.
 673          if ( get_stylesheet() !== $theme ) {
 674              return $return;
 675          }
 676  
 677          // Ensure stylesheet name hasn't changed after the upgrade:
 678          if ( get_stylesheet() === $theme && $theme !== $this->result['destination_name'] ) {
 679              wp_clean_themes_cache();
 680              $stylesheet = $this->result['destination_name'];
 681              switch_theme( $stylesheet );
 682          }
 683  
 684          // Time to remove maintenance mode. Bulk edit handles this separately.
 685          if ( ! $this->bulk ) {
 686              $this->maintenance_mode( false );
 687          }
 688          return $return;
 689      }
 690  
 691      /**
 692       * Delete the old theme during an upgrade.
 693       *
 694       * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
 695       * and Theme_Upgrader::bulk_upgrade().
 696       *
 697       * @since 2.8.0
 698       *
 699       * @global WP_Filesystem_Base $wp_filesystem Subclass
 700       *
 701       * @param bool   $removed
 702       * @param string $local_destination
 703       * @param string $remote_destination
 704       * @param array  $theme
 705       * @return bool
 706       */
 707  	public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
 708          global $wp_filesystem;
 709  
 710          if ( is_wp_error( $removed ) ) {
 711              return $removed; // Pass errors through.
 712          }
 713  
 714          if ( ! isset( $theme['theme'] ) ) {
 715              return $removed;
 716          }
 717  
 718          $theme      = $theme['theme'];
 719          $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
 720          if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
 721              if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) ) {
 722                  return false;
 723              }
 724          }
 725  
 726          return true;
 727      }
 728  
 729      /**
 730       * Get the WP_Theme object for a theme.
 731       *
 732       * @since 2.8.0
 733       * @since 3.0.0 The `$theme` argument was added.
 734       *
 735       * @param string $theme The directory name of the theme. This is optional, and if not supplied,
 736       *                      the directory name from the last result will be used.
 737       * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
 738       *                        and the last result isn't set.
 739       */
 740  	public function theme_info( $theme = null ) {
 741          if ( empty( $theme ) ) {
 742              if ( ! empty( $this->result['destination_name'] ) ) {
 743                  $theme = $this->result['destination_name'];
 744              } else {
 745                  return false;
 746              }
 747          }
 748  
 749          $theme = wp_get_theme( $theme );
 750          $theme->cache_delete();
 751  
 752          return $theme;
 753      }
 754  
 755  }


Generated: Wed Aug 12 01:00:03 2020 Cross-referenced by PHPXref 0.7.1