]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/theme.php
Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / theme.php
1 <?php
2 /*
3  * Theme/template/stylesheet functions.
4  */
5
6 function get_stylesheet() {
7         return apply_filters('stylesheet', get_option('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 = 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_locale_stylesheet_uri() {
29         global $wp_locale;
30         $stylesheet_dir_uri = get_stylesheet_directory_uri();
31         $dir = get_stylesheet_directory();
32         $locale = get_locale();
33         if ( file_exists("$dir/$locale.css") )
34                 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
35         elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
36                 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
37         else
38                 $stylesheet_uri = '';
39         return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
40 }
41
42 function get_template() {
43         return apply_filters('template', get_option('template'));
44 }
45
46 function get_template_directory() {
47         $template = get_template();
48         $template_dir = get_theme_root() . "/$template";
49         return apply_filters('template_directory', $template_dir, $template);
50 }
51
52 function get_template_directory_uri() {
53         $template = get_template();
54         $template_dir_uri = get_theme_root_uri() . "/$template";
55         return apply_filters('template_directory_uri', $template_dir_uri, $template);
56 }
57
58 function get_theme_data( $theme_file ) {
59         $themes_allowed_tags = array(
60                 'a' => array(
61                         'href' => array(),'title' => array()
62                         ),
63                 'abbr' => array(
64                         'title' => array()
65                         ),
66                 'acronym' => array(
67                         'title' => array()
68                         ),
69                 'code' => array(),
70                 'em' => array(),
71                 'strong' => array()
72         );
73
74         $theme_data = implode( '', file( $theme_file ) );
75         $theme_data = str_replace ( '\r', '\n', $theme_data );
76         preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
77         preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
78         preg_match( '|Description:(.*)$|mi', $theme_data, $description );
79         preg_match( '|Author:(.*)$|mi', $theme_data, $author_name );
80         preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri );
81         preg_match( '|Template:(.*)$|mi', $theme_data, $template );
82
83         if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
84                 $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
85         else
86                 $version = '';
87
88         if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
89                 $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
90         else
91                 $status = 'publish';
92
93         $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags );
94         $theme_uri = clean_url( trim( $theme_uri[1] ) );
95         $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) );
96         $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
97
98         $author_uri = clean_url( trim( $author_uri[1] ) );
99
100         if ( empty( $author_uri[1] ) ) {
101                 $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
102         } else {
103                 $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 ) );
104         }
105
106         return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status );
107 }
108
109 function get_themes() {
110         global $wp_themes, $wp_broken_themes;
111
112         if ( isset($wp_themes) )
113                 return $wp_themes;
114
115         $themes = array();
116         $wp_broken_themes = array();
117         $theme_loc = $theme_root = get_theme_root();
118         if ( '/' != ABSPATH ) // don't want to replace all forward slashes, see Trac #4541
119                 $theme_loc = str_replace(ABSPATH, '', $theme_root);
120
121         // Files in wp-content/themes directory and one subdir down
122         $themes_dir = @ opendir($theme_root);
123         if ( !$themes_dir )
124                 return false;
125
126         while ( ($theme_dir = readdir($themes_dir)) !== false ) {
127                 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
128                         if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
129                                 continue;
130                         $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
131                         $found_stylesheet = false;
132                         while ( ($theme_file = readdir($stylish_dir)) !== false ) {
133                                 if ( $theme_file == 'style.css' ) {
134                                         $theme_files[] = $theme_dir . '/' . $theme_file;
135                                         $found_stylesheet = true;
136                                         break;
137                                 }
138                         }
139                         @closedir($stylish_dir);
140                         if ( !$found_stylesheet ) { // look for themes in that dir
141                                 $subdir = "$theme_root/$theme_dir";
142                                 $subdir_name = $theme_dir;
143                                 $theme_subdir = @ opendir( $subdir );
144                                 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
145                                         if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
146                                                 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
147                                                         continue;
148                                                 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
149                                                 $found_stylesheet = false;
150                                                 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
151                                                         if ( $theme_file == 'style.css' ) {
152                                                                 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
153                                                                 $found_stylesheet = true;
154                                                                 break;
155                                                         }
156                                                 }
157                                                 @closedir($stylish_dir);
158                                         }
159                                 }
160                                 @closedir($theme_subdir);
161                                 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
162                         }
163                 }
164         }
165         @closedir($theme_dir);
166
167         if ( !$themes_dir || !$theme_files )
168                 return $themes;
169
170         sort($theme_files);
171
172         foreach ( (array) $theme_files as $theme_file ) {
173                 if ( !is_readable("$theme_root/$theme_file") ) {
174                         $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
175                         continue;
176                 }
177
178                 $theme_data = get_theme_data("$theme_root/$theme_file");
179
180                 $name        = $theme_data['Name'];
181                 $title       = $theme_data['Title'];
182                 $description = wptexturize($theme_data['Description']);
183                 $version     = $theme_data['Version'];
184                 $author      = $theme_data['Author'];
185                 $template    = $theme_data['Template'];
186                 $stylesheet  = dirname($theme_file);
187
188                 $screenshot = false;
189                 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
190                         if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
191                                 $screenshot = "screenshot.$ext";
192                                 break;
193                         }
194                 }
195
196                 if ( empty($name) ) {
197                         $name = dirname($theme_file);
198                         $title = $name;
199                 }
200
201                 if ( empty($template) ) {
202                         if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
203                                 $template = dirname($theme_file);
204                         else
205                                 continue;
206                 }
207
208                 $template = trim($template);
209
210                 if ( !file_exists("$theme_root/$template/index.php") ) {
211                         $parent_dir = dirname(dirname($theme_file));
212                         if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
213                                 $template = "$parent_dir/$template";
214                         } else {
215                                 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
216                                 continue;
217                         }
218                 }
219
220                 $stylesheet_files = array();
221                 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
222                 if ( $stylesheet_dir ) {
223                         while ( ($file = $stylesheet_dir->read()) !== false ) {
224                                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
225                                         $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
226                         }
227                 }
228
229                 $template_files = array();
230                 $template_dir = @ dir("$theme_root/$template");
231                 if ( $template_dir ) {
232                         while(($file = $template_dir->read()) !== false) {
233                                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
234                                         $template_files[] = "$theme_loc/$template/$file";
235                         }
236                 }
237
238                 $template_dir = dirname($template_files[0]);
239                 $stylesheet_dir = dirname($stylesheet_files[0]);
240
241                 if ( empty($template_dir) )
242                         $template_dir = '/';
243                 if ( empty($stylesheet_dir) )
244                         $stylesheet_dir = '/';
245
246                 // Check for theme name collision.  This occurs if a theme is copied to
247                 // a new theme directory and the theme header is not updated.  Whichever
248                 // theme is first keeps the name.  Subsequent themes get a suffix applied.
249                 // The Default and Classic themes always trump their pretenders.
250                 if ( isset($themes[$name]) ) {
251                         if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
252                                          ('default' == $stylesheet || 'classic' == $stylesheet) ) {
253                                 // If another theme has claimed to be one of our default themes, move
254                                 // them aside.
255                                 $suffix = $themes[$name]['Stylesheet'];
256                                 $new_name = "$name/$suffix";
257                                 $themes[$new_name] = $themes[$name];
258                                 $themes[$new_name]['Name'] = $new_name;
259                         } else {
260                                 $name = "$name/$stylesheet";
261                         }
262                 }
263
264                 $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);
265         }
266
267         // Resolve theme dependencies.
268         $theme_names = array_keys($themes);
269
270         foreach ( (array) $theme_names as $theme_name ) {
271                 $themes[$theme_name]['Parent Theme'] = '';
272                 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
273                         foreach ( (array) $theme_names as $parent_theme_name ) {
274                                 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
275                                         $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
276                                         break;
277                                 }
278                         }
279                 }
280         }
281
282         $wp_themes = $themes;
283
284         return $themes;
285 }
286
287 function get_theme($theme) {
288         $themes = get_themes();
289
290         if ( array_key_exists($theme, $themes) )
291                 return $themes[$theme];
292
293         return NULL;
294 }
295
296 function get_current_theme() {
297         $themes = get_themes();
298         $theme_names = array_keys($themes);
299         $current_template = get_option('template');
300         $current_stylesheet = get_option('stylesheet');
301         $current_theme = 'WordPress Default';
302
303         if ( $themes ) {
304                 foreach ( (array) $theme_names as $theme_name ) {
305                         if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
306                                         $themes[$theme_name]['Template'] == $current_template ) {
307                                 $current_theme = $themes[$theme_name]['Name'];
308                                 break;
309                         }
310                 }
311         }
312
313         return $current_theme;
314 }
315
316 function get_theme_root() {
317         return apply_filters('theme_root', ABSPATH . "wp-content/themes");
318 }
319
320 function get_theme_root_uri() {
321         return apply_filters('theme_root_uri', get_option('siteurl') . "/wp-content/themes", get_option('siteurl'));
322 }
323
324 function get_query_template($type) {
325         $template = '';
326         if ( file_exists(TEMPLATEPATH . "/{$type}.php") )
327                 $template = TEMPLATEPATH . "/{$type}.php";
328
329         return apply_filters("{$type}_template", $template);
330 }
331
332 function get_404_template() {
333         return get_query_template('404');
334 }
335
336 function get_archive_template() {
337         return get_query_template('archive');
338 }
339
340 function get_author_template() {
341         return get_query_template('author');
342 }
343
344 function get_category_template() {
345         $template = '';
346         if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
347                 $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
348         elseif ( file_exists(TEMPLATEPATH . "/category.php") )
349                 $template = TEMPLATEPATH . "/category.php";
350
351         return apply_filters('category_template', $template);
352 }
353
354 function get_tag_template() {
355         $template = '';
356         if ( file_exists(TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php') )
357                 $template = TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php';
358         elseif ( file_exists(TEMPLATEPATH . "/tag.php") )
359                 $template = TEMPLATEPATH . "/tag.php";
360
361         return apply_filters('tag_template', $template);
362 }
363
364
365 function get_date_template() {
366         return get_query_template('date');
367 }
368
369 function get_home_template() {
370         $template = '';
371
372         if ( file_exists(TEMPLATEPATH . "/home.php") )
373                 $template = TEMPLATEPATH . "/home.php";
374         elseif ( file_exists(TEMPLATEPATH . "/index.php") )
375                 $template = TEMPLATEPATH . "/index.php";
376
377         return apply_filters('home_template', $template);
378 }
379
380 function get_page_template() {
381         global $wp_query;
382
383         $id = (int) $wp_query->post->ID;
384         $template = get_post_meta($id, '_wp_page_template', true);
385
386         if ( 'default' == $template )
387                 $template = '';
388
389         if ( !empty($template) && file_exists(TEMPLATEPATH . "/$template") )
390                 $template = TEMPLATEPATH . "/$template";
391         elseif ( file_exists(TEMPLATEPATH . "/page.php") )
392                 $template = TEMPLATEPATH . "/page.php";
393         else
394                 $template = '';
395
396         return apply_filters('page_template', $template);
397 }
398
399 function get_paged_template() {
400         return get_query_template('paged');
401 }
402
403 function get_search_template() {
404         return get_query_template('search');
405 }
406
407 function get_single_template() {
408         return get_query_template('single');
409 }
410
411 function get_attachment_template() {
412         global $posts;
413         $type = explode('/', $posts[0]->post_mime_type);
414         if ( $template = get_query_template($type[0]) )
415                 return $template;
416         elseif ( $template = get_query_template($type[1]) )
417                 return $template;
418         elseif ( $template = get_query_template("$type[0]_$type[1]") )
419                 return $template;
420         else
421                 return get_query_template('attachment');
422 }
423
424 function get_comments_popup_template() {
425         if ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
426                 $template = TEMPLATEPATH . '/comments-popup.php';
427         else
428                 $template = get_theme_root() . '/default/comments-popup.php';
429
430         return apply_filters('comments_popup_template', $template);
431 }
432
433 function load_template($_template_file) {
434         global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query,
435                 $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
436
437         if ( is_array($wp_query->query_vars) )
438                 extract($wp_query->query_vars, EXTR_SKIP);
439
440         require_once($_template_file);
441 }
442
443 function locale_stylesheet() {
444         $stylesheet = get_locale_stylesheet_uri();
445         if ( empty($stylesheet) )
446                 return;
447         echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
448 }
449
450 function validate_current_theme() {
451         // Don't validate during an install/upgrade.
452         if ( defined('WP_INSTALLING') )
453                 return true;
454
455         if ( get_template() != 'default' && !file_exists(get_template_directory() . '/index.php') ) {
456                 update_option('template', 'default');
457                 update_option('stylesheet', 'default');
458                 do_action('switch_theme', 'Default');
459                 return false;
460         }
461
462         if ( get_stylesheet() != 'default' && !file_exists(get_template_directory() . '/style.css') ) {
463                 update_option('template', 'default');
464                 update_option('stylesheet', 'default');
465                 do_action('switch_theme', 'Default');
466                 return false;
467         }
468
469         return true;
470 }
471
472 function get_theme_mod($name, $default = false) {
473         $theme = get_current_theme();
474
475         $mods = get_option("mods_$theme");
476
477         if ( isset($mods[$name]) )
478                 return apply_filters( "theme_mod_$name", $mods[$name] );
479
480         return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri()) );
481 }
482
483 function set_theme_mod($name, $value) {
484         $theme = get_current_theme();
485
486         $mods = get_option("mods_$theme");
487
488         $mods[$name] = $value;
489
490         update_option("mods_$theme", $mods);
491         wp_cache_delete("mods_$theme", 'options');
492 }
493
494 function remove_theme_mod( $name ) {
495         $theme = get_current_theme();
496
497         $mods = get_option("mods_$theme");
498
499         if ( !isset($mods[$name]) )
500                 return;
501
502         unset($mods[$name]);
503
504         if ( empty($mods) )
505                 return remove_theme_mods();
506
507         update_option("mods_$theme", $mods);
508         wp_cache_delete("mods_$theme", 'options');
509 }
510
511 function remove_theme_mods() {
512         $theme = get_current_theme();
513
514         delete_option("mods_$theme");
515 }
516
517 function get_header_textcolor() {
518         return get_theme_mod('header_textcolor', HEADER_TEXTCOLOR);
519 }
520
521 function header_textcolor() {
522         echo get_header_textcolor();
523 }
524
525 function get_header_image() {
526         return get_theme_mod('header_image', HEADER_IMAGE);
527 }
528
529 function header_image() {
530         echo get_header_image();
531 }
532
533 function add_custom_image_header($header_callback, $admin_header_callback) {
534         if ( ! empty($header_callback) )
535                 add_action('wp_head', $header_callback);
536
537         if ( ! is_admin() )
538                 return;
539         require_once(ABSPATH . 'wp-admin/custom-header.php');
540         $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback);
541         add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));
542 }
543
544 ?>