[ 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      $themes_dir = trailingslashit( $themes_dir );
  76      $theme_dir  = trailingslashit( $themes_dir . $stylesheet );
  77      $deleted    = $wp_filesystem->delete( $theme_dir, true );
  78  
  79      if ( ! $deleted ) {
  80          return new WP_Error(
  81              'could_not_remove_theme',
  82              /* translators: %s: Theme name. */
  83              sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet )
  84          );
  85      }
  86  
  87      $theme_translations = wp_get_installed_translations( 'themes' );
  88  
  89      // Remove language files, silently.
  90      if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
  91          $translations = $theme_translations[ $stylesheet ];
  92  
  93          foreach ( $translations as $translation => $data ) {
  94              $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
  95              $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
  96  
  97              $json_translation_files = glob( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '-*.json' );
  98              if ( $json_translation_files ) {
  99                  array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
 100              }
 101          }
 102      }
 103  
 104      // Remove the theme from allowed themes on the network.
 105      if ( is_multisite() ) {
 106          WP_Theme::network_disable_theme( $stylesheet );
 107      }
 108  
 109      // Force refresh of theme update information.
 110      delete_site_transient( 'update_themes' );
 111  
 112      return true;
 113  }
 114  
 115  /**
 116   * Gets the page templates available in this theme.
 117   *
 118   * @since 1.5.0
 119   * @since 4.7.0 Added the `$post_type` parameter.
 120   *
 121   * @param WP_Post|null $post      Optional. The post being edited, provided for context.
 122   * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
 123   * @return string[] Array of template file names keyed by the template header name.
 124   */
 125  function get_page_templates( $post = null, $post_type = 'page' ) {
 126      return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
 127  }
 128  
 129  /**
 130   * Tidies a filename for url display by the theme editor.
 131   *
 132   * @since 2.9.0
 133   * @access private
 134   *
 135   * @param string $fullpath Full path to the theme file
 136   * @param string $containingfolder Path of the theme parent folder
 137   * @return string
 138   */
 139  function _get_template_edit_filename( $fullpath, $containingfolder ) {
 140      return str_replace( dirname( dirname( $containingfolder ) ), '', $fullpath );
 141  }
 142  
 143  /**
 144   * Check if there is an update for a theme available.
 145   *
 146   * Will display link, if there is an update available.
 147   *
 148   * @since 2.7.0
 149   * @see get_theme_update_available()
 150   *
 151   * @param WP_Theme $theme Theme data object.
 152   */
 153  function theme_update_available( $theme ) {
 154      echo get_theme_update_available( $theme );
 155  }
 156  
 157  /**
 158   * Retrieve the update link if there is a theme update available.
 159   *
 160   * Will return a link if there is an update available.
 161   *
 162   * @since 3.8.0
 163   *
 164   * @staticvar object $themes_update
 165   *
 166   * @param WP_Theme $theme WP_Theme object.
 167   * @return string|false HTML for the update link, or false if invalid info was passed.
 168   */
 169  function get_theme_update_available( $theme ) {
 170      static $themes_update = null;
 171  
 172      if ( ! current_user_can( 'update_themes' ) ) {
 173          return false;
 174      }
 175  
 176      if ( ! isset( $themes_update ) ) {
 177          $themes_update = get_site_transient( 'update_themes' );
 178      }
 179  
 180      if ( ! ( $theme instanceof WP_Theme ) ) {
 181          return false;
 182      }
 183  
 184      $stylesheet = $theme->get_stylesheet();
 185  
 186      $html = '';
 187  
 188      if ( isset( $themes_update->response[ $stylesheet ] ) ) {
 189          $update      = $themes_update->response[ $stylesheet ];
 190          $theme_name  = $theme->display( 'Name' );
 191          $details_url = add_query_arg(
 192              array(
 193                  'TB_iframe' => 'true',
 194                  'width'     => 1024,
 195                  'height'    => 800,
 196              ),
 197              $update['url']
 198          ); // Theme browser inside WP? Replace this. Also, theme preview JS will override this on the available list.
 199          $update_url  = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
 200  
 201          if ( ! is_multisite() ) {
 202              if ( ! current_user_can( 'update_themes' ) ) {
 203                  $html = sprintf(
 204                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
 205                      '<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>',
 206                      $theme_name,
 207                      esc_url( $details_url ),
 208                      sprintf(
 209                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 210                          /* translators: 1: Theme name, 2: Version number. */
 211                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 212                      ),
 213                      $update['new_version']
 214                  );
 215              } elseif ( empty( $update['package'] ) ) {
 216                  $html = sprintf(
 217                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
 218                      '<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>',
 219                      $theme_name,
 220                      esc_url( $details_url ),
 221                      sprintf(
 222                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 223                          /* translators: 1: Theme name, 2: Version number. */
 224                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 225                      ),
 226                      $update['new_version']
 227                  );
 228              } else {
 229                  $html = sprintf(
 230                      /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
 231                      '<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>',
 232                      $theme_name,
 233                      esc_url( $details_url ),
 234                      sprintf(
 235                          'class="thickbox open-plugin-details-modal" aria-label="%s"',
 236                          /* translators: 1: Theme name, 2: Version number. */
 237                          esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
 238                      ),
 239                      $update['new_version'],
 240                      $update_url,
 241                      sprintf(
 242                          'aria-label="%s" id="update-theme" data-slug="%s"',
 243                          /* translators: %s: Theme name. */
 244                          esc_attr( sprintf( __( 'Update %s now' ), $theme_name ) ),
 245                          $stylesheet
 246                      )
 247                  );
 248              }
 249          }
 250      }
 251  
 252      return $html;
 253  }
 254  
 255  /**
 256   * Retrieve list of WordPress theme features (aka theme tags).
 257   *
 258   * @since 3.1.0
 259   *
 260   * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true.
 261   * @return array Array of features keyed by category with translations keyed by slug.
 262   */
 263  function get_theme_feature_list( $api = true ) {
 264      // Hard-coded list is used if API is not accessible.
 265      $features = array(
 266  
 267          __( 'Subject' )  => array(
 268              'blog'           => __( 'Blog' ),
 269              'e-commerce'     => __( 'E-Commerce' ),
 270              'education'      => __( 'Education' ),
 271              'entertainment'  => __( 'Entertainment' ),
 272              'food-and-drink' => __( 'Food & Drink' ),
 273              'holiday'        => __( 'Holiday' ),
 274              'news'           => __( 'News' ),
 275              'photography'    => __( 'Photography' ),
 276              'portfolio'      => __( 'Portfolio' ),
 277          ),
 278  
 279          __( 'Features' ) => array(
 280              'accessibility-ready'   => __( 'Accessibility Ready' ),
 281              'custom-background'     => __( 'Custom Background' ),
 282              'custom-colors'         => __( 'Custom Colors' ),
 283              'custom-header'         => __( 'Custom Header' ),
 284              'custom-logo'           => __( 'Custom Logo' ),
 285              'editor-style'          => __( 'Editor Style' ),
 286              'featured-image-header' => __( 'Featured Image Header' ),
 287              'featured-images'       => __( 'Featured Images' ),
 288              'footer-widgets'        => __( 'Footer Widgets' ),
 289              'full-width-template'   => __( 'Full Width Template' ),
 290              'post-formats'          => __( 'Post Formats' ),
 291              'sticky-post'           => __( 'Sticky Post' ),
 292              'theme-options'         => __( 'Theme Options' ),
 293          ),
 294  
 295          __( 'Layout' )   => array(
 296              'grid-layout'   => __( 'Grid Layout' ),
 297              'one-column'    => __( 'One Column' ),
 298              'two-columns'   => __( 'Two Columns' ),
 299              'three-columns' => __( 'Three Columns' ),
 300              'four-columns'  => __( 'Four Columns' ),
 301              'left-sidebar'  => __( 'Left Sidebar' ),
 302              'right-sidebar' => __( 'Right Sidebar' ),
 303          ),
 304  
 305      );
 306  
 307      if ( ! $api || ! current_user_can( 'install_themes' ) ) {
 308          return $features;
 309      }
 310  
 311      $feature_list = get_site_transient( 'wporg_theme_feature_list' );
 312      if ( ! $feature_list ) {
 313          set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
 314      }
 315  
 316      if ( ! $feature_list ) {
 317          $feature_list = themes_api( 'feature_list', array() );
 318          if ( is_wp_error( $feature_list ) ) {
 319              return $features;
 320          }
 321      }
 322  
 323      if ( ! $feature_list ) {
 324          return $features;
 325      }
 326  
 327      set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
 328  
 329      $category_translations = array(
 330          'Layout'   => __( 'Layout' ),
 331          'Features' => __( 'Features' ),
 332          'Subject'  => __( 'Subject' ),
 333      );
 334  
 335      // Loop over the wp.org canonical list and apply translations.
 336      $wporg_features = array();
 337      foreach ( (array) $feature_list as $feature_category => $feature_items ) {
 338          if ( isset( $category_translations[ $feature_category ] ) ) {
 339              $feature_category = $category_translations[ $feature_category ];
 340          }
 341          $wporg_features[ $feature_category ] = array();
 342  
 343          foreach ( $feature_items as $feature ) {
 344              if ( isset( $features[ $feature_category ][ $feature ] ) ) {
 345                  $wporg_features[ $feature_category ][ $feature ] = $features[ $feature_category ][ $feature ];
 346              } else {
 347                  $wporg_features[ $feature_category ][ $feature ] = $feature;
 348              }
 349          }
 350      }
 351  
 352      return $wporg_features;
 353  }
 354  
 355  /**
 356   * Retrieves theme installer pages from the WordPress.org Themes API.
 357   *
 358   * It is possible for a theme to override the Themes API result with three
 359   * filters. Assume this is for themes, which can extend on the Theme Info to
 360   * offer more choices. This is very powerful and must be used with care, when
 361   * overriding the filters.
 362   *
 363   * The first filter, {@see 'themes_api_args'}, is for the args and gives the action
 364   * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that
 365   * an object is returned.
 366   *
 367   * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org
 368   * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',
 369   * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed.
 370   *
 371   * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the
 372   * response object or array, depending on the `$action` type.
 373   *
 374   * Supported arguments per action:
 375   *
 376   * | Argument Name      | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list'   |
 377   * | -------------------| :------------: | :-----------------: | :--------: | :--------------: |
 378   * | `$slug`            | No             |  Yes                | No         | No               |
 379   * | `$per_page`        | Yes            |  No                 | No         | No               |
 380   * | `$page`            | Yes            |  No                 | No         | No               |
 381   * | `$number`          | No             |  No                 | Yes        | No               |
 382   * | `$search`          | Yes            |  No                 | No         | No               |
 383   * | `$tag`             | Yes            |  No                 | No         | No               |
 384   * | `$author`          | Yes            |  No                 | No         | No               |
 385   * | `$user`            | Yes            |  No                 | No         | No               |
 386   * | `$browse`          | Yes            |  No                 | No         | No               |
 387   * | `$locale`          | Yes            |  Yes                | No         | No               |
 388   * | `$fields`          | Yes            |  Yes                | No         | No               |
 389   *
 390   * @since 2.8.0
 391   *
 392   * @param string       $action API action to perform: 'query_themes', 'theme_information',
 393   *                             'hot_tags' or 'feature_list'.
 394   * @param array|object $args   {
 395   *     Optional. Array or object of arguments to serialize for the Themes API.
 396   *
 397   *     @type string  $slug     The theme slug. Default empty.
 398   *     @type int     $per_page Number of themes per page. Default 24.
 399   *     @type int     $page     Number of current page. Default 1.
 400   *     @type int     $number   Number of tags to be queried.
 401   *     @type string  $search   A search term. Default empty.
 402   *     @type string  $tag      Tag to filter themes. Default empty.
 403   *     @type string  $author   Username of an author to filter themes. Default empty.
 404   *     @type string  $user     Username to query for their favorites. Default empty.
 405   *     @type string  $browse   Browse view: 'featured', 'popular', 'updated', 'favorites'.
 406   *     @type string  $locale   Locale to provide context-sensitive results. Default is the value of get_locale().
 407   *     @type array   $fields   {
 408   *         Array of fields which should or should not be returned.
 409   *
 410   *         @type bool $description        Whether to return the theme full description. Default false.
 411   *         @type bool $sections           Whether to return the theme readme sections: description, installation,
 412   *                                        FAQ, screenshots, other notes, and changelog. Default false.
 413   *         @type bool $rating             Whether to return the rating in percent and total number of ratings.
 414   *                                        Default false.
 415   *         @type bool $ratings            Whether to return the number of rating for each star (1-5). Default false.
 416   *         @type bool $downloaded         Whether to return the download count. Default false.
 417   *         @type bool $downloadlink       Whether to return the download link for the package. Default false.
 418   *         @type bool $last_updated       Whether to return the date of the last update. Default false.
 419   *         @type bool $tags               Whether to return the assigned tags. Default false.
 420   *         @type bool $homepage           Whether to return the theme homepage link. Default false.
 421   *         @type bool $screenshots        Whether to return the screenshots. Default false.
 422   *         @type int  $screenshot_count   Number of screenshots to return. Default 1.
 423   *         @type bool $screenshot_url     Whether to return the URL of the first screenshot. Default false.
 424   *         @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false.
 425   *         @type bool $template           Whether to return the slug of the parent theme. Default false.
 426   *         @type bool $parent             Whether to return the slug, name and homepage of the parent theme. Default false.
 427   *         @type bool $versions           Whether to return the list of all available versions. Default false.
 428   *         @type bool $theme_url          Whether to return theme's URL. Default false.
 429   *         @type bool $extended_author    Whether to return nicename or nicename and display name. Default false.
 430   *     }
 431   * }
 432   * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
 433   *         {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article}
 434   *         for more information on the make-up of possible return objects depending on the value of `$action`.
 435   */
 436  function themes_api( $action, $args = array() ) {
 437      // Include an unmodified $wp_version.
 438      require  ABSPATH . WPINC . '/version.php';
 439  
 440      if ( is_array( $args ) ) {
 441          $args = (object) $args;
 442      }
 443  
 444      if ( 'query_themes' == $action ) {
 445          if ( ! isset( $args->per_page ) ) {
 446              $args->per_page = 24;
 447          }
 448      }
 449  
 450      if ( ! isset( $args->locale ) ) {
 451          $args->locale = get_user_locale();
 452      }
 453  
 454      if ( ! isset( $args->wp_version ) ) {
 455          $args->wp_version = substr( $wp_version, 0, 3 ); // x.y
 456      }
 457  
 458      /**
 459       * Filters arguments used to query for installer pages from the WordPress.org Themes API.
 460       *
 461       * Important: An object MUST be returned to this filter.
 462       *
 463       * @since 2.8.0
 464       *
 465       * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 466       * @param string $action Requested action. Likely values are 'theme_information',
 467       *                       'feature_list', or 'query_themes'.
 468       */
 469      $args = apply_filters( 'themes_api_args', $args, $action );
 470  
 471      /**
 472       * Filters whether to override the WordPress.org Themes API.
 473       *
 474       * Passing a non-false value will effectively short-circuit the WordPress.org API request.
 475       *
 476       * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
 477       * be passed. If `$action` is 'hot_tags', an array should be passed.
 478       *
 479       * @since 2.8.0
 480       *
 481       * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
 482       * @param string             $action   Requested action. Likely values are 'theme_information',
 483       *                                    'feature_list', or 'query_themes'.
 484       * @param object             $args     Arguments used to query for installer pages from the Themes API.
 485       */
 486      $res = apply_filters( 'themes_api', false, $action, $args );
 487  
 488      if ( ! $res ) {
 489          $url = 'http://api.wordpress.org/themes/info/1.2/';
 490          $url = add_query_arg(
 491              array(
 492                  'action'  => $action,
 493                  'request' => $args,
 494              ),
 495              $url
 496          );
 497  
 498          $http_url = $url;
 499          $ssl      = wp_http_supports( array( 'ssl' ) );
 500          if ( $ssl ) {
 501              $url = set_url_scheme( $url, 'https' );
 502          }
 503  
 504          $http_args = array(
 505              'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 506          );
 507          $request   = wp_remote_get( $url, $http_args );
 508  
 509          if ( $ssl && is_wp_error( $request ) ) {
 510              if ( ! wp_doing_ajax() ) {
 511                  trigger_error(
 512                      sprintf(
 513                          /* translators: %s: Support forums URL. */
 514                          __( '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>.' ),
 515                          __( 'https://wordpress.org/support/forums/' )
 516                      ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 517                      headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 518                  );
 519              }
 520              $request = wp_remote_get( $http_url, $http_args );
 521          }
 522  
 523          if ( is_wp_error( $request ) ) {
 524              $res = new WP_Error(
 525                  'themes_api_failed',
 526                  sprintf(
 527                      /* translators: %s: Support forums URL. */
 528                      __( '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>.' ),
 529                      __( 'https://wordpress.org/support/forums/' )
 530                  ),
 531                  $request->get_error_message()
 532              );
 533          } else {
 534              $res = json_decode( wp_remote_retrieve_body( $request ), true );
 535              if ( is_array( $res ) ) {
 536                  // Object casting is required in order to match the info/1.0 format.
 537                  $res = (object) $res;
 538              } elseif ( null === $res ) {
 539                  $res = new WP_Error(
 540                      'themes_api_failed',
 541                      sprintf(
 542                          /* translators: %s: Support forums URL. */
 543                          __( '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>.' ),
 544                          __( 'https://wordpress.org/support/forums/' )
 545                      ),
 546                      wp_remote_retrieve_body( $request )
 547                  );
 548              }
 549  
 550              if ( isset( $res->error ) ) {
 551                  $res = new WP_Error( 'themes_api_failed', $res->error );
 552              }
 553          }
 554  
 555          // Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects.
 556          if ( 'query_themes' == $action ) {
 557              foreach ( $res->themes as $i => $theme ) {
 558                  $res->themes[ $i ] = (object) $theme;
 559              }
 560          }
 561          // Back-compat for info/1.2 API, downgrade the feature_list result back to an array.
 562          if ( 'feature_list' == $action ) {
 563              $res = (array) $res;
 564          }
 565      }
 566  
 567      /**
 568       * Filters the returned WordPress.org Themes API response.
 569       *
 570       * @since 2.8.0
 571       *
 572       * @param array|object|WP_Error $res    WordPress.org Themes API response.
 573       * @param string                $action Requested action. Likely values are 'theme_information',
 574       *                                      'feature_list', or 'query_themes'.
 575       * @param object                $args   Arguments used to query for installer pages from the WordPress.org Themes API.
 576       */
 577      return apply_filters( 'themes_api_result', $res, $action, $args );
 578  }
 579  
 580  /**
 581   * Prepare themes for JavaScript.
 582   *
 583   * @since 3.8.0
 584   *
 585   * @param WP_Theme[] $themes Optional. Array of theme objects to prepare.
 586   *                           Defaults to all allowed themes.
 587   *
 588   * @return array An associative array of theme data, sorted by name.
 589   */
 590  function wp_prepare_themes_for_js( $themes = null ) {
 591      $current_theme = get_stylesheet();
 592  
 593      /**
 594       * Filters theme data before it is prepared for JavaScript.
 595       *
 596       * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
 597       * early with that value instead.
 598       *
 599       * @since 4.2.0
 600       *
 601       * @param array           $prepared_themes An associative array of theme data. Default empty array.
 602       * @param WP_Theme[]|null $themes          An array of theme objects to prepare, if any.
 603       * @param string          $current_theme   The current theme slug.
 604       */
 605      $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
 606  
 607      if ( ! empty( $prepared_themes ) ) {
 608          return $prepared_themes;
 609      }
 610  
 611      // Make sure the current theme is listed first.
 612      $prepared_themes[ $current_theme ] = array();
 613  
 614      if ( null === $themes ) {
 615          $themes = wp_get_themes( array( 'allowed' => true ) );
 616          if ( ! isset( $themes[ $current_theme ] ) ) {
 617              $themes[ $current_theme ] = wp_get_theme();
 618          }
 619      }
 620  
 621      $updates = array();
 622      if ( current_user_can( 'update_themes' ) ) {
 623          $updates_transient = get_site_transient( 'update_themes' );
 624          if ( isset( $updates_transient->response ) ) {
 625              $updates = $updates_transient->response;
 626          }
 627      }
 628  
 629      WP_Theme::sort_by_name( $themes );
 630  
 631      $parents = array();
 632  
 633      foreach ( $themes as $theme ) {
 634          $slug         = $theme->get_stylesheet();
 635          $encoded_slug = urlencode( $slug );
 636  
 637          $parent = false;
 638          if ( $theme->parent() ) {
 639              $parent           = $theme->parent();
 640              $parents[ $slug ] = $parent->get_stylesheet();
 641              $parent           = $parent->display( 'Name' );
 642          }
 643  
 644          $customize_action = null;
 645          if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
 646              $customize_action = esc_url(
 647                  add_query_arg(
 648                      array(
 649                          'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
 650                      ),
 651                      wp_customize_url( $slug )
 652                  )
 653              );
 654          }
 655  
 656          $prepared_themes[ $slug ] = array(
 657              'id'           => $slug,
 658              'name'         => $theme->display( 'Name' ),
 659              'screenshot'   => array( $theme->get_screenshot() ), // @todo Multiple screenshots.
 660              'description'  => $theme->display( 'Description' ),
 661              'author'       => $theme->display( 'Author', false, true ),
 662              'authorAndUri' => $theme->display( 'Author' ),
 663              'version'      => $theme->display( 'Version' ),
 664              'tags'         => $theme->display( 'Tags' ),
 665              'parent'       => $parent,
 666              'active'       => $slug === $current_theme,
 667              'hasUpdate'    => isset( $updates[ $slug ] ),
 668              'hasPackage'   => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
 669              'update'       => get_theme_update_available( $theme ),
 670              'actions'      => array(
 671                  'activate'  => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
 672                  'customize' => $customize_action,
 673                  'delete'    => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
 674              ),
 675          );
 676      }
 677  
 678      // Remove 'delete' action if theme has an active child.
 679      if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
 680          unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
 681      }
 682  
 683      /**
 684       * Filters the themes prepared for JavaScript, for themes.php.
 685       *
 686       * Could be useful for changing the order, which is by name by default.
 687       *
 688       * @since 3.8.0
 689       *
 690       * @param array $prepared_themes Array of theme data.
 691       */
 692      $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
 693      $prepared_themes = array_values( $prepared_themes );
 694      return array_filter( $prepared_themes );
 695  }
 696  
 697  /**
 698   * Print JS templates for the theme-browsing UI in the Customizer.
 699   *
 700   * @since 4.2.0
 701   */
 702  function customize_themes_print_templates() {
 703      ?>
 704      <script type="text/html" id="tmpl-customize-themes-details-view">
 705          <div class="theme-backdrop"></div>
 706          <div class="theme-wrap wp-clearfix" role="document">
 707              <div class="theme-header">
 708                  <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
 709                  <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
 710                  <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
 711              </div>
 712              <div class="theme-about wp-clearfix">
 713                  <div class="theme-screenshots">
 714                  <# if ( data.screenshot && data.screenshot[0] ) { #>
 715                      <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
 716                  <# } else { #>
 717                      <div class="screenshot blank"></div>
 718                  <# } #>
 719                  </div>
 720  
 721                  <div class="theme-info">
 722                      <# if ( data.active ) { #>
 723                          <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
 724                      <# } #>
 725                      <h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
 726                          <?php
 727                          /* translators: %s: Theme version. */
 728                          printf( __( 'Version: %s' ), '{{ data.version }}' );
 729                          ?>
 730                      </span></h2>
 731                      <h3 class="theme-author">
 732                          <?php
 733                          /* translators: %s: Theme author link. */
 734                          printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' );
 735                          ?>
 736                      </h3>
 737  
 738                      <# if ( data.stars && 0 != data.num_ratings ) { #>
 739                          <div class="theme-rating">
 740                              {{{ data.stars }}}
 741                              <a class="num-ratings" target="_blank" href="{{ data.reviews_url }}">
 742                                  <?php
 743                                  printf(
 744                                      '%1$s <span class="screen-reader-text">%2$s</span>',
 745                                      /* translators: %s: Number of ratings. */
 746                                      sprintf( __( '(%s ratings)' ), '{{ data.num_ratings }}' ),
 747                                      /* translators: Accessibility text. */
 748                                      __( '(opens in a new tab)' )
 749                                  );
 750                                  ?>
 751                              </a>
 752                          </div>
 753                      <# } #>
 754  
 755                      <# if ( data.hasUpdate ) { #>
 756                          <div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
 757                              <h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
 758                              {{{ data.update }}}
 759                          </div>
 760                      <# } #>
 761  
 762                      <# if ( data.parent ) { #>
 763                          <p class="parent-theme">
 764                              <?php
 765                              printf(
 766                                  /* translators: %s: Theme name. */
 767                                  __( 'This is a child theme of %s.' ),
 768                                  '<strong>{{{ data.parent }}}</strong>'
 769                              );
 770                              ?>
 771                          </p>
 772                      <# } #>
 773  
 774                      <p class="theme-description">{{{ data.description }}}</p>
 775  
 776                      <# if ( data.tags ) { #>
 777                          <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
 778                      <# } #>
 779                  </div>
 780              </div>
 781  
 782              <div class="theme-actions">
 783                  <# if ( data.active ) { #>
 784                      <button type="button" class="button button-primary customize-theme"><?php _e( 'Customize' ); ?></button>
 785                  <# } else if ( 'installed' === data.type ) { #>
 786                      <?php if ( current_user_can( 'delete_themes' ) ) { ?>
 787                          <# if ( data.actions && data.actions['delete'] ) { #>
 788                              <a href="{{{ data.actions['delete'] }}}" data-slug="{{ data.id }}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
 789                          <# } #>
 790                      <?php } ?>
 791                      <button type="button" class="button button-primary preview-theme" data-slug="{{ data.id }}"><?php _e( 'Live Preview' ); ?></button>
 792                  <# } else { #>
 793                      <button type="button" class="button theme-install" data-slug="{{ data.id }}"><?php _e( 'Install' ); ?></button>
 794                      <button type="button" class="button button-primary theme-install preview" data-slug="{{ data.id }}"><?php _e( 'Install &amp; Preview' ); ?></button>
 795                  <# } #>
 796              </div>
 797          </div>
 798      </script>
 799      <?php
 800  }
 801  
 802  /**
 803   * Determines whether a theme is technically active but was paused while
 804   * loading.
 805   *
 806   * For more information on this and similar theme functions, check out
 807   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 808   * Conditional Tags} article in the Theme Developer Handbook.
 809   *
 810   * @since 5.2.0
 811   *
 812   * @param string $theme Path to the theme directory relative to the themes directory.
 813   * @return bool True, if in the list of paused themes. False, not in the list.
 814   */
 815  function is_theme_paused( $theme ) {
 816      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
 817          return false;
 818      }
 819  
 820      if ( get_stylesheet() !== $theme && get_template() !== $theme ) {
 821          return false;
 822      }
 823  
 824      return array_key_exists( $theme, $GLOBALS['_paused_themes'] );
 825  }
 826  
 827  /**
 828   * Gets the error that was recorded for a paused theme.
 829   *
 830   * @since 5.2.0
 831   *
 832   * @param string $theme Path to the theme directory relative to the themes
 833   *                      directory.
 834   * @return array|false Array of error information as it was returned by
 835   *                     `error_get_last()`, or false if none was recorded.
 836   */
 837  function wp_get_theme_error( $theme ) {
 838      if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
 839          return false;
 840      }
 841  
 842      if ( ! array_key_exists( $theme, $GLOBALS['_paused_themes'] ) ) {
 843          return false;
 844      }
 845  
 846      return $GLOBALS['_paused_themes'][ $theme ];
 847  }
 848  
 849  /**
 850   * Tries to resume a single theme.
 851   *
 852   * If a redirect was provided and a functions.php file was found, we first ensure that
 853   * functions.php file does not throw fatal errors anymore.
 854   *
 855   * The way it works is by setting the redirection to the error before trying to
 856   * include the file. If the theme fails, then the redirection will not be overwritten
 857   * with the success message and the theme will not be resumed.
 858   *
 859   * @since 5.2.0
 860   *
 861   * @param string $theme    Single theme to resume.
 862   * @param string $redirect Optional. URL to redirect to. Default empty string.
 863   * @return bool|WP_Error True on success, false if `$theme` was not paused,
 864   *                       `WP_Error` on failure.
 865   */
 866  function resume_theme( $theme, $redirect = '' ) {
 867      list( $extension ) = explode( '/', $theme );
 868  
 869      /*
 870       * We'll override this later if the theme could be resumed without
 871       * creating a fatal error.
 872       */
 873      if ( ! empty( $redirect ) ) {
 874          $functions_path = '';
 875          if ( strpos( STYLESHEETPATH, $extension ) ) {
 876              $functions_path = STYLESHEETPATH . '/functions.php';
 877          } elseif ( strpos( TEMPLATEPATH, $extension ) ) {
 878              $functions_path = TEMPLATEPATH . '/functions.php';
 879          }
 880  
 881          if ( ! empty( $functions_path ) ) {
 882              wp_redirect(
 883                  add_query_arg(
 884                      '_error_nonce',
 885                      wp_create_nonce( 'theme-resume-error_' . $theme ),
 886                      $redirect
 887                  )
 888              );
 889  
 890              // Load the theme's functions.php to test whether it throws a fatal error.
 891              ob_start();
 892              if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
 893                  define( 'WP_SANDBOX_SCRAPING', true );
 894              }
 895              include $functions_path;
 896              ob_clean();
 897          }
 898      }
 899  
 900      $result = wp_paused_themes()->delete( $extension );
 901  
 902      if ( ! $result ) {
 903          return new WP_Error(
 904              'could_not_resume_theme',
 905              __( 'Could not resume the theme.' )
 906          );
 907      }
 908  
 909      return true;
 910  }
 911  
 912  /**
 913   * Renders an admin notice in case some themes have been paused due to errors.
 914   *
 915   * @since 5.2.0
 916   */
 917  function paused_themes_notice() {
 918      if ( 'themes.php' === $GLOBALS['pagenow'] ) {
 919          return;
 920      }
 921  
 922      if ( ! current_user_can( 'resume_themes' ) ) {
 923          return;
 924      }
 925  
 926      if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
 927          return;
 928      }
 929  
 930      printf(
 931          '<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>',
 932          __( 'One or more themes failed to load properly.' ),
 933          __( 'You can find more details and make changes on the Themes screen.' ),
 934          esc_url( admin_url( 'themes.php' ) ),
 935          __( 'Go to the Themes screen' )
 936      );
 937  }


Generated: Tue Apr 7 01:00:07 2020 Cross-referenced by PHPXref 0.7.1