]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/theme.php
WordPress 4.3-scripts
[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                 $update_onclick = 'onclick="if ( confirm(\'' . esc_js( __("Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update.") ) . '\') ) {return true;}return false;"';
167
168                 if ( !is_multisite() ) {
169                         if ( ! current_user_can('update_themes') ) {
170                                 $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.' ) . '</strong></p>',
171                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
172                         } elseif ( empty( $update['package'] ) ) {
173                                 $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ) . '</strong></p>',
174                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
175                         } else {
176                                 $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.' ) . '</strong></p>',
177                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'], $update_url, $update_onclick );
178                         }
179                 }
180         }
181
182         return $html;
183 }
184
185 /**
186  * Retrieve list of WordPress theme features (aka theme tags)
187  *
188  * @since 3.1.0
189  *
190  * @param bool $api Optional. Whether try to fetch tags from the WP.org API. Defaults to true.
191  * @return array Array of features keyed by category with translations keyed by slug.
192  */
193 function get_theme_feature_list( $api = true ) {
194         // Hard-coded list is used if api not accessible.
195         $features = array(
196                         __( 'Colors' ) => array(
197                                 'black'   => __( 'Black' ),
198                                 'blue'    => __( 'Blue' ),
199                                 'brown'   => __( 'Brown' ),
200                                 'gray'    => __( 'Gray' ),
201                                 'green'   => __( 'Green' ),
202                                 'orange'  => __( 'Orange' ),
203                                 'pink'    => __( 'Pink' ),
204                                 'purple'  => __( 'Purple' ),
205                                 'red'     => __( 'Red' ),
206                                 'silver'  => __( 'Silver' ),
207                                 'tan'     => __( 'Tan' ),
208                                 'white'   => __( 'White' ),
209                                 'yellow'  => __( 'Yellow' ),
210                                 'dark'    => __( 'Dark' ),
211                                 'light'   => __( 'Light' ),
212                         ),
213
214                 __( 'Layout' ) => array(
215                         'fixed-layout'      => __( 'Fixed Layout' ),
216                         'fluid-layout'      => __( 'Fluid Layout' ),
217                         'responsive-layout' => __( 'Responsive Layout' ),
218                         'one-column'    => __( 'One Column' ),
219                         'two-columns'   => __( 'Two Columns' ),
220                         'three-columns' => __( 'Three Columns' ),
221                         'four-columns'  => __( 'Four Columns' ),
222                         'left-sidebar'  => __( 'Left Sidebar' ),
223                         'right-sidebar' => __( 'Right Sidebar' ),
224                 ),
225
226                 __( 'Features' ) => array(
227                         'accessibility-ready'   => __( 'Accessibility Ready' ),
228                         'blavatar'              => __( 'Blavatar' ),
229                         'buddypress'            => __( 'BuddyPress' ),
230                         'custom-background'     => __( 'Custom Background' ),
231                         'custom-colors'         => __( 'Custom Colors' ),
232                         'custom-header'         => __( 'Custom Header' ),
233                         'custom-menu'           => __( 'Custom Menu' ),
234                         'editor-style'          => __( 'Editor Style' ),
235                         'featured-image-header' => __( 'Featured Image Header' ),
236                         'featured-images'       => __( 'Featured Images' ),
237                         'flexible-header'       => __( 'Flexible Header' ),
238                         'front-page-post-form'  => __( 'Front Page Posting' ),
239                         'full-width-template'   => __( 'Full Width Template' ),
240                         'microformats'          => __( 'Microformats' ),
241                         'post-formats'          => __( 'Post Formats' ),
242                         'rtl-language-support'  => __( 'RTL Language Support' ),
243                         'sticky-post'           => __( 'Sticky Post' ),
244                         'theme-options'         => __( 'Theme Options' ),
245                         'threaded-comments'     => __( 'Threaded Comments' ),
246                         'translation-ready'     => __( 'Translation Ready' ),
247                 ),
248
249                 __( 'Subject' )  => array(
250                         'holiday'       => __( 'Holiday' ),
251                         'photoblogging' => __( 'Photoblogging' ),
252                         'seasonal'      => __( 'Seasonal' ),
253                 )
254         );
255
256         if ( ! $api || ! current_user_can( 'install_themes' ) )
257                 return $features;
258
259         if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
260                 set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
261
262         if ( !$feature_list ) {
263                 $feature_list = themes_api( 'feature_list', array() );
264                 if ( is_wp_error( $feature_list ) )
265                         return $features;
266         }
267
268         if ( !$feature_list )
269                 return $features;
270
271         set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
272
273         $category_translations = array(
274                 'Colors'   => __( 'Colors' ),
275                 'Layout'   => __( 'Layout' ),
276                 'Features' => __( 'Features' ),
277                 'Subject'  => __( 'Subject' )
278         );
279
280         // Loop over the wporg canonical list and apply translations
281         $wporg_features = array();
282         foreach ( (array) $feature_list as $feature_category => $feature_items ) {
283                 if ( isset($category_translations[$feature_category]) )
284                         $feature_category = $category_translations[$feature_category];
285                 $wporg_features[$feature_category] = array();
286
287                 foreach ( $feature_items as $feature ) {
288                         if ( isset($features[$feature_category][$feature]) )
289                                 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
290                         else
291                                 $wporg_features[$feature_category][$feature] = $feature;
292                 }
293         }
294
295         return $wporg_features;
296 }
297
298 /**
299  * Retrieve theme installer pages from WordPress Themes API.
300  *
301  * It is possible for a theme to override the Themes API result with three
302  * filters. Assume this is for themes, which can extend on the Theme Info to
303  * offer more choices. This is very powerful and must be used with care, when
304  * overriding the filters.
305  *
306  * The first filter, 'themes_api_args', is for the args and gives the action as
307  * the second parameter. The hook for 'themes_api_args' must ensure that an
308  * object is returned.
309  *
310  * The second filter, 'themes_api', is the result that would be returned.
311  *
312  * @since 2.8.0
313  *
314  * @param string       $action The requested action. Likely values are 'theme_information',
315  *                             'feature_list', or 'query_themes'.
316  * @param array|object $args   Optional. Arguments to serialize for the Theme Info API.
317  * @return mixed
318  */
319 function themes_api( $action, $args = null ) {
320
321         if ( is_array( $args ) ) {
322                 $args = (object) $args;
323         }
324
325         if ( ! isset( $args->per_page ) ) {
326                 $args->per_page = 24;
327         }
328
329         if ( ! isset( $args->locale ) ) {
330                 $args->locale = get_locale();
331         }
332
333         /**
334          * Filter arguments used to query for installer pages from the WordPress.org Themes API.
335          *
336          * Important: An object MUST be returned to this filter.
337          *
338          * @since 2.8.0
339          *
340          * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
341          * @param string $action Requested action. Likely values are 'theme_information',
342          *                       'feature_list', or 'query_themes'.
343          */
344         $args = apply_filters( 'themes_api_args', $args, $action );
345
346         /**
347          * Filter whether to override the WordPress.org Themes API.
348          *
349          * Returning a value of true to this filter allows a theme to completely
350          * override the built-in WordPress.org API.
351          *
352          * @since 2.8.0
353          *
354          * @param bool   $bool   Whether to override the WordPress.org Themes API. Default false.
355          * @param string $action Requested action. Likely values are 'theme_information',
356          *                       'feature_list', or 'query_themes'.
357          * @param object $args   Arguments used to query for installer pages from the Themes API.
358          */
359         $res = apply_filters( 'themes_api', false, $action, $args );
360
361         if ( ! $res ) {
362                 $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
363                 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
364                         $url = set_url_scheme( $url, 'https' );
365
366                 $http_args = array(
367                         'body' => array(
368                                 'action' => $action,
369                                 'request' => serialize( $args )
370                         )
371                 );
372                 $request = wp_remote_post( $url, $http_args );
373
374                 if ( $ssl && is_wp_error( $request ) ) {
375                         if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
376                                 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 );
377                         }
378                         $request = wp_remote_post( $http_url, $http_args );
379                 }
380
381                 if ( is_wp_error($request) ) {
382                         $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() );
383                 } else {
384                         $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
385                         if ( ! is_object( $res ) && ! is_array( $res ) )
386                                 $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 ) );
387                 }
388         }
389
390         /**
391          * Filter the returned WordPress.org Themes API response.
392          *
393          * @since 2.8.0
394          *
395          * @param array|object $res    WordPress.org Themes API response.
396          * @param string       $action Requested action. Likely values are 'theme_information',
397          *                             'feature_list', or 'query_themes'.
398          * @param object       $args   Arguments used to query for installer pages from the WordPress.org Themes API.
399          */
400         return apply_filters( 'themes_api_result', $res, $action, $args );
401 }
402
403 /**
404  * Prepare themes for JavaScript.
405  *
406  * @since 3.8.0
407  *
408  * @param array $themes Optional. Array of WP_Theme objects to prepare.
409  *                      Defaults to all allowed themes.
410  *
411  * @return array An associative array of theme data, sorted by name.
412  */
413 function wp_prepare_themes_for_js( $themes = null ) {
414         $current_theme = get_stylesheet();
415
416         /**
417          * Filter theme data before it is prepared for JavaScript.
418          *
419          * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
420          * early with that value instead.
421          *
422          * @since 4.2.0
423          *
424          * @param array      $prepared_themes An associative array of theme data. Default empty array.
425          * @param null|array $themes          An array of WP_Theme objects to prepare, if any.
426          * @param string     $current_theme   The current theme slug.
427          */
428         $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
429
430         if ( ! empty( $prepared_themes ) ) {
431                 return $prepared_themes;
432         }
433
434         // Make sure the current theme is listed first.
435         $prepared_themes[ $current_theme ] = array();
436
437         if ( null === $themes ) {
438                 $themes = wp_get_themes( array( 'allowed' => true ) );
439                 if ( ! isset( $themes[ $current_theme ] ) ) {
440                         $themes[ $current_theme ] = wp_get_theme();
441                 }
442         }
443
444         $updates = array();
445         if ( current_user_can( 'update_themes' ) ) {
446                 $updates_transient = get_site_transient( 'update_themes' );
447                 if ( isset( $updates_transient->response ) ) {
448                         $updates = $updates_transient->response;
449                 }
450         }
451
452         WP_Theme::sort_by_name( $themes );
453
454         $parents = array();
455
456         foreach ( $themes as $theme ) {
457                 $slug = $theme->get_stylesheet();
458                 $encoded_slug = urlencode( $slug );
459
460                 $parent = false;
461                 if ( $theme->parent() ) {
462                         $parent = $theme->parent()->display( 'Name' );
463                         $parents[ $slug ] = $theme->parent()->get_stylesheet();
464                 }
465
466                 $prepared_themes[ $slug ] = array(
467                         'id'           => $slug,
468                         'name'         => $theme->display( 'Name' ),
469                         'screenshot'   => array( $theme->get_screenshot() ), // @todo multiple
470                         'description'  => $theme->display( 'Description' ),
471                         'author'       => $theme->display( 'Author', false, true ),
472                         'authorAndUri' => $theme->display( 'Author' ),
473                         'version'      => $theme->display( 'Version' ),
474                         'tags'         => $theme->display( 'Tags' ),
475                         'parent'       => $parent,
476                         'active'       => $slug === $current_theme,
477                         'hasUpdate'    => isset( $updates[ $slug ] ),
478                         'update'       => get_theme_update_available( $theme ),
479                         'actions'      => array(
480                                 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
481                                 'customize' => ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) ? wp_customize_url( $slug ) : null,
482                                 'delete'   => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
483                         ),
484                 );
485         }
486
487         // Remove 'delete' action if theme has an active child
488         if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
489                 unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
490         }
491
492         /**
493          * Filter the themes prepared for JavaScript, for themes.php.
494          *
495          * Could be useful for changing the order, which is by name by default.
496          *
497          * @since 3.8.0
498          *
499          * @param array $prepared_themes Array of themes.
500          */
501         $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
502         $prepared_themes = array_values( $prepared_themes );
503         return array_filter( $prepared_themes );
504 }
505
506 /**
507  * Print JS templates for the theme-browsing UI in the Customizer.
508  *
509  * @since 4.2.0
510  */
511 function customize_themes_print_templates() {
512         $preview_url = esc_url( add_query_arg( 'theme', '__THEME__' ) ); // Token because esc_url() strips curly braces.
513         $preview_url = str_replace( '__THEME__', '{{ data.id }}', $preview_url );
514         ?>
515         <script type="text/html" id="tmpl-customize-themes-details-view">
516                 <div class="theme-backdrop"></div>
517                 <div class="theme-wrap">
518                         <div class="theme-header">
519                                 <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
520                                 <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
521                                 <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
522                         </div>
523                         <div class="theme-about">
524                                 <div class="theme-screenshots">
525                                 <# if ( data.screenshot[0] ) { #>
526                                         <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
527                                 <# } else { #>
528                                         <div class="screenshot blank"></div>
529                                 <# } #>
530                                 </div>
531
532                                 <div class="theme-info">
533                                         <# if ( data.active ) { #>
534                                                 <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
535                                         <# } #>
536                                         <h3 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h3>
537                                         <h4 class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></h4>
538                                         <p class="theme-description">{{{ data.description }}}</p>
539
540                                         <# if ( data.parent ) { #>
541                                                 <p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p>
542                                         <# } #>
543
544                                         <# if ( data.tags ) { #>
545                                                 <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{ data.tags }}</p>
546                                         <# } #>
547                                 </div>
548                         </div>
549
550                         <# if ( ! data.active ) { #>
551                                 <div class="theme-actions">
552                                         <div class="inactive-theme">
553                                                 <a href="<?php echo $preview_url; ?>" target="_top" class="button button-primary"><?php _e( 'Live Preview' ); ?></a>
554                                         </div>
555                                 </div>
556                         <# } #>
557                 </div>
558         </script>
559         <?php
560 }