root/tags/2.1.1/wp-includes/general-template.php

Revision 4775, 31.1 kB (checked in by markjaquith, 2 years ago)

use lang for text/html and xml:lang for all others. Props nickshanks. fixes #3608

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 /* Note: these tags go anywhere in the template */
4
5 function get_header() {
6     do_action( 'get_header' );
7     if ( file_exists( TEMPLATEPATH . '/header.php') )
8         load_template( TEMPLATEPATH . '/header.php');
9     else
10         load_template( ABSPATH . 'wp-content/themes/default/header.php');
11 }
12
13
14 function get_footer() {
15     do_action( 'get_footer' );
16     if ( file_exists( TEMPLATEPATH . '/footer.php') )
17         load_template( TEMPLATEPATH . '/footer.php');
18     else
19         load_template( ABSPATH . 'wp-content/themes/default/footer.php');
20 }
21
22
23 function get_sidebar() {
24     if ( file_exists( TEMPLATEPATH . '/sidebar.php') )
25         load_template( TEMPLATEPATH . '/sidebar.php');
26     else
27         load_template( ABSPATH . 'wp-content/themes/default/sidebar.php');
28 }
29
30
31 function wp_loginout() {
32     if ( ! is_user_logged_in() )
33         $link = '<a href="' . get_option('siteurl') . '/wp-login.php">' . __('Login') . '</a>';
34     else
35         $link = '<a href="' . get_option('siteurl') . '/wp-login.php?action=logout">' . __('Logout') . '</a>';
36
37     echo apply_filters('loginout', $link);
38 }
39
40
41 function wp_register( $before = '<li>', $after = '</li>' ) {
42
43     if ( ! is_user_logged_in() ) {
44         if ( get_option('users_can_register') )
45             $link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after;
46         else
47             $link = '';
48     } else {
49         $link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after;
50     }
51
52     echo apply_filters('register', $link);
53 }
54
55
56 function wp_meta() {
57     do_action('wp_meta');
58 }
59
60
61 function bloginfo($show='') {
62     $info = get_bloginfo($show);
63     if (!strstr($show, 'url') && //don't filter URLs
64         !strstr($show, 'directory') &&
65         !strstr($show, 'home')) {
66         $info = apply_filters('bloginfo', $info, $show);
67         $info = convert_chars($info);
68     } else {
69         $info = apply_filters('bloginfo_url', $info, $show);
70     }
71
72     echo $info;
73 }
74
75
76 function get_bloginfo($show='') {
77
78     switch($show) {
79         case 'url' :
80         case 'home' :
81         case 'siteurl' :
82             $output = get_option('home');
83             break;
84         case 'wpurl' :
85             $output = get_option('siteurl');
86             break;
87         case 'description':
88             $output = get_option('blogdescription');
89             break;
90         case 'rdf_url':
91             $output = get_feed_link('rdf');
92             break;
93         case 'rss_url':
94             $output = get_feed_link('rss');
95             break;
96         case 'rss2_url':
97             $output = get_feed_link('rss2');
98             break;
99         case 'atom_url':
100             $output = get_feed_link('atom');
101             break;
102         case 'comments_rss2_url':
103             $output = get_feed_link('comments_rss2');
104             break;
105         case 'pingback_url':
106             $output = get_option('siteurl') .'/xmlrpc.php';
107             break;
108         case 'stylesheet_url':
109             $output = get_stylesheet_uri();
110             break;
111         case 'stylesheet_directory':
112             $output = get_stylesheet_directory_uri();
113             break;
114         case 'template_directory':
115         case 'template_url':
116             $output = get_template_directory_uri();
117             break;
118         case 'admin_email':
119             $output = get_option('admin_email');
120             break;
121         case 'charset':
122             $output = get_option('blog_charset');
123             if ('' == $output) $output = 'UTF-8';
124             break;
125         case 'html_type' :
126             $output = get_option('html_type');
127             break;
128         case 'version':
129             global $wp_version;
130             $output = $wp_version;
131             break;
132         case 'language':
133             $output = get_locale();
134             $output = str_replace('_', '-', $output);
135             break;
136         case 'text_direction':
137             global $wp_locale;
138             $output = $wp_locale->text_direction;
139             break;
140         case 'name':
141         default:
142             $output = get_option('blogname');
143             break;
144     }
145     return $output;
146 }
147
148
149 function wp_title($sep = '&raquo;', $display = true) {
150     global $wpdb;
151     global $m, $year, $monthnum, $day, $category_name, $wp_locale, $posts;
152
153     $cat = get_query_var('cat');
154     $p = get_query_var('p');
155     $name = get_query_var('name');
156     $category_name = get_query_var('category_name');
157     $author = get_query_var('author');
158     $author_name = get_query_var('author_name');
159     $title = '';
160
161     // If there's a category
162     if ( !empty($cat) ) {
163             // category exclusion
164             if ( !stristr($cat,'-') )
165                 $title = apply_filters('single_cat_title', get_the_category_by_ID($cat));
166     }
167     if ( !empty($category_name) ) {
168         if ( stristr($category_name,'/') ) {
169                 $category_name = explode('/',$category_name);
170                 if ( $category_name[count($category_name)-1] )
171                     $category_name = $category_name[count($category_name)-1]; // no trailing slash
172                 else
173                     $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
174         }
175         $title = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE category_nicename = '$category_name'");
176         $title = apply_filters('single_cat_title', $title);
177     }
178
179     // If there's an author
180     if ( !empty($author) ) {
181         $title = get_userdata($author);
182         $title = $title->display_name;
183     }
184     if ( !empty($author_name) ) {
185         // We do a direct query here because we don't cache by nicename.
186         $title = $wpdb->get_var("SELECT display_name FROM $wpdb->users WHERE user_nicename = '$author_name'");
187     }
188
189     // If there's a month
190     if ( !empty($m) ) {
191         $my_year = substr($m, 0, 4);
192         $my_month = $wp_locale->get_month($m);
193         $title = "$my_year $sep $my_month";
194     }
195
196     if ( !empty($year) ) {
197         $title = $year;
198         if ( !empty($monthnum) )
199             $title .= " $sep ".$wp_locale->get_month($monthnum);
200         if ( !empty($day) )
201             $title .= " $sep ".zeroise($day, 2);
202     }
203
204     // If there is a post
205     if ( is_single() || is_page() ) {
206         $title = strip_tags($posts[0]->post_title);
207         $title = apply_filters('single_post_title', $title);
208     }
209
210     $prefix = '';
211     if ( !empty($title) )
212         $prefix = " $sep ";
213
214     $title = $prefix . $title;
215     $title = apply_filters('wp_title', $title, $sep);
216
217     // Send it out
218     if ( $display )
219         echo $title;
220     else
221         return $title;
222 }
223
224
225 function single_post_title($prefix = '', $display = true) {
226     global $wpdb;
227     $p = get_query_var('p');
228     $name = get_query_var('name');
229
230     if ( intval($p) || '' != $name ) {
231         if ( !$p )
232             $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
233         $post = & get_post($p);
234         $title = $post->post_title;
235         $title = apply_filters('single_post_title', $title);
236         if ( $display )
237             echo $prefix.strip_tags($title);
238         else
239             return strip_tags($title);
240     }
241 }
242
243
244 function single_cat_title($prefix = '', $display = true ) {
245     $cat = intval( get_query_var('cat') );
246     if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
247         $my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
248         if ( !empty($my_cat_name) ) {
249             if ( $display )
250                 echo $prefix.strip_tags($my_cat_name);
251             else
252                 return strip_tags($my_cat_name);
253         }
254     }
255 }
256
257
258 function single_month_title($prefix = '', $display = true ) {
259     global $m, $monthnum, $wp_locale, $year;
260     if ( !empty($monthnum) && !empty($year) ) {
261         $my_year = $year;
262         $my_month = $wp_locale->get_month($monthnum);
263     } elseif ( !empty($m) ) {
264         $my_year = substr($m, 0, 4);
265         $my_month = $wp_locale->get_month(substr($m, 4, 2));
266     }
267
268     if ( empty($my_month) )
269         return false;
270
271     $result = $prefix . $my_month . $prefix . $my_year;
272
273     if ( !$display )
274         return $result;
275     echo $result;
276 }
277
278
279 /* link navigation hack by Orien http://icecode.com/ */
280 function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
281     $text = wptexturize($text);
282     $title_text = attribute_escape($text);
283
284     if ('link' == $format)
285         return "\t<link rel='archives' title='$title_text' href='$url' />\n";
286     elseif ('option' == $format)
287         return "\t<option value='$url'>$before $text $after</option>\n";
288     elseif ('html' == $format)
289         return "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
290     else // custom
291         return "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
292 }
293
294
295 function wp_get_archives($args = '') {
296     global $wp_locale, $wpdb;
297
298     if ( is_array($args) )
299         $r = &$args;
300     else
301         parse_str($args, $r);
302
303     $defaults = array('type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false);
304     $r = array_merge($defaults, $r);
305     extract($r);
306
307     if ( '' == $type )
308         $type = 'monthly';
309
310     if ( '' != $limit ) {
311         $limit = (int) $limit;
312         $limit = ' LIMIT '.$limit;
313     }
314
315     // this is what will separate dates on weekly archive links
316     $archive_week_separator = '&#8211;';
317
318     // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
319     $archive_date_format_over_ride = 0;
320
321     // options for daily archive (only if you over-ride the general date format)
322     $archive_day_date_format = 'Y/m/d';
323
324     // options for weekly archive (only if you over-ride the general date format)
325     $archive_week_start_date_format = 'Y/m/d';
326     $archive_week_end_date_format    = 'Y/m/d';
327
328     if ( !$archive_date_format_over_ride ) {
329         $archive_day_date_format = get_option('date_format');
330         $archive_week_start_date_format = get_option('date_format');
331         $archive_week_end_date_format = get_option('date_format');
332     }
333
334     $add_hours = intval(get_option('gmt_offset'));
335     $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
336
337     if ( 'monthly' == $type ) {
338         $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit);
339         if ( $arcresults ) {
340             $afterafter = $after;
341             foreach ( $arcresults as $arcresult ) {
342                 $url    = get_month_link($arcresult->year,    $arcresult->month);
343                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
344                 if ( $show_post_count )
345                     $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
346                 echo get_archives_link($url, $text, $format, $before, $after);
347             }
348         }
349     } elseif ('yearly' == $type) {
350          $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts WHERE post_type ='post' AND post_status = 'publish' GROUP BY YEAR(post_date) ORDER BY post_date DESC" . $limit);
351         if ($arcresults) {
352             $afterafter = $after;
353             foreach ($arcresults as $arcresult) {
354                 $url = get_year_link($arcresult->year);
355                 $text = sprintf('%d', $arcresult->year);
356                 if ($show_post_count)
357                     $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
358                 echo get_archives_link($url, $text, $format, $before, $after);
359             }
360         }             
361     } elseif ( 'daily' == $type ) {
362         $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" . $limit);
363         if ( $arcresults ) {
364             $afterafter = $after;
365             foreach ( $arcresults as $arcresult ) {
366                 $url    = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
367                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
368                 $text = mysql2date($archive_day_date_format, $date);
369                 if ($show_post_count)
370                     $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
371                 echo get_archives_link($url, $text, $format, $before, $after);
372             }
373         }
374     } elseif ( 'weekly' == $type ) {
375         $start_of_week = get_option('start_of_week');
376         $arcresults = $wpdb->get_results("SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC" . $limit);
377         $arc_w_last = '';
378         $afterafter = $after;
379         if ( $arcresults ) {
380                 foreach ( $arcresults as $arcresult ) {
381                     if ( $arcresult->week != $arc_w_last ) {
382                         $arc_year = $arcresult->yr;
383                         $arc_w_last = $arcresult->week;
384                         $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
385                         $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
386                         $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
387                         $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', get_option('home'), '', '?', '=', $arc_year, '&amp;', '=', $arcresult->week);
388                         $text = $arc_week_start . $archive_week_separator . $arc_week_end;
389                         if ($show_post_count)
390                             $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
391                         echo get_archives_link($url, $text, $format, $before, $after);
392                     }
393                 }
394         }
395     } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
396         ('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
397         $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY $orderby $limit");
398         if ( $arcresults ) {
399             foreach ( $arcresults as $arcresult ) {
400                 if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
401                     $url  = get_permalink($arcresult);
402                     $arc_title = $arcresult->post_title;
403                     if ( $arc_title )
404                         $text = strip_tags($arc_title);
405                     else
406                         $text = $arcresult->ID;
407                     echo get_archives_link($url, $text, $format, $before, $after);
408                 }
409             }
410         }
411     }
412 }
413
414
415 // Used in get_calendar
416 function calendar_week_mod($num) {
417     $base = 7;
418     return ($num - $base*floor($num/$base));
419 }
420
421
422 function get_calendar($initial = true) {
423     global $wpdb, $m, $monthnum, $year, $timedifference, $wp_locale, $posts;
424
425     $key = md5( $m . $monthnum . $year );
426     if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
427         if ( isset( $cache[ $key ] ) ) {
428             echo $cache[ $key ];
429             return;
430         }
431     }
432
433     ob_start();
434     // Quick check. If we have no posts at all, abort!
435     if ( !$posts ) {
436         $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
437         if ( !$gotsome )
438             return;
439     }
440
441     if ( isset($_GET['w']) )
442         $w = ''.intval($_GET['w']);
443
444     // week_begins = 0 stands for Sunday
445     $week_begins = intval(get_option('start_of_week'));
446     $add_hours = intval(get_option('gmt_offset'));
447     $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
448
449     // Let's figure out when we are
450     if ( !empty($monthnum) && !empty($year) ) {
451         $thismonth = ''.zeroise(intval($monthnum), 2);
452         $thisyear = ''.intval($year);
453     } elseif ( !empty($w) ) {
454         // We need to get the month from MySQL
455         $thisyear = ''