[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Aug 20 08:20:01 2019 Cross-referenced by PHPXref 0.7