]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/theme.php
WordPress 4.1.1-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  * @param string $stylesheet Stylesheet of the theme to delete
15  * @param string $redirect Redirect to page when complete.
16  * @return mixed
17  */
18 function delete_theme($stylesheet, $redirect = '') {
19         global $wp_filesystem;
20
21         if ( empty($stylesheet) )
22                 return false;
23
24         ob_start();
25         if ( empty( $redirect ) )
26                 $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet);
27         if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
28                 $data = ob_get_contents();
29                 ob_end_clean();
30                 if ( ! empty($data) ){
31                         include_once( ABSPATH . 'wp-admin/admin-header.php');
32                         echo $data;
33                         include( ABSPATH . 'wp-admin/admin-footer.php');
34                         exit;
35                 }
36                 return;
37         }
38
39         if ( ! WP_Filesystem($credentials) ) {
40                 request_filesystem_credentials($redirect, '', true); // Failed to connect, Error and request again
41                 $data = ob_get_contents();
42                 ob_end_clean();
43                 if ( ! empty($data) ) {
44                         include_once( ABSPATH . 'wp-admin/admin-header.php');
45                         echo $data;
46                         include( ABSPATH . 'wp-admin/admin-footer.php');
47                         exit;
48                 }
49                 return;
50         }
51
52         if ( ! is_object($wp_filesystem) )
53                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
54
55         if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
56                 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
57
58         // Get the base plugin folder.
59         $themes_dir = $wp_filesystem->wp_themes_dir();
60         if ( empty( $themes_dir ) ) {
61                 return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
62         }
63
64         $themes_dir = trailingslashit( $themes_dir );
65         $theme_dir = trailingslashit( $themes_dir . $stylesheet );
66         $deleted = $wp_filesystem->delete( $theme_dir, true );
67
68         if ( ! $deleted ) {
69                 return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
70         }
71
72         $translations_dir = $wp_filesystem->wp_lang_dir();
73         $translations_dir = trailingslashit( $translations_dir );
74
75         $theme_translations = wp_get_installed_translations( 'themes' );
76
77         // Remove language files, silently.
78         if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
79                 $translations = $theme_translations[ $stylesheet ];
80
81                 foreach ( $translations as $translation => $data ) {
82                         $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
83                         $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
84                 }
85         }
86
87         // Force refresh of theme update information.
88         delete_site_transient( 'update_themes' );
89
90         return true;
91 }
92
93 /**
94  * Get the Page Templates available in this theme
95  *
96  * @since 1.5.0
97  *
98  * @param WP_Post|null $post Optional. The post being edited, provided for context.
99  * @return array Key is the template name, value is the filename of the template
100  */
101 function get_page_templates( $post = null ) {
102         return array_flip( wp_get_theme()->get_page_templates( $post ) );
103 }
104
105 /**
106  * Tidies a filename for url display by the theme editor.
107  *
108  * @since 2.9.0
109  * @access private
110  *
111  * @param string $fullpath Full path to the theme file
112  * @param string $containingfolder Path of the theme parent folder
113  * @return string
114  */
115 function _get_template_edit_filename($fullpath, $containingfolder) {
116         return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
117 }
118
119 /**
120  * Check if there is an update for a theme available.
121  *
122  * Will display link, if there is an update available.
123  *
124  * @since 2.7.0
125  * @see get_theme_update_available()
126  *
127  * @param object $theme Theme data object.
128  */
129 function theme_update_available( $theme ) {
130         echo get_theme_update_available( $theme );
131 }
132
133 /**
134  * Retrieve the update link if there is a theme update available.
135  *
136  * Will return a link if there is an update available.
137  *
138  * @since 3.8.0
139  *
140  * @param WP_Theme $theme WP_Theme object.
141  * @return false|string HTML for the update link, or false if invalid info was passed.
142  */
143 function get_theme_update_available( $theme ) {
144         static $themes_update;
145
146         if ( !current_user_can('update_themes' ) )
147                 return false;
148
149         if ( !isset($themes_update) )
150                 $themes_update = get_site_transient('update_themes');
151
152         if ( ! is_a( $theme, 'WP_Theme' ) )
153                 return false;
154
155         $stylesheet = $theme->get_stylesheet();
156
157         $html = '';
158
159         if ( isset($themes_update->response[ $stylesheet ]) ) {
160                 $update = $themes_update->response[ $stylesheet ];
161                 $theme_name = $theme->display('Name');
162                 $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.
163                 $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
164                 $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;"';
165
166                 if ( !is_multisite() ) {
167                         if ( ! current_user_can('update_themes') ) {
168                                 $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>',
169                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
170                         } else if ( empty( $update['package'] ) ) {
171                                 $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>',
172                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
173                         } else {
174                                 $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>',
175                                         $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'], $update_url, $update_onclick );
176                         }
177                 }
178         }
179
180         return $html;
181 }
182
183 /**
184  * Retrieve list of WordPress theme features (aka theme tags)
185  *
186  * @since 3.1.0
187  *
188  * @param bool $api Optional. Whether try to fetch tags from the WP.org API. Defaults to true.
189  * @return array Array of features keyed by category with translations keyed by slug.
190  */
191 function get_theme_feature_list( $api = true ) {
192         // Hard-coded list is used if api not accessible.
193         $features = array(
194                         __( 'Colors' ) => array(
195                                 'black'   => __( 'Black' ),
196                                 'blue'    => __( 'Blue' ),
197                                 'brown'   => __( 'Brown' ),
198                                 'gray'    => __( 'Gray' ),
199                                 'green'   => __( 'Green' ),
200                                 'orange'  => __( 'Orange' ),
201                                 'pink'    => __( 'Pink' ),
202                                 'purple'  => __( 'Purple' ),
203                                 'red'     => __( 'Red' ),
204                                 'silver'  => __( 'Silver' ),
205                                 'tan'     => __( 'Tan' ),
206                                 'white'   => __( 'White' ),
207                                 'yellow'  => __( 'Yellow' ),
208                                 'dark'    => __( 'Dark' ),
209                                 'light'   => __( 'Light' ),
210                         ),
211
212                 __( 'Layout' ) => array(
213                         'fixed-layout'      => __( 'Fixed Layout' ),
214                         'fluid-layout'      => __( 'Fluid Layout' ),
215                         'responsive-layout' => __( 'Responsive Layout' ),
216                         'one-column'    => __( 'One Column' ),
217                         'two-columns'   => __( 'Two Columns' ),
218                         'three-columns' => __( 'Three Columns' ),
219                         'four-columns'  => __( 'Four Columns' ),
220                         'left-sidebar'  => __( 'Left Sidebar' ),
221                         'right-sidebar' => __( 'Right Sidebar' ),
222                 ),
223
224                 __( 'Features' ) => array(
225                         'accessibility-ready'   => __( 'Accessibility Ready' ),
226                         'blavatar'              => __( 'Blavatar' ),
227                         'buddypress'            => __( 'BuddyPress' ),
228                         'custom-background'     => __( 'Custom Background' ),
229                         'custom-colors'         => __( 'Custom Colors' ),
230                         'custom-header'         => __( 'Custom Header' ),
231                         'custom-menu'           => __( 'Custom Menu' ),
232                         'editor-style'          => __( 'Editor Style' ),
233                         'featured-image-header' => __( 'Featured Image Header' ),
234                         'featured-images'       => __( 'Featured Images' ),
235                         'flexible-header'       => __( 'Flexible Header' ),
236                         'front-page-post-form'  => __( 'Front Page Posting' ),
237                         'full-width-template'   => __( 'Full Width Template' ),
238                         'microformats'          => __( 'Microformats' ),
239                         'post-formats'          => __( 'Post Formats' ),
240                         'rtl-language-support'  => __( 'RTL Language Support' ),
241                         'sticky-post'           => __( 'Sticky Post' ),
242                         'theme-options'         => __( 'Theme Options' ),
243                         'threaded-comments'     => __( 'Threaded Comments' ),
244                         'translation-ready'     => __( 'Translation Ready' ),
245                 ),
246
247                 __( 'Subject' )  => array(
248                         'holiday'       => __( 'Holiday' ),
249                         'photoblogging' => __( 'Photoblogging' ),
250                         'seasonal'      => __( 'Seasonal' ),
251                 )
252         );
253
254         if ( ! $api || ! current_user_can( 'install_themes' ) )
255                 return $features;
256
257         if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
258                 set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
259
260         if ( !$feature_list ) {
261                 $feature_list = themes_api( 'feature_list', array() );
262                 if ( is_wp_error( $feature_list ) )
263                         return $features;
264         }
265
266         if ( !$feature_list )
267                 return $features;
268
269         set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
270
271         $category_translations = array(
272                 'Colors'   => __( 'Colors' ),
273                 'Layout'   => __( 'Layout' ),
274                 'Features' => __( 'Features' ),
275                 'Subject'  => __( 'Subject' )
276         );
277
278         // Loop over the wporg canonical list and apply translations
279         $wporg_features = array();
280         foreach ( (array) $feature_list as $feature_category => $feature_items ) {
281                 if ( isset($category_translations[$feature_category]) )
282                         $feature_category = $category_translations[$feature_category];
283                 $wporg_features[$feature_category] = array();
284
285                 foreach ( $feature_items as $feature ) {
286                         if ( isset($features[$feature_category][$feature]) )
287                                 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
288                         else
289                                 $wporg_features[$feature_category][$feature] = $feature;
290                 }
291         }
292
293         return $wporg_features;
294 }
295
296 /**
297  * Retrieve theme installer pages from WordPress Themes API.
298  *
299  * It is possible for a theme to override the Themes API result with three
300  * filters. Assume this is for themes, which can extend on the Theme Info to
301  * offer more choices. This is very powerful and must be used with care, when
302  * overriding the filters.
303  *
304  * The first filter, 'themes_api_args', is for the args and gives the action as
305  * the second parameter. The hook for 'themes_api_args' must ensure that an
306  * object is returned.
307  *
308  * The second filter, 'themes_api', is the result that would be returned.
309  *
310  * @since 2.8.0
311  *
312  * @param string       $action The requested action. Likely values are 'theme_information',
313  *                             'feature_list', or 'query_themes'.
314  * @param array|object $args   Optional. Arguments to serialize for the Theme Info API.
315  * @return mixed
316  */
317 function themes_api( $action, $args = null ) {
318
319         if ( is_array( $args ) ) {
320                 $args = (object) $args;
321         }
322
323         if ( ! isset( $args->per_page ) ) {
324                 $args->per_page = 24;
325         }
326
327         if ( ! isset( $args->locale ) ) {
328                 $args->locale = get_locale();
329         }
330
331         /**
332          * Filter arguments used to query for installer pages from the WordPress.org Themes API.
333          *
334          * Important: An object MUST be returned to this filter.
335          *
336          * @since 2.8.0
337          *
338          * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
339          * @param string $action Requested action. Likely values are 'theme_information',
340          *                       'feature_list', or 'query_themes'.
341          */
342         $args = apply_filters( 'themes_api_args', $args, $action );
343
344         /**
345          * Filter whether to override the WordPress.org Themes API.
346          *
347          * Returning a value of true to this filter allows a theme to completely
348          * override the built-in WordPress.org API.
349          *
350          * @since 2.8.0
351          *
352          * @param bool   $bool   Whether to override the WordPress.org Themes API. Default false.
353          * @param string $action Requested action. Likely values are 'theme_information',
354          *                       'feature_list', or 'query_themes'.
355          * @param object $args   Arguments used to query for installer pages from the Themes API.
356          */
357         $res = apply_filters( 'themes_api', false, $action, $args );
358
359         if ( ! $res ) {
360                 $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
361                 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
362                         $url = set_url_scheme( $url, 'https' );
363
364                 $args = array(
365                         'body' => array(
366                                 'action' => $action,
367                                 'request' => serialize( $args )
368                         )
369                 );
370                 $request = wp_remote_post( $url, $args );
371
372                 if ( $ssl && is_wp_error( $request ) ) {
373                         if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
374                                 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 );
375                         }
376                         $request = wp_remote_post( $http_url, $args );
377                 }
378
379                 if ( is_wp_error($request) ) {
380                         $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() );
381                 } else {
382                         $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
383                         if ( ! is_object( $res ) && ! is_array( $res ) )
384                                 $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 ) );
385                 }
386         }
387
388         /**
389          * Filter the returned WordPress.org Themes API response.
390          *
391          * @since 2.8.0
392          *
393          * @param array|object $res    WordPress.org Themes API response.
394          * @param string       $action Requested action. Likely values are 'theme_information',
395          *                             'feature_list', or 'query_themes'.
396          * @param object       $args   Arguments used to query for installer pages from the WordPress.org Themes API.
397          */
398         return apply_filters( 'themes_api_result', $res, $action, $args );
399 }
400
401 /**
402  * Prepare themes for JavaScript.
403  *
404  * @since 3.8.0
405  *
406  * @param array $themes Optional. Array of WP_Theme objects to prepare.
407  *                      Defaults to all allowed themes.
408  *
409  * @return array An associative array of theme data, sorted by name.
410  */
411 function wp_prepare_themes_for_js( $themes = null ) {
412         $current_theme = get_stylesheet();
413
414         // Make sure the current theme is listed first.
415         $prepared_themes = array( $current_theme => array() );
416
417         if ( null === $themes ) {
418                 $themes = wp_get_themes( array( 'allowed' => true ) );
419                 if ( ! isset( $themes[ $current_theme ] ) ) {
420                         $themes[ $current_theme ] = wp_get_theme();
421                 }
422         }
423
424         $updates = array();
425         if ( current_user_can( 'update_themes' ) ) {
426                 $updates_transient = get_site_transient( 'update_themes' );
427                 if ( isset( $updates_transient->response ) ) {
428                         $updates = $updates_transient->response;
429                 }
430         }
431
432         WP_Theme::sort_by_name( $themes );
433         foreach ( $themes as $theme ) {
434                 $slug = $theme->get_stylesheet();
435                 $encoded_slug = urlencode( $slug );
436
437                 $parent = false;
438                 if ( $theme->parent() ) {
439                         $parent = $theme->parent()->display( 'Name' );
440                         $parents[ $slug ] = $theme->parent()->get_stylesheet();
441                 }
442
443                 $prepared_themes[ $slug ] = array(
444                         'id'           => $slug,
445                         'name'         => $theme->display( 'Name' ),
446                         'screenshot'   => array( $theme->get_screenshot() ), // @todo multiple
447                         'description'  => $theme->display( 'Description' ),
448                         'author'       => $theme->display( 'Author', false, true ),
449                         'authorAndUri' => $theme->display( 'Author' ),
450                         'version'      => $theme->display( 'Version' ),
451                         'tags'         => $theme->display( 'Tags' ),
452                         'parent'       => $parent,
453                         'active'       => $slug === $current_theme,
454                         'hasUpdate'    => isset( $updates[ $slug ] ),
455                         'update'       => get_theme_update_available( $theme ),
456                         'actions'      => array(
457                                 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
458                                 'customize' => ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) ? wp_customize_url( $slug ) : null,
459                                 'preview'   => add_query_arg( array(
460                                         'preview'        => 1,
461                                         'template'       => urlencode( $theme->get_template() ),
462                                         'stylesheet'     => urlencode( $slug ),
463                                         'preview_iframe' => true,
464                                         'TB_iframe'      => true,
465                                 ), home_url( '/' ) ),
466                                 'delete'   => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
467                         ),
468                 );
469         }
470
471         // Remove 'delete' action if theme has an active child
472         if ( isset( $parents ) && array_key_exists( $current_theme, $parents ) ) {
473                 unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
474         }
475
476         /**
477          * Filter the themes prepared for JavaScript, for themes.php.
478          *
479          * Could be useful for changing the order, which is by name by default.
480          *
481          * @since 3.8.0
482          *
483          * @param array $prepared_themes Array of themes.
484          */
485         $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
486         return array_values( $prepared_themes );
487 }