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
48 function delete_theme($template) {
49 global $wp_filesystem;
51 if ( empty($template) )
55 $url = wp_nonce_url('themes.php?action=delete&template=' . $template, 'delete-theme_' . $template);
56 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
57 $data = ob_get_contents();
59 if ( ! empty($data) ){
60 include_once( ABSPATH . 'wp-admin/admin-header.php');
62 include( ABSPATH . 'wp-admin/admin-footer.php');
68 if ( ! WP_Filesystem($credentials) ) {
69 request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
70 $data = ob_get_contents();
72 if ( ! empty($data) ) {
73 include_once( ABSPATH . 'wp-admin/admin-header.php');
75 include( ABSPATH . 'wp-admin/admin-footer.php');
82 if ( ! is_object($wp_filesystem) )
83 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
85 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
86 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
88 //Get the base plugin folder
89 $themes_dir = $wp_filesystem->wp_themes_dir();
90 if ( empty($themes_dir) )
91 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
93 $themes_dir = trailingslashit( $themes_dir );
94 $theme_dir = trailingslashit($themes_dir . $template);
95 $deleted = $wp_filesystem->delete($theme_dir, true);
98 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
100 // Force refresh of theme update information
101 delete_site_transient('update_themes');
107 * {@internal Missing Short Description}}
113 function get_broken_themes() {
114 global $wp_broken_themes;
117 return $wp_broken_themes;
121 * Get the allowed themes for the current blog.
126 * @uses current_theme_info()
127 * @uses get_site_allowed_themes()
128 * @uses wpmu_get_blog_allowedthemes
130 * @return array $themes Array of allowed themes.
132 function get_allowed_themes() {
133 if ( !is_multisite() )
136 $themes = get_themes();
137 $ct = current_theme_info();
138 $allowed_themes = apply_filters("allowed_themes", get_site_allowed_themes() );
139 if ( $allowed_themes == false )
140 $allowed_themes = array();
142 $blog_allowed_themes = wpmu_get_blog_allowedthemes();
143 if ( is_array( $blog_allowed_themes ) )
144 $allowed_themes = array_merge( $allowed_themes, $blog_allowed_themes );
146 if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
147 $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
150 foreach ( $themes as $key => $theme ) {
151 if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
152 unset( $themes[ $key ] );
160 * Get the Page Templates available in this theme
164 * @return array Key is template name, Value is template name
166 function get_page_templates() {
167 $themes = get_themes();
168 $theme = get_current_theme();
169 $templates = $themes[$theme]['Template Files'];
170 $page_templates = array();
172 if ( is_array( $templates ) ) {
173 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
175 foreach ( $templates as $template ) {
176 $basename = str_replace($base, '', $template);
178 // don't allow template files in subdirectories
179 if ( false !== strpos($basename, '/') )
182 $template_data = implode( '', file( $template ));
185 if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
186 $name = _cleanup_header_comment($name[1]);
188 if ( !empty( $name ) ) {
189 $page_templates[trim( $name )] = $basename;
194 return $page_templates;
198 * Tidies a filename for url display by the theme editor.
203 * @param string $fullpath Full path to the theme file
204 * @param string $containingfolder Path of the theme parent folder
207 function _get_template_edit_filename($fullpath, $containingfolder) {
208 return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
212 * Check if there is an update for a theme available.
214 * Will display link, if there is an update available.
218 * @param object $theme Theme data object.
219 * @return bool False if no valid info was passed.
221 function theme_update_available( $theme ) {
222 static $themes_update;
224 if ( !current_user_can('update_themes' ) )
227 if ( !isset($themes_update) )
228 $themes_update = get_site_transient('update_themes');
230 if ( is_object($theme) && isset($theme->stylesheet) )
231 $stylesheet = $theme->stylesheet;
232 elseif ( is_array($theme) && isset($theme['Stylesheet']) )
233 $stylesheet = $theme['Stylesheet'];
235 return false; //No valid info passed.
237 if ( isset($themes_update->response[ $stylesheet ]) ) {
238 $update = $themes_update->response[ $stylesheet ];
239 $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
240 $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.
241 $update_url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
242 $update_onclick = 'onclick="if ( confirm(\'' . esc_js( __("Upgrading this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to upgrade.") ) . '\') ) {return true;}return false;"';
244 if ( ! current_user_can('update_themes') )
245 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']);
246 else if ( empty($update['package']) )
247 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 upgrade unavailable for this theme</em>.') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
249 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>upgrade automatically</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );