]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/general-template.php
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / general-template.php
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         do_action( 'get_sidebar' );
25         if ( file_exists( TEMPLATEPATH . '/sidebar.php') )
26                 load_template( TEMPLATEPATH . '/sidebar.php');
27         else
28                 load_template( ABSPATH . 'wp-content/themes/default/sidebar.php');
29 }
30
31
32 function wp_loginout() {
33         if ( ! is_user_logged_in() )
34                 $link = '<a href="' . get_option('siteurl') . '/wp-login.php">' . __('Login') . '</a>';
35         else
36                 $link = '<a href="' . get_option('siteurl') . '/wp-login.php?action=logout">' . __('Logout') . '</a>';
37
38         echo apply_filters('loginout', $link);
39 }
40
41
42 function wp_register( $before = '<li>', $after = '</li>' ) {
43
44         if ( ! is_user_logged_in() ) {
45                 if ( get_option('users_can_register') )
46                         $link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after;
47                 else
48                         $link = '';
49         } else {
50                 $link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after;
51         }
52
53         echo apply_filters('register', $link);
54 }
55
56
57 function wp_meta() {
58         do_action('wp_meta');
59 }
60
61
62 function bloginfo($show='') {
63         echo get_bloginfo($show, 'display');
64 }
65
66 /**
67  * Note: some of these values are DEPRECATED. Meaning they could be
68  * taken out at any time and shouldn't be relied upon. Options
69  * without "// DEPRECATED" are the preferred and recommended ways
70  * to get the information.
71  */
72 function get_bloginfo($show = '', $filter = 'raw') {
73
74         switch($show) {
75                 case 'url' :
76                 case 'home' : // DEPRECATED
77                 case 'siteurl' : // DEPRECATED
78                         $output = get_option('home');
79                         break;
80                 case 'wpurl' :
81                         $output = get_option('siteurl');
82                         break;
83                 case 'description':
84                         $output = get_option('blogdescription');
85                         break;
86                 case 'rdf_url':
87                         $output = get_feed_link('rdf');
88                         break;
89                 case 'rss_url':
90                         $output = get_feed_link('rss');
91                         break;
92                 case 'rss2_url':
93                         $output = get_feed_link('rss2');
94                         break;
95                 case 'atom_url':
96                         $output = get_feed_link('atom');
97                         break;
98                 case 'comments_atom_url':
99                         $output = get_feed_link('comments_atom');
100                         break;
101                 case 'comments_rss2_url':
102                         $output = get_feed_link('comments_rss2');
103                         break;
104                 case 'pingback_url':
105                         $output = get_option('siteurl') .'/xmlrpc.php';
106                         break;
107                 case 'stylesheet_url':
108                         $output = get_stylesheet_uri();
109                         break;
110                 case 'stylesheet_directory':
111                         $output = get_stylesheet_directory_uri();
112                         break;
113                 case 'template_directory':
114                 case 'template_url':
115                         $output = get_template_directory_uri();
116                         break;
117                 case 'admin_email':
118                         $output = get_option('admin_email');
119                         break;
120                 case 'charset':
121                         $output = get_option('blog_charset');
122                         if ('' == $output) $output = 'UTF-8';
123                         break;
124                 case 'html_type' :
125                         $output = get_option('html_type');
126                         break;
127                 case 'version':
128                         global $wp_version;
129                         $output = $wp_version;
130                         break;
131                 case 'language':
132                         $output = get_locale();
133                         $output = str_replace('_', '-', $output);
134                         break;
135                 case 'text_direction':
136                         global $wp_locale;
137                         $output = $wp_locale->text_direction;
138                         break;
139                 case 'name':
140                 default:
141                         $output = get_option('blogname');
142                         break;
143         }
144
145         $url = true;
146         if (strpos($show, 'url') === false &&
147                 strpos($show, 'directory') === false &&
148                 strpos($show, 'home') === false)
149                 $url = false;
150
151         if ( 'display' == $filter ) {
152                 if ( $url )
153                         $output = apply_filters('bloginfo_url', $output, $show);
154                 else
155                         $output = apply_filters('bloginfo', $output, $show);
156         }
157
158         return $output;
159 }
160
161
162 function wp_title($sep = '&raquo;', $display = true) {
163         global $wpdb, $wp_locale, $wp_query;
164
165         $cat = get_query_var('cat');
166         $tag = get_query_var('tag_id');
167         $p = get_query_var('p');
168         $name = get_query_var('name');
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("SELECT display_name FROM $wpdb->users WHERE user_nicename = '$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         $title = $prefix . $title;
241         $title = apply_filters('wp_title', $title, $sep);
242
243         // Send it out
244         if ( $display )
245                 echo $title;
246         else
247                 return $title;
248 }
249
250
251 function single_post_title($prefix = '', $display = true) {
252         global $wpdb;
253         $p = get_query_var('p');
254         $name = get_query_var('name');
255
256         if ( intval($p) || '' != $name ) {
257                 if ( !$p )
258                         $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
259                 $post = & get_post($p);
260                 $title = $post->post_title;
261                 $title = apply_filters('single_post_title', $title);
262                 if ( $display )
263                         echo $prefix.strip_tags($title);
264                 else
265                         return strip_tags($title);
266         }
267 }
268
269
270 function single_cat_title($prefix = '', $display = true ) {
271         $cat = intval( get_query_var('cat') );
272         if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
273                 $my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
274                 if ( !empty($my_cat_name) ) {
275                         if ( $display )
276                                 echo $prefix.strip_tags($my_cat_name);
277                         else
278                                 return strip_tags($my_cat_name);
279                 }
280         } else if ( is_tag() ) {
281                 return single_tag_title($prefix, $display);
282         }
283 }
284
285
286 function single_tag_title($prefix = '', $display = true ) {
287         if ( !is_tag() )
288                 return;
289
290         $tag_id = intval( get_query_var('tag_id') );
291
292         if ( !empty($tag_id) ) {
293                 $my_tag = &get_term($tag_id, 'post_tag', OBJECT, 'display');
294                 if ( is_wp_error( $my_tag ) ) 
295                         return false;
296                 $my_tag_name = apply_filters('single_tag_title', $my_tag->name);
297                 if ( !empty($my_tag_name) ) {
298                         if ( $display )
299                                 echo $prefix . $my_tag_name;
300                         else
301                                 return $my_tag_name;
302                 }
303         }
304 }
305
306
307 function single_month_title($prefix = '', $display = true ) {
308         global $wp_locale;
309
310         $m = get_query_var('m');
311         $year = get_query_var('year');
312         $monthnum = get_query_var('monthnum');
313
314         if ( !empty($monthnum) && !empty($year) ) {
315                 $my_year = $year;
316                 $my_month = $wp_locale->get_month($monthnum);
317         } elseif ( !empty($m) ) {
318                 $my_year = substr($m, 0, 4);
319                 $my_month = $wp_locale->get_month(substr($m, 4, 2));
320         }
321
322         if ( empty($my_month) )
323                 return false;
324
325         $result = $prefix . $my_month . $prefix . $my_year;
326
327         if ( !$display )
328                 return $result;
329         echo $result;
330 }
331
332
333 /* link navigation hack by Orien http://icecode.com/ */
334 function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
335         $text = wptexturize($text);
336         $title_text = attribute_escape($text);
337         $url = clean_url($url);
338
339         if ('link' == $format)
340                 return "\t<link rel='archives' title='$title_text' href='$url' />\n";
341         elseif ('option' == $format)
342                 return "\t<option value='$url'>$before $text $after</option>\n";
343         elseif ('html' == $format)
344                 return "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
345         else // custom
346                 return "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
347 }
348
349
350 function wp_get_archives($args = '') {
351         global $wpdb, $wp_locale;
352
353         $defaults = array(
354                 'type' => 'monthly', 'limit' => '',
355                 'format' => 'html', 'before' => '',
356                 'after' => '', 'show_post_count' => false
357         );
358
359         $r = wp_parse_args( $args, $defaults );
360         extract( $r, EXTR_SKIP );
361
362         if ( '' == $type )
363                 $type = 'monthly';
364
365         if ( '' != $limit ) {
366                 $limit = (int) $limit;
367                 $limit = ' LIMIT '.$limit;
368         }
369
370         // this is what will separate dates on weekly archive links
371         $archive_week_separator = '&#8211;';
372
373         // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
374         $archive_date_format_over_ride = 0;
375
376         // options for daily archive (only if you over-ride the general date format)
377         $archive_day_date_format = 'Y/m/d';
378
379         // options for weekly archive (only if you over-ride the general date format)
380         $archive_week_start_date_format = 'Y/m/d';
381         $archive_week_end_date_format   = 'Y/m/d';
382
383         if ( !$archive_date_format_over_ride ) {
384                 $archive_day_date_format = get_option('date_format');
385                 $archive_week_start_date_format = get_option('date_format');
386                 $archive_week_end_date_format = get_option('date_format');
387         }
388
389         $add_hours = intval(get_option('gmt_offset'));
390         $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
391
392         //filters
393         $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
394         $join = apply_filters('getarchives_join', "", $r);
395
396         if ( 'monthly' == $type ) {
397                 $arcresults = $wpdb->get_results("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);
398                 if ( $arcresults ) {
399                         $afterafter = $after;
400                         foreach ( $arcresults as $arcresult ) {
401                                 $url    = get_month_link($arcresult->year,      $arcresult->month);
402                                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
403                                 if ( $show_post_count )
404                                         $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
405                                 echo get_archives_link($url, $text, $format, $before, $after);
406                         }
407                 }
408         } elseif ('yearly' == $type) {
409          $arcresults = $wpdb->get_results("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);
410                 if ($arcresults) {
411                         $afterafter = $after;
412                         foreach ($arcresults as $arcresult) {
413                                 $url = get_year_link($arcresult->year);
414                                 $text = sprintf('%d', $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 ( 'daily' == $type ) {
421                 $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 $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" . $limit);
422                 if ( $arcresults ) {
423                         $afterafter = $after;
424                         foreach ( $arcresults as $arcresult ) {
425                                 $url    = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
426                                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
427                                 $text = mysql2date($archive_day_date_format, $date);
428                                 if ($show_post_count)
429                                         $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
430                                 echo get_archives_link($url, $text, $format, $before, $after);
431                         }
432                 }
433         } elseif ( 'weekly' == $type ) {
434                 $start_of_week = get_option('start_of_week');
435                 $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 $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC" . $limit);
436                 $arc_w_last = '';
437                 $afterafter = $after;
438                 if ( $arcresults ) {
439                                 foreach ( $arcresults as $arcresult ) {
440                                         if ( $arcresult->week != $arc_w_last ) {
441                                                 $arc_year = $arcresult->yr;
442                                                 $arc_w_last = $arcresult->week;
443                                                 $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
444                                                 $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
445                                                 $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
446                                                 $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);
447                                                 $text = $arc_week_start . $archive_week_separator . $arc_week_end;
448                                                 if ($show_post_count)
449                                                         $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
450                                                 echo get_archives_link($url, $text, $format, $before, $after);
451                                         }
452                                 }
453                 }
454         } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
455                 ('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
456                 $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit");
457                 if ( $arcresults ) {
458                         foreach ( $arcresults as $arcresult ) {
459                                 if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
460                                         $url  = get_permalink($arcresult);
461                                         $arc_title = $arcresult->post_title;
462                                         if ( $arc_title )
463                                                 $text = strip_tags(apply_filters('the_title', $arc_title));
464                                         else
465                                                 $text = $arcresult->ID;
466                                         echo get_archives_link($url, $text, $format, $before, $after);
467                                 }
468                         }
469                 }
470         }
471 }
472
473
474 // Used in get_calendar
475 function calendar_week_mod($num) {
476         $base = 7;
477         return ($num - $base*floor($num/$base));
478 }
479
480
481 function get_calendar($initial = true) {
482         global $wpdb, $m, $monthnum, $year, $timedifference, $wp_locale, $posts;
483
484         $key = md5( $m . $monthnum . $year );
485         if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
486                 if ( isset( $cache[ $key ] ) ) {
487                         echo $cache[ $key ];
488                         return;
489                 }
490         }
491
492         ob_start();
493         // Quick check. If we have no posts at all, abort!
494         if ( !$posts ) {
495                 $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
496                 if ( !$gotsome )
497                         return;
498         }
499
500         if ( isset($_GET['w']) )
501                 $w = ''.intval($_GET['w']);
502
503         // week_begins = 0 stands for Sunday
504         $week_begins = intval(get_option('start_of_week'));
505         $add_hours = intval(get_option('gmt_offset'));
506         $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
507
508         // Let's figure out when we are
509         if ( !empty($monthnum) && !empty($year) ) {
510                 $thismonth = ''.zeroise(intval($monthnum), 2);
511                 $thisyear = ''.intval($year);
512         } elseif ( !empty($w) ) {
513                 // We need to get the month from MySQL
514                 $thisyear = ''.intval(substr($m, 0, 4));
515                 $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
516                 $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')");
517         } elseif ( !empty($m) ) {
518                 $calendar = substr($m, 0, 6);
519                 $thisyear = ''.intval(substr($m, 0, 4));
520                 if ( strlen($m) < 6 )
521                                 $thismonth = '01';
522                 else
523                                 $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
524         } else {
525                 $thisyear = gmdate('Y', current_time('timestamp'));
526                 $thismonth = gmdate('m', current_time('timestamp'));
527         }
528
529         $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
530
531         // Get the next and previous month and year with at least one post
532         $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
533                 FROM $wpdb->posts
534                 WHERE post_date < '$thisyear-$thismonth-01'
535                 AND post_type = 'post' AND post_status = 'publish'
536                         ORDER BY post_date DESC
537                         LIMIT 1");
538         $next = $wpdb->get_row("SELECT  DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
539                 FROM $wpdb->posts
540                 WHERE post_date >       '$thisyear-$thismonth-01'
541                 AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
542                 AND post_type = 'post' AND post_status = 'publish'
543                         ORDER   BY post_date ASC
544                         LIMIT 1");
545
546         echo '<table id="wp-calendar" summary="' . __('Calendar') . '">
547         <caption>' . $wp_locale->get_month($thismonth) . ' ' . date('Y', $unixmonth) . '</caption>
548         <thead>
549         <tr>';
550
551         $myweek = array();
552
553         for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
554                 $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
555         }
556
557         foreach ( $myweek as $wd ) {
558                 $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
559                 echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">$day_name</th>";
560         }
561
562         echo '
563         </tr>
564         </thead>
565
566         <tfoot>
567         <tr>';
568
569         if ( $previous ) {
570                 echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($previous->month) . '" colspan="3" id="prev"><a href="' .
571                 get_month_link($previous->year, $previous->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month),
572                         date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
573         } else {
574                 echo "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
575         }
576
577         echo "\n\t\t".'<td class="pad">&nbsp;</td>';
578
579         if ( $next ) {
580                 echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($next->month) . '" colspan="3" id="next"><a href="' .
581                 get_month_link($next->year, $next->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month),
582                         date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
583         } else {
584                 echo "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
585         }
586
587         echo '
588         </tr>
589         </tfoot>
590
591         <tbody>
592         <tr>';
593
594         // Get days with posts
595         $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
596                 FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
597                 AND YEAR(post_date) = '$thisyear'
598                 AND post_type = 'post' AND post_status = 'publish'
599                 AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
600         if ( $dayswithposts ) {
601                 foreach ( $dayswithposts as $daywith ) {
602                         $daywithpost[] = $daywith[0];
603                 }
604         } else {
605                 $daywithpost = array();
606         }
607
608         if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false)
609                 $ak_title_separator = "\n";
610         else
611                 $ak_title_separator = ', ';
612
613         $ak_titles_for_day = array();
614         $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
615                 ."FROM $wpdb->posts "
616                 ."WHERE YEAR(post_date) = '$thisyear' "
617                 ."AND MONTH(post_date) = '$thismonth' "
618                 ."AND post_date < '".current_time('mysql')."' "
619                 ."AND post_type = 'post' AND post_status = 'publish'"
620         );
621         if ( $ak_post_titles ) {
622                 foreach ( $ak_post_titles as $ak_post_title ) {
623
624                                 $post_title = apply_filters( "the_title", $ak_post_title->post_title );
625                                 $post_title = str_replace('"', '&quot;', wptexturize( $post_title ));
626
627                                 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
628                                         $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
629                                 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
630                                         $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
631                                 else
632                                         $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
633                 }
634         }
635
636
637         // See how much we should pad in the beginning
638         $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
639         if ( 0 != $pad )
640                 echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>';
641
642         $daysinmonth = intval(date('t', $unixmonth));
643         for ( $day = 1; $day <= $daysinmonth; ++$day ) {
644                 if ( isset($newrow) && $newrow )
645                         echo "\n\t</tr>\n\t<tr>\n\t\t";
646                 $newrow = false;
647
648                 if ( $day == gmdate('j', (time() + (get_option('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_option('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_option('gmt_offset') * 3600)) )
649                         echo '<td id="today">';
650                 else
651                         echo '<td>';
652
653                 if ( in_array($day, $daywithpost) ) // any posts today?
654                                 echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>";
655                 else
656                         echo $day;
657                 echo '</td>';
658
659                 if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
660                         $newrow = true;
661         }
662
663         $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
664         if ( $pad != 0 && $pad != 7 )
665                 echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
666
667         echo "\n\t</tr>\n\t</tbody>\n\t</table>";
668
669         $output = ob_get_contents();
670         ob_end_clean();
671         echo $output;
672         $cache[ $key ] = $output;
673         wp_cache_set( 'get_calendar', $cache, 'calendar' );
674 }
675
676 function delete_get_calendar_cache() {
677         wp_cache_delete( 'get_calendar', 'calendar' );
678 }
679 add_action( 'save_post', 'delete_get_calendar_cache' );
680 add_action( 'delete_post', 'delete_get_calendar_cache' );
681 add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' );
682 add_action( 'update_option_gmt_offset', 'delete_get_calendar_cache' );
683 add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' );
684
685
686 function allowed_tags() {
687         global $allowedtags;
688         $allowed = '';
689         foreach ( $allowedtags as $tag => $attributes ) {
690                 $allowed .= '<'.$tag;
691                 if ( 0 < count($attributes) ) {
692                         foreach ( $attributes as $attribute => $limits ) {
693                                 $allowed .= ' '.$attribute.'=""';
694                         }
695                 }
696                 $allowed .= '> ';
697         }
698         return htmlentities($allowed);
699 }
700
701
702 /***** Date/Time tags *****/
703
704
705 function the_date_xml() {
706         global $post;
707         echo mysql2date('Y-m-d', $post->post_date);
708         //echo ""+$post->post_date;
709 }
710
711
712 function the_date($d='', $before='', $after='', $echo = true) {
713         global $id, $post, $day, $previousday;
714         $the_date = '';
715         if ( $day != $previousday ) {
716                 $the_date .= $before;
717                 if ( $d=='' )
718                         $the_date .= mysql2date(get_option('date_format'), $post->post_date);
719                 else
720                         $the_date .= mysql2date($d, $post->post_date);
721                 $the_date .= $after;
722                 $previousday = $day;
723         }
724         $the_date = apply_filters('the_date', $the_date, $d, $before, $after);
725         if ( $echo )
726                 echo $the_date;
727         else
728                 return $the_date;
729 }
730
731
732 function the_modified_date($d = '') {
733         echo apply_filters('the_modified_date', get_the_modified_date($d), $d);
734 }
735
736
737 function get_the_modified_date($d = '') {
738         if ( '' == $d )
739                 $the_time = get_post_modified_time(get_option('date_format'));
740         else
741                 $the_time = get_post_modified_time($d);
742         return apply_filters('get_the_modified_date', $the_time, $d);
743 }
744
745
746 function the_time( $d = '' ) {
747         echo apply_filters('the_time', get_the_time( $d ), $d);
748 }
749
750
751 function get_the_time( $d = '' ) {
752         if ( '' == $d )
753                 $the_time = get_post_time(get_option('time_format'));
754         else
755                 $the_time = get_post_time($d);
756         return apply_filters('get_the_time', $the_time, $d);
757 }
758
759
760 function get_post_time( $d = 'U', $gmt = false ) { // returns timestamp
761         global $post;
762         if ( $gmt )
763                 $time = $post->post_date_gmt;
764         else
765                 $time = $post->post_date;
766
767         $time = mysql2date($d, $time);
768         return apply_filters('get_the_time', $time, $d, $gmt);
769 }
770
771
772 function the_modified_time($d = '') {
773         echo apply_filters('the_modified_time', get_the_modified_time($d), $d);
774 }
775
776
777 function get_the_modified_time($d = '') {
778         if ( '' == $d )
779                 $the_time = get_post_modified_time(get_option('time_format'));
780         else
781                 $the_time = get_post_modified_time($d);
782         return apply_filters('get_the_modified_time', $the_time, $d);
783 }
784
785
786 function get_post_modified_time( $d = 'U', $gmt = false ) { // returns timestamp
787         global $post;
788
789         if ( $gmt )
790                 $time = $post->post_modified_gmt;
791         else
792                 $time = $post->post_modified;
793         $time = mysql2date($d, $time);
794
795         return apply_filters('get_the_modified_time', $time, $d, $gmt);
796 }
797
798
799 function the_weekday() {
800         global $wp_locale, $id, $post;
801         $the_weekday = $wp_locale->get_weekday(mysql2date('w', $post->post_date));
802         $the_weekday = apply_filters('the_weekday', $the_weekday);
803         echo $the_weekday;
804 }
805
806
807 function the_weekday_date($before='',$after='') {
808         global $wp_locale, $id, $post, $day, $previousweekday;
809         $the_weekday_date = '';
810         if ( $day != $previousweekday ) {
811                 $the_weekday_date .= $before;
812                 $the_weekday_date .= $wp_locale->get_weekday(mysql2date('w', $post->post_date));
813                 $the_weekday_date .= $after;
814                 $previousweekday = $day;
815         }
816         $the_weekday_date = apply_filters('the_weekday_date', $the_weekday_date, $before, $after);
817         echo $the_weekday_date;
818 }
819
820 function wp_head() {
821         do_action('wp_head');
822 }
823
824 function wp_footer() {
825         do_action('wp_footer');
826 }
827
828 function rsd_link() {
829         echo '  <link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . get_bloginfo('wpurl') . "/xmlrpc.php?rsd\" />\n";
830 }
831
832 function wlwmanifest_link() {
833         echo ' <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="'
834                 . get_bloginfo('wpurl') . '/wp-includes/wlwmanifest.xml" /> ';
835 }
836
837 function noindex() {
838         // If the blog is not public, tell robots to go away.
839         if ( '0' == get_option('blog_public') )
840                 echo "<meta name='robots' content='noindex,nofollow' />\n";
841 }
842
843 function rich_edit_exists() {
844         global $wp_rich_edit_exists;
845         if ( !isset($wp_rich_edit_exists) )
846                 $wp_rich_edit_exists = file_exists(ABSPATH . WPINC . '/js/tinymce/tiny_mce.js');
847         return $wp_rich_edit_exists;
848 }
849
850 function user_can_richedit() {
851         global $wp_rich_edit, $pagenow;
852
853         if ( !isset( $wp_rich_edit) ) {
854                 if ( get_user_option( 'rich_editing' ) == 'true' &&
855                         ( ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && intval($match[1]) >= 420 ) ||
856                                 !preg_match( '!opera[ /][2-8]|konqueror|safari!i', $_SERVER['HTTP_USER_AGENT'] ) )
857                                 && 'comment.php' != $pagenow ) {
858                         $wp_rich_edit = true;
859                 } else {
860                         $wp_rich_edit = false;
861                 }
862         }
863
864         return apply_filters('user_can_richedit', $wp_rich_edit);
865 }
866
867 function the_editor($content, $id = 'content', $prev_id = 'title') {
868         $rows = get_option('default_post_edit_rows');
869         if (($rows < 3) || ($rows > 100))
870                 $rows = 12;
871
872         $rows = "rows='$rows'";
873
874         if ( user_can_richedit() ) :
875                 add_filter('the_editor_content', 'wp_richedit_pre');
876
877                 //      The following line moves the border so that the active button "attaches" to the toolbar. Only IE needs it.
878         ?>
879         <style type="text/css">
880                 #postdivrich table, #postdivrich #quicktags {border-top: none;}
881                 #quicktags {border-bottom: none; padding-bottom: 2px; margin-bottom: -1px;}
882                 #edButtons {border-bottom: 1px solid #ccc;}
883         </style>
884         <div id='edButtons' style='display:none;'>
885                 <div class='zerosize'><input accesskey='e' type='button' onclick='switchEditors("<?php echo $id; ?>")' /></div>
886                 <input id='edButtonPreview' class='edButtonFore' type='button' value='<?php _e('Visual'); ?>' />
887                 <input id='edButtonHTML' class='edButtonBack' type='button' value='<?php _e('Code'); ?>' onclick='switchEditors("<?php echo $id; ?>")' />
888         </div>
889         <script type="text/javascript">
890         // <![CDATA[
891                 if ( typeof tinyMCE != "undefined" && tinyMCE.configs.length > 0 )
892                         document.getElementById('edButtons').style.display = 'block';
893         // ]]>
894         </script>
895
896         <?php endif; ?>
897         <div id="quicktags">
898         <?php wp_print_scripts( 'quicktags' ); ?>
899         <script type="text/javascript">edToolbar()</script>
900         </div>
901         <script type="text/javascript">
902         // <![CDATA[
903                 if ( typeof tinyMCE != "undefined" && tinyMCE.configs.length > 0 )
904                         document.getElementById("quicktags").style.display="none";
905         // ]]>
906         </script>
907         <?php
908
909         $the_editor = apply_filters('the_editor', "<div><textarea class='mceEditor' $rows cols='40' name='$id' tabindex='2' id='$id'>%s</textarea></div>\n");
910         $the_editor_content = apply_filters('the_editor_content', $content);
911
912         printf($the_editor, $the_editor_content);
913
914         ?>
915         <script type="text/javascript">
916         //<!--
917         edCanvas = document.getElementById('<?php echo $id; ?>');
918         <?php if ( $prev_id && user_can_richedit() ) : ?>
919         // If tinyMCE is defined.
920         if ( typeof tinyMCE != 'undefined' ) {
921         // This code is meant to allow tabbing from Title to Post (TinyMCE).
922                 if ( tinyMCE.isMSIE ) {
923                         document.getElementById('<?php echo $prev_id; ?>').onkeydown = function (e) {
924                                 e = e ? e : window.event;
925                                 if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
926                                         var i = tinyMCE.getInstanceById('<?php echo $id; ?>');
927                                         if(typeof i ==  'undefined')
928                                                 return true;
929                                         tinyMCE.execCommand("mceStartTyping");
930                                         this.blur();
931                                         i.contentWindow.focus();
932                                         e.returnValue = false;
933                                         return false;
934                                 }
935                         }
936                 } else {
937                         document.getElementById('<?php echo $prev_id; ?>').onkeypress = function (e) {
938                                 e = e ? e : window.event;
939                                 if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
940                                         var i = tinyMCE.getInstanceById('<?php echo $id; ?>');
941                                         if(typeof i ==  'undefined')
942                                                 return true;
943                                         tinyMCE.execCommand("mceStartTyping");
944                                         this.blur();
945                                         i.contentWindow.focus();
946                                         e.returnValue = false;
947                                         return false;
948                                 }
949                         }
950                 }
951         }
952         <?php endif; ?>
953         //-->
954         </script>
955         <?php
956 }
957
958 function get_search_query() {
959         return apply_filters( 'get_search_query', stripslashes( get_query_var( 's' ) ) );
960 }
961
962 function the_search_query() {
963         echo attribute_escape( apply_filters( 'the_search_query', get_search_query() ) );
964 }
965
966 function language_attributes() {
967         $output = '';
968         if ( $dir = get_bloginfo('text_direction') )
969                 $output = "dir=\"$dir\"";
970         if ( $lang = get_bloginfo('language') ) {
971                 if ( $dir ) $output .= ' ';
972                 if ( get_option('html_type') == 'text/html' )
973                         $output .= "lang=\"$lang\"";
974                 else $output .= "xml:lang=\"$lang\"";
975         }
976
977         echo $output;
978 }
979
980 function paginate_links( $args = '' ) {
981         $defaults = array(
982                 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
983                 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
984                 'total' => 1,
985                 'current' => 0,
986                 'show_all' => false,
987                 'prev_next' => true,
988                 'prev_text' => __('&laquo; Previous'),
989                 'next_text' => __('Next &raquo;'),
990                 'end_size' => 1, // How many numbers on either end including the end
991                 'mid_size' => 2, // How many numbers to either side of current not including current
992                 'type' => 'plain',
993                 'add_args' => false // array of query args to aadd
994         );
995
996         $args = wp_parse_args( $args, $defaults );
997         extract($args, EXTR_SKIP);
998
999         // Who knows what else people pass in $args
1000         $total    = (int) $total;
1001         if ( $total < 2 )
1002                 return;
1003         $current  = (int) $current;
1004         $end_size = 0  < (int) $end_size ? (int) $end_size : 1; // Out of bounds?  Make it the default.
1005         $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
1006         $add_args = is_array($add_args) ? $add_args : false;
1007         $r = '';
1008         $page_links = array();
1009         $n = 0;
1010         $dots = false;
1011
1012         if ( $prev_next && $current && 1 < $current ) :
1013                 $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
1014                 $link = str_replace('%#%', $current - 1, $link);
1015                 if ( $add_args )
1016                         $link = add_query_arg( $add_args, $link );
1017                 $page_links[] = "<a class='prev page-numbers' href='" . clean_url($link) . "'>$prev_text</a>";
1018         endif;
1019         for ( $n = 1; $n <= $total; $n++ ) :
1020                 if ( $n == $current ) :
1021                         $page_links[] = "<span class='page-numbers current'>$n</span>";
1022                         $dots = true;
1023                 else :
1024                         if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
1025                                 $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
1026                                 $link = str_replace('%#%', $n, $link);
1027                                 if ( $add_args )
1028                                         $link = add_query_arg( $add_args, $link );
1029                                 $page_links[] = "<a class='page-numbers' href='" . clean_url($link) . "'>$n</a>";
1030                                 $dots = true;
1031                         elseif ( $dots && !$show_all ) :
1032                                 $page_links[] = "<span class='page-numbers dots'>...</span>";
1033                                 $dots = false;
1034                         endif;
1035                 endif;
1036         endfor;
1037         if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
1038                 $link = str_replace('%_%', $format, $base);
1039                 $link = str_replace('%#%', $current + 1, $link);
1040                 if ( $add_args )
1041                         $link = add_query_arg( $add_args, $link );
1042                 $page_links[] = "<a class='next page-numbers' href='" . clean_url($link) . "'>$next_text</a>";
1043         endif;
1044         switch ( $type ) :
1045                 case 'array' :
1046                         return $page_links;
1047                         break;
1048                 case 'list' :
1049                         $r .= "<ul class='page-numbers'>\n\t<li>";
1050                         $r .= join("</li>\n\t<li>", $page_links);
1051                         $r .= "</li>\n</ul>\n";
1052                         break;
1053                 default :
1054                         $r = join("\n", $page_links);
1055                         break;
1056         endswitch;
1057         return $r;
1058 }
1059
1060 function wp_admin_css_uri( $file = 'wp-admin' ) {
1061         if ( defined('WP_INSTALLING') )
1062         {
1063                 $_file = add_query_arg( 'version', get_bloginfo( 'version' ), "./$file.css" );
1064         } else {
1065                 $_file = add_query_arg( 'version', get_bloginfo( 'version' ), get_option( 'siteurl' ) . "/wp-admin/$file.css" );
1066         }
1067         return apply_filters( 'wp_admin_css_uri', $_file, $file );
1068 }
1069
1070 function wp_admin_css( $file = 'wp-admin' ) {
1071         echo apply_filters( 'wp_admin_css', "<link rel='stylesheet' href='" . wp_admin_css_uri( $file ) . "' type='text/css' />\n", $file );
1072         if ( 'rtl' == get_bloginfo( 'text_direction' ) ) {
1073                 $rtl = ( 'wp-admin' == $file ) ? 'rtl' : "$file-rtl";
1074                 echo apply_filters( 'wp_admin_css', "<link rel='stylesheet' href='" . wp_admin_css_uri( $rtl ) . "' type='text/css' />\n", $rtl );
1075         }
1076 }
1077
1078 ?>