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();
22 $ct->name = $current_theme;
26 if ( ! isset( $themes[$current_theme] ) ) {
27 delete_option( 'current_theme' );
28 $current_theme = get_current_theme();
32 $ct->name = $current_theme;
33 $ct->title = $themes[$current_theme]['Title'];
34 $ct->version = $themes[$current_theme]['Version'];
35 $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
36 $ct->template_dir = $themes[$current_theme]['Template Dir'];
37 $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
38 $ct->template = $themes[$current_theme]['Template'];
39 $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
40 $ct->screenshot = $themes[$current_theme]['Screenshot'];
41 $ct->description = $themes[$current_theme]['Description'];
42 $ct->author = $themes[$current_theme]['Author'];
43 $ct->tags = $themes[$current_theme]['Tags'];
44 $ct->theme_root = $themes[$current_theme]['Theme Root'];
45 $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI'];
54 * @param string $template Template directory of the theme to delete
55 * @param string $redirect Redirect to page when complete.
58 function delete_theme($template, $redirect = '') {
59 global $wp_filesystem;
61 if ( empty($template) )
65 if ( empty( $redirect ) )
66 $redirect = wp_nonce_url('themes.php?action=delete&template=' . $template, 'delete-theme_' . $template);
67 if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
68 $data = ob_get_contents();
70 if ( ! empty($data) ){
71 include_once( ABSPATH . 'wp-admin/admin-header.php');
73 include( ABSPATH . 'wp-admin/admin-footer.php');
79 if ( ! WP_Filesystem($credentials) ) {
80 request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
81 $data = ob_get_contents();
83 if ( ! empty($data) ) {
84 include_once( ABSPATH . 'wp-admin/admin-header.php');
86 include( ABSPATH . 'wp-admin/admin-footer.php');
93 if ( ! is_object($wp_filesystem) )
94 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
96 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
97 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
99 //Get the base plugin folder
100 $themes_dir = $wp_filesystem->wp_themes_dir();
101 if ( empty($themes_dir) )
102 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
104 $themes_dir = trailingslashit( $themes_dir );
105 $theme_dir = trailingslashit($themes_dir . $template);
106 $deleted = $wp_filesystem->delete($theme_dir, true);
109 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
111 // Force refresh of theme update information
112 delete_site_transient('update_themes');
118 * {@internal Missing Short Description}}
124 function get_broken_themes() {
125 global $wp_broken_themes;
128 return $wp_broken_themes;
132 * Get the allowed themes for the current blog.
137 * @uses current_theme_info()
138 * @uses get_site_allowed_themes()
139 * @uses wpmu_get_blog_allowedthemes
141 * @return array $themes Array of allowed themes.
143 function get_allowed_themes() {
144 if ( !is_multisite() )
147 $themes = get_themes();
148 $ct = current_theme_info();
149 $allowed_themes = apply_filters("allowed_themes", get_site_allowed_themes() );
150 if ( $allowed_themes == false )
151 $allowed_themes = array();
153 $blog_allowed_themes = wpmu_get_blog_allowedthemes();
154 if ( is_array( $blog_allowed_themes ) )
155 $allowed_themes = array_merge( $allowed_themes, $blog_allowed_themes );
157 if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
158 $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
161 foreach ( $themes as $key => $theme ) {
162 if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
163 unset( $themes[ $key ] );
171 * Get the Page Templates available in this theme
175 * @return array Key is the template name, value is the filename of the template
177 function get_page_templates() {
178 $themes = get_themes();
179 $theme = get_current_theme();
180 $templates = $themes[$theme]['Template Files'];
181 $page_templates = array();
183 if ( is_array( $templates ) ) {
184 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
186 foreach ( $templates as $template ) {
187 $basename = str_replace($base, '', $template);
189 // don't allow template files in subdirectories
190 if ( false !== strpos($basename, '/') )
193 if ( 'functions.php' == $basename )
196 $template_data = implode( '', file( $template ));
199 if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
200 $name = _cleanup_header_comment($name[1]);
202 if ( !empty( $name ) ) {
203 $page_templates[trim( $name )] = $basename;
208 return $page_templates;
212 * Tidies a filename for url display by the theme editor.
217 * @param string $fullpath Full path to the theme file
218 * @param string $containingfolder Path of the theme parent folder
221 function _get_template_edit_filename($fullpath, $containingfolder) {
222 return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
226 * Check if there is an update for a theme available.
228 * Will display link, if there is an update available.
232 * @param object $theme Theme data object.
233 * @return bool False if no valid info was passed.
235 function theme_update_available( $theme ) {
236 static $themes_update;
238 if ( !current_user_can('update_themes' ) )
241 if ( !isset($themes_update) )
242 $themes_update = get_site_transient('update_themes');
244 if ( is_object($theme) && isset($theme->stylesheet) )
245 $stylesheet = $theme->stylesheet;
246 elseif ( is_array($theme) && isset($theme['Stylesheet']) )
247 $stylesheet = $theme['Stylesheet'];
249 return false; //No valid info passed.
251 if ( isset($themes_update->response[ $stylesheet ]) ) {
252 $update = $themes_update->response[ $stylesheet ];
253 $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
254 $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.
255 $update_url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
256 $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;"';
258 if ( !is_multisite() ) {
259 if ( ! current_user_can('update_themes') )
260 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']);
261 else if ( empty($update['package']) )
262 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']);
264 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 );
270 * Retrieve list of WordPress theme features (aka theme tags)
274 * @return array Array of features keyed by category with translations keyed by slug.
276 function get_theme_feature_list() {
277 // Hard-coded list is used if api not accessible.
279 __('Colors') => array(
280 'black' => __( 'Black' ),
281 'blue' => __( 'Blue' ),
282 'brown' => __( 'Brown' ),
283 'gray' => __( 'Gray' ),
284 'green' => __( 'Green' ),
285 'orange' => __( 'Orange' ),
286 'pink' => __( 'Pink' ),
287 'purple' => __( 'Purple' ),
288 'red' => __( 'Red' ),
289 'silver' => __( 'Silver' ),
290 'tan' => __( 'Tan' ),
291 'white' => __( 'White' ),
292 'yellow' => __( 'Yellow' ),
293 'dark' => __( 'Dark' ),
294 'light' => __( 'Light ')
297 __('Columns') => array(
298 'one-column' => __( 'One Column' ),
299 'two-columns' => __( 'Two Columns' ),
300 'three-columns' => __( 'Three Columns' ),
301 'four-columns' => __( 'Four Columns' ),
302 'left-sidebar' => __( 'Left Sidebar' ),
303 'right-sidebar' => __( 'Right Sidebar' )
306 __('Width') => array(
307 'fixed-width' => __( 'Fixed Width' ),
308 'flexible-width' => __( 'Flexible Width' )
311 __( 'Features' ) => array(
312 'blavatar' => __( 'Blavatar' ),
313 'buddypress' => __( 'BuddyPress' ),
314 'custom-background' => __( 'Custom Background' ),
315 'custom-colors' => __( 'Custom Colors' ),
316 'custom-header' => __( 'Custom Header' ),
317 'custom-menu' => __( 'Custom Menu' ),
318 'editor-style' => __( 'Editor Style' ),
319 'featured-image-header' => __( 'Featured Image Header' ),
320 'featured-images' => __( 'Featured Images' ),
321 'front-page-post-form' => __( 'Front Page Posting' ),
322 'full-width-template' => __( 'Full Width Template' ),
323 'microformats' => __( 'Microformats' ),
324 'post-formats' => __( 'Post Formats' ),
325 'rtl-language-support' => __( 'RTL Language Support' ),
326 'sticky-post' => __( 'Sticky Post' ),
327 'theme-options' => __( 'Theme Options' ),
328 'threaded-comments' => __( 'Threaded Comments' ),
329 'translation-ready' => __( 'Translation Ready' )
332 __( 'Subject' ) => array(
333 'holiday' => __( 'Holiday' ),
334 'photoblogging' => __( 'Photoblogging' ),
335 'seasonal' => __( 'Seasonal' )
339 if ( !current_user_can('install_themes') )
342 if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
343 set_site_transient( 'wporg_theme_feature_list', array( ), 10800);
345 if ( !$feature_list ) {
346 $feature_list = themes_api( 'feature_list', array( ) );
347 if ( is_wp_error( $feature_list ) )
351 if ( !$feature_list )
354 set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
356 $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
357 'Features' => __('Features'), 'Subject' => __('Subject') );
359 // Loop over the wporg canonical list and apply translations
360 $wporg_features = array();
361 foreach ( (array) $feature_list as $feature_category => $feature_items ) {
362 if ( isset($category_translations[$feature_category]) )
363 $feature_category = $category_translations[$feature_category];
364 $wporg_features[$feature_category] = array();
366 foreach ( $feature_items as $feature ) {
367 if ( isset($features[$feature_category][$feature]) )
368 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
370 $wporg_features[$feature_category][$feature] = $feature;
374 return $wporg_features;
378 * Retrieve theme installer pages from WordPress Themes API.
380 * It is possible for a theme to override the Themes API result with three
381 * filters. Assume this is for themes, which can extend on the Theme Info to
382 * offer more choices. This is very powerful and must be used with care, when
383 * overridding the filters.
385 * The first filter, 'themes_api_args', is for the args and gives the action as
386 * the second parameter. The hook for 'themes_api_args' must ensure that an
387 * object is returned.
389 * The second filter, 'themes_api', is the result that would be returned.
393 * @param string $action
394 * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
397 function themes_api($action, $args = null) {
399 if ( is_array($args) )
400 $args = (object)$args;
402 if ( !isset($args->per_page) )
403 $args->per_page = 24;
405 $args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
406 $res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
409 $request = wp_remote_post('http://api.wordpress.org/themes/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
410 if ( is_wp_error($request) ) {
411 $res = new WP_Error('themes_api_failed', __('An Unexpected HTTP Error occurred during the API request.'), $request->get_error_message() );
413 $res = unserialize( wp_remote_retrieve_body( $request ) );
415 $res = new WP_Error('themes_api_failed', __('An unknown error occurred.'), wp_remote_retrieve_body( $request ) );
418 //var_dump(array($args, $res));
419 return apply_filters('themes_api_result', $res, $action, $args);