[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Theme Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Removes a theme.
  11   *
  12   * @since 2.8.0
  13   *
  14   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  15   *
  16   * @param string $stylesheet Stylesheet of the theme to delete.
  17   * @param string $redirect   Redirect to page when complete.
  18   * @return bool|null|WP_Error True on success, false if `$stylesheet` is empty, WP_Error on failure.
  19   *                            Null if filesystem credentials are required to proceed.
  20   */
  21  function delete_theme( $stylesheet, $redirect = '' ) {
  22      global $wp_filesystem;
  23  
  24      if ( empty( $stylesheet ) ) {
  25          return false;
  26      }
  27  
  28      if ( empty( $redirect ) ) {
  29          $redirect = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );
  30      }
  31  
  32      ob_start();
  33      $credentials = request_filesystem_credentials( $redirect );
  34      $data        = ob_get_clean();
  35  
  36      if ( false === $credentials ) {
  37          if ( ! empty( $data ) ) {
  38              require_once ABSPATH . 'wp-admin/admin-header.php';
  39              echo $data;
  40              require_once ABSPATH . 'wp-admin/admin-footer.php';
  41              exit;
  42          }
  43          return;
  44      }
  45  
  46      if ( ! WP_Filesystem( $credentials ) ) {
  47          ob_start();
  48          // Failed to connect. Error and request again.
  49          request_filesystem_credentials( $redirect, '', true );
  50          $data = ob_get_clean();
  51  
  52          if ( ! empty( $data ) ) {
  53              require_once ABSPATH . 'wp-admin/admin-header.php';
  54              echo $data;
  55              require_once ABSPATH . 'wp-admin/admin-footer.php';
  56              exit;
  57          }
  58          return;
  59      }
  60  
  61      if ( ! is_object( $wp_filesystem ) ) {
  62          return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
  63      }
  64  
  65      if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
  66          return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
  67      }
  68  
  69      // Get the base plugin folder.
  70      $themes_dir = $wp_filesystem->wp_themes_dir();
  71      if ( empty( $themes_dir ) ) {
  72          return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
  73      }
  74  
  75      /**
  76       * Fires immediately before a theme deletion attempt.
  77       *
  78       * @since 5.8.0
  79       *
  80       * @param string $stylesheet Stylesheet of the theme to delete.
  81       */
  82      do_action( 'delete_theme', $stylesheet );
  83  
  84      $themes_dir = trailingslashit( $themes_dir );
  85      $theme_dir  = trailingslashit( $themes_dir . $stylesheet );
  86      $deleted    = $wp_filesystem->delete( $theme_dir, true );
  87  
  88      /**
  89       * Fires immediately after a theme deletion attempt.
  90       *
  91       * @since 5.8.0
  92       *
  93       * @param string $stylesheet Stylesheet of the theme to delete.
  94       * @param bool   $deleted    Whether the theme deletion was successful.
  95       */
  96      do_action( 'deleted_theme', $stylesheet, $deleted );
  97  
  98      if ( ! $deleted ) {
  99          return new WP_Error(
 100              'could_not_remove_theme',
 101              /* translators: %s: Theme name. */
 102              sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet )
 103          );
 104      }
 105  
 106      $theme_translations = wp_get_installed_translations( 'themes' );
 107  
 108      // Remove language files, silently.
 109      if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
 110          $translations = $theme_translations[ $stylesheet ];
 111  
 112          foreach ( $translations as $translation => $data ) {
 113              $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
 114              $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
 115  
 116              $json_translation_files = glob( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '-*.json' );
 117              if ( $json_translation_files ) {
 118                  array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
 119              }
 120          }
 121      }
 122  
 123      // Remove the theme from allowed themes on the network.
 124      if ( is_multisite() ) {
 125          WP_Theme::network_disable_theme( $stylesheet );
 126      }
 127  
 128      // Force refresh of theme update information.
 129      delete_site_transient( 'update_themes' );
 130  
 131      return true;
 132  }
 133  
 134  /**
 135   * Gets the page templates available in this theme.
 136   *
 137   * @since 1.5.0
 138   * @since 4.7.0 Added the `$post_type` parameter.
 139   *
 140   * @param WP_Post|null $post      Optional. The post being edited, provided for context.
 141   * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
 142   * @return string[] Array of template file names keyed by the template header name.
 143   */
 144  function get_page_templates( $post = null, $post_type = 'page' ) {
 145      return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
 146  }
 147  
 148  /**
 149   * Tidies a filename for url display by the theme file editor.
 150   *
 151   * @since 2.9.0
 152   * @access private
 153   *
 154   * @param string $fullpath Full path to the theme file
 155   * @param string $containingfolder Path of the theme parent folder
 156   * @return string
 157   */
 158  function _get_template_edit_filename( $fullpath, $containingfolder ) {
 159      return str_replace( dirname( dirname( $containingfolder ) ), '', $fullpath );
 160  }
 161  
 162  /**
 163   * Check if there is an update for a theme available.
 164   *
 165   * Will display link, if there is an update available.
 166   *
 167   * @since 2.7.0
 168   *
 169   * @see get_theme_update_available()
 170   *
 171   * @param WP_Theme $theme Theme data object.
 172   */
 173  function theme_update_available( $theme ) {
 174      echo get_theme_update_available( $theme );
 175  }
 176  
 177  /**
 178   * Retrieves the update link if there is a theme update available.
 179   *
 180   * Will return a link if there is an update available.
 181   *
 182   * @since 3.8.0
 183   *
 184   * @param WP_Theme $theme WP_Theme object.
 185   * @return string|false HTML for the update link, or false if invalid info was passed.
 186   */
 187  function get_theme_update_available( $theme ) {
 188      static $themes_update = null;
 189  
 190      if ( ! current_user_can( 'update_themes' ) ) {
 191          return false;
 192      }
 193  
 194      if ( ! isset( $themes_update ) ) {
 195          $themes_update = get_site_transient( 'update_themes' );
 196      }
 197  
 198      if ( ! ( $theme instanceof WP_Theme ) ) {
 199          return false;
 200      }
 201  
 202      $stylesheet = $theme->get_stylesheet();
 203  
 204      $html = '';
 205  
 206      if ( isset( $themes_update->response[ $stylesheet ] ) ) {
 207          $update      = $themes_update->response[ $stylesheet ];
 208          $theme_name  = $theme->display( 'Name' );
 209          $details_url = add_query_arg(
 210              array(
 211                  'TB_iframe' => 'true',
 212                  'width'     => 1024,
 213                  'height'    => 800,
 214              ),
 215              $update['url']
 216          ); // Theme browser inside WP? Replace this. Also, theme preview JS will override this on the available list.
 217          $update_url  = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
 218  
 219          if ( ! is_multisite() ) {
 220              if ( ! current_user_can( 'update_themes' ) ) {
 221                  $html = sprintf(
 222                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
 223                      '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ) . '</strong></p>',
 224                      $theme_name,
 225                      esc_url( $details_url ),
 226                      sprintf(
 227                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 228                          /* translators: 1: Theme name, 2: Version number. */
 229                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 230                      ),
 231                      $update['new_version']
 232                  );
 233              } elseif ( empty( $update['package'] ) ) {
 234                  $html = sprintf(
 235                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
 236                      '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ) . '</strong></p>',
 237                      $theme_name,
 238                      esc_url( $details_url ),
 239                      sprintf(
 240                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 241                          /* translators: 1: Theme name, 2: Version number. */
 242                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 243                      ),
 244                      $update['new_version']
 245                  );
 246              } else {
 247                  $html = sprintf(
 248                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
 249                      '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ) . '</strong></p>',
 250                      $theme_name,
 251                      esc_url( $details_url ),
 252                      sprintf(
 253                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 254                          /* translators: 1: Theme name, 2: Version number. */
 255                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 256                      ),
 257                      $update['new_version'],
 258                      $update_url,
 259                      sprintf(
 260                          'aria-label="%s" id="update-theme" data-slug="%s"',
 261                          /* translators: %s: Theme name. */
 262                          esc_attr( sprintf( _x( 'Update %s now', 'theme' ), $theme_name ) ),
 263                          $stylesheet
 264                      )
 265                  );
 266              }
 267          }
 268      }
 269  
 270      return $html;
 271  }
 272  
 273  /**
 274   * Retrieves list of WordPress theme features (aka theme tags).
 275   *
 276   * @since 3.1.0
 277   * @since 3.2.0 Added 'Gray' color and 'Featured Image Header', 'Featured Images',
 278   *              'Full Width Template', and 'Post Formats' features.
 279   * @since 3.5.0 Added 'Flexible Header' feature.
 280   * @since 3.8.0 Renamed 'Width' filter to 'Layout'.
 281   * @since 3.8.0 Renamed 'Fixed Width' and 'Flexible Width' options
 282   *              to 'Fixed Layout' and 'Fluid Layout'.
 283   * @since 3.8.0 Added 'Accessibility Ready' feature and 'Responsive Layout' option.
 284   * @since 3.9.0 Combined 'Layout' and 'Columns' filters.
 285   * @since 4.6.0 Removed 'Colors' filter.
 286   * @since 4.6.0 Added 'Grid Layout' option.
 287   *              Removed 'Fixed Layout', 'Fluid Layout', and 'Responsive Layout' options.
 288   * @since 4.6.0 Added 'Custom Logo' and 'Footer Widgets' features.
 289   *              Removed 'Blavatar' feature.
 290   * @since 4.6.0 Added 'Blog', 'E-Commerce', 'Education', 'Entertainment', 'Food & Drink',
 291   *              'Holiday', 'News', 'Photography', and 'Portfolio' subjects.
 292   *              Removed 'Photoblogging' and 'Seasonal' subjects.
 293   * @since 4.9.0 Reordered the filters from 'Layout', 'Features', 'Subject'
 294   *              to 'Subject', 'Features', 'Layout'.
 295   * @since 4.9.0 Removed 'BuddyPress', 'Custom Menu', 'Flexible Header',
 296   *              'Front Page Posting', 'Microformats', 'RTL Language Support',
 297   *              'Threaded Comments', and 'Translation Ready' features.
 298   * @since 5.5.0 Added 'Block Editor Patterns', 'Block Editor Styles',
 299   *              and 'Full Site Editing' features.
 300   * @since 5.5.0 Added 'Wide Blocks' layout option.
 301   * @since 5.8.1 Added 'Template Editing' feature.
 302   *
 303   * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true.
 304   * @return array Array of features keyed by category with translations keyed by slug.
 305   */
 306  function get_theme_feature_list( $api = true ) {
 307      // Hard-coded list is used if API is not accessible.
 308      $features = array(
 309  
 310          __( 'Subject' )  => array(
 311              'blog'           => __( 'Blog' ),
 312              'e-commerce'     => __( 'E-Commerce' ),
 313              'education'      => __( 'Education' ),
 314              'entertainment'  => __( 'Entertainment' ),
 315              'food-and-drink' => __( 'Food & Drink' ),
 316              'holiday'        => __( 'Holiday' ),
 317              'news'           => __( 'News' ),
 318              'photography'    => __( 'Photography' ),
 319              'portfolio'      => __( 'Portfolio' ),
 320          ),
 321  
 322          __( 'Features' ) => array(
 323              'accessibility-ready'   => __( 'Accessibility Ready' ),
 324              'block-patterns'        => __( 'Block Editor Patterns' ),
 325              'block-styles'          => __( 'Block Editor Styles' ),
 326              'custom-background'     => __( 'Custom Background' ),
 327              'custom-colors'         => __( 'Custom Colors' ),
 328              'custom-header'         => __( 'Custom Header' ),
 329              'custom-logo'           => __( 'Custom Logo' ),
 330              'editor-style'          => __( 'Editor Style' ),
 331              'featured-image-header' => __( 'Featured Image Header' ),
 332              'featured-images'       => __( 'Featured Images' ),
 333              'footer-widgets'        => __( 'Footer Widgets' ),
 334              'full-site-editing'     => __( 'Full Site Editing' ),
 335              'full-width-template'   => __( 'Full Width Template' ),
 336              'post-formats'          => __( 'Post Formats' ),
 337              'sticky-post'           => __( 'Sticky Post' ),
 338              'template-editing'      => __( 'Template Editing' ),
 339              'theme-options'         => __( 'Theme Options' ),
 340          ),
 341  
 342          __( 'Layout' )   => array(
 343              'grid-layout'   => __( 'Grid Layout' ),
 344              'one-column'    => __( 'One Column' ),
 345              'two-columns'   => __( 'Two Columns' ),
 346              'three-columns' => __( 'Three Columns' ),
 347              'four-columns'  => __( 'Four Columns' ),
 348              'left-sidebar'  => __( 'Left Sidebar' ),
 349              'right-sidebar' => __( 'Right Sidebar' ),
 350              'wide-blocks'   => __( 'Wide Blocks' ),
 351          ),
 352  
 353      );
 354  
 355      if ( ! $api || ! current_user_can( 'install_themes' ) ) {
 356          return $features;
 357      }
 358  
 359      $feature_list = get_site_transient( 'wporg_theme_feature_list' );
 360      if ( ! $feature_list ) {
 361          set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
 362      }
 363  
 364      if ( ! $feature_list ) {
 365          $feature_list = themes_api( 'feature_list', array() );
 366          if ( is_wp_error( $feature_list ) ) {
 367              return $features;
 368          }
 369      }
 370  
 371      if ( ! $feature_list ) {
 372          return $features;
 373      }
 374  
 375      set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
 376  
 377      $category_translations = array(
 378          'Layout'   => __( 'Layout' ),
 379          'Features' => __( 'Features' ),
 380          'Subject'  => __( 'Subject' ),
 381      );
 382  
 383      $wporg_features = array();
 384  
 385      // Loop over the wp.org canonical list and apply translations.
 386      foreach ( (array) $feature_list as $feature_category => $feature_items ) {
 387          if ( isset( $category_translations[ $feature_category ] ) ) {
 388              $feature_category = $category_translations[ $feature_category ];
 389          }
 390  
 391          $wporg_features[ $feature_category ] = array();
 392  
 393          foreach ( $feature_items as $feature ) {
 394              if ( isset( $features[ $feature_category ][ $feature ] ) ) {
 395                  $wporg_features[ $feature_category ][ $feature ] = $features[ $feature_category ][ $feature ];
 396              } else {
 397                  $wporg_features[ $feature_category ][ $feature ] = $feature;
 398              }
 399          }
 400      }
 401  
 402      return $wporg_features;
 403  }
 404  
 405  /**
 406   * Retrieves theme installer pages from the WordPress.org Themes API.
 407   *
 408   * It is possible for a theme to override the Themes API result with three
 409   * filters. Assume this is for themes, which can extend on the Theme Info to
 410   * offer more choices. This is very powerful and must be used with care, when
 411   * overriding the filters.
 412   *
 413   * The first filter, {@see 'themes_api_args'}, is for the args and gives the action
 414   * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that
 415   * an object is returned.
 416   *
 417   * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org
 418   * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',
 419   * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed.
 420   *
 421   * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the
 422   * response object or array, depending on the `$action` type.
 423   *
 424   * Supported arguments per action:
 425   *
 426   * | Argument Name      | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list'   |
 427   * | -------------------| :------------: | :-----------------: | :--------: | :--------------: |
 428   * | `$slug`            | No             |  Yes                | No         | No               |
 429   * | `$per_page`        | Yes            |  No                 | No         | No               |
 430   * | `$page`            | Yes            |  No                 | No         | No               |
 431   * | `$number`          | No             |  No                 | Yes        | No               |
 432   * | `$search`          | Yes            |  No                 | No         | No               |
 433   * | `$tag`             | Yes            |  No                 | No         | No               |
 434   * | `$author`          | Yes            |  No                 | No         | No               |
 435   * | `$user`            | Yes            |  No                 | No         | No               |
 436   * | `$browse`          | Yes            |  No                 | No         | No               |
 437   * | `$locale`          | Yes            |  Yes                | No         | No               |
 438   * | `$fields`          | Yes            |  Yes                | No         | No               |
 439   *
 440   * @since 2.8.0
 441   *
 442   * @param string       $action API action to perform: 'query_themes', 'theme_information',
 443   *                             'hot_tags' or 'feature_list'.
 444   * @param array|object $args   {
 445   *     Optional. Array or object of arguments to serialize for the Themes API.
 446   *
 447   *     @type string  $slug     The theme slug. Default empty.
 448   *     @type int     $per_page Number of themes per page. Default 24.
 449   *     @type int     $page     Number of current page. Default 1.
 450   *     @type int     $number   Number of tags to be queried.
 451   *     @type string  $search   A search term. Default empty.
 452   *     @type string  $tag      Tag to filter themes. Default empty.
 453   *     @type string  $author   Username of an author to filter themes. Default empty.
 454   *     @type string  $user     Username to query for their favorites. Default empty.
 455   *     @type string  $browse   Browse view: 'featured', 'popular', 'updated', 'favorites'.
 456   *     @type string  $locale   Locale to provide context-sensitive results. Default is the value of get_locale().
 457   *     @type array   $fields   {
 458   *         Array of fields which should or should not be returned.
 459   *
 460   *         @type bool $description        Whether to return the theme full description. Default false.
 461   *         @type bool $sections           Whether to return the theme readme sections: description, installation,
 462   *                                        FAQ, screenshots, other notes, and changelog. Default false.
 463   *         @type bool $rating             Whether to return the rating in percent and total number of ratings.
 464   *                                        Default false.
 465   *         @type bool $ratings            Whether to return the number of rating for each star (1-5). Default false.
 466   *         @type bool $downloaded         Whether to return the download count. Default false.
 467   *         @type bool $downloadlink       Whether to return the download link for the package. Default false.
 468   *         @type bool $last_updated       Whether to return the date of the last update. Default false.
 469   *         @type bool $tags               Whether to return the assigned tags. Default false.
 470   *         @type bool $homepage           Whether to return the theme homepage link. Default false.
 471   *         @type bool $screenshots        Whether to return the screenshots. Default false.
 472   *         @type int  $screenshot_count   Number of screenshots to return. Default 1.
 473   *         @type bool $screenshot_url     Whether to return the URL of the first screenshot. Default false.
 474   *         @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false.
 475   *         @type bool $template           Whether to return the slug of the parent theme. Default false.
 476   *         @type bool $parent             Whether to return the slug, name and homepage of the parent theme. Default false.
 477   *         @type bool $versions           Whether to return the list of all available versions. Default false.
 478   *         @type bool $theme_url          Whether to return theme's URL. Default false.
 479   *         @type bool $extended_author    Whether to return nicename or nicename and display name. Default false.
 480   *     }
 481   * }
 482   * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
 483   *         {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article}
 484   *         for more information on the make-up of possible return objects depending on the value of `$action`.
 485   */
 486  function themes_api( $action, $args = array() ) {
 487      // Include an unmodified $wp_version.
 488      require  ABSPATH . WPINC . '/version.php';
 489  
 490      if ( is_array( $args ) ) {
 491          $args = (object) $args;
 492      }
 493  
 494      if ( 'query_themes' === $action ) {
 495          if ( ! isset( $args->per_page ) ) {
 496              $args->per_page = 24;
 497          }
 498      }
 499  
 500      if ( ! isset( $args->locale ) ) {
 501          $args->locale = get_user_locale();
 502      }
 503  
 504      if ( ! isset( $args->wp_version ) ) {
 505          $args->wp_version = substr( $wp_version, 0, 3 ); // x.y
 506      }
 507  
 508      /**
 509       * Filters arguments used to query for installer pages from the WordPress.org Themes API.
 510       *
 511       * Important: An object MUST be returned to this filter.
 512       *
 513       * @since 2.8.0
 514       *
 515       * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 516       * @param string $action Requested action. Likely values are 'theme_information',
 517       *                       'feature_list', or 'query_themes'.
 518       */
 519      $args = apply_filters( 'themes_api_args', $args, $action );
 520  
 521      /**
 522       * Filters whether to override the WordPress.org Themes API.
 523       *
 524       * Returning a non-false value will effectively short-circuit the WordPress.org API request.
 525       *
 526       * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
 527       * be passed. If `$action` is 'hot_tags', an array should be passed.
 528       *
 529       * @since 2.8.0
 530       *
 531       * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
 532       * @param string             $action   Requested action. Likely values are 'theme_information',
 533       *                                    'feature_list', or 'query_themes'.
 534       * @param object             $args     Arguments used to query for installer pages from the Themes API.
 535       */
 536      $res = apply_filters( 'themes_api', false, $action, $args );
 537  
 538      if ( ! $res ) {
 539          $url = 'http://api.wordpress.org/themes/info/1.2/';
 540          $url = add_query_arg(
 541              array(
 542                  'action'  => $action,
 543                  'request' => $args,
 544              ),
 545              $url
 546          );
 547  
 548          $http_url = $url;
 549          $ssl      = wp_http_supports( array( 'ssl' ) );
 550          if ( $ssl ) {
 551              $url = set_url_scheme( $url, 'https' );
 552          }
 553  
 554          $http_args = array(
 555              'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 556          );
 557          $request   = wp_remote_get( $url, $http_args );
 558  
 559          if ( $ssl && is_wp_error( $request ) ) {
 560              if ( ! wp_doing_ajax() ) {
 561                  trigger_error(
 562                      sprintf(
 563                          /* translators: %s: Support forums URL. */
 564                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 565                          __( 'https://wordpress.org/support/forums/' )
 566                      ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 567                      headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 568                  );
 569              }
 570              $request = wp_remote_get( $http_url, $http_args );
 571          }
 572  
 573          if ( is_wp_error( $request ) ) {
 574              $res = new WP_Error(
 575                  'themes_api_failed',
 576                  sprintf(
 577                      /* translators: %s: Support forums URL. */
 578                      __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 579                      __( 'https://wordpress.org/support/forums/' )
 580                  ),
 581                  $request->get_error_message()
 582              );
 583          } else {
 584              $res = json_decode( wp_remote_retrieve_body( $request ), true );
 585              if ( is_array( $res ) ) {
 586                  // Object casting is required in order to match the info/1.0 format.
 587                  $res = (object) $res;
 588              } elseif ( null === $res ) {
 589                  $res = new WP_Error(
 590                      'themes_api_failed',
 591                      sprintf(
 592                          /* translators: %s: Support forums URL. */
 593                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 594                          __( 'https://wordpress.org/support/forums/' )
 595                      ),
 596                      wp_remote_retrieve_body( $request )
 597                  );
 598              }
 599  
 600              if ( isset( $res->error ) ) {
 601                  $res = new WP_Error( 'themes_api_failed', $res->error );
 602              }
 603          }
 604  
 605          if ( ! is_wp_error( $res ) ) {
 606              // Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects.
 607              if ( 'query_themes' === $action ) {
 608                  foreach ( $res->themes as $i => $theme ) {
 609                      $res->themes[ $i ] = (object) $theme;
 610                  }
 611              }
 612  
 613              // Back-compat for info/1.2 API, downgrade the feature_list result back to an array.
 614              if ( 'feature_list' === $action ) {
 615                  $res = (array) $res;
 616              }
 617          }
 618      }
 619  
 620      /**
 621       * Filters the returned WordPress.org Themes API response.
 622       *
 623       * @since 2.8.0
 624       *
 625       * @param array|stdClass|WP_Error $res    WordPress.org Themes API response.
 626       * @param string                  $action Requested action. Likely values are 'theme_information',
 627       *                                        'feature_list', or 'query_themes'.
 628       * @param stdClass                $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 629       */
 630      return apply_filters( 'themes_api_result', $res, $action, $args );
 631  }
 632  
 633  /**
 634   * Prepares themes for JavaScript.
 635   *
 636   * @since 3.8.0
 637   *
 638   * @param WP_Theme[] $themes Optional. Array of theme objects to prepare.
 639   *                           Defaults to all allowed themes.
 640   *
 641   * @return array An associative array of theme data, sorted by name.
 642   */
 643  function wp_prepare_themes_for_js( $themes = null ) {
 644      $current_theme = get_stylesheet();
 645  
 646      /**
 647       * Filters theme data before it is prepared for JavaScript.
 648       *
 649       * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
 650       * early with that value instead.
 651       *
 652       * @since 4.2.0
 653       *
 654       * @param array           $prepared_themes An associative array of theme data. Default empty array.
 655       * @param WP_Theme[]|null $themes          An array of theme objects to prepare, if any.
 656       * @param string          $current_theme   The active theme slug.
 657       */
 658      $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
 659  
 660      if ( ! empty( $prepared_themes ) ) {
 661          return $prepared_themes;
 662      }
 663  
 664      // Make sure the active theme is listed first.
 665      $prepared_themes[ $current_theme ] = array();
 666  
 667      if ( null === $themes ) {
 668          $themes = wp_get_themes( array( 'allowed' => true ) );
 669          if ( ! isset( $themes[ $current_theme ] ) ) {
 670              $themes[ $current_theme ] = wp_get_theme();
 671          }
 672      }
 673  
 674      $updates    = array();
 675      $no_updates = array();
 676      if ( ! is_multisite() && current_user_can( 'update_themes' ) ) {
 677          $updates_transient = get_site_transient( 'update_themes' );
 678          if ( isset( $updates_transient->response ) ) {
 679              $updates = $updates_transient->response;
 680          }
 681          if ( isset( $updates_transient->no_update ) ) {
 682              $no_updates = $updates_transient->no_update;
 683          }
 684      }
 685  
 686      WP_Theme::sort_by_name( $themes );
 687  
 688      $parents = array();
 689  
 690      $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
 691  
 692      foreach ( $themes as $theme ) {
 693          $slug         = $theme->get_stylesheet();
 694          $encoded_slug = urlencode( $slug );
 695  
 696          $parent = false;
 697          if ( $theme->parent() ) {
 698              $parent           = $theme->parent();
 699              $parents[ $slug ] = $parent->get_stylesheet();
 700              $parent           = $parent->display( 'Name' );
 701          }
 702  
 703          $customize_action = null;
 704  
 705          $can_edit_theme_options = current_user_can( 'edit_theme_options' );
 706          $can_customize          = current_user_can( 'customize' );
 707          $is_block_theme         = $theme->is_block_theme();
 708  
 709          if ( $is_block_theme && $can_edit_theme_options ) {
 710              $customize_action = esc_url( admin_url( 'site-editor.php' ) );
 711          } elseif ( ! $is_block_theme && $can_customize && $can_edit_theme_options ) {
 712              $customize_action = esc_url(
 713                  add_query_arg(
 714                      array(
 715                          'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
 716                      ),
 717                      wp_customize_url( $slug )
 718                  )
 719              );
 720          }
 721  
 722          $update_requires_wp  = isset( $updates[ $slug ]['requires'] ) ? $updates[ $slug ]['requires'] : null;
 723          $update_requires_php = isset( $updates[ $slug ]['requires_php'] ) ? $updates[ $slug ]['requires_php'] : null;
 724  
 725          $auto_update        = in_array( $slug, $auto_updates, true );
 726          $auto_update_action = $auto_update ? 'disable-auto-update' : 'enable-auto-update';
 727  
 728          if ( isset( $updates[ $slug ] ) ) {
 729              $auto_update_supported      = true;
 730              $auto_update_filter_payload = (object) $updates[ $slug ];
 731          } elseif ( isset( $no_updates[ $slug ] ) ) {
 732              $auto_update_supported      = true;
 733              $auto_update_filter_payload = (object) $no_updates[ $slug ];
 734          } else {
 735              $auto_update_supported = false;
 736              /*
 737               * Create the expected payload for the auto_update_theme filter, this is the same data
 738               * as contained within $updates or $no_updates but used when the Theme is not known.
 739               */
 740              $auto_update_filter_payload = (object) array(
 741                  'theme'        => $slug,
 742                  'new_version'  => $theme->get( 'Version' ),
 743                  'url'          => '',
 744                  'package'      => '',
 745                  'requires'     => $theme->get( 'RequiresWP' ),
 746                  'requires_php' => $theme->get( 'RequiresPHP' ),
 747              );
 748          }
 749  
 750          $auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $auto_update_filter_payload );
 751  
 752          $prepared_themes[ $slug ] = array(
 753              'id'             => $slug,
 754              'name'           => $theme->display( 'Name' ),
 755              'screenshot'     => array( $theme->get_screenshot() ), // @todo Multiple screenshots.
 756              'description'    => $theme->display( 'Description' ),
 757              'author'         => $theme->display( 'Author', false, true ),
 758              'authorAndUri'   => $theme->display( 'Author' ),
 759              'tags'           => $theme->display( 'Tags' ),
 760              'version'        => $theme->get( 'Version' ),
 761              'compatibleWP'   => is_wp_version_compatible( $theme->get( 'RequiresWP' ) ),
 762              'compatiblePHP'  => is_php_version_compatible( $theme->get( 'RequiresPHP' ) ),
 763              'updateResponse' => array(
 764                  'compatibleWP'  => is_wp_version_compatible( $update_requires_wp ),
 765                  'compatiblePHP' => is_php_version_compatible( $update_requires_php ),
 766              ),
 767              'parent'         => $parent,
 768              'active'         => $slug === $current_theme,
 769              'hasUpdate'      => isset( $updates[ $slug ] ),
 770              'hasPackage'     => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
 771              'update'         => get_theme_update_available( $theme ),
 772              'autoupdate'     => array(
 773                  'enabled'   => $auto_update || $auto_update_forced,
 774                  'supported' => $auto_update_supported,
 775                  'forced'    => $auto_update_forced,
 776              ),
 777              'actions'        => array(
 778                  'activate'   => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
 779                  'customize'  => $customize_action,
 780                  'delete'     => ( ! is_multisite() && current_user_can( 'delete_themes' ) ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
 781                  'autoupdate' => wp_is_auto_update_enabled_for_type( 'theme' ) && ! is_multisite() && current_user_can( 'update_themes' )
 782                      ? wp_nonce_url( admin_url( 'themes.php?action=' . $auto_update_action . '&amp;stylesheet=' . $encoded_slug ), 'updates' )
 783                      : null,
 784              ),
 785              'blockTheme'     => $theme->is_block_theme(),
 786          );
 787      }
 788  
 789      // Remove 'delete' action if theme has an active child.
 790      if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
 791          unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
 792      }
 793  
 794      /**
 795       * Filters the themes prepared for JavaScript, for themes.php.
 796       *
 797       * Could be useful for changing the order, which is by name by default.
 798       *
 799       * @since 3.8.0
 800       *
 801       * @param array $prepared_themes Array of theme data.
 802       */
 803      $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
 804      $prepared_themes = array_values( $prepared_themes );
 805      return array_filter( $prepared_themes );
 806  }
 807  
 808  /**
 809   * Prints JS templates for the theme-browsing UI in the Customizer.
 810   *
 811   * @since 4.2.0
 812   */
 813  function customize_themes_print_templates() {
 814      ?>
 815      <script type="text/html" id="tmpl-customize-themes-details-view">
 816          <div class="theme-backdrop"></div>
 817          <div class="theme-wrap wp-clearfix" role="document">
 818              <div class="theme-header">
 819                  <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
 820                  <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
 821                  <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
 822              </div>
 823              <div class="theme-about wp-clearfix">
 824                  <div class="theme-screenshots">
 825                  <# if ( data.screenshot && data.screenshot[0] ) { #>
 826                      <div class="screenshot"><img src="{{ data.screenshot[0] }}?ver={{ data.version }}" alt="" /></div>
 827                  <# } else { #>
 828                      <div class="screenshot blank"></div>
 829                  <# } #>
 830                  </div>
 831  
 832                  <div class="theme-info">
 833                      <# if ( data.active ) { #>
 834                          <span class="current-label"><?php _e( 'Active Theme' ); ?></span>
 835                      <# } #>
 836                      <h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
 837                          <?php
 838                          /* translators: %s: Theme version. */
 839                          printf( __( 'Version: %s' ), '{{ data.version }}' );
 840                          ?>
 841                      </span></h2>
 842                      <h3 class="theme-author">
 843                          <?php
 844                          /* translators: %s: Theme author link. */
 845                          printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' );
 846                          ?>
 847                      </h3>
 848  
 849                      <# if ( data.stars && 0 != data.num_ratings ) { #>
 850                          <div class="theme-rating">
 851                              {{{ data.stars }}}
 852                              <a class="num-ratings" target="_blank" href="{{ data.reviews_url }}">
 853                                  <?php
 854                                  printf(
 855                                      '%1$s <span class="screen-reader-text">%2$s</span>',
 856                                      /* translators: %s: Number of ratings. */
 857                                      sprintf( __( '(%s ratings)' ), '{{ data.num_ratings }}' ),
 858                                      /* translators: Accessibility text. */
 859                                      __( '(opens in a new tab)' )
 860                                  );
 861                                  ?>
 862                              </a>
 863                          </div>
 864                      <# } #>
 865  
 866                      <# if ( data.hasUpdate ) { #>
 867                          <# if ( data.updateResponse.compatibleWP && data.updateResponse.compatiblePHP ) { #>
 868                              <div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
 869                                  <h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
 870                                  {{{ data.update }}}
 871                              </div>
 872                          <# } else { #>
 873                              <div class="notice notice-error notice-alt notice-large" data-slug="{{ data.id }}">
 874                                  <h3 class="notice-title"><?php _e( 'Update Incompatible' ); ?></h3>
 875                                  <p>
 876                                      <# if ( ! data.updateResponse.compatibleWP && ! data.updateResponse.compatiblePHP ) { #>
 877                                          <?php
 878                                          printf(
 879                                              /* translators: %s: Theme name. */
 880                                              __( 'There is a new version of %s available, but it does not work with your versions of WordPress and PHP.' ),
 881                                              '{{{ data.name }}}'
 882                                          );
 883                                          if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
 884                                              printf(
 885                                                  /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
 886                                                  ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
 887                                                  self_admin_url( 'update-core.php' ),
 888                                                  esc_url( wp_get_update_php_url() )
 889                                              );
 890                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 891                                          } elseif ( current_user_can( 'update_core' ) ) {
 892                                              printf(
 893                                                  /* translators: %s: URL to WordPress Updates screen. */
 894                                                  ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 895                                                  self_admin_url( 'update-core.php' )
 896                                              );
 897                                          } elseif ( current_user_can( 'update_php' ) ) {
 898                                              printf(
 899                                                  /* translators: %s: URL to Update PHP page. */
 900                                                  ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 901                                                  esc_url( wp_get_update_php_url() )
 902                                              );
 903                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 904                                          }
 905                                          ?>
 906                                      <# } else if ( ! data.updateResponse.compatibleWP ) { #>
 907                                          <?php
 908                                          printf(
 909                                              /* translators: %s: Theme name. */
 910                                              __( 'There is a new version of %s available, but it does not work with your version of WordPress.' ),
 911                                              '{{{ data.name }}}'
 912                                          );
 913                                          if ( current_user_can( 'update_core' ) ) {
 914                                              printf(
 915                                                  /* translators: %s: URL to WordPress Updates screen. */
 916                                                  ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 917                                                  self_admin_url( 'update-core.php' )
 918                                              );
 919                                          }
 920                                          ?>
 921                                      <# } else if ( ! data.updateResponse.compatiblePHP ) { #>
 922                                          <?php
 923                                          printf(
 924                                              /* translators: %s: Theme name. */
 925                                              __( 'There is a new version of %s available, but it does not work with your version of PHP.' ),
 926                                              '{{{ data.name }}}'
 927                                          );
 928                                          if ( current_user_can( 'update_php' ) ) {
 929                                              printf(
 930                                                  /* translators: %s: URL to Update PHP page. */
 931                                                  ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 932                                                  esc_url( wp_get_update_php_url() )
 933                                              );
 934                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 935                                          }
 936                                          ?>
 937                                      <# } #>
 938                                  </p>
 939                              </div>
 940                          <# } #>
 941                      <# } #>
 942  
 943                      <# if ( data.parent ) { #>
 944                          <p class="parent-theme">
 945                              <?php
 946                              printf(
 947                                  /* translators: %s: Theme name. */
 948                                  __( 'This is a child theme of %s.' ),
 949                                  '<strong>{{{ data.parent }}}</strong>'
 950                              );
 951                              ?>
 952                          </p>
 953                      <# } #>
 954  
 955                      <# if ( ! data.compatibleWP || ! data.compatiblePHP ) { #>
 956                          <div class="notice notice-error notice-alt notice-large"><p>
 957                              <# if ( ! data.compatibleWP && ! data.compatiblePHP ) { #>
 958                                  <?php
 959                                  _e( 'This theme does not work with your versions of WordPress and PHP.' );
 960                                  if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
 961                                      printf(
 962                                          /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
 963                                          ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
 964                                          self_admin_url( 'update-core.php' ),
 965                                          esc_url( wp_get_update_php_url() )
 966                                      );
 967                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
 968                                  } elseif ( current_user_can( 'update_core' ) ) {
 969                                      printf(
 970                                          /* translators: %s: URL to WordPress Updates screen. */
 971                                          ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 972                                          self_admin_url( 'update-core.php' )
 973                                      );
 974                                  } elseif ( current_user_can( 'update_php' ) ) {
 975                                      printf(
 976                                          /* translators: %s: URL to Update PHP page. */
 977                                          ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 978                                          esc_url( wp_get_update_php_url() )
 979                                      );
 980                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
 981                                  }
 982                                  ?>
 983                              <# } else if ( ! data.compatibleWP ) { #>
 984                                  <?php
 985                                  _e( 'This theme does not work with your version of WordPress.' );
 986                                  if ( current_user_can( 'update_core' ) ) {
 987                                      printf(
 988                                          /* translators: %s: URL to WordPress Updates screen. */
 989                                          ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 990                                          self_admin_url( 'update-core.php' )
 991                                      );
 992                                  }
 993                                  ?>
 994                              <# } else if ( ! data.compatiblePHP ) { #>
 995                                  <?php
 996                                  _e( 'This theme does not work with your version of PHP.' );
 997                                  if ( current_user_can( 'update_php' ) ) {
 998                                      printf(
 999                                          /* translators: %s: URL to Update PHP page. */
1000                                          ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
1001                                          esc_url( wp_get_update_php_url() )
1002                                      );
1003                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
1004                                  }
1005                                  ?>
1006                              <# } #>
1007                          </p></div>
1008                      <# } else if ( ! data.active && data.blockTheme ) { #>
1009                          <div class="notice notice-error notice-alt notice-large"><p>
1010                          <?php
1011                              _e( 'This theme doesn\'t support Customizer.' );
1012                          ?>
1013                          <# if ( data.actions.activate ) { #>
1014                              <?php
1015                              printf(
1016                                  /* translators: %s: URL to the themes page (also it activates the theme). */
1017                                  ' ' . __( 'However, you can still <a href="%s">activate this theme</a>, and use the Site Editor to customize it.' ),
1018                                  '{{{ data.actions.activate }}}'
1019                              );
1020                              ?>
1021                          <# } #>
1022                          </p></div>
1023                      <# } #>
1024  
1025                      <p class="theme-description">{{{ data.description }}}</p>
1026  
1027                      <# if ( data.tags ) { #>
1028                          <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
1029                      <# } #>
1030                  </div>
1031              </div>
1032  
1033              <div class="theme-actions">
1034                  <# if ( data.active ) { #>
1035                      <button type="button" class="button button-primary customize-theme"><?php _e( 'Customize' ); ?></button>
1036                  <# } else if ( 'installed' === data.type ) { #>
1037                      <?php if ( current_user_can( 'delete_themes' ) ) { ?>
1038                          <# if ( data.actions && data.actions['delete'] ) { #>
1039                              <a href="{{{ data.actions['delete'] }}}" data-slug="{{ data.id }}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
1040                          <# } #>
1041                      <?php } ?>
1042  
1043                      <# if ( data.blockTheme ) { #>
1044                          <?php
1045                              /* translators: %s: Theme name. */
1046                              $aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
1047                          ?>
1048                          <# if ( data.compatibleWP && data.compatiblePHP && data.actions.activate ) { #>
1049                              <a href="{{{ data.actions.activate }}}" class="button button-primary activate" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
1050                          <# } #>
1051                      <# } else { #>
1052                          <# if ( data.compatibleWP && data.compatiblePHP ) { #>
1053                              <button type="button" class="button button-primary preview-theme" data-slug="{{ data.id }}"><?php _e( 'Live Preview' ); ?></button>
1054                          <# } else { #>
1055                              <button class="button button-primary disabled"><?php _e( 'Live Preview' ); ?></button>
1056                          <# } #>
1057                      <# } #>
1058                  <# } else { #>
1059                      <# if ( data.compatibleWP && data.compatiblePHP ) { #>
1060                          <button type="button" class="button theme-install" data-slug="{{ data.id }}"><?php _e( 'Install' ); ?></button>
1061                          <button type="button" class="button button-primary theme-install preview" data-slug="{{ data.id }}"><?php _e( 'Install &amp; Preview' ); ?></button>
1062                      <# } else { #>
1063                          <button type="button" class="button disabled"><?php _ex( 'Cannot Install', 'theme' ); ?></button>
1064                          <button type="button" class="button button-primary disabled"><?php _e( 'Install &amp; Preview' ); ?></button>
1065                      <# } #>
1066                  <# } #>
1067              </div>
1068          </div>
1069      </script>
1070      <?php
1071  }
1072  
1073  /**
1074   * Determines whether a theme is technically active but was paused while
1075   * loading.
1076   *
1077   * For more information on this and similar theme functions, check out
1078   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1079   * Conditional Tags} article in the Theme Developer Handbook.
1080   *
1081   * @since 5.2.0
1082   *
1083   * @param string $theme Path to the theme directory relative to the themes directory.
1084   * @return bool True, if in the list of paused themes. False, not in the list.
1085   */
1086  function is_theme_paused( $theme ) {
1087      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
1088          return false;
1089      }
1090  
1091      if ( get_stylesheet() !== $theme && get_template() !== $theme ) {
1092          return false;
1093      }
1094  
1095      return array_key_exists( $theme, $GLOBALS['_paused_themes'] );
1096  }
1097  
1098  /**
1099   * Gets the error that was recorded for a paused theme.
1100   *
1101   * @since 5.2.0
1102   *
1103   * @param string $theme Path to the theme directory relative to the themes
1104   *                      directory.
1105   * @return array|false Array of error information as it was returned by
1106   *                     `error_get_last()`, or false if none was recorded.
1107   */
1108  function wp_get_theme_error( $theme ) {
1109      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
1110          return false;
1111      }
1112  
1113      if ( ! array_key_exists( $theme, $GLOBALS['_paused_themes'] ) ) {
1114          return false;
1115      }
1116  
1117      return $GLOBALS['_paused_themes'][ $theme ];
1118  }
1119  
1120  /**
1121   * Tries to resume a single theme.
1122   *
1123   * If a redirect was provided and a functions.php file was found, we first ensure that
1124   * functions.php file does not throw fatal errors anymore.
1125   *
1126   * The way it works is by setting the redirection to the error before trying to
1127   * include the file. If the theme fails, then the redirection will not be overwritten
1128   * with the success message and the theme will not be resumed.
1129   *
1130   * @since 5.2.0
1131   *
1132   * @param string $theme    Single theme to resume.
1133   * @param string $redirect Optional. URL to redirect to. Default empty string.
1134   * @return bool|WP_Error True on success, false if `$theme` was not paused,
1135   *                       `WP_Error` on failure.
1136   */
1137  function resume_theme( $theme, $redirect = '' ) {
1138      list( $extension ) = explode( '/', $theme );
1139  
1140      /*
1141       * We'll override this later if the theme could be resumed without
1142       * creating a fatal error.
1143       */
1144      if ( ! empty( $redirect ) ) {
1145          $functions_path = '';
1146          if ( strpos( STYLESHEETPATH, $extension ) ) {
1147              $functions_path = STYLESHEETPATH . '/functions.php';
1148          } elseif ( strpos( TEMPLATEPATH, $extension ) ) {
1149              $functions_path = TEMPLATEPATH . '/functions.php';
1150          }
1151  
1152          if ( ! empty( $functions_path ) ) {
1153              wp_redirect(
1154                  add_query_arg(
1155                      '_error_nonce',
1156                      wp_create_nonce( 'theme-resume-error_' . $theme ),
1157                      $redirect
1158                  )
1159              );
1160  
1161              // Load the theme's functions.php to test whether it throws a fatal error.
1162              ob_start();
1163              if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
1164                  define( 'WP_SANDBOX_SCRAPING', true );
1165              }
1166              include $functions_path;
1167              ob_clean();
1168          }
1169      }
1170  
1171      $result = wp_paused_themes()->delete( $extension );
1172  
1173      if ( ! $result ) {
1174          return new WP_Error(
1175              'could_not_resume_theme',
1176              __( 'Could not resume the theme.' )
1177          );
1178      }
1179  
1180      return true;
1181  }
1182  
1183  /**
1184   * Renders an admin notice in case some themes have been paused due to errors.
1185   *
1186   * @since 5.2.0
1187   *
1188   * @global string $pagenow The filename of the current screen.
1189   */
1190  function paused_themes_notice() {
1191      if ( 'themes.php' === $GLOBALS['pagenow'] ) {
1192          return;
1193      }
1194  
1195      if ( ! current_user_can( 'resume_themes' ) ) {
1196          return;
1197      }
1198  
1199      if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
1200          return;
1201      }
1202  
1203      printf(
1204          '<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>',
1205          __( 'One or more themes failed to load properly.' ),
1206          __( 'You can find more details and make changes on the Themes screen.' ),
1207          esc_url( admin_url( 'themes.php' ) ),
1208          __( 'Go to the Themes screen' )
1209      );
1210  }


Generated: Thu Dec 5 01:00:03 2024 Cross-referenced by PHPXref 0.7.1