[ 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       * Checks that the package source contains a valid theme.
 514       *
 515       * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
 516       *
 517       * @since 3.3.0
 518       *
 519       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 520       * @global string             $wp_version    The WordPress version string.
 521       *
 522       * @param string $source The path to the downloaded package source.
 523       * @return string|WP_Error The source as passed, or a WP_Error object on failure.
 524       */
 525  	public function check_package( $source ) {
 526          global $wp_filesystem, $wp_version;
 527  
 528          $this->new_theme_data = array();
 529  
 530          if ( is_wp_error( $source ) ) {
 531              return $source;
 532          }
 533  
 534          // Check that the folder contains a valid theme.
 535          $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
 536          if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
 537              return $source;
 538          }
 539  
 540          // A proper archive should have a style.css file in the single subdirectory.
 541          if ( ! file_exists( $working_directory . 'style.css' ) ) {
 542              return new WP_Error(
 543                  'incompatible_archive_theme_no_style',
 544                  $this->strings['incompatible_archive'],
 545                  sprintf(
 546                      /* translators: %s: style.css */
 547                      __( 'The theme is missing the %s stylesheet.' ),
 548                      '<code>style.css</code>'
 549                  )
 550              );
 551          }
 552  
 553          // All these headers are needed on Theme_Installer_Skin::do_overwrite().
 554          $info = get_file_data(
 555              $working_directory . 'style.css',
 556              array(
 557                  'Name'        => 'Theme Name',
 558                  'Version'     => 'Version',
 559                  'Author'      => 'Author',
 560                  'Template'    => 'Template',
 561                  'RequiresWP'  => 'Requires at least',
 562                  'RequiresPHP' => 'Requires PHP',
 563              )
 564          );
 565  
 566          if ( empty( $info['Name'] ) ) {
 567              return new WP_Error(
 568                  'incompatible_archive_theme_no_name',
 569                  $this->strings['incompatible_archive'],
 570                  sprintf(
 571                      /* translators: %s: style.css */
 572                      __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
 573                      '<code>style.css</code>'
 574                  )
 575              );
 576          }
 577  
 578          // If it's not a child theme, it must have at least an index.php to be legit.
 579          if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
 580              return new WP_Error(
 581                  'incompatible_archive_theme_no_index',
 582                  $this->strings['incompatible_archive'],
 583                  sprintf(
 584                      /* translators: %s: index.php */
 585                      __( 'The theme is missing the %s file.' ),
 586                      '<code>index.php</code>'
 587                  )
 588              );
 589          }
 590  
 591          $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
 592          $requires_wp  = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
 593  
 594          if ( ! is_php_version_compatible( $requires_php ) ) {
 595              $error = sprintf(
 596                  /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
 597                  __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
 598                  phpversion(),
 599                  $requires_php
 600              );
 601  
 602              return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
 603          }
 604          if ( ! is_wp_version_compatible( $requires_wp ) ) {
 605              $error = sprintf(
 606                  /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
 607                  __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
 608                  $wp_version,
 609                  $requires_wp
 610              );
 611  
 612              return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
 613          }
 614  
 615          $this->new_theme_data = $info;
 616  
 617          return $source;
 618      }
 619  
 620      /**
 621       * Turn on maintenance mode before attempting to upgrade the current theme.
 622       *
 623       * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
 624       * Theme_Upgrader::bulk_upgrade().
 625       *
 626       * @since 2.8.0
 627       *
 628       * @param bool|WP_Error $return Upgrade offer return.
 629       * @param array         $theme  Theme arguments.
 630       * @return bool|WP_Error The passed in $return param or WP_Error.
 631       */
 632  	public function current_before( $return, $theme ) {
 633          if ( is_wp_error( $return ) ) {
 634              return $return;
 635          }
 636  
 637          $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
 638  
 639          // Only run if current theme
 640          if ( get_stylesheet() !== $theme ) {
 641              return $return;
 642          }
 643  
 644          // Change to maintenance mode. Bulk edit handles this separately.
 645          if ( ! $this->bulk ) {
 646              $this->maintenance_mode( true );
 647          }
 648  
 649          return $return;
 650      }
 651  
 652      /**
 653       * Turn off maintenance mode after upgrading the current theme.
 654       *
 655       * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
 656       * and Theme_Upgrader::bulk_upgrade().
 657       *
 658       * @since 2.8.0
 659       *
 660       * @param bool|WP_Error $return Upgrade offer return.
 661       * @param array         $theme  Theme arguments.
 662       * @return bool|WP_Error The passed in $return param or WP_Error.
 663       */
 664  	public function current_after( $return, $theme ) {
 665          if ( is_wp_error( $return ) ) {
 666              return $return;
 667          }
 668  
 669          $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
 670  
 671          // Only run if current theme.
 672          if ( get_stylesheet() !== $theme ) {
 673              return $return;
 674          }
 675  
 676          // Ensure stylesheet name hasn't changed after the upgrade:
 677          if ( get_stylesheet() === $theme && $theme !== $this->result['destination_name'] ) {
 678              wp_clean_themes_cache();
 679              $stylesheet = $this->result['destination_name'];
 680              switch_theme( $stylesheet );
 681          }
 682  
 683          // Time to remove maintenance mode. Bulk edit handles this separately.
 684          if ( ! $this->bulk ) {
 685              $this->maintenance_mode( false );
 686          }
 687          return $return;
 688      }
 689  
 690      /**
 691       * Delete the old theme during an upgrade.
 692       *
 693       * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
 694       * and Theme_Upgrader::bulk_upgrade().
 695       *
 696       * @since 2.8.0
 697       *
 698       * @global WP_Filesystem_Base $wp_filesystem Subclass
 699       *
 700       * @param bool   $removed
 701       * @param string $local_destination
 702       * @param string $remote_destination
 703       * @param array  $theme
 704       * @return bool
 705       */
 706  	public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
 707          global $wp_filesystem;
 708  
 709          if ( is_wp_error( $removed ) ) {
 710              return $removed; // Pass errors through.
 711          }
 712  
 713          if ( ! isset( $theme['theme'] ) ) {
 714              return $removed;
 715          }
 716  
 717          $theme      = $theme['theme'];
 718          $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
 719          if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
 720              if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) ) {
 721                  return false;
 722              }
 723          }
 724  
 725          return true;
 726      }
 727  
 728      /**
 729       * Get the WP_Theme object for a theme.
 730       *
 731       * @since 2.8.0
 732       * @since 3.0.0 The `$theme` argument was added.
 733       *
 734       * @param string $theme The directory name of the theme. This is optional, and if not supplied,
 735       *                      the directory name from the last result will be used.
 736       * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
 737       *                        and the last result isn't set.
 738       */
 739  	public function theme_info( $theme = null ) {
 740          if ( empty( $theme ) ) {
 741              if ( ! empty( $this->result['destination_name'] ) ) {
 742                  $theme = $this->result['destination_name'];
 743              } else {
 744                  return false;
 745              }
 746          }
 747  
 748          $theme = wp_get_theme( $theme );
 749          $theme->cache_delete();
 750  
 751          return $theme;
 752      }
 753  
 754  }


Generated: Wed Oct 21 01:00:04 2020 Cross-referenced by PHPXref 0.7.1