Wordpress 3.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  * {@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 the template name, value is the filename of the template
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                         if ( 'functions.php' == $basename )
185                                 continue;
186
187                         $template_data = implode( '', file( $template ));
188
189                         $name = '';
190                         if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
191                                 $name = _cleanup_header_comment($name[1]);
192
193                         if ( !empty( $name ) ) {
194                                 $page_templates[trim( $name )] = $basename;
195                         }
196                 }
197         }
198
199         return $page_templates;
200 }
201
202 /**
203  * Tidies a filename for url display by the theme editor.
204  *
205  * @since 2.9.0
206  * @access private
207  *
208  * @param string $fullpath Full path to the theme file
209  * @param string $containingfolder Path of the theme parent folder
210  * @return string
211  */
212 function _get_template_edit_filename($fullpath, $containingfolder) {
213         return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
214 }
215
216 /**
217  * Check if there is an update for a theme available.
218  *
219  * Will display link, if there is an update available.
220  *
221  * @since 2.7.0
222  *
223  * @param object $theme Theme data object.
224  * @return bool False if no valid info was passed.
225  */
226 function theme_update_available( $theme ) {
227         static $themes_update;
228
229         if ( !current_user_can('update_themes' ) )
230                 return;
231
232         if ( !isset($themes_update) )
233                 $themes_update = get_site_transient('update_themes');
234
235         if ( is_object($theme) && isset($theme->stylesheet) )
236                 $stylesheet = $theme->stylesheet;
237         elseif ( is_array($theme) && isset($theme['Stylesheet']) )
238                 $stylesheet = $theme['Stylesheet'];
239         else
240                 return false; //No valid info passed.
241
242         if ( isset($themes_update->response[ $stylesheet ]) ) {
243                 $update = $themes_update->response[ $stylesheet ];
244                 $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
245                 $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.
246                 $update_url = wp_nonce_url('update.php?action=upgrade-theme&amp;theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
247                 $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;"';
248
249                 if ( !is_multisite() ) {
250                         if ( ! current_user_can('update_themes') )
251                                 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']);
252                         else if ( empty($update['package']) )
253                                 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']);
254                         else
255                                 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 );
256                 }
257         }
258 }
259
260 /**
261  * Retrieve list of WordPress theme features (aka theme tags)
262  *
263  * @since 3.1.0
264  *
265  * @return array  Array of features keyed by category with translations keyed by slug.
266  */
267 function get_theme_feature_list() {
268         // Hard-coded list is used if api not accessible.
269         $features = array(
270                         __('Colors') => array(
271                                 'black'   => __( 'Black' ),
272                                 'blue'    => __( 'Blue' ),
273                                 'brown'   => __( 'Brown' ),
274                                 'gray'    => __( 'Gray' ),
275                                 'green'   => __( 'Green' ),
276                                 'orange'  => __( 'Orange' ),
277                                 'pink'    => __( 'Pink' ),
278                                 'purple'  => __( 'Purple' ),
279                                 'red'     => __( 'Red' ),
280                                 'silver'  => __( 'Silver' ),
281                                 'tan'     => __( 'Tan' ),
282                                 'white'   => __( 'White' ),
283                                 'yellow'  => __( 'Yellow' ),
284                                 'dark'    => __( 'Dark' ),
285                                 'light'   => __( 'Light ')
286                         ),
287
288                 __('Columns') => array(
289                         'one-column'    => __( 'One Column' ),
290                         'two-columns'   => __( 'Two Columns' ),
291                         'three-columns' => __( 'Three Columns' ),
292                         'four-columns'  => __( 'Four Columns' ),
293                         'left-sidebar'  => __( 'Left Sidebar' ),
294                         'right-sidebar' => __( 'Right Sidebar' )
295                 ),
296
297                 __('Width') => array(
298                         'fixed-width'    => __( 'Fixed Width' ),
299                         'flexible-width' => __( 'Flexible Width' )
300                 ),
301
302                 __( 'Features' ) => array(
303                         'blavatar'              => __( 'Blavatar' ),
304                         'buddypress'            => __( 'BuddyPress' ),
305                         'custom-background'     => __( 'Custom Background' ),
306                         'custom-colors'         => __( 'Custom Colors' ),
307                         'custom-header'         => __( 'Custom Header' ),
308                         'custom-menu'           => __( 'Custom Menu' ),
309                         'editor-style'          => __( 'Editor Style' ),
310                         'featured-image-header' => __( 'Featured Image Header' ),
311                         'featured-images'       => __( 'Featured Images' ),
312                         'front-page-post-form'  => __( 'Front Page Posting' ),
313                         'full-width-template'   => __( 'Full Width Template' ),
314                         'microformats'          => __( 'Microformats' ),
315                         'post-formats'          => __( 'Post Formats' ),
316                         'rtl-language-support'  => __( 'RTL Language Support' ),
317                         'sticky-post'           => __( 'Sticky Post' ),
318                         'theme-options'         => __( 'Theme Options' ),
319                         'threaded-comments'     => __( 'Threaded Comments' ),
320                         'translation-ready'     => __( 'Translation Ready' )
321                 ),
322
323                 __( 'Subject' )  => array(
324                         'holiday'       => __( 'Holiday' ),
325                         'photoblogging' => __( 'Photoblogging' ),
326                         'seasonal'      => __( 'Seasonal' )
327                 )
328         );
329
330         if ( !current_user_can('install_themes') )
331                 return $features;
332
333         if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
334                 set_site_transient( 'wporg_theme_feature_list', array( ),  10800);
335
336         if ( !$feature_list ) {
337                 $feature_list = themes_api( 'feature_list', array( ) );
338                 if ( is_wp_error( $feature_list ) )
339                         return $features;
340         }
341
342         if ( !$feature_list )
343                 return $features;
344
345         set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
346
347         $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
348                                                                    'Features' => __('Features'), 'Subject' => __('Subject') );
349
350         // Loop over the wporg canonical list and apply translations
351         $wporg_features = array();
352         foreach ( (array) $feature_list as $feature_category => $feature_items ) {
353                 if ( isset($category_translations[$feature_category]) )
354                         $feature_category = $category_translations[$feature_category];
355                 $wporg_features[$feature_category] = array();
356
357                 foreach ( $feature_items as $feature ) {
358                         if ( isset($features[$feature_category][$feature]) )
359                                 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
360                         else
361                                 $wporg_features[$feature_category][$feature] = $feature;
362                 }
363         }
364
365         return $wporg_features;
366 }
367
368 /**
369  * Retrieve theme installer pages from WordPress Themes API.
370  *
371  * It is possible for a theme to override the Themes API result with three
372  * filters. Assume this is for themes, which can extend on the Theme Info to
373  * offer more choices. This is very powerful and must be used with care, when
374  * overridding the filters.
375  *
376  * The first filter, 'themes_api_args', is for the args and gives the action as
377  * the second parameter. The hook for 'themes_api_args' must ensure that an
378  * object is returned.
379  *
380  * The second filter, 'themes_api', is the result that would be returned.
381  *
382  * @since 2.8.0
383  *
384  * @param string $action
385  * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
386  * @return mixed
387  */
388 function themes_api($action, $args = null) {
389
390         if ( is_array($args) )
391                 $args = (object)$args;
392
393         if ( !isset($args->per_page) )
394                 $args->per_page = 24;
395
396         $args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
397         $res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
398
399         if ( ! $res ) {
400                 $request = wp_remote_post('http://api.wordpress.org/themes/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
401                 if ( is_wp_error($request) ) {
402                         $res = new WP_Error('themes_api_failed', __('An Unexpected HTTP Error occurred during the API request.'), $request->get_error_message() );
403                 } else {
404                         $res = unserialize( wp_remote_retrieve_body( $request ) );
405                         if ( ! $res )
406                         $res = new WP_Error('themes_api_failed', __('An unknown error occurred.'), wp_remote_retrieve_body( $request ) );
407                 }
408         }
409         //var_dump(array($args, $res));
410         return apply_filters('themes_api_result', $res, $action, $args);
411 }
412
413 ?>