]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/theme.php
Wordpress 3.0.1-scripts
[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 unknown
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  * @return mixed
47  */
48 function delete_theme($template) {
49         global $wp_filesystem;
50
51         if ( empty($template) )
52                 return false;
53
54         ob_start();
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();
58                 ob_end_clean();
59                 if ( ! empty($data) ){
60                         include_once( ABSPATH . 'wp-admin/admin-header.php');
61                         echo $data;
62                         include( ABSPATH . 'wp-admin/admin-footer.php');
63                         exit;
64                 }
65                 return;
66         }
67
68         if ( ! WP_Filesystem($credentials) ) {
69                 request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
70                 $data = ob_get_contents();
71                 ob_end_clean();
72                 if ( ! empty($data) ) {
73                         include_once( ABSPATH . 'wp-admin/admin-header.php');
74                         echo $data;
75                         include( ABSPATH . 'wp-admin/admin-footer.php');
76                         exit;
77                 }
78                 return;
79         }
80
81
82         if ( ! is_object($wp_filesystem) )
83                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
84
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);
87
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.'));
92
93         $themes_dir = trailingslashit( $themes_dir );
94         $theme_dir = trailingslashit($themes_dir . $template);
95         $deleted = $wp_filesystem->delete($theme_dir, true);
96
97         if ( ! $deleted )
98                 return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
99
100         // Force refresh of theme update information
101         delete_site_transient('update_themes');
102
103         return true;
104 }
105
106 /**
107  * {@internal Missing Short Description}}
108  *
109  * @since unknown
110  *
111  * @return unknown
112  */
113 function get_broken_themes() {
114         global $wp_broken_themes;
115
116         get_themes();
117         return $wp_broken_themes;
118 }
119
120 /**
121  * Get the allowed themes for the current blog.
122  *
123  * @since 3.0.0
124  *
125  * @uses get_themes()
126  * @uses current_theme_info()
127  * @uses get_site_allowed_themes()
128  * @uses wpmu_get_blog_allowedthemes
129  *
130  * @return array $themes Array of allowed themes.
131  */
132 function get_allowed_themes() {
133         if ( !is_multisite() )
134                 return get_themes();
135
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();
141
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 );
145
146         if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
147                 $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
148
149         reset( $themes );
150         foreach ( $themes as $key => $theme ) {
151                 if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
152                         unset( $themes[ $key ] );
153         }
154         reset( $themes );
155
156         return $themes;
157 }
158
159 /**
160  * Get the Page Templates available in this theme
161  *
162  * @since unknown
163  *
164  * @return array Key is template name, Value is template name
165  */
166 function get_page_templates() {
167         $themes = get_themes();
168         $theme = get_current_theme();
169         $templates = $themes[$theme]['Template Files'];
170         $page_templates = array();
171
172         if ( is_array( $templates ) ) {
173                 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
174
175                 foreach ( $templates as $template ) {
176                         $basename = str_replace($base, '', $template);
177
178                         // don't allow template files in subdirectories
179                         if ( false !== strpos($basename, '/') )
180                                 continue;
181
182                         $template_data = implode( '', file( $template ));
183
184                         $name = '';
185                         if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
186                                 $name = _cleanup_header_comment($name[1]);
187
188                         if ( !empty( $name ) ) {
189                                 $page_templates[trim( $name )] = $basename;
190                         }
191                 }
192         }
193
194         return $page_templates;
195 }
196
197 /**
198  * Tidies a filename for url display by the theme editor.
199  *
200  * @since 2.9.0
201  * @access private
202  *
203  * @param string $fullpath Full path to the theme file
204  * @param string $containingfolder Path of the theme parent folder
205  * @return string
206  */
207 function _get_template_edit_filename($fullpath, $containingfolder) {
208         return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
209 }
210
211 /**
212  * Check if there is an update for a theme available.
213  *
214  * Will display link, if there is an update available.
215  *
216  * @since 2.7.0
217  *
218  * @param object $theme Theme data object.
219  * @return bool False if no valid info was passed.
220  */
221 function theme_update_available( $theme ) {
222         static $themes_update;
223
224         if ( !current_user_can('update_themes' ) )
225                 return;
226
227         if ( !isset($themes_update) )
228                 $themes_update = get_site_transient('update_themes');
229
230         if ( is_object($theme) && isset($theme->stylesheet) )
231                 $stylesheet = $theme->stylesheet;
232         elseif ( is_array($theme) && isset($theme['Stylesheet']) )
233                 $stylesheet = $theme['Stylesheet'];
234         else
235                 return false; //No valid info passed.
236
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&amp;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;"';
243
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']);
248                 else
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 );
250         }
251 }
252
253 ?>