[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref