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