]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/theme.php
Wordpress 3.1.4
[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  * {@internal Missing Short Description}}
11  *
12  * @since 2.0.0
13  *
14  * @return unknown
15  */
16 function current_theme_info() {
17         $themes = get_themes();
18         $current_theme = get_current_theme();
19         if ( ! isset( $themes[$current_theme] ) ) {
20                 delete_option( 'current_theme' );
21                 $current_theme = get_current_theme();
22         }
23         $ct->name = $current_theme;
24         $ct->title = $themes[$current_theme]['Title'];
25         $ct->version = $themes[$current_theme]['Version'];
26         $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
27         $ct->template_dir = $themes[$current_theme]['Template Dir'];
28         $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
29         $ct->template = $themes[$current_theme]['Template'];
30         $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
31         $ct->screenshot = $themes[$current_theme]['Screenshot'];
32         $ct->description = $themes[$current_theme]['Description'];
33         $ct->author = $themes[$current_theme]['Author'];
34         $ct->tags = $themes[$current_theme]['Tags'];
35         $ct->theme_root = $themes[$current_theme]['Theme Root'];
36         $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI'];
37         return $ct;
38 }
39
40 /**
41  * Remove a theme
42  *
43  * @since 2.8.0
44  *
45  * @param string $template Template directory of the theme to delete
46  * @param string $redirect Redirect to page when complete.
47  * @return mixed
48  */
49 function delete_theme($template, $redirect = '') {
50         global $wp_filesystem;
51
52         if ( empty($template) )
53                 return false;
54
55         ob_start();
56         if ( empty( $redirect ) )
57                 $redirect = wp_nonce_url('themes.php?action=delete&template=' . $template, 'delete-theme_' . $template);
58         if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
59                 $data = ob_get_contents();
60                 ob_end_clean();
61                 if ( ! empty($data) ){
62                         include_once( ABSPATH . 'wp-admin/admin-header.php');
63                         echo $data;
64                         include( ABSPATH . 'wp-admin/admin-footer.php');
65                         exit;
66                 }
67                 return;
68         }
69
70         if ( ! WP_Filesystem($credentials) ) {
71                 request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
72                 $data = ob_get_contents();
73                 ob_end_clean();
74                 if ( ! empty($data) ) {
75                         include_once( ABSPATH . 'wp-admin/admin-header.php');
76                         echo $data;
77                         include( ABSPATH . 'wp-admin/admin-footer.php');
78                         exit;
79                 }
80                 return;
81         }
82
83
84         if ( ! is_object($wp_filesystem) )
85                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
86
87         if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
88                 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
89
90         //Get the base plugin folder
91         $themes_dir = $wp_filesystem->wp_themes_dir();
92         if ( empty($themes_dir) )
93                 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
94
95         $themes_dir = trailingslashit( $themes_dir );
96         $theme_dir = trailingslashit($themes_dir . $template);
97         $deleted = $wp_filesystem->delete($theme_dir, true);
98
99         if ( ! $deleted )
100                 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
101
102         // Force refresh of theme update information
103         delete_site_transient('update_themes');
104
105         return true;
106 }
107
108 /**
109  * {@internal Missing Short Description}}
110  *
111  * @since 1.5.0
112  *
113  * @return unknown
114  */
115 function get_broken_themes() {
116         global $wp_broken_themes;
117
118         get_themes();
119         return $wp_broken_themes;
120 }
121
122 /**
123  * Get the allowed themes for the current blog.
124  *
125  * @since 3.0.0
126  *
127  * @uses get_themes()
128  * @uses current_theme_info()
129  * @uses get_site_allowed_themes()
130  * @uses wpmu_get_blog_allowedthemes
131  *
132  * @return array $themes Array of allowed themes.
133  */
134 function get_allowed_themes() {
135         if ( !is_multisite() )
136                 return get_themes();
137
138         $themes = get_themes();
139         $ct = current_theme_info();
140         $allowed_themes = apply_filters("allowed_themes", get_site_allowed_themes() );
141         if ( $allowed_themes == false )
142                 $allowed_themes = array();
143
144         $blog_allowed_themes = wpmu_get_blog_allowedthemes();
145         if ( is_array( $blog_allowed_themes ) )
146                 $allowed_themes = array_merge( $allowed_themes, $blog_allowed_themes );
147
148         if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
149                 $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
150
151         reset( $themes );
152         foreach ( $themes as $key => $theme ) {
153                 if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
154                         unset( $themes[ $key ] );
155         }
156         reset( $themes );
157
158         return $themes;
159 }
160
161 /**
162  * Get the Page Templates available in this theme
163  *
164  * @since 1.5.0
165  *
166  * @return array Key is template name, Value is template name
167  */
168 function get_page_templates() {
169         $themes = get_themes();
170         $theme = get_current_theme();
171         $templates = $themes[$theme]['Template Files'];
172         $page_templates = array();
173
174         if ( is_array( $templates ) ) {
175                 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
176
177                 foreach ( $templates as $template ) {
178                         $basename = str_replace($base, '', $template);
179
180                         // don't allow template files in subdirectories
181                         if ( false !== strpos($basename, '/') )
182                                 continue;
183
184                         $template_data = implode( '', file( $template ));
185
186                         $name = '';
187                         if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
188                                 $name = _cleanup_header_comment($name[1]);
189
190                         if ( !empty( $name ) ) {
191                                 $page_templates[trim( $name )] = $basename;
192                         }
193                 }
194         }
195
196         return $page_templates;
197 }
198
199 /**
200  * Tidies a filename for url display by the theme editor.
201  *
202  * @since 2.9.0
203  * @access private
204  *
205  * @param string $fullpath Full path to the theme file
206  * @param string $containingfolder Path of the theme parent folder
207  * @return string
208  */
209 function _get_template_edit_filename($fullpath, $containingfolder) {
210         return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
211 }
212
213 /**
214  * Check if there is an update for a theme available.
215  *
216  * Will display link, if there is an update available.
217  *
218  * @since 2.7.0
219  *
220  * @param object $theme Theme data object.
221  * @return bool False if no valid info was passed.
222  */
223 function theme_update_available( $theme ) {
224         static $themes_update;
225
226         if ( !current_user_can('update_themes' ) )
227                 return;
228
229         if ( !isset($themes_update) )
230                 $themes_update = get_site_transient('update_themes');
231
232         if ( is_object($theme) && isset($theme->stylesheet) )
233                 $stylesheet = $theme->stylesheet;
234         elseif ( is_array($theme) && isset($theme['Stylesheet']) )
235                 $stylesheet = $theme['Stylesheet'];
236         else
237                 return false; //No valid info passed.
238
239         if ( isset($themes_update->response[ $stylesheet ]) ) {
240                 $update = $themes_update->response[ $stylesheet ];
241                 $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
242                 $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.
243                 $update_url = wp_nonce_url('update.php?action=upgrade-theme&amp;theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
244                 $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;"';
245
246                 if ( !is_multisite() ) {
247                         if ( ! current_user_can('update_themes') )
248                                 printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
249                         else if ( empty($update['package']) )
250                                 printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>. <em>Automatic update is unavailable for this theme.</em>') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
251                         else
252                                 printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a> or <a href="%4$s" %5$s>update automatically</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );
253                 }
254         }
255 }
256
257 /**
258  * Retrieve list of WordPress theme features (aka theme tags)
259  *
260  * @since 3.1.0
261  *
262  * @return array  Array of features keyed by category with translations keyed by slug.
263  */
264 function get_theme_feature_list() {
265         // Hard-coded list is used if api not accessible.
266         $features = array(
267                         __('Colors') => array(
268                                 'black'   => __( 'Black' ),
269                                 'blue'    => __( 'Blue' ),
270                                 'brown'   => __( 'Brown' ),
271                                 'green'   => __( 'Green' ),
272                                 'orange'  => __( 'Orange' ),
273                                 'pink'    => __( 'Pink' ),
274                                 'purple'  => __( 'Purple' ),
275                                 'red'     => __( 'Red' ),
276                                 'silver'  => __( 'Silver' ),
277                                 'tan'     => __( 'Tan' ),
278                                 'white'   => __( 'White' ),
279                                 'yellow'  => __( 'Yellow' ),
280                                 'dark'    => __( 'Dark' ),
281                                 'light'   => __( 'Light ')
282                         ),
283
284                 __('Columns') => array(
285                         'one-column'    => __( 'One Column' ),
286                         'two-columns'   => __( 'Two Columns' ),
287                         'three-columns' => __( 'Three Columns' ),
288                         'four-columns'  => __( 'Four Columns' ),
289                         'left-sidebar'  => __( 'Left Sidebar' ),
290                         'right-sidebar' => __( 'Right Sidebar' )
291                 ),
292
293                 __('Width') => array(
294                         'fixed-width'    => __( 'Fixed Width' ),
295                         'flexible-width' => __( 'Flexible Width' )
296                 ),
297
298                 __( 'Features' ) => array(
299                         'blavatar'             => __( 'Blavatar' ),
300                         'buddypress'           => __( 'BuddyPress' ),
301                         'custom-background'    => __( 'Custom Background' ),
302                         'custom-colors'        => __( 'Custom Colors' ),
303                         'custom-header'        => __( 'Custom Header' ),
304                         'custom-menu'          => __( 'Custom Menu' ),
305                         'editor-style'         => __( 'Editor Style' ),
306                         'front-page-post-form' => __( 'Front Page Posting' ),
307                         'microformats'         => __( 'Microformats' ),
308                         'sticky-post'          => __( 'Sticky Post' ),
309                         'theme-options'        => __( 'Theme Options' ),
310                         'threaded-comments'    => __( 'Threaded Comments' ),
311                         'translation-ready'    => __( 'Translation Ready' ),
312                         'rtl-language-support' => __( 'RTL Language Support' )
313                 ),
314
315                 __( 'Subject' )  => array(
316                         'holiday' => __( 'Holiday' ),
317                         'photoblogging' => __( 'Photoblogging' ),
318                         'seasonal' => __( 'Seasonal' )
319                 )
320         );
321
322         if ( !current_user_can('install_themes') )
323                 return $features;
324
325         if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
326                 set_site_transient( 'wporg_theme_feature_list', array( ),  10800);
327
328         if ( !$feature_list ) {
329                 $feature_list = themes_api( 'feature_list', array( ) );
330                 if ( is_wp_error( $feature_list ) )
331                         return $features;
332         }
333
334         if ( !$feature_list )
335                 return $features;
336
337         set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
338
339         $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
340                                                                    'Features' => __('Features'), 'Subject' => __('Subject') );
341
342         // Loop over the wporg canonical list and apply translations
343         $wporg_features = array();
344         foreach ( (array) $feature_list as $feature_category => $feature_items ) {
345                 if ( isset($category_translations[$feature_category]) )
346                         $feature_category = $category_translations[$feature_category];
347                 $wporg_features[$feature_category] = array();
348
349                 foreach ( $feature_items as $feature ) {
350                         if ( isset($features[$feature_category][$feature]) )
351                                 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
352                         else
353                                 $wporg_features[$feature_category][$feature] = $feature;
354                 }
355         }
356
357         return $wporg_features;
358 }
359
360 /**
361  * Retrieve theme installer pages from WordPress Themes API.
362  *
363  * It is possible for a theme to override the Themes API result with three
364  * filters. Assume this is for themes, which can extend on the Theme Info to
365  * offer more choices. This is very powerful and must be used with care, when
366  * overridding the filters.
367  *
368  * The first filter, 'themes_api_args', is for the args and gives the action as
369  * the second parameter. The hook for 'themes_api_args' must ensure that an
370  * object is returned.
371  *
372  * The second filter, 'themes_api', is the result that would be returned.
373  *
374  * @since 2.8.0
375  *
376  * @param string $action
377  * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
378  * @return mixed
379  */
380 function themes_api($action, $args = null) {
381
382         if ( is_array($args) )
383                 $args = (object)$args;
384
385         if ( !isset($args->per_page) )
386                 $args->per_page = 24;
387
388         $args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
389         $res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
390
391         if ( ! $res ) {
392                 $request = wp_remote_post('http://api.wordpress.org/themes/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
393                 if ( is_wp_error($request) ) {
394                         $res = new WP_Error('themes_api_failed', __('An Unexpected HTTP Error occurred during the API request.'), $request->get_error_message() );
395                 } else {
396                         $res = unserialize($request['body']);
397                         if ( ! $res )
398                         $res = new WP_Error('themes_api_failed', __('An unknown error occurred.'), $request['body']);
399                 }
400         }
401         //var_dump(array($args, $res));
402         return apply_filters('themes_api_result', $res, $action, $args);
403 }
404
405 ?>