root/trunk/wp-includes/theme.php

Revision 8629, 19.2 kB (checked in by ryan, 1 week ago)

Quote array indexes. Props wet. fixes #7502

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * @package WordPress
4  * @subpackage Themes
5  * @since 0.0
6  * Theme/template/stylesheet functions.
7  */
8 function get_stylesheet() {
9     return apply_filters('stylesheet', get_option('stylesheet'));
10 }
11
12 function get_stylesheet_directory() {
13     $stylesheet = get_stylesheet();
14     $stylesheet_dir = get_theme_root() . "/$stylesheet";
15     return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
16 }
17
18 function get_stylesheet_directory_uri() {
19     $stylesheet = get_stylesheet();
20     $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
21     return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
22 }
23
24 function get_stylesheet_uri() {
25     $stylesheet_dir_uri = get_stylesheet_directory_uri();
26     $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
27     return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
28 }
29
30 function get_locale_stylesheet_uri() {
31     global $wp_locale;
32     $stylesheet_dir_uri = get_stylesheet_directory_uri();
33     $dir = get_stylesheet_directory();
34     $locale = get_locale();
35     if ( file_exists("$dir/$locale.css") )
36         $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
37     elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
38         $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
39     else
40         $stylesheet_uri = '';
41     return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
42 }
43
44 function get_template() {
45     return apply_filters('template', get_option('template'));
46 }
47
48 function get_template_directory() {
49     $template = get_template();
50     $template_dir = get_theme_root() . "/$template";
51     return apply_filters('template_directory', $template_dir, $template);
52 }
53
54 function get_template_directory_uri() {
55     $template = get_template();
56     $template_dir_uri = get_theme_root_uri() . "/$template";
57     return apply_filters('template_directory_uri', $template_dir_uri, $template);
58 }
59
60 function get_theme_data( $theme_file ) {
61     $themes_allowed_tags = array(
62         'a' => array(
63             'href' => array(),'title' => array()
64             ),
65         'abbr' => array(
66             'title' => array()
67             ),
68         'acronym' => array(
69             'title' => array()
70             ),
71         'code' => array(),
72         'em' => array(),
73         'strong' => array()
74     );
75
76     $theme_data = implode( '', file( $theme_file ) );
77     $theme_data = str_replace ( '\r', '\n', $theme_data );
78     preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
79     preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
80     preg_match( '|Description:(.*)$|mi', $theme_data, $description );
81
82     if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
83         $author_uri = clean_url( trim( $author_uri[1]) );
84     else
85         $author_uti = '';
86
87     if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
88         $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
89     else
90         $template = '';
91
92     if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
93         $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
94     else
95         $version = '';
96
97     if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
98         $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
99     else
100         $status = 'publish';
101
102     if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
103         $tags = array_map( 'trim', explode( ',', wp_kses( trim( $tags[1] ), array() ) ) );
104     else
105         $tags = array();
106
107     $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags );
108     $theme_uri = clean_url( trim( $theme_uri[1] ) );
109     $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) );
110
111     if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
112         if ( empty( $author_uri ) ) {
113             $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
114         } else {
115             $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) );
116         }
117     } else {
118         $author = __('Anonymous');
119     }
120
121     return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
122 }
123
124 function get_themes() {
125     global $wp_themes, $wp_broken_themes;
126
127     if ( isset($wp_themes) )
128         return $wp_themes;
129
130     $themes = array();
131     $wp_broken_themes = array();
132     $theme_loc = $theme_root = get_theme_root();
133     if ( '/' != WP_CONTENT_DIR ) // don't want to replace all forward slashes, see Trac #4541
134         $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
135
136     // Files in wp-content/themes directory and one subdir down
137     $themes_dir = @ opendir($theme_root);
138     if ( !$themes_dir )
139         return false;
140
141     while ( ($theme_dir = readdir($themes_dir)) !== false ) {
142         if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
143             if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
144                 continue;
145             $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
146             $found_stylesheet = false;
147             while ( ($theme_file = readdir($stylish_dir)) !== false ) {
148                 if ( $theme_file == 'style.css' ) {
149                     $theme_files[] = $theme_dir . '/' . $theme_file;
150                     $found_stylesheet = true;
151                     break;
152                 }
153             }
154             @closedir($stylish_dir);
155             if ( !$found_stylesheet ) { // look for themes in that dir
156                 $subdir = "$theme_root/$theme_dir";
157                 $subdir_name = $theme_dir;
158                 $theme_subdir = @ opendir( $subdir );
159                 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
160                     if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
161                         if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
162                             continue;
163                         $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
164                         $found_stylesheet = false;
165                         while ( ($theme_file = readdir($stylish_dir)) !== false ) {
166                             if ( $theme_file == 'style.css' ) {
167                                 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
168                                 $found_stylesheet = true;
169                                 break;
170                             }
171                         }
172                         @closedir($stylish_dir);
173                     }
174                 }
175                 @closedir($theme_subdir);
176                 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
177             }
178         }
179     }
180     if ( is_dir( $theme_dir ) )
181         @closedir( $theme_dir );
182
183     if ( !$themes_dir || !$theme_files )
184         return $themes;
185
186     sort($theme_files);
187
188     foreach ( (array) $theme_files as $theme_file ) {
189         if ( !is_readable("$theme_root/$theme_file") ) {
190             $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
191             continue;
192         }
193
194         $theme_data = get_theme_data("$theme_root/$theme_file");
195
196         $name        = $theme_data['Name'];
197         $title       = $theme_data['Title'];
198         $description = wptexturize($theme_data['Description']);
199         $version     = $theme_data['Version'];
200         $author      = $theme_data['Author'];
201         $template    = $theme_data['Template'];
202         $stylesheet  = dirname($theme_file);
203
204         $screenshot = false;
205         foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
206             if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
207                 $screenshot = "screenshot.$ext";
208                 break;
209             }
210         }
211
212         if ( empty($name) ) {
213             $name = dirname($theme_file);
214             $title = $name;
215         }
216
217         if ( empty($template) ) {
218             if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
219                 $template = dirname($theme_file);
220             else
221                 continue;
222         }
223
224         $template = trim($template);
225
226         if ( !file_exists("$theme_root/$template/index.php") ) {
227             $parent_dir = dirname(dirname($theme_file));
228             if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
229                 $template = "$parent_dir/$template";
230             } else {
231                 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
232                 continue;
233             }
234         }
235
236         $stylesheet_files = array();
237         $template_files = array();
238
239         $stylesheet_dir = @ dir("$theme_root/$stylesheet");
240         if ( $stylesheet_dir ) {
241             while ( ($file = $stylesheet_dir->read()) !== false ) {
242                 if ( !preg_match('|^\.+$|', $file) ) {
243                     if ( preg_match('|\.css$|', $file) )
244                         $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
245                     elseif ( preg_match('|\.php$|', $file) )
246                         $template_files[] = "$theme_loc/$stylesheet/$file";
247                 }
248             }
249         }
250
251         $template_dir = @ dir("$theme_root/$template");
252         if ( $template_dir ) {
253             while(($file = $template_dir->read()) !== false) {
254                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
255                     $template_files[] = "$theme_loc/$template/$file";
256             }
257         }
258
259         $template_dir = dirname($template_files[0]);
260         $stylesheet_dir = dirname($stylesheet_files[0]);
261
262         if ( empty($template_dir) )
263             $template_dir = '/';
264         if ( empty($stylesheet_dir) )
265             $stylesheet_dir = '/';
266
267         // Check for theme name collision.  This occurs if a theme is copied to
268         // a new theme directory and the theme header is not updated.  Whichever
269         // theme is first keeps the name.  Subsequent themes get a suffix applied.
270         // The Default and Classic themes always trump their pretenders.
271         if ( isset($themes[$name]) ) {
272             if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
273                      ('default' == $stylesheet || 'classic' == $stylesheet) ) {
274                 // If another theme has claimed to be one of our default themes, move
275                 // them aside.
276                 $suffix = $themes[$name]['Stylesheet'];
277                 $new_name = "$name/$suffix";
278                 $themes[$new_name] = $themes[$name];
279                 $themes[$new_name]['Name'] = $new_name;
280             } else {
281                 $name = "$name/$stylesheet";
282             }
283         }
284
285         $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, 'Tags' => $theme_data['Tags']);
286     }
287
288     // Resolve theme dependencies.
289     $theme_names = array_keys($themes);
290
291     foreach ( (array) $theme_names as $theme_name ) {
292         $themes[$theme_name]['Parent Theme'] = '';
293         if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
294             foreach ( (array) $theme_names as $parent_theme_name ) {
295                 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
296                     $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
297                     break;
298                 }
299             }
300         }
301     }
302
303     $wp_themes = $themes;
304
305     return $themes;
306 }
307
308 function get_theme($theme) {
309     $themes = get_themes();
310
311     if ( array_key_exists($theme, $themes) )
312         return $themes[$theme];
313
314     return NULL;
315 }
316
317 function get_current_theme() {
318     if ( $theme = get_option('current_theme') )
319         return $theme;
320
321     $themes = get_themes();
322     $theme_names = array_keys($themes);
323     $current_template = get_option('template');
324     $current_stylesheet = get_option('stylesheet');
325     $current_theme = 'WordPress Default';
326
327     if ( $themes ) {
328         foreach ( (array) $theme_names as $theme_name ) {
329             if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
330                     $themes[$theme_name]['Template'] == $current_template ) {
331                 $current_theme = $themes[$theme_name]['Name'];
332                 break;
333             }
334         }
335     }
336
337     update_option('current_theme', $current_theme);
338
339     return $current_theme;
340 }
341
342 function get_theme_root() {
343     return apply_filters('theme_root', WP_CONTENT_DIR . "/themes");
344 }
345
346 function get_theme_root_uri() {
347     return apply_filters('theme_root_uri', content_url('themes'), get_option('siteurl'));
348 }
349
350 function get_query_template($type) {
351     $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
352     return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
353 }
354
355 function get_404_template() {
356     return get_query_template('404');
357 }
358
359 function get_archive_template() {
360     return get_query_template('archive');
361 }
362
363 function get_author_template() {
364     return get_query_template('author');
365 }
366
367 function get_category_template() {
368     $template =locate_template(array("category-" . absint( get_query_var('cat') ) . '.php',"category.php"));
369     return apply_filters('category_template', $template);
370 }
371
372 function get_tag_template() {
373     $template = locate_template(array("tag-" . absint( get_query_var('tag') ) . '.php',"tag.php"));
374     return apply_filters('tag_template', $template);
375 }
376
377 function get_taxonomy_template() {
378     $taxonomy = get_query_var('taxonomy');
379     $term = get_query_var('term');
380
381     $templates = array();
382     if ( $taxonomy && $term )
383         $templates[] = "taxonomy-$taxonomy-$term.php";
384     if ( $taxonomy )
385         $templates[] = "taxonomy-$taxonomy.php";
386     
387     $templates[] = "taxonomy.php";
388
389     $template = locate_template($templates);
390     return apply_filters('taxonomy_template', $template);
391 }
392
393 function get_date_template() {
394     return get_query_template('date');
395 }
396
397 function get_home_template() {
398     $template = locate_template(array('home.php','index.php'));
399     return apply_filters('home_template', $template);
400 }
401
402 function get_page_template() {
403     global $wp_query;
404
405     $id = (int) $wp_query->post->ID;
406     $template = get_post_meta($id, '_wp_page_template', true);
407
408     if ( 'default' == $template )
409         $template = '';
410
411     $templates = array();
412     if ( !empty($template) && !validate_file($template) )
413         $templates[] = $template;
414     
415     $templates[] = "page.php";
416
417     return apply_filters('page_template', locate_template($templates));
418 }
419
420 function get_paged_template() {
421     return get_query_template('paged');
422 }
423
424 function get_search_template() {
425     return get_query_template('search');
426 }
427
428 function get_single_template() {
429     return get_query_template('single');
430 }
431
432 function get_attachment_template() {
433     global $posts;
434     $type = explode('/', $posts[0]->post_mime_type);
435     if ( $template = get_query_template($type[0]) )
436         return $template;
437     elseif ( $template = get_query_template($type[1]) )
438         return $template;
439     elseif ( $template = get_query_template("$type[0]_$type[1]") )
440         return $template;
441     else
442         return get_query_template('attachment');
443 }
444
445 function get_comments_popup_template() {
446     $template = locate_template(array("comments-popup.php"));
447     if ('' == $template)
448         $template = get_theme_root() . '/default/comments-popup.php';
449
450     return apply_filters('comments_popup_template', $template);
451 }
452
453 /**
454  * Returns the name of the highest priority template file that exists
455  *
456  * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
457  * inherit from a parent theme can just overload one file.
458  * @since 2.7
459  *
460  * @param array $template_names Array of template files to search for in priority order
461  * @param bool $load If true the template file will be loaded if it is found.
462  * @return string The template filename if one is located.
463  */
464 function locate_template($template_names, $load = false) {
465     if (!is_array($template_names))
466         return '';
467     
468     $located = '';
469     foreach($template_names as $template_name) {
470         if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
471             $located = STYLESHEETPATH . '/' . $template_name;
472             break;
473         } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
474             $located = TEMPLATEPATH . '/' . $template_name;
475