[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Upgrader API: Theme_Installer_Skin class
   4   *
   5   * @package WordPress
   6   * @subpackage Upgrader
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Theme Installer Skin for the WordPress Theme Installer.
  12   *
  13   * @since 2.8.0
  14   * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  15   *
  16   * @see WP_Upgrader_Skin
  17   */
  18  class Theme_Installer_Skin extends WP_Upgrader_Skin {
  19      public $api;
  20      public $type;
  21      public $url;
  22      public $overwrite;
  23  
  24      private $is_downgrading = false;
  25  
  26      /**
  27       * @param array $args
  28       */
  29  	public function __construct( $args = array() ) {
  30          $defaults = array(
  31              'type'      => 'web',
  32              'url'       => '',
  33              'theme'     => '',
  34              'nonce'     => '',
  35              'title'     => '',
  36              'overwrite' => '',
  37          );
  38          $args     = wp_parse_args( $args, $defaults );
  39  
  40          $this->type      = $args['type'];
  41          $this->url       = $args['url'];
  42          $this->api       = isset( $args['api'] ) ? $args['api'] : array();
  43          $this->overwrite = $args['overwrite'];
  44  
  45          parent::__construct( $args );
  46      }
  47  
  48      /**
  49       * Action to perform before installing a theme.
  50       *
  51       * @since 2.8.0
  52       */
  53  	public function before() {
  54          if ( ! empty( $this->api ) ) {
  55              $this->upgrader->strings['process_success'] = sprintf(
  56                  $this->upgrader->strings['process_success_specific'],
  57                  $this->api->name,
  58                  $this->api->version
  59              );
  60          }
  61      }
  62  
  63      /**
  64       * Hides the `process_failed` error when updating a theme by uploading a zip file.
  65       *
  66       * @since 5.5.0
  67       *
  68       * @param WP_Error $wp_error WP_Error.
  69       * @return bool
  70       */
  71  	public function hide_process_failed( $wp_error ) {
  72          if (
  73              'upload' === $this->type &&
  74              '' === $this->overwrite &&
  75              $wp_error->get_error_code() === 'folder_exists'
  76          ) {
  77              return true;
  78          }
  79  
  80          return false;
  81      }
  82  
  83      /**
  84       * Action to perform following a single theme install.
  85       *
  86       * @since 2.8.0
  87       */
  88  	public function after() {
  89          if ( $this->do_overwrite() ) {
  90              return;
  91          }
  92  
  93          if ( empty( $this->upgrader->result['destination_name'] ) ) {
  94              return;
  95          }
  96  
  97          $theme_info = $this->upgrader->theme_info();
  98          if ( empty( $theme_info ) ) {
  99              return;
 100          }
 101  
 102          $name       = $theme_info->display( 'Name' );
 103          $stylesheet = $this->upgrader->result['destination_name'];
 104          $template   = $theme_info->get_template();
 105  
 106          $activate_link = add_query_arg(
 107              array(
 108                  'action'     => 'activate',
 109                  'template'   => urlencode( $template ),
 110                  'stylesheet' => urlencode( $stylesheet ),
 111              ),
 112              admin_url( 'themes.php' )
 113          );
 114          $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
 115  
 116          $install_actions = array();
 117  
 118          if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
 119              $customize_url = add_query_arg(
 120                  array(
 121                      'theme'  => urlencode( $stylesheet ),
 122                      'return' => urlencode( admin_url( 'web' === $this->type ? 'theme-install.php' : 'themes.php' ) ),
 123                  ),
 124                  admin_url( 'customize.php' )
 125              );
 126  
 127              $install_actions['preview'] = sprintf(
 128                  '<a href="%s" class="hide-if-no-customize load-customize">' .
 129                  '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
 130                  esc_url( $customize_url ),
 131                  __( 'Live Preview' ),
 132                  /* translators: %s: Theme name. */
 133                  sprintf( __( 'Live Preview &#8220;%s&#8221;' ), $name )
 134              );
 135          }
 136  
 137          $install_actions['activate'] = sprintf(
 138              '<a href="%s" class="activatelink">' .
 139              '<span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
 140              esc_url( $activate_link ),
 141              __( 'Activate' ),
 142              /* translators: %s: Theme name. */
 143              sprintf( _x( 'Activate &#8220;%s&#8221;', 'theme' ), $name )
 144          );
 145  
 146          if ( is_network_admin() && current_user_can( 'manage_network_themes' ) ) {
 147              $install_actions['network_enable'] = sprintf(
 148                  '<a href="%s" target="_parent">%s</a>',
 149                  esc_url( wp_nonce_url( 'themes.php?action=enable&amp;theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ),
 150                  __( 'Network Enable' )
 151              );
 152          }
 153  
 154          if ( 'web' === $this->type ) {
 155              $install_actions['themes_page'] = sprintf(
 156                  '<a href="%s" target="_parent">%s</a>',
 157                  self_admin_url( 'theme-install.php' ),
 158                  __( 'Return to Theme Installer' )
 159              );
 160          } elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) ) {
 161              $install_actions['themes_page'] = sprintf(
 162                  '<a href="%s" target="_parent">%s</a>',
 163                  self_admin_url( 'themes.php' ),
 164                  __( 'Return to Themes page' )
 165              );
 166          }
 167  
 168          if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() || ! current_user_can( 'switch_themes' ) ) {
 169              unset( $install_actions['activate'], $install_actions['preview'] );
 170          } elseif ( get_option( 'template' ) === $stylesheet ) {
 171              unset( $install_actions['activate'] );
 172          }
 173  
 174          /**
 175           * Filters the list of action links available following a single theme installation.
 176           *
 177           * @since 2.8.0
 178           *
 179           * @param string[] $install_actions Array of theme action links.
 180           * @param object   $api             Object containing WordPress.org API theme data.
 181           * @param string   $stylesheet      Theme directory name.
 182           * @param WP_Theme $theme_info      Theme object.
 183           */
 184          $install_actions = apply_filters( 'install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info );
 185          if ( ! empty( $install_actions ) ) {
 186              $this->feedback( implode( ' | ', (array) $install_actions ) );
 187          }
 188      }
 189  
 190      /**
 191       * Check if the theme can be overwritten and output the HTML for overwriting a theme on upload.
 192       *
 193       * @since 5.5.0
 194       *
 195       * @return bool Whether the theme can be overwritten and HTML was outputted.
 196       */
 197  	private function do_overwrite() {
 198          if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
 199              return false;
 200          }
 201  
 202          $folder = $this->result->get_error_data( 'folder_exists' );
 203          $folder = rtrim( $folder, '/' );
 204  
 205          $current_theme_data = false;
 206          $all_themes         = wp_get_themes( array( 'errors' => null ) );
 207  
 208          foreach ( $all_themes as $theme ) {
 209              if ( rtrim( $theme->get_stylesheet_directory(), '/' ) !== $folder ) {
 210                  continue;
 211              }
 212  
 213              $current_theme_data = $theme;
 214          }
 215  
 216          $new_theme_data = $this->upgrader->new_theme_data;
 217  
 218          if ( ! $current_theme_data || ! $new_theme_data ) {
 219              return false;
 220          }
 221  
 222          echo '<h2 class="update-from-upload-heading">' . esc_html( __( 'This theme is already installed.' ) ) . '</h2>';
 223  
 224          // Check errors for current theme.
 225          if ( is_wp_error( $current_theme_data->errors() ) ) {
 226              $this->feedback( 'current_theme_has_errors', $current_theme_data->errors()->get_error_message() );
 227          }
 228  
 229          $this->is_downgrading = version_compare( $current_theme_data['Version'], $new_theme_data['Version'], '>' );
 230  
 231          $is_invalid_parent = false;
 232          if ( ! empty( $new_theme_data['Template'] ) ) {
 233              $is_invalid_parent = ! in_array( $new_theme_data['Template'], array_keys( $all_themes ), true );
 234          }
 235  
 236          $rows = array(
 237              'Name'        => __( 'Theme name' ),
 238              'Version'     => __( 'Version' ),
 239              'Author'      => __( 'Author' ),
 240              'RequiresWP'  => __( 'Required WordPress version' ),
 241              'RequiresPHP' => __( 'Required PHP version' ),
 242              'Template'    => __( 'Parent theme' ),
 243          );
 244  
 245          $table  = '<table class="update-from-upload-comparison"><tbody>';
 246          $table .= '<tr><th></th><th>' . esc_html( __( 'Current' ) ) . '</th><th>' . esc_html( __( 'Uploaded' ) ) . '</th></tr>';
 247  
 248          $is_same_theme = true; // Let's consider only these rows.
 249  
 250          foreach ( $rows as $field => $label ) {
 251              $old_value = $current_theme_data->display( $field, false );
 252              $old_value = $old_value ? (string) $old_value : '-';
 253  
 254              $new_value = ! empty( $new_theme_data[ $field ] ) ? (string) $new_theme_data[ $field ] : '-';
 255  
 256              if ( $old_value === $new_value && '-' === $new_value && 'Template' === $field ) {
 257                  continue;
 258              }
 259  
 260              $is_same_theme = $is_same_theme && ( $old_value === $new_value );
 261  
 262              $diff_field     = ( 'Version' !== $field && $new_value !== $old_value );
 263              $diff_version   = ( 'Version' === $field && $this->is_downgrading );
 264              $invalid_parent = false;
 265  
 266              if ( 'Template' === $field && $is_invalid_parent ) {
 267                  $invalid_parent = true;
 268                  $new_value     .= ' ' . __( '(not found)' );
 269              }
 270  
 271              $table .= '<tr><td class="name-label">' . $label . '</td><td>' . wp_strip_all_tags( $old_value ) . '</td>';
 272              $table .= ( $diff_field || $diff_version || $invalid_parent ) ? '<td class="warning">' : '<td>';
 273              $table .= wp_strip_all_tags( $new_value ) . '</td></tr>';
 274          }
 275  
 276          $table .= '</tbody></table>';
 277  
 278          /**
 279           * Filters the compare table output for overwriting a theme package on upload.
 280           *
 281           * @since 5.5.0
 282           *
 283           * @param string $table              The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
 284           * @param array  $current_theme_data Array with current theme data.
 285           * @param array  $new_theme_data     Array with uploaded theme data.
 286           */
 287          echo apply_filters( 'install_theme_overwrite_comparison', $table, $current_theme_data, $new_theme_data );
 288  
 289          $install_actions = array();
 290          $can_update      = true;
 291  
 292          $blocked_message  = '<p>' . esc_html( __( 'The theme cannot be updated due to the following:' ) ) . '</p>';
 293          $blocked_message .= '<ul class="ul-disc">';
 294  
 295          $requires_php = isset( $new_theme_data['RequiresPHP'] ) ? $new_theme_data['RequiresPHP'] : null;
 296          $requires_wp  = isset( $new_theme_data['RequiresWP'] ) ? $new_theme_data['RequiresWP'] : null;
 297  
 298          if ( ! is_php_version_compatible( $requires_php ) ) {
 299              $error = sprintf(
 300                  /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
 301                  __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
 302                  phpversion(),
 303                  $requires_php
 304              );
 305  
 306              $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
 307              $can_update       = false;
 308          }
 309  
 310          if ( ! is_wp_version_compatible( $requires_wp ) ) {
 311              $error = sprintf(
 312                  /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
 313                  __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
 314                  get_bloginfo( 'version' ),
 315                  $requires_wp
 316              );
 317  
 318              $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
 319              $can_update       = false;
 320          }
 321  
 322          $blocked_message .= '</ul>';
 323  
 324          if ( $can_update ) {
 325              if ( $this->is_downgrading ) {
 326                  $warning = sprintf(
 327                      /* translators: %s: Documentation URL. */
 328                      __( 'You are uploading an older version of a current theme. You can continue to install the older version, but be sure to <a href="%s">back up your database and files</a> first.' ),
 329                      __( 'https://wordpress.org/support/article/wordpress-backups/' )
 330                  );
 331              } else {
 332                  $warning = sprintf(
 333                      /* translators: %s: Documentation URL. */
 334                      __( 'You are updating a theme. Be sure to <a href="%s">back up your database and files</a> first.' ),
 335                      __( 'https://wordpress.org/support/article/wordpress-backups/' )
 336                  );
 337              }
 338  
 339              echo '<p class="update-from-upload-notice">' . $warning . '</p>';
 340  
 341              $overwrite = $this->is_downgrading ? 'downgrade-theme' : 'update-theme';
 342  
 343              $install_actions['overwrite_theme'] = sprintf(
 344                  '<a class="button button-primary update-from-upload-overwrite" href="%s" target="_parent">%s</a>',
 345                  wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'theme-upload' ),
 346                  __( 'Replace current with uploaded' )
 347              );
 348          } else {
 349              echo $blocked_message;
 350          }
 351  
 352          $cancel_url = add_query_arg( 'action', 'upload-theme-cancel-overwrite', $this->url );
 353  
 354          $install_actions['themes_page'] = sprintf(
 355              '<a class="button" href="%s" target="_parent">%s</a>',
 356              wp_nonce_url( $cancel_url, 'theme-upload-cancel-overwrite' ),
 357              __( 'Cancel and go back' )
 358          );
 359  
 360          /**
 361           * Filters the list of action links available following a single theme installation
 362           * failure when overwriting is allowed.
 363           *
 364           * @since 5.5.0
 365           *
 366           * @param string[] $install_actions Array of theme action links.
 367           * @param object   $api             Object containing WordPress.org API theme data.
 368           * @param array    $new_theme_data  Array with uploaded theme data.
 369           */
 370          $install_actions = apply_filters( 'install_theme_overwrite_actions', $install_actions, $this->api, $new_theme_data );
 371  
 372          if ( ! empty( $install_actions ) ) {
 373              printf(
 374                  '<p class="update-from-upload-expired hidden">%s</p>',
 375                  __( 'The uploaded file has expired. Please go back and upload it again.' )
 376              );
 377              echo '<p class="update-from-upload-actions">' . implode( ' ', (array) $install_actions ) . '</p>';
 378          }
 379  
 380          return true;
 381      }
 382  }


Generated: Mon Aug 10 01:00:03 2020 Cross-referenced by PHPXref 0.7.1