root/tags/1_0-rc1/wp-includes/theme.php

Revision 543, 11.1 kB (checked in by matt, 3 years ago)

Lots and lots of changes.

Line 
1 <?php
2 /*
3  * Theme/template/stylesheet functions. 
4  */
5
6 function get_stylesheet() {
7     return apply_filters('stylesheet', get_settings('stylesheet'));
8 }
9
10 function get_stylesheet_directory() {
11     $stylesheet = get_stylesheet();
12     $stylesheet_dir = get_theme_root() . "/$stylesheet";
13     return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
14 }
15
16 function get_stylesheet_directory_uri() {
17     $stylesheet = rawurlencode( get_stylesheet() );
18     $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
19     return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
20 }
21
22 function get_stylesheet_uri() {
23     $stylesheet_dir_uri = get_stylesheet_directory_uri();
24     $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
25     return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
26 }
27
28 function get_template() {
29     $template = get_settings('template');
30     if (!file_exists(get_theme_root() . "/$template")) { //works for dirs too
31         update_option('template', 'default');
32         update_option('stylesheet', 'default');
33     }
34     return apply_filters('template', get_settings('template'));
35 }
36
37 function get_template_directory() {
38     $template = get_template();
39     $template_dir = get_theme_root() . "/$template";
40     return apply_filters('template_directory', $template_dir, $template);
41 }
42
43 function get_template_directory_uri() {
44     $template = get_template();
45     $template_dir_uri = get_theme_root_uri() . "/$template";
46     return apply_filters('template_directory_uri', $template_dir_uri, $template);
47 }
48
49 function get_theme_data($theme_file) {
50     $theme_data = implode('', file($theme_file));
51     preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
52     preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
53     preg_match("|Description:(.*)|i", $theme_data, $description);
54     preg_match("|Author:(.*)|i", $theme_data, $author_name);
55     preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
56     preg_match("|Template:(.*)|i", $theme_data, $template);
57     if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
58         $version = $version[1];
59     else
60         $version ='';
61     if ( preg_match("|Status:(.*)|i", $theme_data, $status) )
62         $status = $status[1];
63     else
64         $status ='publish';
65
66     $description = wptexturize($description[1]);
67
68     $name = $theme_name[1];
69     $name = trim($name);
70     $theme = $name;
71
72     if ( '' == $author_uri[1] ) {
73         $author = $author_name[1];
74     } else {
75         $author = '<a href="' . $author_uri[1] . '" title="' . __('Visit author homepage') . '">' . $author_name[1] . '</a>';
76     }
77
78     return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1], 'Status' => $status);
79 }
80
81 function get_themes() {
82     global $wp_themes;
83     global $wp_broken_themes;
84
85     if ( isset($wp_themes) )
86         return $wp_themes;
87
88     $themes = array();
89     $wp_broken_themes = array();
90     $theme_root = get_theme_root();
91     $theme_loc = str_replace(ABSPATH, '', $theme_root);
92
93     // Files in wp-content/themes directory
94     $themes_dir = @ dir($theme_root);
95     if ( $themes_dir ) {
96         while(($theme_dir = $themes_dir->read()) !== false) {
97             if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
98                 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) {
99                     continue;
100                 }
101                 $stylish_dir = @ dir($theme_root . '/' . $theme_dir);
102                 $found_stylesheet = false;
103                 while (($theme_file = $stylish_dir->read()) !== false) {
104                     if ( $theme_file == 'style.css' ) {
105                         $theme_files[] = $theme_dir . '/' . $theme_file;
106                         $found_stylesheet = true;
107                         break;
108                     }
109                 }
110                 if ( !$found_stylesheet ) {
111                     $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
112                 }
113             }
114         }
115     }
116
117     if ( !$themes_dir || !$theme_files ) {
118         return $themes;
119     }
120
121     sort($theme_files);
122
123     foreach($theme_files as $theme_file) {
124         if ( ! is_readable("$theme_root/$theme_file") ) {
125             $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
126             continue;
127         }
128
129         $theme_data = get_theme_data("$theme_root/$theme_file");
130
131         $name = $theme_data['Name'];
132         $title = $theme_data['Title'];
133         $description = wptexturize($theme_data['Description']);
134         $version = $theme_data['Version'];
135         $author = $theme_data['Author'];
136         $template = $theme_data['Template'];
137         $stylesheet = dirname($theme_file);
138
139         foreach (array('png', 'gif', 'jpg', 'jpeg') as $ext) {
140             if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
141                 $screenshot = "screenshot.$ext";
142                 break;
143             }
144         }
145
146         if ( empty($name) ) {
147             $name = dirname($theme_file);
148             $title = $name;
149         }
150
151         if ( empty($template) ) {
152             if ( file_exists(dirname("$theme_root/$theme_file/index.php")) ) {
153                 $template = dirname($theme_file);
154             } else {
155                 continue;
156             }
157         }
158
159         $template = trim($template);
160
161         if ( !file_exists("$theme_root/$template/index.php") ) {
162             $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
163             continue;
164         }
165
166         $stylesheet_files = array();
167         $stylesheet_dir = @ dir("$theme_root/$stylesheet");
168         if ( $stylesheet_dir ) {
169             while(($file = $stylesheet_dir->read()) !== false) {
170                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
171                     $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
172             }
173         }
174
175         $template_files = array();
176         $template_dir = @ dir("$theme_root/$template");
177         if ( $template_dir ) {
178             while(($file = $template_dir->read()) !== false) {
179                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
180                     $template_files[] = "$theme_loc/$template/$file";
181             }
182         }
183
184         $template_dir = dirname($template_files[0]);
185         $stylesheet_dir = dirname($stylesheet_files[0]);
186
187         if ( empty($template_dir) )
188             $template_dir = '/';
189         if ( empty($stylesheet_dir) )
190             $stylesheet_dir = '/';
191
192         // Check for theme name collision.  This occurs if a theme is copied to
193         // a new theme directory and the theme header is not updated.  Whichever
194         // theme is first keeps the name.  Subsequent themes get a suffix applied.
195         // The Default and Classic themes always trump their pretenders.
196         if ( isset($themes[$name]) ) {
197             if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
198                      ('default' == $stylesheet || 'classic' == $stylesheet) ) {
199                 // If another theme has claimed to be one of our default themes, move
200                 // them aside.
201                 $suffix = $themes[$name]['Stylesheet'];
202                 $new_name = "$name/$suffix";
203                 $themes[$new_name] = $themes[$name];
204                 $themes[$new_name]['Name'] = $new_name;
205             } else {
206                 $name = "$name/$stylesheet";
207             }
208         }
209
210         $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot);
211     }
212
213     // Resolve theme dependencies.
214     $theme_names = array_keys($themes);
215
216     foreach ($theme_names as $theme_name) {
217         $themes[$theme_name]['Parent Theme'] = '';
218         if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
219             foreach ($theme_names as $parent_theme_name) {
220                 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
221                     $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
222                     break;
223                 }
224             }
225         }
226     }
227
228     $wp_themes = $themes;
229
230     return $themes;
231 }
232
233 function get_theme($theme) {
234     $themes = get_themes();
235
236     if ( array_key_exists($theme, $themes) )
237         return $themes[$theme];
238
239     return NULL;
240 }
241
242 function get_current_theme() {
243     $themes = get_themes();
244     $theme_names = array_keys($themes);
245     $current_template = get_settings('template');
246     $current_stylesheet = get_settings('stylesheet');
247     $current_theme = 'WordPress Default';
248
249     if ( $themes ) {
250         foreach ($theme_names as $theme_name) {
251             if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
252                     $themes[$theme_name]['Template'] == $current_template ) {
253                 $current_theme = $themes[$theme_name]['Name'];
254                 break;
255             }
256         }
257     }
258
259     return $current_theme;
260 }
261
262 function get_theme_root() {
263     return apply_filters('theme_root', ABSPATH . "wp-content/themes");
264 }
265
266 function get_theme_root_uri() {
267     return apply_filters('theme_root_uri', get_settings('siteurl') . "/wp-content/themes", get_settings('siteurl'));
268 }
269
270 function get_query_template($type) {
271     $template = '';
272     if ( file_exists(TEMPLATEPATH . "/{$type}.php") )
273         $template = TEMPLATEPATH . "/{$type}.php";
274
275     return apply_filters("{$type}_template", $template);
276 }
277
278 function get_404_template() {
279     return get_query_template('404');
280 }
281
282 function get_archive_template() {
283     return get_query_template('archive');
284 }
285
286 function get_author_template() {
287     return get_query_template('author');
288 }
289
290 function get_category_template() {
291     $template = '';
292     if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
293         $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
294     else if ( file_exists(TEMPLATEPATH . "/category.php") )
295         $template = TEMPLATEPATH . "/category.php";
296
297     return apply_filters('category_template', $template);
298 }
299
300 function get_date_template() {
301     return get_query_template('date');
302 }
303
304 function get_home_template() {
305     $template = '';
306
307     if ( file_exists(TEMPLATEPATH . "/home.php") )
308         $template = TEMPLATEPATH . "/home.php";
309     else if ( file_exists(TEMPLATEPATH . "/index.php") )
310         $template = TEMPLATEPATH . "/index.php";
311
312     return apply_filters('home_template', $template);
313 }
314
315 function get_page_template() {
316     global $wp_query;
317
318     $id = $wp_query->post->ID;
319     $template = get_post_meta($id, '_wp_page_template', true);
320
321     if ( 'default' == $template )
322         $template = '';
323
324     if ( ! empty($template) && file_exists(TEMPLATEPATH . "/$template") )
325         $template = TEMPLATEPATH . "/$template";
326     else if ( file_exists(TEMPLATEPATH . "/page.php") )
327         $template = TEMPLATEPATH . "/page.php";
328     else
329         $template = '';
330
331     return apply_filters('page_template', $template);
332 }
333
334 function get_paged_template() {
335     return get_query_template('paged');
336 }
337
338 function get_search_template() {
339     return get_query_template('search');
340 }
341
342 function get_single_template() {
343     return get_query_template('single');
344 }
345
346 function get_attachment_template() {
347     global $posts;
348     $type = explode('/', $posts[0]->post_mime_type);
349     if ( $template = get_query_template($type[0]) )
350         return $template;
351     elseif ( $template = get_query_template($type[1]) )
352         return $template;
353     elseif ( $template = get_query_template("$type[0]_$type[1]") )
354         return $template;
355     else
356         return get_query_template('attachment');
357 }
358
359 function get_comments_popup_template() {
360     if ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
361         $template = TEMPLATEPATH . '/comments-popup.php';
362     else
363         $template = get_theme_root() . '/default/comments-popup.php';
364
365     return apply_filters('comments_popup_template', $template);
366 }
367
368 function load_template($file) {
369     global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query,
370         $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment;
371
372     if( is_array( $wp_query->query_vars ) )
373         extract($wp_query->query_vars);
374
375     require_once($file);
376 }
377
378 ?>
379
Note: See TracBrowser for help on using the browser.