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