3 * WordPress Theme Administration API
6 * @subpackage Administration
14 * @param string $stylesheet Stylesheet of the theme to delete
15 * @param string $redirect Redirect to page when complete.
18 function delete_theme($stylesheet, $redirect = '') {
19 global $wp_filesystem;
21 if ( empty($stylesheet) )
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();
30 if ( ! empty($data) ){
31 include_once( ABSPATH . 'wp-admin/admin-header.php');
33 include( ABSPATH . 'wp-admin/admin-footer.php');
39 if ( ! WP_Filesystem($credentials) ) {
40 request_filesystem_credentials($redirect, '', true); // Failed to connect, Error and request again
41 $data = ob_get_contents();
43 if ( ! empty($data) ) {
44 include_once( ABSPATH . 'wp-admin/admin-header.php');
46 include( ABSPATH . 'wp-admin/admin-footer.php');
52 if ( ! is_object($wp_filesystem) )
53 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
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);
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.'));
63 $themes_dir = trailingslashit( $themes_dir );
64 $theme_dir = trailingslashit($themes_dir . $stylesheet);
65 $deleted = $wp_filesystem->delete($theme_dir, true);
68 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $stylesheet) );
70 // Force refresh of theme update information
71 delete_site_transient('update_themes');
77 * Get the Page Templates available in this theme
81 * @return array Key is the template name, value is the filename of the template
83 function get_page_templates() {
84 return array_flip( wp_get_theme()->get_page_templates() );
88 * Tidies a filename for url display by the theme editor.
93 * @param string $fullpath Full path to the theme file
94 * @param string $containingfolder Path of the theme parent folder
97 function _get_template_edit_filename($fullpath, $containingfolder) {
98 return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
102 * Check if there is an update for a theme available.
104 * Will display link, if there is an update available.
107 * @see get_theme_update_available()
109 * @param object $theme Theme data object.
111 function theme_update_available( $theme ) {
112 echo get_theme_update_available( $theme );
116 * Retrieve the update link if there is a theme update available.
118 * Will return a link if there is an update available.
122 * @param WP_Theme $theme WP_Theme object.
123 * @return string|bool HTML for the update link, or false if invalid info was passed.
125 function get_theme_update_available( $theme ) {
126 static $themes_update;
128 if ( !current_user_can('update_themes' ) )
131 if ( !isset($themes_update) )
132 $themes_update = get_site_transient('update_themes');
134 if ( ! is_a( $theme, 'WP_Theme' ) )
137 $stylesheet = $theme->get_stylesheet();
141 if ( isset($themes_update->response[ $stylesheet ]) ) {
142 $update = $themes_update->response[ $stylesheet ];
143 $theme_name = $theme->display('Name');
144 $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.
145 $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
146 $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;"';
148 if ( !is_multisite() ) {
149 if ( ! current_user_can('update_themes') ) {
150 $html = sprintf( '<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']);
151 } else if ( empty( $update['package'] ) ) {
152 $html = sprintf( '<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']);
154 $html = sprintf( '<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 now</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );
163 * Retrieve list of WordPress theme features (aka theme tags)
167 * @param bool $api Optional. Whether try to fetch tags from the WP.org API. Defaults to true.
168 * @return array Array of features keyed by category with translations keyed by slug.
170 function get_theme_feature_list( $api = true ) {
171 // Hard-coded list is used if api not accessible.
173 __( 'Colors' ) => array(
174 'black' => __( 'Black' ),
175 'blue' => __( 'Blue' ),
176 'brown' => __( 'Brown' ),
177 'gray' => __( 'Gray' ),
178 'green' => __( 'Green' ),
179 'orange' => __( 'Orange' ),
180 'pink' => __( 'Pink' ),
181 'purple' => __( 'Purple' ),
182 'red' => __( 'Red' ),
183 'silver' => __( 'Silver' ),
184 'tan' => __( 'Tan' ),
185 'white' => __( 'White' ),
186 'yellow' => __( 'Yellow' ),
187 'dark' => __( 'Dark' ),
188 'light' => __( 'Light' ),
191 __( 'Columns' ) => array(
192 'one-column' => __( 'One Column' ),
193 'two-columns' => __( 'Two Columns' ),
194 'three-columns' => __( 'Three Columns' ),
195 'four-columns' => __( 'Four Columns' ),
196 'left-sidebar' => __( 'Left Sidebar' ),
197 'right-sidebar' => __( 'Right Sidebar' ),
200 __( 'Layout' ) => array(
201 'fixed-layout' => __( 'Fixed Layout' ),
202 'fluid-layout' => __( 'Fluid Layout' ),
203 'responsive-layout' => __( 'Responsive Layout' ),
206 __( 'Features' ) => array(
207 'accessibility-ready' => __( 'Accessibility Ready' ),
208 'blavatar' => __( 'Blavatar' ),
209 'buddypress' => __( 'BuddyPress' ),
210 'custom-background' => __( 'Custom Background' ),
211 'custom-colors' => __( 'Custom Colors' ),
212 'custom-header' => __( 'Custom Header' ),
213 'custom-menu' => __( 'Custom Menu' ),
214 'editor-style' => __( 'Editor Style' ),
215 'featured-image-header' => __( 'Featured Image Header' ),
216 'featured-images' => __( 'Featured Images' ),
217 'flexible-header' => __( 'Flexible Header' ),
218 'front-page-post-form' => __( 'Front Page Posting' ),
219 'full-width-template' => __( 'Full Width Template' ),
220 'microformats' => __( 'Microformats' ),
221 'post-formats' => __( 'Post Formats' ),
222 'rtl-language-support' => __( 'RTL Language Support' ),
223 'sticky-post' => __( 'Sticky Post' ),
224 'theme-options' => __( 'Theme Options' ),
225 'threaded-comments' => __( 'Threaded Comments' ),
226 'translation-ready' => __( 'Translation Ready' ),
229 __( 'Subject' ) => array(
230 'holiday' => __( 'Holiday' ),
231 'photoblogging' => __( 'Photoblogging' ),
232 'seasonal' => __( 'Seasonal' ),
236 if ( ! $api || ! current_user_can( 'install_themes' ) )
239 if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
240 set_site_transient( 'wporg_theme_feature_list', array(), 10800);
242 if ( !$feature_list ) {
243 $feature_list = themes_api( 'feature_list', array() );
244 if ( is_wp_error( $feature_list ) )
248 if ( !$feature_list )
251 set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
253 $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
254 'Features' => __('Features'), 'Subject' => __('Subject') );
256 // Loop over the wporg canonical list and apply translations
257 $wporg_features = array();
258 foreach ( (array) $feature_list as $feature_category => $feature_items ) {
259 if ( isset($category_translations[$feature_category]) )
260 $feature_category = $category_translations[$feature_category];
261 $wporg_features[$feature_category] = array();
263 foreach ( $feature_items as $feature ) {
264 if ( isset($features[$feature_category][$feature]) )
265 $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
267 $wporg_features[$feature_category][$feature] = $feature;
271 return $wporg_features;
275 * Retrieve theme installer pages from WordPress Themes API.
277 * It is possible for a theme to override the Themes API result with three
278 * filters. Assume this is for themes, which can extend on the Theme Info to
279 * offer more choices. This is very powerful and must be used with care, when
280 * overridding the filters.
282 * The first filter, 'themes_api_args', is for the args and gives the action as
283 * the second parameter. The hook for 'themes_api_args' must ensure that an
284 * object is returned.
286 * The second filter, 'themes_api', is the result that would be returned.
290 * @param string $action The requested action. Likely values are 'theme_information',
291 * 'feature_list', or 'query_themes'.
292 * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
295 function themes_api( $action, $args = null ) {
297 if ( is_array($args) )
298 $args = (object)$args;
300 if ( !isset($args->per_page) )
301 $args->per_page = 24;
303 * Filter arguments used to query for installer pages from the WordPress.org Themes API.
305 * Important: An object MUST be returned to this filter.
309 * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
310 * @param string $action Requested action. Likely values are 'theme_information',
311 * 'feature_list', or 'query_themes'.
313 $args = apply_filters( 'themes_api_args', $args, $action );
316 * Filter whether to override the WordPress.org Themes API.
318 * Returning a value of true to this filter allows a theme to completely
319 * override the built-in WordPress.org API.
323 * @param bool $bool Whether to override the WordPress.org Themes API. Default false.
324 * @param string $action Requested action. Likely values are 'theme_information',
325 * 'feature_list', or 'query_themes'.
326 * @param object $args Arguments used to query for installer pages from the Themes API.
328 $res = apply_filters( 'themes_api', false, $action, $args );
331 $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
332 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
333 $url = set_url_scheme( $url, 'https' );
338 'request' => serialize( $args )
341 $request = wp_remote_post( $url, $args );
343 if ( $ssl && is_wp_error( $request ) ) {
344 trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://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 );
345 $request = wp_remote_post( $http_url, $args );
348 if ( is_wp_error($request) ) {
349 $res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
351 $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
352 if ( ! is_object( $res ) && ! is_array( $res ) )
353 $res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
358 * Filter the returned WordPress.org Themes API response.
362 * @param array|object $res WordPress.org Themes API response.
363 * @param string $action Requested action. Likely values are 'theme_information',
364 * 'feature_list', or 'query_themes'.
365 * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
367 return apply_filters( 'themes_api_result', $res, $action, $args );
371 * Prepare themes for JavaScript.
375 * @param array $themes Optional. Array of WP_Theme objects to prepare.
376 * Defaults to all allowed themes.
378 * @return array An associative array of theme data, sorted by name.
380 function wp_prepare_themes_for_js( $themes = null ) {
381 $current_theme = get_stylesheet();
383 // Make sure the current theme is listed first.
384 $prepared_themes = array( $current_theme => array() );
386 if ( null === $themes ) {
387 $themes = wp_get_themes( array( 'allowed' => true ) );
388 if ( ! isset( $themes[ $current_theme ] ) ) {
389 $themes[ $current_theme ] = wp_get_theme();
394 if ( current_user_can( 'update_themes' ) ) {
395 $updates_transient = get_site_transient( 'update_themes' );
396 if ( isset( $updates_transient->response ) ) {
397 $updates = $updates_transient->response;
401 WP_Theme::sort_by_name( $themes );
402 foreach ( $themes as $theme ) {
404 if ( $theme->parent() ) {
405 $parent = $theme->parent()->display( 'Name' );
408 $slug = $theme->get_stylesheet();
409 $encoded_slug = urlencode( $slug );
411 $prepared_themes[ $slug ] = array(
413 'name' => $theme->display( 'Name' ),
414 'screenshot' => array( $theme->get_screenshot() ), // @todo multiple
415 'description' => $theme->display( 'Description' ),
416 'author' => $theme->display( 'Author', false, true ),
417 'authorAndUri' => $theme->display( 'Author' ),
418 'version' => $theme->display( 'Version' ),
419 'tags' => $theme->display( 'Tags' ),
421 'active' => $slug === $current_theme,
422 'hasUpdate' => isset( $updates[ $slug ] ),
423 'update' => get_theme_update_available( $theme ),
425 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
426 'customize'=> current_user_can( 'edit_theme_options' ) ? wp_customize_url( $slug ) : null,
427 'preview' => add_query_arg( array(
429 'template' => urlencode( $theme->get_template() ),
430 'stylesheet' => urlencode( $slug ),
431 'preview_iframe' => true,
433 ), home_url( '/' ) ),
434 'delete' => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
440 * Filter the themes prepared for JavaScript, for themes.php.
442 * Could be useful for changing the order, which is by name by default.
446 * @param array $prepared_themes Array of themes.
448 $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
449 return array_values( $prepared_themes );