[ 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   * Remove 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 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   * Retrieve 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   * Retrieve 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   *
 302   * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true.
 303   * @return array Array of features keyed by category with translations keyed by slug.
 304   */
 305  function get_theme_feature_list( $api = true ) {
 306      // Hard-coded list is used if API is not accessible.
 307      $features = array(
 308  
 309          __( 'Subject' )  => array(
 310              'blog'           => __( 'Blog' ),
 311              'e-commerce'     => __( 'E-Commerce' ),
 312              'education'      => __( 'Education' ),
 313              'entertainment'  => __( 'Entertainment' ),
 314              'food-and-drink' => __( 'Food & Drink' ),
 315              'holiday'        => __( 'Holiday' ),
 316              'news'           => __( 'News' ),
 317              'photography'    => __( 'Photography' ),
 318              'portfolio'      => __( 'Portfolio' ),
 319          ),
 320  
 321          __( 'Features' ) => array(
 322              'accessibility-ready'   => __( 'Accessibility Ready' ),
 323              'block-patterns'        => __( 'Block Editor Patterns' ),
 324              'block-styles'          => __( 'Block Editor Styles' ),
 325              'custom-background'     => __( 'Custom Background' ),
 326              'custom-colors'         => __( 'Custom Colors' ),
 327              'custom-header'         => __( 'Custom Header' ),
 328              'custom-logo'           => __( 'Custom Logo' ),
 329              'editor-style'          => __( 'Editor Style' ),
 330              'featured-image-header' => __( 'Featured Image Header' ),
 331              'featured-images'       => __( 'Featured Images' ),
 332              'footer-widgets'        => __( 'Footer Widgets' ),
 333              'full-site-editing'     => __( 'Full Site Editing' ),
 334              'full-width-template'   => __( 'Full Width Template' ),
 335              'post-formats'          => __( 'Post Formats' ),
 336              'sticky-post'           => __( 'Sticky Post' ),
 337              'theme-options'         => __( 'Theme Options' ),
 338          ),
 339  
 340          __( 'Layout' )   => array(
 341              'grid-layout'   => __( 'Grid Layout' ),
 342              'one-column'    => __( 'One Column' ),
 343              'two-columns'   => __( 'Two Columns' ),
 344              'three-columns' => __( 'Three Columns' ),
 345              'four-columns'  => __( 'Four Columns' ),
 346              'left-sidebar'  => __( 'Left Sidebar' ),
 347              'right-sidebar' => __( 'Right Sidebar' ),
 348              'wide-blocks'   => __( 'Wide Blocks' ),
 349          ),
 350  
 351      );
 352  
 353      if ( ! $api || ! current_user_can( 'install_themes' ) ) {
 354          return $features;
 355      }
 356  
 357      $feature_list = get_site_transient( 'wporg_theme_feature_list' );
 358      if ( ! $feature_list ) {
 359          set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
 360      }
 361  
 362      if ( ! $feature_list ) {
 363          $feature_list = themes_api( 'feature_list', array() );
 364          if ( is_wp_error( $feature_list ) ) {
 365              return $features;
 366          }
 367      }
 368  
 369      if ( ! $feature_list ) {
 370          return $features;
 371      }
 372  
 373      set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
 374  
 375      $category_translations = array(
 376          'Layout'   => __( 'Layout' ),
 377          'Features' => __( 'Features' ),
 378          'Subject'  => __( 'Subject' ),
 379      );
 380  
 381      $wporg_features = array();
 382  
 383      // Loop over the wp.org canonical list and apply translations.
 384      foreach ( (array) $feature_list as $feature_category => $feature_items ) {
 385          if ( isset( $category_translations[ $feature_category ] ) ) {
 386              $feature_category = $category_translations[ $feature_category ];
 387          }
 388  
 389          $wporg_features[ $feature_category ] = array();
 390  
 391          foreach ( $feature_items as $feature ) {
 392              if ( isset( $features[ $feature_category ][ $feature ] ) ) {
 393                  $wporg_features[ $feature_category ][ $feature ] = $features[ $feature_category ][ $feature ];
 394              } else {
 395                  $wporg_features[ $feature_category ][ $feature ] = $feature;
 396              }
 397          }
 398      }
 399  
 400      return $wporg_features;
 401  }
 402  
 403  /**
 404   * Retrieves theme installer pages from the WordPress.org Themes API.
 405   *
 406   * It is possible for a theme to override the Themes API result with three
 407   * filters. Assume this is for themes, which can extend on the Theme Info to
 408   * offer more choices. This is very powerful and must be used with care, when
 409   * overriding the filters.
 410   *
 411   * The first filter, {@see 'themes_api_args'}, is for the args and gives the action
 412   * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that
 413   * an object is returned.
 414   *
 415   * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org
 416   * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',
 417   * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed.
 418   *
 419   * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the
 420   * response object or array, depending on the `$action` type.
 421   *
 422   * Supported arguments per action:
 423   *
 424   * | Argument Name      | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list'   |
 425   * | -------------------| :------------: | :-----------------: | :--------: | :--------------: |
 426   * | `$slug`            | No             |  Yes                | No         | No               |
 427   * | `$per_page`        | Yes            |  No                 | No         | No               |
 428   * | `$page`            | Yes            |  No                 | No         | No               |
 429   * | `$number`          | No             |  No                 | Yes        | No               |
 430   * | `$search`          | Yes            |  No                 | No         | No               |
 431   * | `$tag`             | Yes            |  No                 | No         | No               |
 432   * | `$author`          | Yes            |  No                 | No         | No               |
 433   * | `$user`            | Yes            |  No                 | No         | No               |
 434   * | `$browse`          | Yes            |  No                 | No         | No               |
 435   * | `$locale`          | Yes            |  Yes                | No         | No               |
 436   * | `$fields`          | Yes            |  Yes                | No         | No               |
 437   *
 438   * @since 2.8.0
 439   *
 440   * @param string       $action API action to perform: 'query_themes', 'theme_information',
 441   *                             'hot_tags' or 'feature_list'.
 442   * @param array|object $args   {
 443   *     Optional. Array or object of arguments to serialize for the Themes API.
 444   *
 445   *     @type string  $slug     The theme slug. Default empty.
 446   *     @type int     $per_page Number of themes per page. Default 24.
 447   *     @type int     $page     Number of current page. Default 1.
 448   *     @type int     $number   Number of tags to be queried.
 449   *     @type string  $search   A search term. Default empty.
 450   *     @type string  $tag      Tag to filter themes. Default empty.
 451   *     @type string  $author   Username of an author to filter themes. Default empty.
 452   *     @type string  $user     Username to query for their favorites. Default empty.
 453   *     @type string  $browse   Browse view: 'featured', 'popular', 'updated', 'favorites'.
 454   *     @type string  $locale   Locale to provide context-sensitive results. Default is the value of get_locale().
 455   *     @type array   $fields   {
 456   *         Array of fields which should or should not be returned.
 457   *
 458   *         @type bool $description        Whether to return the theme full description. Default false.
 459   *         @type bool $sections           Whether to return the theme readme sections: description, installation,
 460   *                                        FAQ, screenshots, other notes, and changelog. Default false.
 461   *         @type bool $rating             Whether to return the rating in percent and total number of ratings.
 462   *                                        Default false.
 463   *         @type bool $ratings            Whether to return the number of rating for each star (1-5). Default false.
 464   *         @type bool $downloaded         Whether to return the download count. Default false.
 465   *         @type bool $downloadlink       Whether to return the download link for the package. Default false.
 466   *         @type bool $last_updated       Whether to return the date of the last update. Default false.
 467   *         @type bool $tags               Whether to return the assigned tags. Default false.
 468   *         @type bool $homepage           Whether to return the theme homepage link. Default false.
 469   *         @type bool $screenshots        Whether to return the screenshots. Default false.
 470   *         @type int  $screenshot_count   Number of screenshots to return. Default 1.
 471   *         @type bool $screenshot_url     Whether to return the URL of the first screenshot. Default false.
 472   *         @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false.
 473   *         @type bool $template           Whether to return the slug of the parent theme. Default false.
 474   *         @type bool $parent             Whether to return the slug, name and homepage of the parent theme. Default false.
 475   *         @type bool $versions           Whether to return the list of all available versions. Default false.
 476   *         @type bool $theme_url          Whether to return theme's URL. Default false.
 477   *         @type bool $extended_author    Whether to return nicename or nicename and display name. Default false.
 478   *     }
 479   * }
 480   * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
 481   *         {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article}
 482   *         for more information on the make-up of possible return objects depending on the value of `$action`.
 483   */
 484  function themes_api( $action, $args = array() ) {
 485      // Include an unmodified $wp_version.
 486      require  ABSPATH . WPINC . '/version.php';
 487  
 488      if ( is_array( $args ) ) {
 489          $args = (object) $args;
 490      }
 491  
 492      if ( 'query_themes' === $action ) {
 493          if ( ! isset( $args->per_page ) ) {
 494              $args->per_page = 24;
 495          }
 496      }
 497  
 498      if ( ! isset( $args->locale ) ) {
 499          $args->locale = get_user_locale();
 500      }
 501  
 502      if ( ! isset( $args->wp_version ) ) {
 503          $args->wp_version = substr( $wp_version, 0, 3 ); // x.y
 504      }
 505  
 506      /**
 507       * Filters arguments used to query for installer pages from the WordPress.org Themes API.
 508       *
 509       * Important: An object MUST be returned to this filter.
 510       *
 511       * @since 2.8.0
 512       *
 513       * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 514       * @param string $action Requested action. Likely values are 'theme_information',
 515       *                       'feature_list', or 'query_themes'.
 516       */
 517      $args = apply_filters( 'themes_api_args', $args, $action );
 518  
 519      /**
 520       * Filters whether to override the WordPress.org Themes API.
 521       *
 522       * Passing a non-false value will effectively short-circuit the WordPress.org API request.
 523       *
 524       * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
 525       * be passed. If `$action` is 'hot_tags', an array should be passed.
 526       *
 527       * @since 2.8.0
 528       *
 529       * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
 530       * @param string             $action   Requested action. Likely values are 'theme_information',
 531       *                                    'feature_list', or 'query_themes'.
 532       * @param object             $args     Arguments used to query for installer pages from the Themes API.
 533       */
 534      $res = apply_filters( 'themes_api', false, $action, $args );
 535  
 536      if ( ! $res ) {
 537          $url = 'http://api.wordpress.org/themes/info/1.2/';
 538          $url = add_query_arg(
 539              array(
 540                  'action'  => $action,
 541                  'request' => $args,
 542              ),
 543              $url
 544          );
 545  
 546          $http_url = $url;
 547          $ssl      = wp_http_supports( array( 'ssl' ) );
 548          if ( $ssl ) {
 549              $url = set_url_scheme( $url, 'https' );
 550          }
 551  
 552          $http_args = array(
 553              'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 554          );
 555          $request   = wp_remote_get( $url, $http_args );
 556  
 557          if ( $ssl && is_wp_error( $request ) ) {
 558              if ( ! wp_doing_ajax() ) {
 559                  trigger_error(
 560                      sprintf(
 561                          /* translators: %s: Support forums URL. */
 562                          __( '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>.' ),
 563                          __( 'https://wordpress.org/support/forums/' )
 564                      ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 565                      headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 566                  );
 567              }
 568              $request = wp_remote_get( $http_url, $http_args );
 569          }
 570  
 571          if ( is_wp_error( $request ) ) {
 572              $res = new WP_Error(
 573                  'themes_api_failed',
 574                  sprintf(
 575                      /* translators: %s: Support forums URL. */
 576                      __( '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>.' ),
 577                      __( 'https://wordpress.org/support/forums/' )
 578                  ),
 579                  $request->get_error_message()
 580              );
 581          } else {
 582              $res = json_decode( wp_remote_retrieve_body( $request ), true );
 583              if ( is_array( $res ) ) {
 584                  // Object casting is required in order to match the info/1.0 format.
 585                  $res = (object) $res;
 586              } elseif ( null === $res ) {
 587                  $res = new WP_Error(
 588                      'themes_api_failed',
 589                      sprintf(
 590                          /* translators: %s: Support forums URL. */
 591                          __( '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>.' ),
 592                          __( 'https://wordpress.org/support/forums/' )
 593                      ),
 594                      wp_remote_retrieve_body( $request )
 595                  );
 596              }
 597  
 598              if ( isset( $res->error ) ) {
 599                  $res = new WP_Error( 'themes_api_failed', $res->error );
 600              }
 601          }
 602  
 603          // Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects.
 604          if ( 'query_themes' === $action ) {
 605              foreach ( $res->themes as $i => $theme ) {
 606                  $res->themes[ $i ] = (object) $theme;
 607              }
 608          }
 609          // Back-compat for info/1.2 API, downgrade the feature_list result back to an array.
 610          if ( 'feature_list' === $action ) {
 611              $res = (array) $res;
 612          }
 613      }
 614  
 615      /**
 616       * Filters the returned WordPress.org Themes API response.
 617       *
 618       * @since 2.8.0
 619       *
 620       * @param array|object|WP_Error $res    WordPress.org Themes API response.
 621       * @param string                $action Requested action. Likely values are 'theme_information',
 622       *                                      'feature_list', or 'query_themes'.
 623       * @param object                $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 624       */
 625      return apply_filters( 'themes_api_result', $res, $action, $args );
 626  }
 627  
 628  /**
 629   * Prepare themes for JavaScript.
 630   *
 631   * @since 3.8.0
 632   *
 633   * @param WP_Theme[] $themes Optional. Array of theme objects to prepare.
 634   *                           Defaults to all allowed themes.
 635   *
 636   * @return array An associative array of theme data, sorted by name.
 637   */
 638  function wp_prepare_themes_for_js( $themes = null ) {
 639      $current_theme = get_stylesheet();
 640  
 641      /**
 642       * Filters theme data before it is prepared for JavaScript.
 643       *
 644       * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
 645       * early with that value instead.
 646       *
 647       * @since 4.2.0
 648       *
 649       * @param array           $prepared_themes An associative array of theme data. Default empty array.
 650       * @param WP_Theme[]|null $themes          An array of theme objects to prepare, if any.
 651       * @param string          $current_theme   The current theme slug.
 652       */
 653      $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
 654  
 655      if ( ! empty( $prepared_themes ) ) {
 656          return $prepared_themes;
 657      }
 658  
 659      // Make sure the current theme is listed first.
 660      $prepared_themes[ $current_theme ] = array();
 661  
 662      if ( null === $themes ) {
 663          $themes = wp_get_themes( array( 'allowed' => true ) );
 664          if ( ! isset( $themes[ $current_theme ] ) ) {
 665              $themes[ $current_theme ] = wp_get_theme();
 666          }
 667      }
 668  
 669      $updates    = array();
 670      $no_updates = array();
 671      if ( ! is_multisite() && current_user_can( 'update_themes' ) ) {
 672          $updates_transient = get_site_transient( 'update_themes' );
 673          if ( isset( $updates_transient->response ) ) {
 674              $updates = $updates_transient->response;
 675          }
 676          if ( isset( $updates_transient->no_update ) ) {
 677              $no_updates = $updates_transient->no_update;
 678          }
 679      }
 680  
 681      WP_Theme::sort_by_name( $themes );
 682  
 683      $parents = array();
 684  
 685      $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
 686  
 687      foreach ( $themes as $theme ) {
 688          $slug         = $theme->get_stylesheet();
 689          $encoded_slug = urlencode( $slug );
 690  
 691          $parent = false;
 692          if ( $theme->parent() ) {
 693              $parent           = $theme->parent();
 694              $parents[ $slug ] = $parent->get_stylesheet();
 695              $parent           = $parent->display( 'Name' );
 696          }
 697  
 698          $customize_action = null;
 699          if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
 700              $customize_action = esc_url(
 701                  add_query_arg(
 702                      array(
 703                          'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
 704                      ),
 705                      wp_customize_url( $slug )
 706                  )
 707              );
 708          }
 709  
 710          $update_requires_wp  = isset( $updates[ $slug ]['requires'] ) ? $updates[ $slug ]['requires'] : null;
 711          $update_requires_php = isset( $updates[ $slug ]['requires_php'] ) ? $updates[ $slug ]['requires_php'] : null;
 712  
 713          $auto_update        = in_array( $slug, $auto_updates, true );
 714          $auto_update_action = $auto_update ? 'disable-auto-update' : 'enable-auto-update';
 715  
 716          if ( isset( $updates[ $slug ] ) ) {
 717              $auto_update_supported      = true;
 718              $auto_update_filter_payload = (object) $updates[ $slug ];
 719          } elseif ( isset( $no_updates[ $slug ] ) ) {
 720              $auto_update_supported      = true;
 721              $auto_update_filter_payload = (object) $no_updates[ $slug ];
 722          } else {
 723              $auto_update_supported = false;
 724              /*
 725               * Create the expected payload for the auto_update_theme filter, this is the same data
 726               * as contained within $updates or $no_updates but used when the Theme is not known.
 727               */
 728              $auto_update_filter_payload = (object) array(
 729                  'theme'        => $slug,
 730                  'new_version'  => $theme->get( 'Version' ),
 731                  'url'          => '',
 732                  'package'      => '',
 733                  'requires'     => $theme->get( 'RequiresWP' ),
 734                  'requires_php' => $theme->get( 'RequiresPHP' ),
 735              );
 736          }
 737  
 738          $auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $auto_update_filter_payload );
 739  
 740          $prepared_themes[ $slug ] = array(
 741              'id'             => $slug,
 742              'name'           => $theme->display( 'Name' ),
 743              'screenshot'     => array( $theme->get_screenshot() ), // @todo Multiple screenshots.
 744              'description'    => $theme->display( 'Description' ),
 745              'author'         => $theme->display( 'Author', false, true ),
 746              'authorAndUri'   => $theme->display( 'Author' ),
 747              'tags'           => $theme->display( 'Tags' ),
 748              'version'        => $theme->get( 'Version' ),
 749              'compatibleWP'   => is_wp_version_compatible( $theme->get( 'RequiresWP' ) ),
 750              'compatiblePHP'  => is_php_version_compatible( $theme->get( 'RequiresPHP' ) ),
 751              'updateResponse' => array(
 752                  'compatibleWP'  => is_wp_version_compatible( $update_requires_wp ),
 753                  'compatiblePHP' => is_php_version_compatible( $update_requires_php ),
 754              ),
 755              'parent'         => $parent,
 756              'active'         => $slug === $current_theme,
 757              'hasUpdate'      => isset( $updates[ $slug ] ),
 758              'hasPackage'     => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
 759              'update'         => get_theme_update_available( $theme ),
 760              'autoupdate'     => array(
 761                  'enabled'   => $auto_update || $auto_update_forced,
 762                  'supported' => $auto_update_supported,
 763                  'forced'    => $auto_update_forced,
 764              ),
 765              'actions'        => array(
 766                  'activate'   => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
 767                  'customize'  => $customize_action,
 768                  '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,
 769                  'autoupdate' => wp_is_auto_update_enabled_for_type( 'theme' ) && ! is_multisite() && current_user_can( 'update_themes' )
 770                      ? wp_nonce_url( admin_url( 'themes.php?action=' . $auto_update_action . '&amp;stylesheet=' . $encoded_slug ), 'updates' )
 771                      : null,
 772              ),
 773          );
 774      }
 775  
 776      // Remove 'delete' action if theme has an active child.
 777      if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
 778          unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
 779      }
 780  
 781      /**
 782       * Filters the themes prepared for JavaScript, for themes.php.
 783       *
 784       * Could be useful for changing the order, which is by name by default.
 785       *
 786       * @since 3.8.0
 787       *
 788       * @param array $prepared_themes Array of theme data.
 789       */
 790      $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
 791      $prepared_themes = array_values( $prepared_themes );
 792      return array_filter( $prepared_themes );
 793  }
 794  
 795  /**
 796   * Print JS templates for the theme-browsing UI in the Customizer.
 797   *
 798   * @since 4.2.0
 799   */
 800  function customize_themes_print_templates() {
 801      ?>
 802      <script type="text/html" id="tmpl-customize-themes-details-view">
 803          <div class="theme-backdrop"></div>
 804          <div class="theme-wrap wp-clearfix" role="document">
 805              <div class="theme-header">
 806                  <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
 807                  <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
 808                  <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
 809              </div>
 810              <div class="theme-about wp-clearfix">
 811                  <div class="theme-screenshots">
 812                  <# if ( data.screenshot && data.screenshot[0] ) { #>
 813                      <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
 814                  <# } else { #>
 815                      <div class="screenshot blank"></div>
 816                  <# } #>
 817                  </div>
 818  
 819                  <div class="theme-info">
 820                      <# if ( data.active ) { #>
 821                          <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
 822                      <# } #>
 823                      <h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
 824                          <?php
 825                          /* translators: %s: Theme version. */
 826                          printf( __( 'Version: %s' ), '{{ data.version }}' );
 827                          ?>
 828                      </span></h2>
 829                      <h3 class="theme-author">
 830                          <?php
 831                          /* translators: %s: Theme author link. */
 832                          printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' );
 833                          ?>
 834                      </h3>
 835  
 836                      <# if ( data.stars && 0 != data.num_ratings ) { #>
 837                          <div class="theme-rating">
 838                              {{{ data.stars }}}
 839                              <a class="num-ratings" target="_blank" href="{{ data.reviews_url }}">
 840                                  <?php
 841                                  printf(
 842                                      '%1$s <span class="screen-reader-text">%2$s</span>',
 843                                      /* translators: %s: Number of ratings. */
 844                                      sprintf( __( '(%s ratings)' ), '{{ data.num_ratings }}' ),
 845                                      /* translators: Accessibility text. */
 846                                      __( '(opens in a new tab)' )
 847                                  );
 848                                  ?>
 849                              </a>
 850                          </div>
 851                      <# } #>
 852  
 853                      <# if ( data.hasUpdate ) { #>
 854                          <# if ( data.updateResponse.compatibleWP && data.updateResponse.compatiblePHP ) { #>
 855                              <div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
 856                                  <h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
 857                                  {{{ data.update }}}
 858                              </div>
 859                          <# } else { #>
 860                              <div class="notice notice-error notice-alt notice-large" data-slug="{{ data.id }}">
 861                                  <h3 class="notice-title"><?php _e( 'Update Incompatible' ); ?></h3>
 862                                  <p>
 863                                      <# if ( ! data.updateResponse.compatibleWP && ! data.updateResponse.compatiblePHP ) { #>
 864                                          <?php
 865                                          printf(
 866                                              /* translators: %s: Theme name. */
 867                                              __( 'There is a new version of %s available, but it doesn&#8217;t work with your versions of WordPress and PHP.' ),
 868                                              '{{{ data.name }}}'
 869                                          );
 870                                          if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
 871                                              printf(
 872                                                  /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
 873                                                  ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
 874                                                  self_admin_url( 'update-core.php' ),
 875                                                  esc_url( wp_get_update_php_url() )
 876                                              );
 877                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 878                                          } elseif ( current_user_can( 'update_core' ) ) {
 879                                              printf(
 880                                                  /* translators: %s: URL to WordPress Updates screen. */
 881                                                  ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 882                                                  self_admin_url( 'update-core.php' )
 883                                              );
 884                                          } elseif ( current_user_can( 'update_php' ) ) {
 885                                              printf(
 886                                                  /* translators: %s: URL to Update PHP page. */
 887                                                  ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 888                                                  esc_url( wp_get_update_php_url() )
 889                                              );
 890                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 891                                          }
 892                                          ?>
 893                                      <# } else if ( ! data.updateResponse.compatibleWP ) { #>
 894                                          <?php
 895                                          printf(
 896                                              /* translators: %s: Theme name. */
 897                                              __( 'There is a new version of %s available, but it doesn&#8217;t work with your version of WordPress.' ),
 898                                              '{{{ data.name }}}'
 899                                          );
 900                                          if ( current_user_can( 'update_core' ) ) {
 901                                              printf(
 902                                                  /* translators: %s: URL to WordPress Updates screen. */
 903                                                  ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 904                                                  self_admin_url( 'update-core.php' )
 905                                              );
 906                                          }
 907                                          ?>
 908                                      <# } else if ( ! data.updateResponse.compatiblePHP ) { #>
 909                                          <?php
 910                                          printf(
 911                                              /* translators: %s: Theme name. */
 912                                              __( 'There is a new version of %s available, but it doesn&#8217;t work with your version of PHP.' ),
 913                                              '{{{ data.name }}}'
 914                                          );
 915                                          if ( current_user_can( 'update_php' ) ) {
 916                                              printf(
 917                                                  /* translators: %s: URL to Update PHP page. */
 918                                                  ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 919                                                  esc_url( wp_get_update_php_url() )
 920                                              );
 921                                              wp_update_php_annotation( '</p><p><em>', '</em>' );
 922                                          }
 923                                          ?>
 924                                      <# } #>
 925                                  </p>
 926                              </div>
 927                          <# } #>
 928                      <# } #>
 929  
 930                      <# if ( data.parent ) { #>
 931                          <p class="parent-theme">
 932                              <?php
 933                              printf(
 934                                  /* translators: %s: Theme name. */
 935                                  __( 'This is a child theme of %s.' ),
 936                                  '<strong>{{{ data.parent }}}</strong>'
 937                              );
 938                              ?>
 939                          </p>
 940                      <# } #>
 941  
 942                      <# if ( ! data.compatibleWP || ! data.compatiblePHP ) { #>
 943                          <div class="notice notice-error notice-alt notice-large"><p>
 944                              <# if ( ! data.compatibleWP && ! data.compatiblePHP ) { #>
 945                                  <?php
 946                                  _e( 'This theme doesn&#8217;t work with your versions of WordPress and PHP.' );
 947                                  if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
 948                                      printf(
 949                                          /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
 950                                          ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
 951                                          self_admin_url( 'update-core.php' ),
 952                                          esc_url( wp_get_update_php_url() )
 953                                      );
 954                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
 955                                  } elseif ( current_user_can( 'update_core' ) ) {
 956                                      printf(
 957                                          /* translators: %s: URL to WordPress Updates screen. */
 958                                          ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 959                                          self_admin_url( 'update-core.php' )
 960                                      );
 961                                  } elseif ( current_user_can( 'update_php' ) ) {
 962                                      printf(
 963                                          /* translators: %s: URL to Update PHP page. */
 964                                          ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 965                                          esc_url( wp_get_update_php_url() )
 966                                      );
 967                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
 968                                  }
 969                                  ?>
 970                              <# } else if ( ! data.compatibleWP ) { #>
 971                                  <?php
 972                                  _e( 'This theme doesn&#8217;t work with your version of WordPress.' );
 973                                  if ( current_user_can( 'update_core' ) ) {
 974                                      printf(
 975                                          /* translators: %s: URL to WordPress Updates screen. */
 976                                          ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
 977                                          self_admin_url( 'update-core.php' )
 978                                      );
 979                                  }
 980                                  ?>
 981                              <# } else if ( ! data.compatiblePHP ) { #>
 982                                  <?php
 983                                  _e( 'This theme doesn&#8217;t work with your version of PHP.' );
 984                                  if ( current_user_can( 'update_php' ) ) {
 985                                      printf(
 986                                          /* translators: %s: URL to Update PHP page. */
 987                                          ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
 988                                          esc_url( wp_get_update_php_url() )
 989                                      );
 990                                      wp_update_php_annotation( '</p><p><em>', '</em>' );
 991                                  }
 992                                  ?>
 993                              <# } #>
 994                          </p></div>
 995                      <# } #>
 996  
 997                      <p class="theme-description">{{{ data.description }}}</p>
 998  
 999                      <# if ( data.tags ) { #>
1000                          <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
1001                      <# } #>
1002                  </div>
1003              </div>
1004  
1005              <div class="theme-actions">
1006                  <# if ( data.active ) { #>
1007                      <button type="button" class="button button-primary customize-theme"><?php _e( 'Customize' ); ?></button>
1008                  <# } else if ( 'installed' === data.type ) { #>
1009                      <?php if ( current_user_can( 'delete_themes' ) ) { ?>
1010                          <# if ( data.actions && data.actions['delete'] ) { #>
1011                              <a href="{{{ data.actions['delete'] }}}" data-slug="{{ data.id }}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
1012                          <# } #>
1013                      <?php } ?>
1014  
1015                      <# if ( data.compatibleWP && data.compatiblePHP ) { #>
1016                          <button type="button" class="button button-primary preview-theme" data-slug="{{ data.id }}"><?php _e( 'Live Preview' ); ?></button>
1017                      <# } else { #>
1018                          <button class="button button-primary disabled"><?php _e( 'Live Preview' ); ?></button>
1019                      <# } #>
1020                  <# } else { #>
1021                      <# if ( data.compatibleWP && data.compatiblePHP ) { #>
1022                          <button type="button" class="button theme-install" data-slug="{{ data.id }}"><?php _e( 'Install' ); ?></button>
1023                          <button type="button" class="button button-primary theme-install preview" data-slug="{{ data.id }}"><?php _e( 'Install &amp; Preview' ); ?></button>
1024                      <# } else { #>
1025                          <button type="button" class="button disabled"><?php _ex( 'Cannot Install', 'theme' ); ?></button>
1026                          <button type="button" class="button button-primary disabled"><?php _e( 'Install &amp; Preview' ); ?></button>
1027                      <# } #>
1028                  <# } #>
1029              </div>
1030          </div>
1031      </script>
1032      <?php
1033  }
1034  
1035  /**
1036   * Determines whether a theme is technically active but was paused while
1037   * loading.
1038   *
1039   * For more information on this and similar theme functions, check out
1040   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1041   * Conditional Tags} article in the Theme Developer Handbook.
1042   *
1043   * @since 5.2.0
1044   *
1045   * @param string $theme Path to the theme directory relative to the themes directory.
1046   * @return bool True, if in the list of paused themes. False, not in the list.
1047   */
1048  function is_theme_paused( $theme ) {
1049      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
1050          return false;
1051      }
1052  
1053      if ( get_stylesheet() !== $theme && get_template() !== $theme ) {
1054          return false;
1055      }
1056  
1057      return array_key_exists( $theme, $GLOBALS['_paused_themes'] );
1058  }
1059  
1060  /**
1061   * Gets the error that was recorded for a paused theme.
1062   *
1063   * @since 5.2.0
1064   *
1065   * @param string $theme Path to the theme directory relative to the themes
1066   *                      directory.
1067   * @return array|false Array of error information as it was returned by
1068   *                     `error_get_last()`, or false if none was recorded.
1069   */
1070  function wp_get_theme_error( $theme ) {
1071      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
1072          return false;
1073      }
1074  
1075      if ( ! array_key_exists( $theme, $GLOBALS['_paused_themes'] ) ) {
1076          return false;
1077      }
1078  
1079      return $GLOBALS['_paused_themes'][ $theme ];
1080  }
1081  
1082  /**
1083   * Tries to resume a single theme.
1084   *
1085   * If a redirect was provided and a functions.php file was found, we first ensure that
1086   * functions.php file does not throw fatal errors anymore.
1087   *
1088   * The way it works is by setting the redirection to the error before trying to
1089   * include the file. If the theme fails, then the redirection will not be overwritten
1090   * with the success message and the theme will not be resumed.
1091   *
1092   * @since 5.2.0
1093   *
1094   * @param string $theme    Single theme to resume.
1095   * @param string $redirect Optional. URL to redirect to. Default empty string.
1096   * @return bool|WP_Error True on success, false if `$theme` was not paused,
1097   *                       `WP_Error` on failure.
1098   */
1099  function resume_theme( $theme, $redirect = '' ) {
1100      list( $extension ) = explode( '/', $theme );
1101  
1102      /*
1103       * We'll override this later if the theme could be resumed without
1104       * creating a fatal error.
1105       */
1106      if ( ! empty( $redirect ) ) {
1107          $functions_path = '';
1108          if ( strpos( STYLESHEETPATH, $extension ) ) {
1109              $functions_path = STYLESHEETPATH . '/functions.php';
1110          } elseif ( strpos( TEMPLATEPATH, $extension ) ) {
1111              $functions_path = TEMPLATEPATH . '/functions.php';
1112          }
1113  
1114          if ( ! empty( $functions_path ) ) {
1115              wp_redirect(
1116                  add_query_arg(
1117                      '_error_nonce',
1118                      wp_create_nonce( 'theme-resume-error_' . $theme ),
1119                      $redirect
1120                  )
1121              );
1122  
1123              // Load the theme's functions.php to test whether it throws a fatal error.
1124              ob_start();
1125              if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
1126                  define( 'WP_SANDBOX_SCRAPING', true );
1127              }
1128              include $functions_path;
1129              ob_clean();
1130          }
1131      }
1132  
1133      $result = wp_paused_themes()->delete( $extension );
1134  
1135      if ( ! $result ) {
1136          return new WP_Error(
1137              'could_not_resume_theme',
1138              __( 'Could not resume the theme.' )
1139          );
1140      }
1141  
1142      return true;
1143  }
1144  
1145  /**
1146   * Renders an admin notice in case some themes have been paused due to errors.
1147   *
1148   * @since 5.2.0
1149   *
1150   * @global string $pagenow
1151   */
1152  function paused_themes_notice() {
1153      if ( 'themes.php' === $GLOBALS['pagenow'] ) {
1154          return;
1155      }
1156  
1157      if ( ! current_user_can( 'resume_themes' ) ) {
1158          return;
1159      }
1160  
1161      if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
1162          return;
1163      }
1164  
1165      printf(
1166          '<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>',
1167          __( 'One or more themes failed to load properly.' ),
1168          __( 'You can find more details and make changes on the Themes screen.' ),
1169          esc_url( admin_url( 'themes.php' ) ),
1170          __( 'Go to the Themes screen' )
1171      );
1172  }


Generated: Wed Jun 23 01:00:04 2021 Cross-referenced by PHPXref 0.7.1