]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/theme.php
WordPress 4.6.3
[autoinstalls/wordpress.git] / wp-admin / includes / theme.php
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 Subclass
15  *
16  * @param string $stylesheet Stylesheet of the theme to delete
17  * @param string $redirect Redirect to page when complete.
18  * @return void|bool|WP_Error When void, echoes content.
19  */
20 function delete_theme($stylesheet, $redirect = '') {
21         global $wp_filesystem;
22
23         if ( empty($stylesheet) )
24                 return false;
25
26         if ( empty( $redirect ) ) {
27                 $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet);
28         }
29
30         ob_start();
31         $credentials = request_filesystem_credentials( $redirect );
32         $data = ob_get_clean();
33
34         if ( false === $credentials ) {
35                 if ( ! empty( $data ) ){
36                         include_once( ABSPATH . 'wp-admin/admin-header.php');
37                         echo $data;
38                         include( ABSPATH . 'wp-admin/admin-footer.php');
39                         exit;
40                 }
41                 return;
42         }
43
44         if ( ! WP_Filesystem( $credentials ) ) {
45                 ob_start();
46                 request_filesystem_credentials( $redirect, '', true ); // Failed to connect, Error and request again.
47                 $data = ob_get_clean();
48
49                 if ( ! empty($data) ) {
50                         include_once( ABSPATH . 'wp-admin/admin-header.php');
51                         echo $data;
52                         include( ABSPATH . 'wp-admin/admin-footer.php');
53                         exit;
54                 }
55                 return;
56         }
57
58         if ( ! is_object($wp_filesystem) )
59                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
60
61         if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
62                 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
63
64         // Get the base plugin folder.
65         $themes_dir = $wp_filesystem->wp_themes_dir();
66         if ( empty( $themes_dir ) ) {
67                 return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
68         }
69
70         $themes_dir = trailingslashit( $themes_dir );
71         $theme_dir = trailingslashit( $themes_dir . $stylesheet );
72         $deleted = $wp_filesystem->delete( $theme_dir, true );
73
74         if ( ! $deleted ) {
75                 return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
76         }
77
78         $theme_translations = wp_get_installed_translations( 'themes' );
79
80         // Remove language files, silently.
81         if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
82                 $translations = $theme_translations[ $stylesheet ];
83
84                 foreach ( $translations as $translation => $data ) {
85                         $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
86                         $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
87                 }
88         }
89
90         // Remove the theme from allowed themes on the network.
91         if ( is_multisite() ) {
92                 WP_Theme::network_disable_theme( $stylesheet );
93         }
94
95         // Force refresh of theme update information.
96         delete_site_transient( 'update_themes' );
97
98         return true;
99 }
100
101 /**
102  * Get the Page Templates available in this theme
103  *
104  * @since 1.5.0
105  *
106  * @param WP_Post|null $post Optional. The post being edited, provided for context.
107  * @return array Key is the template name, value is the filename of the template
108  */
109 function get_page_templates( $post = null ) {
110         return array_flip( wp_get_theme()->get_page_templates( $post ) );
111 }
112
113 /**
114  * Tidies a filename for url display by the theme editor.
115  *
116  * @since 2.9.0
117  * @access private
118  *
119  * @param string $fullpath Full path to the theme file
120  * @param string $containingfolder Path of the theme parent folder
121  * @return string
122  */
123 function _get_template_edit_filename($fullpath, $containingfolder) {
124         return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
125 }
126
127 /**
128  * Check if there is an update for a theme available.
129  *
130  * Will display link, if there is an update available.
131  *
132  * @since 2.7.0
133  * @see get_theme_update_available()
134  *
135  * @param WP_Theme $theme Theme data object.
136  */
137 function theme_update_available( $theme ) {
138         echo get_theme_update_available( $theme );
139 }
140
141 /**
142  * Retrieve the update link if there is a theme update available.
143  *
144  * Will return a link if there is an update available.
145  *
146  * @since 3.8.0
147  *
148  * @staticvar object $themes_update
149  *
150  * @param WP_Theme $theme WP_Theme object.
151  * @return false|string HTML for the update link, or false if invalid info was passed.
152  */
153 function get_theme_update_available( $theme ) {
154         static $themes_update = null;
155
156         if ( !current_user_can('update_themes' ) )
157                 return false;
158
159         if ( !isset($themes_update) )
160                 $themes_update = get_site_transient('update_themes');
161
162         if ( ! ( $theme instanceof WP_Theme ) ) {
163                 return false;
164         }
165
166         $stylesheet = $theme->get_stylesheet();
167
168         $html = '';
169
170         if ( isset($themes_update->response[ $stylesheet ]) ) {
171                 $update = $themes_update->response[ $stylesheet ];
172                 $theme_name = $theme->display('Name');
173                 $details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']); //Theme browser inside WP? replace this, Also, theme preview JS will override this on the available list.
174                 $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
175
176                 if ( !is_multisite() ) {
177                         if ( ! current_user_can('update_themes') ) {
178                                 /* translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number */
179                                 $html = sprintf( '<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>',
180                                         $theme_name,
181                                         esc_url( $details_url ),
182                                         sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
183                                                 /* translators: 1: theme name, 2: version number */
184                                                 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
185                                         ),
186                                         $update['new_version']
187                                 );
188                         } elseif ( empty( $update['package'] ) ) {
189                                 /* translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number */
190                                 $html = sprintf( '<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>',
191                                         $theme_name,
192                                         esc_url( $details_url ),
193                                         sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
194                                                 /* translators: 1: theme name, 2: version number */
195                                                 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
196                                         ),
197                                         $update['new_version']
198                                 );
199                         } else {
200                                 /* translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number, 5: update URL, 6: additional link attributes */
201                                 $html = sprintf( '<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>',
202                                         $theme_name,
203                                         esc_url( $details_url ),
204                                         sprintf( '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                                         $update_url,
210                                         sprintf( 'aria-label="%s" id="update-theme" data-slug="%s"',
211                                                 /* translators: %s: theme name */
212                                                 esc_attr( sprintf( __( 'Update %s now' ), $theme_name ) ),
213                                                 $stylesheet
214                                         )
215                                 );
216                         }
217                 }
218         }
219
220         return $html;
221 }
222
223 /**
224  * Retrieve list of WordPress theme features (aka theme tags)
225  *
226  * @since 3.1.0
227  *
228  * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true.
229  * @return array Array of features keyed by category with translations keyed by slug.
230  */
231 function get_theme_feature_list( $api = true ) {
232         // Hard-coded list is used if api not accessible.
233         $features = array(
234
235                 __( 'Layout' ) => array(
236                         'grid-layout'   => __( 'Grid Layout' ),
237                         'one-column'    => __( 'One Column' ),
238                         'two-columns'   => __( 'Two Columns' ),
239                         'three-columns' => __( 'Three Columns' ),
240                         'four-columns'  => __( 'Four Columns' ),
241                         'left-sidebar'  => __( 'Left Sidebar' ),
242                         'right-sidebar' => __( 'Right Sidebar' ),
243                 ),
244
245                 __( 'Features' ) => array(
246                         'accessibility-ready'   => __( 'Accessibility Ready' ),
247                         'buddypress'            => __( 'BuddyPress' ),
248                         'custom-background'     => __( 'Custom Background' ),
249                         'custom-colors'         => __( 'Custom Colors' ),
250                         'custom-header'         => __( 'Custom Header' ),
251                         'custom-logo'           => __( 'Custom Logo' ),
252                         'custom-menu'           => __( 'Custom Menu' ),
253                         'editor-style'          => __( 'Editor Style' ),
254                         'featured-image-header' => __( 'Featured Image Header' ),
255                         'featured-images'       => __( 'Featured Images' ),
256                         'flexible-header'       => __( 'Flexible Header' ),
257                         'footer-widgets'        => __( 'Footer Widgets' ),
258                         'front-page-post-form'  => __( 'Front Page Posting' ),
259                         'full-width-template'   => __( 'Full Width Template' ),
260                         'microformats'          => __( 'Microformats' ),
261                         'post-formats'          => __( 'Post Formats' ),
262                         'rtl-language-support'  => __( 'RTL Language Support' ),
263                         'sticky-post'           => __( 'Sticky Post' ),
264                         'theme-options'         => __( 'Theme Options' ),
265                         'threaded-comments'     => __( 'Threaded Comments' ),
266                         'translation-ready'     => __( 'Translation Ready' ),
267                 ),
268
269                 __( 'Subject' )  => array(
270                         'blog'           => __( 'Blog' ),
271                         'e-commerce'     => __( 'E-Commerce' ),
272                         'education'      => __( 'Education' ),
273                         'entertainment'  => __( 'Entertainment' ),
274                         'food-and-drink' => __( 'Food & Drink' ),
275                         'holiday'        => __( 'Holiday' ),
276                         'news'           => __( 'News' ),
277                         'photography'    => __( 'Photography' ),
278                         'portfolio'      => __( 'Portfolio' ),
279                 )
280         );
281
282         if ( ! $api || ! current_user_can( 'install_themes' ) )
283                 return $features;
284
285         if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
286                 set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
287
288         if ( !$feature_list ) {
289                 $feature_list = themes_api( 'feature_list', array() );
290                 if ( is_wp_error( $feature_list ) )
291                         return $features;
292         }
293
294         if ( !$feature_list )
295                 return $features;
296
297         set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
298
299         $category_translations = array(
300                 'Layout'   => __( 'Layout' ),
301                 'Features' => __( 'Features' ),
302                 'Subject'  => __( 'Subject' ),
303         );
304
305         // Loop over the wporg canonical list and apply translations
306         $wporg_features = array();
307         foreach ( (array) $feature_list as $feature_category => $feature_items ) {
308                 if ( isset($category_translations[$feature_category]) )
309                         $feature_category = $category_translations[$feature_category];
310                 $wporg_features[$feature_category] = array();
311
312                 foreach ( $feature_items as $feature ) {
313                         if ( isset($features[$feature_category][$feature]) )
314                                 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
315                         else
316                                 $wporg_features[$feature_category][$feature] = $feature;
317                 }
318         }
319
320         return $wporg_features;
321 }
322
323 /**
324  * Retrieves theme installer pages from the WordPress.org Themes API.
325  *
326  * It is possible for a theme to override the Themes API result with three
327  * filters. Assume this is for themes, which can extend on the Theme Info to
328  * offer more choices. This is very powerful and must be used with care, when
329  * overriding the filters.
330  *
331  * The first filter, {@see 'themes_api_args'}, is for the args and gives the action
332  * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that
333  * an object is returned.
334  *
335  * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org
336  * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',
337  * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed.
338  *
339  * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the
340  * response object or array, depending on the `$action` type.
341  *
342  * Supported arguments per action:
343  *
344  * | Argument Name      | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list'   |
345  * | -------------------| :------------: | :-----------------: | :--------: | :--------------: |
346  * | `$slug`            | No             |  Yes                | No         | No               |
347  * | `$per_page`        | Yes            |  No                 | No         | No               |
348  * | `$page`            | Yes            |  No                 | No         | No               |
349  * | `$number`          | No             |  No                 | Yes        | No               |
350  * | `$search`          | Yes            |  No                 | No         | No               |
351  * | `$tag`             | Yes            |  No                 | No         | No               |
352  * | `$author`          | Yes            |  No                 | No         | No               |
353  * | `$user`            | Yes            |  No                 | No         | No               |
354  * | `$browse`          | Yes            |  No                 | No         | No               |
355  * | `$locale`          | Yes            |  Yes                | No         | No               |
356  * | `$fields`          | Yes            |  Yes                | No         | No               |
357  *
358  * @since 2.8.0
359  *
360  * @param string       $action API action to perform: 'query_themes', 'theme_information',
361  *                             'hot_tags' or 'feature_list'.
362  * @param array|object $args   {
363  *     Optional. Array or object of arguments to serialize for the Plugin Info API.
364  *
365  *     @type string  $slug     The plugin slug. Default empty.
366  *     @type int     $per_page Number of themes per page. Default 24.
367  *     @type int     $page     Number of current page. Default 1.
368  *     @type int     $number   Number of tags to be queried.
369  *     @type string  $search   A search term. Default empty.
370  *     @type string  $tag      Tag to filter themes. Default empty.
371  *     @type string  $author   Username of an author to filter themes. Default empty.
372  *     @type string  $user     Username to query for their favorites. Default empty.
373  *     @type string  $browse   Browse view: 'featured', 'popular', 'updated', 'favorites'.
374  *     @type string  $locale   Locale to provide context-sensitive results. Default is the value of get_locale().
375  *     @type array   $fields   {
376  *         Array of fields which should or should not be returned.
377  *
378  *         @type bool $description        Whether to return the theme full description. Default false.
379  *         @type bool $sections           Whether to return the theme readme sections: description, installation,
380  *                                        FAQ, screenshots, other notes, and changelog. Default false.
381  *         @type bool $rating             Whether to return the rating in percent and total number of ratings.
382  *                                        Default false.
383  *         @type bool $ratings            Whether to return the number of rating for each star (1-5). Default false.
384  *         @type bool $downloaded         Whether to return the download count. Default false.
385  *         @type bool $downloadlink       Whether to return the download link for the package. Default false.
386  *         @type bool $last_updated       Whether to return the date of the last update. Default false.
387  *         @type bool $tags               Whether to return the assigned tags. Default false.
388  *         @type bool $homepage           Whether to return the theme homepage link. Default false.
389  *         @type bool $screenshots        Whether to return the screenshots. Default false.
390  *         @type int  $screenshot_count   Number of screenshots to return. Default 1.
391  *         @type bool $screenshot_url     Whether to return the URL of the first screenshot. Default false.
392  *         @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false.
393  *         @type bool $template           Whether to return the slug of the parent theme. Default false.
394  *         @type bool $parent             Whether to return the slug, name and homepage of the parent theme. Default false.
395  *         @type bool $versions           Whether to return the list of all available versions. Default false.
396  *         @type bool $theme_url          Whether to return theme's URL. Default false.
397  *         @type bool $extended_author    Whether to return nicename or nicename and display name. Default false.
398  *     }
399  * }
400  * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
401  *         {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article}
402  *         for more information on the make-up of possible return objects depending on the value of `$action`.
403  */
404 function themes_api( $action, $args = array() ) {
405
406         if ( is_array( $args ) ) {
407                 $args = (object) $args;
408         }
409
410         if ( ! isset( $args->per_page ) ) {
411                 $args->per_page = 24;
412         }
413
414         if ( ! isset( $args->locale ) ) {
415                 $args->locale = get_locale();
416         }
417
418         /**
419          * Filters arguments used to query for installer pages from the WordPress.org Themes API.
420          *
421          * Important: An object MUST be returned to this filter.
422          *
423          * @since 2.8.0
424          *
425          * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
426          * @param string $action Requested action. Likely values are 'theme_information',
427          *                       'feature_list', or 'query_themes'.
428          */
429         $args = apply_filters( 'themes_api_args', $args, $action );
430
431         /**
432          * Filters whether to override the WordPress.org Themes API.
433          *
434          * Passing a non-false value will effectively short-circuit the WordPress.org API request.
435          *
436          * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
437          * be passed. If `$action` is 'hot_tags', an array should be passed.
438          *
439          * @since 2.8.0
440          *
441          * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
442          * @param string             $action   Requested action. Likely values are 'theme_information',
443          *                                    'feature_list', or 'query_themes'.
444          * @param object             $args     Arguments used to query for installer pages from the Themes API.
445          */
446         $res = apply_filters( 'themes_api', false, $action, $args );
447
448         if ( ! $res ) {
449                 $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
450                 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
451                         $url = set_url_scheme( $url, 'https' );
452
453                 $http_args = array(
454                         'body' => array(
455                                 'action' => $action,
456                                 'request' => serialize( $args )
457                         )
458                 );
459                 $request = wp_remote_post( $url, $http_args );
460
461                 if ( $ssl && is_wp_error( $request ) ) {
462                         if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
463                                 trigger_error( __( '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="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
464                         }
465                         $request = wp_remote_post( $http_url, $http_args );
466                 }
467
468                 if ( is_wp_error($request) ) {
469                         $res = new WP_Error('themes_api_failed', __( '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="https://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
470                 } else {
471                         $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
472                         if ( ! is_object( $res ) && ! is_array( $res ) )
473                                 $res = new WP_Error('themes_api_failed', __( '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="https://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
474                 }
475         }
476
477         /**
478          * Filters the returned WordPress.org Themes API response.
479          *
480          * @since 2.8.0
481          *
482          * @param array|object|WP_Error $res    WordPress.org Themes API response.
483          * @param string                $action Requested action. Likely values are 'theme_information',
484          *                                      'feature_list', or 'query_themes'.
485          * @param object                $args   Arguments used to query for installer pages from the WordPress.org Themes API.
486          */
487         return apply_filters( 'themes_api_result', $res, $action, $args );
488 }
489
490 /**
491  * Prepare themes for JavaScript.
492  *
493  * @since 3.8.0
494  *
495  * @param array $themes Optional. Array of WP_Theme objects to prepare.
496  *                      Defaults to all allowed themes.
497  *
498  * @return array An associative array of theme data, sorted by name.
499  */
500 function wp_prepare_themes_for_js( $themes = null ) {
501         $current_theme = get_stylesheet();
502
503         /**
504          * Filters theme data before it is prepared for JavaScript.
505          *
506          * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
507          * early with that value instead.
508          *
509          * @since 4.2.0
510          *
511          * @param array      $prepared_themes An associative array of theme data. Default empty array.
512          * @param null|array $themes          An array of WP_Theme objects to prepare, if any.
513          * @param string     $current_theme   The current theme slug.
514          */
515         $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
516
517         if ( ! empty( $prepared_themes ) ) {
518                 return $prepared_themes;
519         }
520
521         // Make sure the current theme is listed first.
522         $prepared_themes[ $current_theme ] = array();
523
524         if ( null === $themes ) {
525                 $themes = wp_get_themes( array( 'allowed' => true ) );
526                 if ( ! isset( $themes[ $current_theme ] ) ) {
527                         $themes[ $current_theme ] = wp_get_theme();
528                 }
529         }
530
531         $updates = array();
532         if ( current_user_can( 'update_themes' ) ) {
533                 $updates_transient = get_site_transient( 'update_themes' );
534                 if ( isset( $updates_transient->response ) ) {
535                         $updates = $updates_transient->response;
536                 }
537         }
538
539         WP_Theme::sort_by_name( $themes );
540
541         $parents = array();
542
543         foreach ( $themes as $theme ) {
544                 $slug = $theme->get_stylesheet();
545                 $encoded_slug = urlencode( $slug );
546
547                 $parent = false;
548                 if ( $theme->parent() ) {
549                         $parent = $theme->parent()->display( 'Name' );
550                         $parents[ $slug ] = $theme->parent()->get_stylesheet();
551                 }
552
553                 $customize_action = null;
554                 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
555                         $customize_action = esc_url( add_query_arg(
556                                 array(
557                                         'return' => urlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ),
558                                 ),
559                                 wp_customize_url( $slug )
560                         ) );
561                 }
562
563                 $prepared_themes[ $slug ] = array(
564                         'id'           => $slug,
565                         'name'         => $theme->display( 'Name' ),
566                         'screenshot'   => array( $theme->get_screenshot() ), // @todo multiple
567                         'description'  => $theme->display( 'Description' ),
568                         'author'       => $theme->display( 'Author', false, true ),
569                         'authorAndUri' => $theme->display( 'Author' ),
570                         'version'      => $theme->display( 'Version' ),
571                         'tags'         => $theme->display( 'Tags' ),
572                         'parent'       => $parent,
573                         'active'       => $slug === $current_theme,
574                         'hasUpdate'    => isset( $updates[ $slug ] ),
575                         'update'       => get_theme_update_available( $theme ),
576                         'actions'      => array(
577                                 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
578                                 'customize' => $customize_action,
579                                 'delete'   => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
580                         ),
581                 );
582         }
583
584         // Remove 'delete' action if theme has an active child
585         if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
586                 unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
587         }
588
589         /**
590          * Filters the themes prepared for JavaScript, for themes.php.
591          *
592          * Could be useful for changing the order, which is by name by default.
593          *
594          * @since 3.8.0
595          *
596          * @param array $prepared_themes Array of themes.
597          */
598         $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
599         $prepared_themes = array_values( $prepared_themes );
600         return array_filter( $prepared_themes );
601 }
602
603 /**
604  * Print JS templates for the theme-browsing UI in the Customizer.
605  *
606  * @since 4.2.0
607  */
608 function customize_themes_print_templates() {
609         $preview_url = esc_url( add_query_arg( 'theme', '__THEME__' ) ); // Token because esc_url() strips curly braces.
610         $preview_url = str_replace( '__THEME__', '{{ data.id }}', $preview_url );
611         ?>
612         <script type="text/html" id="tmpl-customize-themes-details-view">
613                 <div class="theme-backdrop"></div>
614                 <div class="theme-wrap wp-clearfix">
615                         <div class="theme-header">
616                                 <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
617                                 <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
618                                 <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
619                         </div>
620                         <div class="theme-about wp-clearfix">
621                                 <div class="theme-screenshots">
622                                 <# if ( data.screenshot[0] ) { #>
623                                         <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
624                                 <# } else { #>
625                                         <div class="screenshot blank"></div>
626                                 <# } #>
627                                 </div>
628
629                                 <div class="theme-info">
630                                         <# if ( data.active ) { #>
631                                                 <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
632                                         <# } #>
633                                         <h2 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h2>
634                                         <h3 class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></h3>
635                                         <p class="theme-description">{{{ data.description }}}</p>
636
637                                         <# if ( data.parent ) { #>
638                                                 <p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p>
639                                         <# } #>
640
641                                         <# if ( data.tags ) { #>
642                                                 <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{ data.tags }}</p>
643                                         <# } #>
644                                 </div>
645                         </div>
646
647                         <# if ( ! data.active ) { #>
648                                 <div class="theme-actions">
649                                         <div class="inactive-theme">
650                                                 <?php
651                                                 /* translators: %s: Theme name */
652                                                 $aria_label = sprintf( __( 'Preview %s' ), '{{ data.name }}' );
653                                                 ?>
654                                                 <a href="<?php echo $preview_url; ?>" target="_top" class="button button-primary" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Live Preview' ); ?></a>
655                                         </div>
656                                 </div>
657                         <# } #>
658                 </div>
659         </script>
660         <?php
661 }