root/branches/2.0/wp-includes/template-functions-general.php

Revision 5035, 21.6 kB (checked in by ryan, 2 years ago)

wp_title() fixes from dwc. fixes #3967

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