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

Revision 7562, 37.1 kB (checked in by westi, 5 months ago)

Remove the slash from html generators and camelcase WP. Fixes #6440 props lloydbudd.

  • 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( $name = null ) {
24     do_action( 'get_sidebar' );
25     if ( isset($name) && file_exists( TEMPLATEPATH . "/sidebar-{$name}.php") )
26         load_template( TEMPLATEPATH . "/sidebar-{$name}.php");
27     elseif ( file_exists( TEMPLATEPATH . '/sidebar.php') )
28         load_template( TEMPLATEPATH . '/sidebar.php');
29     else
30         load_template( ABSPATH . 'wp-content/themes/default/sidebar.php');
31 }
32
33
34 function wp_loginout() {
35     if ( ! is_user_logged_in() )
36         $link = '<a href="' . get_option('siteurl') . '/wp-login.php">' . __('Log in') . '</a>';
37     else
38         $link = '<a href="' . get_option('siteurl') . '/wp-login.php?action=logout">' . __('Log out') . '</a>';
39
40     echo apply_filters('loginout', $link);
41 }
42
43
44 function wp_register( $before = '<li>', $after = '</li>' ) {
45
46     if ( ! is_user_logged_in() ) {
47         if ( get_option('users_can_register') )
48             $link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after;
49         else
50             $link = '';
51     } else {
52         $link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after;
53     }
54
55     echo apply_filters('register', $link);
56 }
57
58
59 function wp_meta() {
60     do_action('wp_meta');
61 }
62
63
64 function bloginfo($show='') {
65     echo get_bloginfo($show, 'display');
66 }
67
68 /**
69  * Note: some of these values are DEPRECATED. Meaning they could be
70  * taken out at any time and shouldn't be relied upon. Options
71  * without "// DEPRECATED" are the preferred and recommended ways
72  * to get the information.
73  */
74 function get_bloginfo($show = '', $filter = 'raw') {
75
76     switch($show) {
77         case 'url' :
78         case 'home' : // DEPRECATED
79         case 'siteurl' : // DEPRECATED
80             $output = get_option('home');
81             break;
82         case 'wpurl' :
83             $output = get_option('siteurl');
84             break;
85         case 'description':
86             $output = get_option('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_atom_url':
101             $output = get_feed_link('comments_atom');
102             break;
103         case 'comments_rss2_url':
104             $output = get_feed_link('comments_rss2');
105             break;
106         case 'pingback_url':
107             $output = get_option('siteurl') .'/xmlrpc.php';
108             break;
109         case 'stylesheet_url':
110             $output = get_stylesheet_uri();
111             break;
112         case 'stylesheet_directory':
113             $output = get_stylesheet_directory_uri();
114             break;
115         case 'template_directory':
116         case 'template_url':
117             $output = get_template_directory_uri();
118             break;
119         case 'admin_email':
120             $output = get_option('admin_email');
121             break;
122         case 'charset':
123             $output = get_option('blog_charset');
124             if ('' == $output) $output = 'UTF-8';
125             break;
126         case 'html_type' :
127             $output = get_option('html_type');
128             break;
129         case 'version':
130             global $wp_version;
131             $output = $wp_version;
132             break;
133         case 'language':
134             $output = get_locale();
135             $output = str_replace('_', '-', $output);
136             break;
137         case 'text_direction':
138             global $wp_locale;
139             $output = $wp_locale->text_direction;
140             break;
141         case 'name':
142         default:
143             $output = get_option('blogname');
144             break;
145     }
146
147     $url = true;
148     if (strpos($show, 'url') === false &&
149         strpos($show, 'directory') === false &&
150         strpos($show, 'home') === false)
151         $url = false;
152
153     if ( 'display' == $filter ) {
154         if ( $url )
155             $output = apply_filters('bloginfo_url', $output, $show);
156         else
157             $output = apply_filters('bloginfo', $output, $show);
158     }
159
160     return $output;
161 }
162
163
164 function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
165     global $wpdb, $wp_locale, $wp_query;
166
167     $cat = get_query_var('cat');
168     $tag = get_query_var('tag_id');
169     $category_name = get_query_var('category_name');
170     $author = get_query_var('author');
171     $author_name = get_query_var('author_name');
172     $m = get_query_var('m');
173     $year = get_query_var('year');
174     $monthnum = get_query_var('monthnum');
175     $day = get_query_var('day');
176     $title = '';
177
178     // If there's a category
179     if ( !empty($cat) ) {
180             // category exclusion
181             if ( !stristr($cat,'-') )
182                 $title = apply_filters('single_cat_title', get_the_category_by_ID($cat));
183     } elseif ( !empty($category_name) ) {
184         if ( stristr($category_name,'/') ) {
185                 $category_name = explode('/',$category_name);
186                 if ( $category_name[count($category_name)-1] )
187                     $category_name = $category_name[count($category_name)-1]; // no trailing slash
188                 else
189                     $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
190         }
191         $cat = get_term_by('slug', $category_name, 'category', OBJECT, 'display');
192         if ( $cat )
193             $title = apply_filters('single_cat_title', $cat->name);
194     }
195
196     if ( !empty($tag) ) {
197         $tag = get_term($tag, 'post_tag', OBJECT, 'display');
198         if ( is_wp_error( $tag ) )
199             return $tag;
200         if ( ! empty($tag->name) )
201             $title = apply_filters('single_tag_title', $tag->name);
202     }
203
204     // If there's an author
205     if ( !empty($author) ) {
206         $title = get_userdata($author);
207         $title = $title->display_name;
208     }
209     if ( !empty($author_name) ) {
210         // We do a direct query here because we don't cache by nicename.
211         $title = $wpdb->get_var($wpdb->prepare("SELECT display_name FROM $wpdb->users WHERE user_nicename = %s", $author_name));
212     }
213
214     // If there's a month
215     if ( !empty($m) ) {
216         $my_year = substr($m, 0, 4);
217         $my_month = $wp_locale->get_month(substr($m, 4, 2));
218         $my_day = intval(substr($m, 6, 2));
219         $title = "$my_year" . ($my_month ? "$sep $my_month" : "") . ($my_day ? "$sep $my_day" : "");
220     }
221
222     if ( !empty($year) ) {
223         $title = $year;
224         if ( !empty($monthnum) )
225             $title .= " $sep " . $wp_locale->get_month($monthnum);
226         if ( !empty($day) )
227             $title .= " $sep " . zeroise($day, 2);
228     }
229
230     // If there is a post
231     if ( is_single() || is_page() ) {
232         $post = $wp_query->get_queried_object();
233         $title = strip_tags( apply_filters( 'single_post_title', $post->post_title ) );
234     }
235
236     $prefix = '';
237     if ( !empty($title) )
238         $prefix = " $sep ";
239
240      // Determines position of the separator
241     if ( 'right' == $seplocation )
242         $title = $title . $prefix;
243     else
244         $title = $prefix . $title;
245
246     $title = apply_filters('wp_title', $title, $sep);
247
248     // Send it out
249     if ( $display )
250         echo $title;
251     else
252         return $title;
253
254 }
255
256
257 function single_post_title($prefix = '', $display = true) {
258     global $wpdb;
259     $p = get_query_var('p');
260     $name = get_query_var('name');
261
262     if ( intval($p) || '' != $name ) {
263         if ( !$p )
264             $p = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name));
265         $post = & get_post($p);
266         $title = $post->post_title;
267         $title = apply_filters('single_post_title', $title);
268         if ( $display )
269             echo $prefix.strip_tags($title);
270         else
271             return strip_tags($title);
272     }
273 }
274
275
276 function single_cat_title($prefix = '', $display = true ) {
277     $cat = intval( get_query_var('cat') );
278     if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
279         $my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
280         if ( !empty($my_cat_name) ) {
281             if ( $display )
282                 echo $prefix.strip_tags($my_cat_name);
283             else
284                 return strip_tags($my_cat_name);
285         }
286     } else if ( is_tag() ) {
287         return single_tag_title($prefix, $display);
288     }
289 }
290
291
292 function single_tag_title($prefix = '', $display = true ) {
293     if ( !is_tag() )
294         return;
295
296     $tag_id = intval( get_query_var('tag_id') );
297
298     if ( !empty($tag_id) ) {
299         $my_tag = &get_term($tag_id, 'post_tag', OBJECT, 'display');
300         if ( is_wp_error( $my_tag ) )
301             return false;
302         $my_tag_name = apply_filters('single_tag_title', $my_tag->name);
303         if ( !empty($my_tag_name) ) {
304             if ( $display )
305                 echo $prefix . $my_tag_name;
306             else
307                 return $my_tag_name;
308         }
309     }
310 }
311
312
313 function single_month_title($prefix = '', $display = true ) {
314     global $wp_locale;
315
316     $m = get_query_var('m');
317     $year = get_query_var('year');
318     $monthnum = get_query_var('monthnum');
319
320     if ( !empty($monthnum) && !empty($year) ) {
321         $my_year = $year;
322         $my_month = $wp_locale->get_month($monthnum);
323     } elseif ( !empty($m) ) {
324         $my_year = substr($m, 0, 4);
325         $my_month = $wp_locale->get_month(substr($m, 4, 2));
326     }
327
328     if ( empty($my_month) )
329         return false;
330
331     $result = $prefix . $my_month . $prefix . $my_year;
332
333     if ( !$display )
334         return $result;
335     echo $result;
336 }
337
338
339 /* link navigation hack by Orien http://icecode.com/ */
340 function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
341     $text = wptexturize($text);
342     $title_text = attribute_escape($text);
343     $url = clean_url($url);
344
345     if ('link' == $format)
346         return "\t<link rel='archives' title='$title_text' href='$url' />\n";
347     elseif ('option' == $format)
348         return "\t<option value='$url'>$before $text $after</option>\n";
349     elseif ('html' == $format)
350         return "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
351     else // custom
352         return "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
353 }
354
355
356 function wp_get_archives($args = '') {
357     global $wpdb, $wp_locale;
358
359     $defaults = array(
360         'type' => 'monthly', 'limit' => '',
361         'format' => 'html', 'before' => '',
362         'after' => '', 'show_post_count' => false
363     );
364
365     $r = wp_parse_args( $args, $defaults );
366     extract( $r, EXTR_SKIP );
367
368     if ( '' == $type )
369         $type = 'monthly';
370
371     if ( '' != $limit ) {
372         $limit = absint($limit);
373         $limit = ' LIMIT '.$limit;
374     }
375
376     // this is what will separate dates on weekly archive links
377     $archive_week_separator = '&#8211;';
378
379     // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
380     $archive_date_format_over_ride = 0;
381
382     // options for daily archive (only if you over-ride the general date format)
383     $archive_day_date_format = 'Y/m/d';
384
385     // options for weekly archive (only if you over-ride the general date format)
386     $archive_week_start_date_format = 'Y/m/d';
387     $archive_week_end_date_format    = 'Y/m/d';
388
389     if ( !$archive_date_format_over_ride ) {
390         $archive_day_date_format = get_option('date_format');
391         $archive_week_start_date_format = get_option('date_format');
392         $archive_week_end_date_format = get_option('date_format');
393     }
394
395     //filters
396     $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
397     $join = apply_filters('getarchives_join', "", $r);
398
399     if ( 'monthly' == $type ) {
400         $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
401         $key = md5($query);
402         $cache = wp_cache_get( 'wp_get_archives' , 'general');
403         if ( !isset( $cache[ $key ] ) ) {
404             $arcresults = $wpdb->get_results($query);
405             $cache[ $key ] = $arcresults;
406             wp_cache_add( 'wp_get_archives', $cache, 'general' );
407         } else {
408             $arcresults = $cache[ $key ];
409         }
410         if ( $arcresults ) {
411             $afterafter = $after;
412             foreach ( $arcresults as $arcresult ) {
413                 $url    = get_month_link($arcresult->year,    $arcresult->month);
414                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
415                 if ( $show_post_count )
416                     $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
417                 echo get_archives_link($url, $text, $format, $before, $after);
418             }
419         }
420     } elseif ('yearly' == $type) {
421         $query = "SELECT DISTINCT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit";
422         $key = md5($query);
423         $cache = wp_cache_get( 'wp_get_archives' , 'general');
424         if ( !isset( $cache[ $key ] ) ) {
425             $arcresults = $wpdb->get_results($query);
426             $cache[ $key ] = $arcresults;
427             wp_cache_add( 'wp_get_archives', $cache, 'general' );
428         } else {
429             $arcresults = $cache[ $key ];
430         }
431         if ($arcresults) {
432             $afterafter = $after;
433             foreach ($arcresults as $arcresult) {
434                 $url = get_year_link($arcresult->year);
435                 $text = sprintf('%d', $arcresult->year);
436                 if ($show_post_count)
437                     $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
438                 echo get_archives_link($url, $text, $format, $before, $after);
439             }
440         }
441     } elseif ( 'daily' == $type ) {
442         $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit";
443         $key = md5($query);
444         $cache = wp_cache_get( 'wp_get_archives' , 'general');
445         if ( !isset( $cache[ $key ] ) ) {
446             $arcresults = $wpdb->get_results($query);
447             $cache[ $key ] = $arcresults;
448             wp_cache_add( 'wp_get_archives', $cache, 'general' );
449         } else {
450             $arcresults = $cache[ $key ];
451         }
452         if ( $arcresults ) {
453             $afterafter = $after;
454             foreach ( $arcresults as $arcresult ) {
455                 $url    = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
456                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
457                 $text = mysql2date($archive_day_date_format, $date);
458                 if ($show_post_count)
459                     $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
460                 echo get_archives_link($url, $text, $format, $before, $after);
461             }
462         }
463     } elseif ( 'weekly' == $type ) {
464         $start_of_week = get_option('start_of_week');
465         $query = "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 $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit";
466         $key