3 * WordPress Theme Administration API
6 * @subpackage Administration
10 * {@internal Missing Short Description}}
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();
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'];
45 * @param string $template Template directory of the theme to delete
46 * @param string $redirect Redirect to page when complete.
49 function delete_theme($template, $redirect = '') {
50 global $wp_filesystem;
52 if ( empty($template) )
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();
61 if ( ! empty($data) ){
62 include_once( ABSPATH . 'wp-admin/admin-header.php');
64 include( ABSPATH . 'wp-admin/admin-footer.php');
70 if ( ! WP_Filesystem($credentials) ) {
71 request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
72 $data = ob_get_contents();
74 if ( ! empty($data) ) {
75 include_once( ABSPATH . 'wp-admin/admin-header.php');
77 include( ABSPATH . 'wp-admin/admin-footer.php');
84 if ( ! is_object($wp_filesystem) )
85 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
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);
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.'));
95 $themes_dir = trailingslashit( $themes_dir );
96 $theme_dir = trailingslashit($themes_dir . $template);
97 $deleted = $wp_filesystem->delete($theme_dir, true);
100 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
102 // Force refresh of theme update information
103 delete_site_transient('update_themes');
109 * {@internal Missing Short Description}}
115 function get_broken_themes() {
116 global $wp_broken_themes;
119 return $wp_broken_themes;
123 * Get the allowed themes for the current blog.
128 * @uses current_theme_info()
129 * @uses get_site_allowed_themes()
130 * @uses wpmu_get_blog_allowedthemes
132 * @return array $themes Array of allowed themes.
134 function get_allowed_themes() {
135 if ( !is_multisite() )
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();
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 );
148 if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
149 $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
152 foreach ( $themes as $key => $theme ) {
153 if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
154 unset( $themes[ $key ] );
162 * Get the Page Templates available in this theme
166 * @return array Key is template name, Value is template name
168 function get_page_templates() {
169 $themes = get_themes();
170 $theme = get_current_theme();
171 $templates = $themes[$theme]['Template Files'];
172 $page_templates = array();
174 if ( is_array( $templates ) ) {
175 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
177 foreach ( $templates as $template ) {
178 $basename = str_replace($base, '', $template);
180 // don't allow template files in subdirectories
181 if ( false !== strpos($basename, '/') )
184 $template_data = implode( '', file( $template ));
187 if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
188 $name = _cleanup_header_comment($name[1]);
190 if ( !empty( $name ) ) {
191 $page_templates[trim( $name )] = $basename;
196 return $page_templates;
200 * Tidies a filename for url display by the theme editor.
205 * @param string $fullpath Full path to the theme file
206 * @param string $containingfolder Path of the theme parent folder
209 function _get_template_edit_filename($fullpath, $containingfolder) {
210 return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
214 * Check if there is an update for a theme available.
216 * Will display link, if there is an update available.
220 * @param object $theme Theme data object.
221 * @return bool False if no valid info was passed.
223 function theme_update_available( $theme ) {
224 static $themes_update;
226 if ( !current_user_can('update_themes' ) )
229 if ( !isset($themes_update) )
230 $themes_update = get_site_transient('update_themes');
232 if ( is_object($theme) && isset($theme->stylesheet) )
233 $stylesheet = $theme->stylesheet;
234 elseif ( is_array($theme) && isset($theme['Stylesheet']) )
235 $stylesheet = $theme['Stylesheet'];
237 return false; //No valid info passed.
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&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;"';
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']);
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 );
258 * Retrieve list of WordPress theme features (aka theme tags)
262 * @return array Array of features keyed by category with translations keyed by slug.
264 function get_theme_feature_list() {
265 // Hard-coded list is used if api not accessible.
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 ')
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' )
293 __('Width') => array(
294 'fixed-width' => __( 'Fixed Width' ),
295 'flexible-width' => __( 'Flexible Width' )
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' )
315 __( 'Subject' ) => array(
316 'holiday' => __( 'Holiday' ),
317 'photoblogging' => __( 'Photoblogging' ),
318 'seasonal' => __( 'Seasonal' )
322 if ( !current_user_can('install_themes') )
325 if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
326 set_site_transient( 'wporg_theme_feature_list', array( ), 10800);
328 if ( !$feature_list ) {
329 $feature_list = themes_api( 'feature_list', array( ) );
330 if ( is_wp_error( $feature_list ) )
334 if ( !$feature_list )
337 set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
339 $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
340 'Features' => __('Features'), 'Subject' => __('Subject') );
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();
349 foreach ( $feature_items as $feature ) {
350 if ( isset($features[$feature_category][$feature]) )
351 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
353 $wporg_features[$feature_category][$feature] = $feature;
357 return $wporg_features;
361 * Retrieve theme installer pages from WordPress Themes API.
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.
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.
372 * The second filter, 'themes_api', is the result that would be returned.
376 * @param string $action
377 * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
380 function themes_api($action, $args = null) {
382 if ( is_array($args) )
383 $args = (object)$args;
385 if ( !isset($args->per_page) )
386 $args->per_page = 24;
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.
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() );
396 $res = unserialize($request['body']);
398 $res = new WP_Error('themes_api_failed', __('An unknown error occurred.'), $request['body']);
401 //var_dump(array($args, $res));
402 return apply_filters('themes_api_result', $res, $action, $args);