]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/general-template.php
WordPress 3.4.1-scripts
[autoinstalls/wordpress.git] / wp-includes / general-template.php
1 <?php
2 /**
3  * General template tags that can go anywhere in a template.
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * Load header template.
11  *
12  * Includes the header template for a theme or if a name is specified then a
13  * specialised header will be included.
14  *
15  * For the parameter, if the file is called "header-special.php" then specify
16  * "special".
17  *
18  * @uses locate_template()
19  * @since 1.5.0
20  * @uses do_action() Calls 'get_header' action.
21  *
22  * @param string $name The name of the specialised header.
23  */
24 function get_header( $name = null ) {
25         do_action( 'get_header', $name );
26
27         $templates = array();
28         if ( isset($name) )
29                 $templates[] = "header-{$name}.php";
30
31         $templates[] = 'header.php';
32
33         // Backward compat code will be removed in a future release
34         if ('' == locate_template($templates, true))
35                 load_template( ABSPATH . WPINC . '/theme-compat/header.php');
36 }
37
38 /**
39  * Load footer template.
40  *
41  * Includes the footer template for a theme or if a name is specified then a
42  * specialised footer will be included.
43  *
44  * For the parameter, if the file is called "footer-special.php" then specify
45  * "special".
46  *
47  * @uses locate_template()
48  * @since 1.5.0
49  * @uses do_action() Calls 'get_footer' action.
50  *
51  * @param string $name The name of the specialised footer.
52  */
53 function get_footer( $name = null ) {
54         do_action( 'get_footer', $name );
55
56         $templates = array();
57         if ( isset($name) )
58                 $templates[] = "footer-{$name}.php";
59
60         $templates[] = 'footer.php';
61
62         // Backward compat code will be removed in a future release
63         if ('' == locate_template($templates, true))
64                 load_template( ABSPATH . WPINC . '/theme-compat/footer.php');
65 }
66
67 /**
68  * Load sidebar template.
69  *
70  * Includes the sidebar template for a theme or if a name is specified then a
71  * specialised sidebar will be included.
72  *
73  * For the parameter, if the file is called "sidebar-special.php" then specify
74  * "special".
75  *
76  * @uses locate_template()
77  * @since 1.5.0
78  * @uses do_action() Calls 'get_sidebar' action.
79  *
80  * @param string $name The name of the specialised sidebar.
81  */
82 function get_sidebar( $name = null ) {
83         do_action( 'get_sidebar', $name );
84
85         $templates = array();
86         if ( isset($name) )
87                 $templates[] = "sidebar-{$name}.php";
88
89         $templates[] = 'sidebar.php';
90
91         // Backward compat code will be removed in a future release
92         if ('' == locate_template($templates, true))
93                 load_template( ABSPATH . WPINC . '/theme-compat/sidebar.php');
94 }
95
96 /**
97  * Load a template part into a template
98  *
99  * Makes it easy for a theme to reuse sections of code in a easy to overload way
100  * for child themes.
101  *
102  * Includes the named template part for a theme or if a name is specified then a
103  * specialised part will be included. If the theme contains no {slug}.php file
104  * then no template will be included.
105  *
106  * The template is included using require, not require_once, so you may include the
107  * same template part multiple times.
108  *
109  * For the $name parameter, if the file is called "{slug}-special.php" then specify
110  * "special".
111  *
112  * @uses locate_template()
113  * @since 3.0.0
114  * @uses do_action() Calls 'get_template_part_{$slug}' action.
115  *
116  * @param string $slug The slug name for the generic template.
117  * @param string $name The name of the specialised template.
118  */
119 function get_template_part( $slug, $name = null ) {
120         do_action( "get_template_part_{$slug}", $slug, $name );
121
122         $templates = array();
123         if ( isset($name) )
124                 $templates[] = "{$slug}-{$name}.php";
125
126         $templates[] = "{$slug}.php";
127
128         locate_template($templates, true, false);
129 }
130
131 /**
132  * Display search form.
133  *
134  * Will first attempt to locate the searchform.php file in either the child or
135  * the parent, then load it. If it doesn't exist, then the default search form
136  * will be displayed. The default search form is HTML, which will be displayed.
137  * There is a filter applied to the search form HTML in order to edit or replace
138  * it. The filter is 'get_search_form'.
139  *
140  * This function is primarily used by themes which want to hardcode the search
141  * form into the sidebar and also by the search widget in WordPress.
142  *
143  * There is also an action that is called whenever the function is run called,
144  * 'get_search_form'. This can be useful for outputting JavaScript that the
145  * search relies on or various formatting that applies to the beginning of the
146  * search. To give a few examples of what it can be used for.
147  *
148  * @since 2.7.0
149  * @param boolean $echo Default to echo and not return the form.
150  */
151 function get_search_form($echo = true) {
152         do_action( 'get_search_form' );
153
154         $search_form_template = locate_template('searchform.php');
155         if ( '' != $search_form_template ) {
156                 require($search_form_template);
157                 return;
158         }
159
160         $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
161         <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
162         <input type="text" value="' . get_search_query() . '" name="s" id="s" />
163         <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
164         </div>
165         </form>';
166
167         if ( $echo )
168                 echo apply_filters('get_search_form', $form);
169         else
170                 return apply_filters('get_search_form', $form);
171 }
172
173 /**
174  * Display the Log In/Out link.
175  *
176  * Displays a link, which allows users to navigate to the Log In page to log in
177  * or log out depending on whether they are currently logged in.
178  *
179  * @since 1.5.0
180  * @uses apply_filters() Calls 'loginout' hook on HTML link content.
181  *
182  * @param string $redirect Optional path to redirect to on login/logout.
183  * @param boolean $echo Default to echo and not return the link.
184  */
185 function wp_loginout($redirect = '', $echo = true) {
186         if ( ! is_user_logged_in() )
187                 $link = '<a href="' . esc_url( wp_login_url($redirect) ) . '">' . __('Log in') . '</a>';
188         else
189                 $link = '<a href="' . esc_url( wp_logout_url($redirect) ) . '">' . __('Log out') . '</a>';
190
191         if ( $echo )
192                 echo apply_filters('loginout', $link);
193         else
194                 return apply_filters('loginout', $link);
195 }
196
197 /**
198  * Returns the Log Out URL.
199  *
200  * Returns the URL that allows the user to log out of the site
201  *
202  * @since 2.7.0
203  * @uses wp_nonce_url() To protect against CSRF
204  * @uses site_url() To generate the log in URL
205  * @uses apply_filters() calls 'logout_url' hook on final logout url
206  *
207  * @param string $redirect Path to redirect to on logout.
208  */
209 function wp_logout_url($redirect = '') {
210         $args = array( 'action' => 'logout' );
211         if ( !empty($redirect) ) {
212                 $args['redirect_to'] = urlencode( $redirect );
213         }
214
215         $logout_url = add_query_arg($args, site_url('wp-login.php', 'login'));
216         $logout_url = wp_nonce_url( $logout_url, 'log-out' );
217
218         return apply_filters('logout_url', $logout_url, $redirect);
219 }
220
221 /**
222  * Returns the Log In URL.
223  *
224  * Returns the URL that allows the user to log in to the site
225  *
226  * @since 2.7.0
227  * @uses site_url() To generate the log in URL
228  * @uses apply_filters() calls 'login_url' hook on final login url
229  *
230  * @param string $redirect Path to redirect to on login.
231  * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present. Default is false.
232  * @return string A log in url
233  */
234 function wp_login_url($redirect = '', $force_reauth = false) {
235         $login_url = site_url('wp-login.php', 'login');
236
237         if ( !empty($redirect) )
238                 $login_url = add_query_arg('redirect_to', urlencode($redirect), $login_url);
239
240         if ( $force_reauth )
241                 $login_url = add_query_arg('reauth', '1', $login_url);
242
243         return apply_filters('login_url', $login_url, $redirect);
244 }
245
246 /**
247  * Provides a simple login form for use anywhere within WordPress. By default, it echoes
248  * the HTML immediately. Pass array('echo'=>false) to return the string instead.
249  *
250  * @since 3.0.0
251  * @param array $args Configuration options to modify the form output
252  * @return Void, or string containing the form
253  */
254 function wp_login_form( $args = array() ) {
255         $defaults = array( 'echo' => true,
256                                                 'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], // Default redirect is back to the current page
257                                                 'form_id' => 'loginform',
258                                                 'label_username' => __( 'Username' ),
259                                                 'label_password' => __( 'Password' ),
260                                                 'label_remember' => __( 'Remember Me' ),
261                                                 'label_log_in' => __( 'Log In' ),
262                                                 'id_username' => 'user_login',
263                                                 'id_password' => 'user_pass',
264                                                 'id_remember' => 'rememberme',
265                                                 'id_submit' => 'wp-submit',
266                                                 'remember' => true,
267                                                 'value_username' => '',
268                                                 'value_remember' => false, // Set this to true to default the "Remember me" checkbox to checked
269                                         );
270         $args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );
271
272         $form = '
273                 <form name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
274                         ' . apply_filters( 'login_form_top', '', $args ) . '
275                         <p class="login-username">
276                                 <label for="' . esc_attr( $args['id_username'] ) . '">' . esc_html( $args['label_username'] ) . '</label>
277                                 <input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" size="20" tabindex="10" />
278                         </p>
279                         <p class="login-password">
280                                 <label for="' . esc_attr( $args['id_password'] ) . '">' . esc_html( $args['label_password'] ) . '</label>
281                                 <input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" size="20" tabindex="20" />
282                         </p>
283                         ' . apply_filters( 'login_form_middle', '', $args ) . '
284                         ' . ( $args['remember'] ? '<p class="login-remember"><label><input name="rememberme" type="checkbox" id="' . esc_attr( $args['id_remember'] ) . '" value="forever" tabindex="90"' . ( $args['value_remember'] ? ' checked="checked"' : '' ) . ' /> ' . esc_html( $args['label_remember'] ) . '</label></p>' : '' ) . '
285                         <p class="login-submit">
286                                 <input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" tabindex="100" />
287                                 <input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />
288                         </p>
289                         ' . apply_filters( 'login_form_bottom', '', $args ) . '
290                 </form>';
291
292         if ( $args['echo'] )
293                 echo $form;
294         else
295                 return $form;
296 }
297
298 /**
299  * Returns the Lost Password URL.
300  *
301  * Returns the URL that allows the user to retrieve the lost password
302  *
303  * @since 2.8.0
304  * @uses site_url() To generate the lost password URL
305  * @uses apply_filters() calls 'lostpassword_url' hook on the lostpassword url
306  *
307  * @param string $redirect Path to redirect to on login.
308  */
309 function wp_lostpassword_url( $redirect = '' ) {
310         $args = array( 'action' => 'lostpassword' );
311         if ( !empty($redirect) ) {
312                 $args['redirect_to'] = $redirect;
313         }
314
315         $lostpassword_url = add_query_arg( $args, network_site_url('wp-login.php', 'login') );
316         return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
317 }
318
319 /**
320  * Display the Registration or Admin link.
321  *
322  * Display a link which allows the user to navigate to the registration page if
323  * not logged in and registration is enabled or to the dashboard if logged in.
324  *
325  * @since 1.5.0
326  * @uses apply_filters() Calls 'register' hook on register / admin link content.
327  *
328  * @param string $before Text to output before the link (defaults to <li>).
329  * @param string $after Text to output after the link (defaults to </li>).
330  * @param boolean $echo Default to echo and not return the link.
331  */
332 function wp_register( $before = '<li>', $after = '</li>', $echo = true ) {
333
334         if ( ! is_user_logged_in() ) {
335                 if ( get_option('users_can_register') )
336                         $link = $before . '<a href="' . site_url('wp-login.php?action=register', 'login') . '">' . __('Register') . '</a>' . $after;
337                 else
338                         $link = '';
339         } else {
340                 $link = $before . '<a href="' . admin_url() . '">' . __('Site Admin') . '</a>' . $after;
341         }
342
343         if ( $echo )
344                 echo apply_filters('register', $link);
345         else
346                 return apply_filters('register', $link);
347 }
348
349 /**
350  * Theme container function for the 'wp_meta' action.
351  *
352  * The 'wp_meta' action can have several purposes, depending on how you use it,
353  * but one purpose might have been to allow for theme switching.
354  *
355  * @since 1.5.0
356  * @link http://trac.wordpress.org/ticket/1458 Explanation of 'wp_meta' action.
357  * @uses do_action() Calls 'wp_meta' hook.
358  */
359 function wp_meta() {
360         do_action('wp_meta');
361 }
362
363 /**
364  * Display information about the blog.
365  *
366  * @see get_bloginfo() For possible values for the parameter.
367  * @since 0.71
368  *
369  * @param string $show What to display.
370  */
371 function bloginfo( $show='' ) {
372         echo get_bloginfo( $show, 'display' );
373 }
374
375 /**
376  * Retrieve information about the blog.
377  *
378  * Some show parameter values are deprecated and will be removed in future
379  * versions. These options will trigger the _deprecated_argument() function.
380  * The deprecated blog info options are listed in the function contents.
381  *
382  * The possible values for the 'show' parameter are listed below.
383  * <ol>
384  * <li><strong>url</strong> - Blog URI to homepage.</li>
385  * <li><strong>wpurl</strong> - Blog URI path to WordPress.</li>
386  * <li><strong>description</strong> - Secondary title</li>
387  * </ol>
388  *
389  * The feed URL options can be retrieved from 'rdf_url' (RSS 0.91),
390  * 'rss_url' (RSS 1.0), 'rss2_url' (RSS 2.0), or 'atom_url' (Atom feed). The
391  * comment feeds can be retrieved from the 'comments_atom_url' (Atom comment
392  * feed) or 'comments_rss2_url' (RSS 2.0 comment feed).
393  *
394  * @since 0.71
395  *
396  * @param string $show Blog info to retrieve.
397  * @param string $filter How to filter what is retrieved.
398  * @return string Mostly string values, might be empty.
399  */
400 function get_bloginfo( $show = '', $filter = 'raw' ) {
401
402         switch( $show ) {
403                 case 'home' : // DEPRECATED
404                 case 'siteurl' : // DEPRECATED
405                         _deprecated_argument( __FUNCTION__, '2.2', sprintf( __('The <code>%s</code> option is deprecated for the family of <code>bloginfo()</code> functions.' ), $show ) . ' ' . sprintf( __( 'Use the <code>%s</code> option instead.' ), 'url'  ) );
406                 case 'url' :
407                         $output = home_url();
408                         break;
409                 case 'wpurl' :
410                         $output = site_url();
411                         break;
412                 case 'description':
413                         $output = get_option('blogdescription');
414                         break;
415                 case 'rdf_url':
416                         $output = get_feed_link('rdf');
417                         break;
418                 case 'rss_url':
419                         $output = get_feed_link('rss');
420                         break;
421                 case 'rss2_url':
422                         $output = get_feed_link('rss2');
423                         break;
424                 case 'atom_url':
425                         $output = get_feed_link('atom');
426                         break;
427                 case 'comments_atom_url':
428                         $output = get_feed_link('comments_atom');
429                         break;
430                 case 'comments_rss2_url':
431                         $output = get_feed_link('comments_rss2');
432                         break;
433                 case 'pingback_url':
434                         $output = get_option('siteurl') .'/xmlrpc.php';
435                         break;
436                 case 'stylesheet_url':
437                         $output = get_stylesheet_uri();
438                         break;
439                 case 'stylesheet_directory':
440                         $output = get_stylesheet_directory_uri();
441                         break;
442                 case 'template_directory':
443                 case 'template_url':
444                         $output = get_template_directory_uri();
445                         break;
446                 case 'admin_email':
447                         $output = get_option('admin_email');
448                         break;
449                 case 'charset':
450                         $output = get_option('blog_charset');
451                         if ('' == $output) $output = 'UTF-8';
452                         break;
453                 case 'html_type' :
454                         $output = get_option('html_type');
455                         break;
456                 case 'version':
457                         global $wp_version;
458                         $output = $wp_version;
459                         break;
460                 case 'language':
461                         $output = get_locale();
462                         $output = str_replace('_', '-', $output);
463                         break;
464                 case 'text_direction':
465                         //_deprecated_argument( __FUNCTION__, '2.2', sprintf( __('The <code>%s</code> option is deprecated for the family of <code>bloginfo()</code> functions.' ), $show ) . ' ' . sprintf( __( 'Use the <code>%s</code> function instead.' ), 'is_rtl()'  ) );
466                         if ( function_exists( 'is_rtl' ) ) {
467                                 $output = is_rtl() ? 'rtl' : 'ltr';
468                         } else {
469                                 $output = 'ltr';
470                         }
471                         break;
472                 case 'name':
473                 default:
474                         $output = get_option('blogname');
475                         break;
476         }
477
478         $url = true;
479         if (strpos($show, 'url') === false &&
480                 strpos($show, 'directory') === false &&
481                 strpos($show, 'home') === false)
482                 $url = false;
483
484         if ( 'display' == $filter ) {
485                 if ( $url )
486                         $output = apply_filters('bloginfo_url', $output, $show);
487                 else
488                         $output = apply_filters('bloginfo', $output, $show);
489         }
490
491         return $output;
492 }
493
494 /**
495  * Retrieve the current blog id
496  *
497  * @since 3.1.0
498  *
499  * @return int Blog id
500  */
501 function get_current_blog_id() {
502         global $blog_id;
503         return absint($blog_id);
504 }
505
506 /**
507  * Display or retrieve page title for all areas of blog.
508  *
509  * By default, the page title will display the separator before the page title,
510  * so that the blog title will be before the page title. This is not good for
511  * title display, since the blog title shows up on most tabs and not what is
512  * important, which is the page that the user is looking at.
513  *
514  * There are also SEO benefits to having the blog title after or to the 'right'
515  * or the page title. However, it is mostly common sense to have the blog title
516  * to the right with most browsers supporting tabs. You can achieve this by
517  * using the seplocation parameter and setting the value to 'right'. This change
518  * was introduced around 2.5.0, in case backwards compatibility of themes is
519  * important.
520  *
521  * @since 1.0.0
522  *
523  * @param string $sep Optional, default is '&raquo;'. How to separate the various items within the page title.
524  * @param bool $display Optional, default is true. Whether to display or retrieve title.
525  * @param string $seplocation Optional. Direction to display title, 'right'.
526  * @return string|null String on retrieve, null when displaying.
527  */
528 function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
529         global $wpdb, $wp_locale;
530
531         $m = get_query_var('m');
532         $year = get_query_var('year');
533         $monthnum = get_query_var('monthnum');
534         $day = get_query_var('day');
535         $search = get_query_var('s');
536         $title = '';
537
538         $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
539
540         // If there is a post
541         if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
542                 $title = single_post_title( '', false );
543         }
544
545         // If there's a category or tag
546         if ( is_category() || is_tag() ) {
547                 $title = single_term_title( '', false );
548         }
549
550         // If there's a taxonomy
551         if ( is_tax() ) {
552                 $term = get_queried_object();
553                 $tax = get_taxonomy( $term->taxonomy );
554                 $title = single_term_title( $tax->labels->name . $t_sep, false );
555         }
556
557         // If there's an author
558         if ( is_author() ) {
559                 $author = get_queried_object();
560                 $title = $author->display_name;
561         }
562
563         // If there's a post type archive
564         if ( is_post_type_archive() )
565                 $title = post_type_archive_title( '', false );
566
567         // If there's a month
568         if ( is_archive() && !empty($m) ) {
569                 $my_year = substr($m, 0, 4);
570                 $my_month = $wp_locale->get_month(substr($m, 4, 2));
571                 $my_day = intval(substr($m, 6, 2));
572                 $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
573         }
574
575         // If there's a year
576         if ( is_archive() && !empty($year) ) {
577                 $title = $year;
578                 if ( !empty($monthnum) )
579                         $title .= $t_sep . $wp_locale->get_month($monthnum);
580                 if ( !empty($day) )
581                         $title .= $t_sep . zeroise($day, 2);
582         }
583
584         // If it's a search
585         if ( is_search() ) {
586                 /* translators: 1: separator, 2: search phrase */
587                 $title = sprintf(__('Search Results %1$s %2$s'), $t_sep, strip_tags($search));
588         }
589
590         // If it's a 404 page
591         if ( is_404() ) {
592                 $title = __('Page not found');
593         }
594
595         $prefix = '';
596         if ( !empty($title) )
597                 $prefix = " $sep ";
598
599         // Determines position of the separator and direction of the breadcrumb
600         if ( 'right' == $seplocation ) { // sep on right, so reverse the order
601                 $title_array = explode( $t_sep, $title );
602                 $title_array = array_reverse( $title_array );
603                 $title = implode( " $sep ", $title_array ) . $prefix;
604         } else {
605                 $title_array = explode( $t_sep, $title );
606                 $title = $prefix . implode( " $sep ", $title_array );
607         }
608
609         $title = apply_filters('wp_title', $title, $sep, $seplocation);
610
611         // Send it out
612         if ( $display )
613                 echo $title;
614         else
615                 return $title;
616
617 }
618
619 /**
620  * Display or retrieve page title for post.
621  *
622  * This is optimized for single.php template file for displaying the post title.
623  *
624  * It does not support placing the separator after the title, but by leaving the
625  * prefix parameter empty, you can set the title separator manually. The prefix
626  * does not automatically place a space between the prefix, so if there should
627  * be a space, the parameter value will need to have it at the end.
628  *
629  * @since 0.71
630  *
631  * @param string $prefix Optional. What to display before the title.
632  * @param bool $display Optional, default is true. Whether to display or retrieve title.
633  * @return string|null Title when retrieving, null when displaying or failure.
634  */
635 function single_post_title($prefix = '', $display = true) {
636         $_post = get_queried_object();
637
638         if ( !isset($_post->post_title) )
639                 return;
640
641         $title = apply_filters('single_post_title', $_post->post_title, $_post);
642         if ( $display )
643                 echo $prefix . $title;
644         else
645                 return $title;
646 }
647
648 /**
649  * Display or retrieve title for a post type archive.
650  *
651  * This is optimized for archive.php and archive-{$post_type}.php template files
652  * for displaying the title of the post type.
653  *
654  * @since 3.1.0
655  *
656  * @param string $prefix Optional. What to display before the title.
657  * @param bool $display Optional, default is true. Whether to display or retrieve title.
658  * @return string|null Title when retrieving, null when displaying or failure.
659  */
660 function post_type_archive_title( $prefix = '', $display = true ) {
661         if ( ! is_post_type_archive() )
662                 return;
663
664         $post_type_obj = get_queried_object();
665         $title = apply_filters('post_type_archive_title', $post_type_obj->labels->name );
666
667         if ( $display )
668                 echo $prefix . $title;
669         else
670                 return $title;
671 }
672
673 /**
674  * Display or retrieve page title for category archive.
675  *
676  * This is useful for category template file or files, because it is optimized
677  * for category page title and with less overhead than {@link wp_title()}.
678  *
679  * It does not support placing the separator after the title, but by leaving the
680  * prefix parameter empty, you can set the title separator manually. The prefix
681  * does not automatically place a space between the prefix, so if there should
682  * be a space, the parameter value will need to have it at the end.
683  *
684  * @since 0.71
685  *
686  * @param string $prefix Optional. What to display before the title.
687  * @param bool $display Optional, default is true. Whether to display or retrieve title.
688  * @return string|null Title when retrieving, null when displaying or failure.
689  */
690 function single_cat_title( $prefix = '', $display = true ) {
691         return single_term_title( $prefix, $display );
692 }
693
694 /**
695  * Display or retrieve page title for tag post archive.
696  *
697  * Useful for tag template files for displaying the tag page title. It has less
698  * overhead than {@link wp_title()}, because of its limited implementation.
699  *
700  * It does not support placing the separator after the title, but by leaving the
701  * prefix parameter empty, you can set the title separator manually. The prefix
702  * does not automatically place a space between the prefix, so if there should
703  * be a space, the parameter value will need to have it at the end.
704  *
705  * @since 2.3.0
706  *
707  * @param string $prefix Optional. What to display before the title.
708  * @param bool $display Optional, default is true. Whether to display or retrieve title.
709  * @return string|null Title when retrieving, null when displaying or failure.
710  */
711 function single_tag_title( $prefix = '', $display = true ) {
712         return single_term_title( $prefix, $display );
713 }
714
715 /**
716  * Display or retrieve page title for taxonomy term archive.
717  *
718  * Useful for taxonomy term template files for displaying the taxonomy term page title.
719  * It has less overhead than {@link wp_title()}, because of its limited implementation.
720  *
721  * It does not support placing the separator after the title, but by leaving the
722  * prefix parameter empty, you can set the title separator manually. The prefix
723  * does not automatically place a space between the prefix, so if there should
724  * be a space, the parameter value will need to have it at the end.
725  *
726  * @since 3.1.0
727  *
728  * @param string $prefix Optional. What to display before the title.
729  * @param bool $display Optional, default is true. Whether to display or retrieve title.
730  * @return string|null Title when retrieving, null when displaying or failure.
731  */
732 function single_term_title( $prefix = '', $display = true ) {
733         $term = get_queried_object();
734
735         if ( !$term )
736                 return;
737
738         if ( is_category() )
739                 $term_name = apply_filters( 'single_cat_title', $term->name );
740         elseif ( is_tag() )
741                 $term_name = apply_filters( 'single_tag_title', $term->name );
742         elseif ( is_tax() )
743                 $term_name = apply_filters( 'single_term_title', $term->name );
744         else
745                 return;
746
747         if ( empty( $term_name ) )
748                 return;
749
750         if ( $display )
751                 echo $prefix . $term_name;
752         else
753                 return $term_name;
754 }
755
756 /**
757  * Display or retrieve page title for post archive based on date.
758  *
759  * Useful for when the template only needs to display the month and year, if
760  * either are available. Optimized for just this purpose, so if it is all that
761  * is needed, should be better than {@link wp_title()}.
762  *
763  * It does not support placing the separator after the title, but by leaving the
764  * prefix parameter empty, you can set the title separator manually. The prefix
765  * does not automatically place a space between the prefix, so if there should
766  * be a space, the parameter value will need to have it at the end.
767  *
768  * @since 0.71
769  *
770  * @param string $prefix Optional. What to display before the title.
771  * @param bool $display Optional, default is true. Whether to display or retrieve title.
772  * @return string|null Title when retrieving, null when displaying or failure.
773  */
774 function single_month_title($prefix = '', $display = true ) {
775         global $wp_locale;
776
777         $m = get_query_var('m');
778         $year = get_query_var('year');
779         $monthnum = get_query_var('monthnum');
780
781         if ( !empty($monthnum) && !empty($year) ) {
782                 $my_year = $year;
783                 $my_month = $wp_locale->get_month($monthnum);
784         } elseif ( !empty($m) ) {
785                 $my_year = substr($m, 0, 4);
786                 $my_month = $wp_locale->get_month(substr($m, 4, 2));
787         }
788
789         if ( empty($my_month) )
790                 return false;
791
792         $result = $prefix . $my_month . $prefix . $my_year;
793
794         if ( !$display )
795                 return $result;
796         echo $result;
797 }
798
799 /**
800  * Retrieve archive link content based on predefined or custom code.
801  *
802  * The format can be one of four styles. The 'link' for head element, 'option'
803  * for use in the select element, 'html' for use in list (either ol or ul HTML
804  * elements). Custom content is also supported using the before and after
805  * parameters.
806  *
807  * The 'link' format uses the link HTML element with the <em>archives</em>
808  * relationship. The before and after parameters are not used. The text
809  * parameter is used to describe the link.
810  *
811  * The 'option' format uses the option HTML element for use in select element.
812  * The value is the url parameter and the before and after parameters are used
813  * between the text description.
814  *
815  * The 'html' format, which is the default, uses the li HTML element for use in
816  * the list HTML elements. The before parameter is before the link and the after
817  * parameter is after the closing link.
818  *
819  * The custom format uses the before parameter before the link ('a' HTML
820  * element) and the after parameter after the closing link tag. If the above
821  * three values for the format are not used, then custom format is assumed.
822  *
823  * @since 1.0.0
824  *
825  * @param string $url URL to archive.
826  * @param string $text Archive text description.
827  * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
828  * @param string $before Optional.
829  * @param string $after Optional.
830  * @return string HTML link content for archive.
831  */
832 function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
833         $text = wptexturize($text);
834         $title_text = esc_attr($text);
835         $url = esc_url($url);
836
837         if ('link' == $format)
838                 $link_html = "\t<link rel='archives' title='$title_text' href='$url' />\n";
839         elseif ('option' == $format)
840                 $link_html = "\t<option value='$url'>$before $text $after</option>\n";
841         elseif ('html' == $format)
842                 $link_html = "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
843         else // custom
844                 $link_html = "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
845
846         $link_html = apply_filters( 'get_archives_link', $link_html );
847
848         return $link_html;
849 }
850
851 /**
852  * Display archive links based on type and format.
853  *
854  * The 'type' argument offers a few choices and by default will display monthly
855  * archive links. The other options for values are 'daily', 'weekly', 'monthly',
856  * 'yearly', 'postbypost' or 'alpha'. Both 'postbypost' and 'alpha' display the
857  * same archive link list, the difference between the two is that 'alpha'
858  * will order by post title and 'postbypost' will order by post date.
859  *
860  * The date archives will logically display dates with links to the archive post
861  * page. The 'postbypost' and 'alpha' values for 'type' argument will display
862  * the post titles.
863  *
864  * The 'limit' argument will only display a limited amount of links, specified
865  * by the 'limit' integer value. By default, there is no limit. The
866  * 'show_post_count' argument will show how many posts are within the archive.
867  * By default, the 'show_post_count' argument is set to false.
868  *
869  * For the 'format', 'before', and 'after' arguments, see {@link
870  * get_archives_link()}. The values of these arguments have to do with that
871  * function.
872  *
873  * @since 1.2.0
874  *
875  * @param string|array $args Optional. Override defaults.
876  */
877 function wp_get_archives($args = '') {
878         global $wpdb, $wp_locale;
879
880         $defaults = array(
881                 'type' => 'monthly', 'limit' => '',
882                 'format' => 'html', 'before' => '',
883                 'after' => '', 'show_post_count' => false,
884                 'echo' => 1
885         );
886
887         $r = wp_parse_args( $args, $defaults );
888         extract( $r, EXTR_SKIP );
889
890         if ( '' == $type )
891                 $type = 'monthly';
892
893         if ( '' != $limit ) {
894                 $limit = absint($limit);
895                 $limit = ' LIMIT '.$limit;
896         }
897
898         // this is what will separate dates on weekly archive links
899         $archive_week_separator = '&#8211;';
900
901         // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
902         $archive_date_format_over_ride = 0;
903
904         // options for daily archive (only if you over-ride the general date format)
905         $archive_day_date_format = 'Y/m/d';
906
907         // options for weekly archive (only if you over-ride the general date format)
908         $archive_week_start_date_format = 'Y/m/d';
909         $archive_week_end_date_format   = 'Y/m/d';
910
911         if ( !$archive_date_format_over_ride ) {
912                 $archive_day_date_format = get_option('date_format');
913                 $archive_week_start_date_format = get_option('date_format');
914                 $archive_week_end_date_format = get_option('date_format');
915         }
916
917         //filters
918         $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
919         $join = apply_filters( 'getarchives_join', '', $r );
920
921         $output = '';
922
923         if ( 'monthly' == $type ) {
924                 $query = "SELECT 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";
925                 $key = md5($query);
926                 $cache = wp_cache_get( 'wp_get_archives' , 'general');
927                 if ( !isset( $cache[ $key ] ) ) {
928                         $arcresults = $wpdb->get_results($query);
929                         $cache[ $key ] = $arcresults;
930                         wp_cache_set( 'wp_get_archives', $cache, 'general' );
931                 } else {
932                         $arcresults = $cache[ $key ];
933                 }
934                 if ( $arcresults ) {
935                         $afterafter = $after;
936                         foreach ( (array) $arcresults as $arcresult ) {
937                                 $url = get_month_link( $arcresult->year, $arcresult->month );
938                                 /* translators: 1: month name, 2: 4-digit year */
939                                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
940                                 if ( $show_post_count )
941                                         $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
942                                 $output .= get_archives_link($url, $text, $format, $before, $after);
943                         }
944                 }
945         } elseif ('yearly' == $type) {
946                 $query = "SELECT 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";
947                 $key = md5($query);
948                 $cache = wp_cache_get( 'wp_get_archives' , 'general');
949                 if ( !isset( $cache[ $key ] ) ) {
950                         $arcresults = $wpdb->get_results($query);
951                         $cache[ $key ] = $arcresults;
952                         wp_cache_set( 'wp_get_archives', $cache, 'general' );
953                 } else {
954                         $arcresults = $cache[ $key ];
955                 }
956                 if ($arcresults) {
957                         $afterafter = $after;
958                         foreach ( (array) $arcresults as $arcresult) {
959                                 $url = get_year_link($arcresult->year);
960                                 $text = sprintf('%d', $arcresult->year);
961                                 if ($show_post_count)
962                                         $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
963                                 $output .= get_archives_link($url, $text, $format, $before, $after);
964                         }
965                 }
966         } elseif ( 'daily' == $type ) {
967                 $query = "SELECT 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";
968                 $key = md5($query);
969                 $cache = wp_cache_get( 'wp_get_archives' , 'general');
970                 if ( !isset( $cache[ $key ] ) ) {
971                         $arcresults = $wpdb->get_results($query);
972                         $cache[ $key ] = $arcresults;
973                         wp_cache_set( 'wp_get_archives', $cache, 'general' );
974                 } else {
975                         $arcresults = $cache[ $key ];
976                 }
977                 if ( $arcresults ) {
978                         $afterafter = $after;
979                         foreach ( (array) $arcresults as $arcresult ) {
980                                 $url    = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
981                                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
982                                 $text = mysql2date($archive_day_date_format, $date);
983                                 if ($show_post_count)
984                                         $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
985                                 $output .= get_archives_link($url, $text, $format, $before, $after);
986                         }
987                 }
988         } elseif ( 'weekly' == $type ) {
989                 $week = _wp_mysql_week( '`post_date`' );
990                 $query = "SELECT DISTINCT $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, YEAR( `post_date` ) ORDER BY `post_date` DESC $limit";
991                 $key = md5($query);
992                 $cache = wp_cache_get( 'wp_get_archives' , 'general');
993                 if ( !isset( $cache[ $key ] ) ) {
994                         $arcresults = $wpdb->get_results($query);
995                         $cache[ $key ] = $arcresults;
996                         wp_cache_set( 'wp_get_archives', $cache, 'general' );
997                 } else {
998                         $arcresults = $cache[ $key ];
999                 }
1000                 $arc_w_last = '';
1001                 $afterafter = $after;
1002                 if ( $arcresults ) {
1003                                 foreach ( (array) $arcresults as $arcresult ) {
1004                                         if ( $arcresult->week != $arc_w_last ) {
1005                                                 $arc_year = $arcresult->yr;
1006                                                 $arc_w_last = $arcresult->week;
1007                                                 $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
1008                                                 $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
1009                                                 $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
1010                                                 $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $arcresult->week);
1011                                                 $text = $arc_week_start . $archive_week_separator . $arc_week_end;
1012                                                 if ($show_post_count)
1013                                                         $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
1014                                                 $output .= get_archives_link($url, $text, $format, $before, $after);
1015                                         }
1016                                 }
1017                 }
1018         } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
1019                 $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
1020                 $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
1021                 $key = md5($query);
1022                 $cache = wp_cache_get( 'wp_get_archives' , 'general');
1023                 if ( !isset( $cache[ $key ] ) ) {
1024                         $arcresults = $wpdb->get_results($query);
1025                         $cache[ $key ] = $arcresults;
1026                         wp_cache_set( 'wp_get_archives', $cache, 'general' );
1027                 } else {
1028                         $arcresults = $cache[ $key ];
1029                 }
1030                 if ( $arcresults ) {
1031                         foreach ( (array) $arcresults as $arcresult ) {
1032                                 if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
1033                                         $url  = get_permalink( $arcresult );
1034                                         if ( $arcresult->post_title )
1035                                                 $text = strip_tags( apply_filters( 'the_title', $arcresult->post_title, $arcresult->ID ) );
1036                                         else
1037                                                 $text = $arcresult->ID;
1038                                         $output .= get_archives_link($url, $text, $format, $before, $after);
1039                                 }
1040                         }
1041                 }
1042         }
1043         if ( $echo )
1044                 echo $output;
1045         else
1046                 return $output;
1047 }
1048
1049 /**
1050  * Get number of days since the start of the week.
1051  *
1052  * @since 1.5.0
1053  * @usedby get_calendar()
1054  *
1055  * @param int $num Number of day.
1056  * @return int Days since the start of the week.
1057  */
1058 function calendar_week_mod($num) {
1059         $base = 7;
1060         return ($num - $base*floor($num/$base));
1061 }
1062
1063 /**
1064  * Display calendar with days that have posts as links.
1065  *
1066  * The calendar is cached, which will be retrieved, if it exists. If there are
1067  * no posts for the month, then it will not be displayed.
1068  *
1069  * @since 1.0.0
1070  *
1071  * @param bool $initial Optional, default is true. Use initial calendar names.
1072  * @param bool $echo Optional, default is true. Set to false for return.
1073  */
1074 function get_calendar($initial = true, $echo = true) {
1075         global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
1076
1077         $cache = array();
1078         $key = md5( $m . $monthnum . $year );
1079         if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
1080                 if ( is_array($cache) && isset( $cache[ $key ] ) ) {
1081                         if ( $echo ) {
1082                                 echo apply_filters( 'get_calendar',  $cache[$key] );
1083                                 return;
1084                         } else {
1085                                 return apply_filters( 'get_calendar',  $cache[$key] );
1086                         }
1087                 }
1088         }
1089
1090         if ( !is_array($cache) )
1091                 $cache = array();
1092
1093         // Quick check. If we have no posts at all, abort!
1094         if ( !$posts ) {
1095                 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
1096                 if ( !$gotsome ) {
1097                         $cache[ $key ] = '';
1098                         wp_cache_set( 'get_calendar', $cache, 'calendar' );
1099                         return;
1100                 }
1101         }
1102
1103         if ( isset($_GET['w']) )
1104                 $w = ''.intval($_GET['w']);
1105
1106         // week_begins = 0 stands for Sunday
1107         $week_begins = intval(get_option('start_of_week'));
1108
1109         // Let's figure out when we are
1110         if ( !empty($monthnum) && !empty($year) ) {
1111                 $thismonth = ''.zeroise(intval($monthnum), 2);
1112                 $thisyear = ''.intval($year);
1113         } elseif ( !empty($w) ) {
1114                 // We need to get the month from MySQL
1115                 $thisyear = ''.intval(substr($m, 0, 4));
1116                 $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
1117                 $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
1118         } elseif ( !empty($m) ) {
1119                 $thisyear = ''.intval(substr($m, 0, 4));
1120                 if ( strlen($m) < 6 )
1121                                 $thismonth = '01';
1122                 else
1123                                 $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
1124         } else {
1125                 $thisyear = gmdate('Y', current_time('timestamp'));
1126                 $thismonth = gmdate('m', current_time('timestamp'));
1127         }
1128
1129         $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
1130         $last_day = date('t', $unixmonth);
1131
1132         // Get the next and previous month and year with at least one post
1133         $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
1134                 FROM $wpdb->posts
1135                 WHERE post_date < '$thisyear-$thismonth-01'
1136                 AND post_type = 'post' AND post_status = 'publish'
1137                         ORDER BY post_date DESC
1138                         LIMIT 1");
1139         $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
1140                 FROM $wpdb->posts
1141                 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
1142                 AND post_type = 'post' AND post_status = 'publish'
1143                         ORDER BY post_date ASC
1144                         LIMIT 1");
1145
1146         /* translators: Calendar caption: 1: month name, 2: 4-digit year */
1147         $calendar_caption = _x('%1$s %2$s', 'calendar caption');
1148         $calendar_output = '<table id="wp-calendar">
1149         <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
1150         <thead>
1151         <tr>';
1152
1153         $myweek = array();
1154
1155         for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
1156                 $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
1157         }
1158
1159         foreach ( $myweek as $wd ) {
1160                 $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
1161                 $wd = esc_attr($wd);
1162                 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
1163         }
1164
1165         $calendar_output .= '
1166         </tr>
1167         </thead>
1168
1169         <tfoot>
1170         <tr>';
1171
1172         if ( $previous ) {
1173                 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), 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>';
1174         } else {
1175                 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
1176         }
1177
1178         $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
1179
1180         if ( $next ) {
1181                 $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), 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>';
1182         } else {
1183                 $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
1184         }
1185
1186         $calendar_output .= '
1187         </tr>
1188         </tfoot>
1189
1190         <tbody>
1191         <tr>';
1192
1193         // Get days with posts
1194         $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
1195                 FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
1196                 AND post_type = 'post' AND post_status = 'publish'
1197                 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
1198         if ( $dayswithposts ) {
1199                 foreach ( (array) $dayswithposts as $daywith ) {
1200                         $daywithpost[] = $daywith[0];
1201                 }
1202         } else {
1203                 $daywithpost = array();
1204         }
1205
1206         if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
1207                 $ak_title_separator = "\n";
1208         else
1209                 $ak_title_separator = ', ';
1210
1211         $ak_titles_for_day = array();
1212         $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
1213                 ."FROM $wpdb->posts "
1214                 ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
1215                 ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
1216                 ."AND post_type = 'post' AND post_status = 'publish'"
1217         );
1218         if ( $ak_post_titles ) {
1219                 foreach ( (array) $ak_post_titles as $ak_post_title ) {
1220
1221                                 $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
1222
1223                                 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
1224                                         $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
1225                                 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
1226                                         $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
1227                                 else
1228                                         $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
1229                 }
1230         }
1231
1232         // See how much we should pad in the beginning
1233         $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
1234         if ( 0 != $pad )
1235                 $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
1236
1237         $daysinmonth = intval(date('t', $unixmonth));
1238         for ( $day = 1; $day <= $daysinmonth; ++$day ) {
1239                 if ( isset($newrow) && $newrow )
1240                         $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
1241                 $newrow = false;
1242
1243                 if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
1244                         $calendar_output .= '<td id="today">';
1245                 else
1246                         $calendar_output .= '<td>';
1247
1248                 if ( in_array($day, $daywithpost) ) // any posts today?
1249                                 $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
1250                 else
1251                         $calendar_output .= $day;
1252                 $calendar_output .= '</td>';
1253
1254                 if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
1255                         $newrow = true;
1256         }
1257
1258         $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
1259         if ( $pad != 0 && $pad != 7 )
1260                 $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
1261
1262         $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
1263
1264         $cache[ $key ] = $calendar_output;
1265         wp_cache_set( 'get_calendar', $cache, 'calendar' );
1266
1267         if ( $echo )
1268                 echo apply_filters( 'get_calendar',  $calendar_output );
1269         else
1270                 return apply_filters( 'get_calendar',  $calendar_output );
1271
1272 }
1273
1274 /**
1275  * Purge the cached results of get_calendar.
1276  *
1277  * @see get_calendar
1278  * @since 2.1.0
1279  */
1280 function delete_get_calendar_cache() {
1281         wp_cache_delete( 'get_calendar', 'calendar' );
1282 }
1283 add_action( 'save_post', 'delete_get_calendar_cache' );
1284 add_action( 'delete_post', 'delete_get_calendar_cache' );
1285 add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' );
1286 add_action( 'update_option_gmt_offset', 'delete_get_calendar_cache' );
1287
1288 /**
1289  * Display all of the allowed tags in HTML format with attributes.
1290  *
1291  * This is useful for displaying in the comment area, which elements and
1292  * attributes are supported. As well as any plugins which want to display it.
1293  *
1294  * @since 1.0.1
1295  * @uses $allowedtags
1296  *
1297  * @return string HTML allowed tags entity encoded.
1298  */
1299 function allowed_tags() {
1300         global $allowedtags;
1301         $allowed = '';
1302         foreach ( (array) $allowedtags as $tag => $attributes ) {
1303                 $allowed .= '<'.$tag;
1304                 if ( 0 < count($attributes) ) {
1305                         foreach ( $attributes as $attribute => $limits ) {
1306                                 $allowed .= ' '.$attribute.'=""';
1307                         }
1308                 }
1309                 $allowed .= '> ';
1310         }
1311         return htmlentities($allowed);
1312 }
1313
1314 /***** Date/Time tags *****/
1315
1316 /**
1317  * Outputs the date in iso8601 format for xml files.
1318  *
1319  * @since 1.0.0
1320  */
1321 function the_date_xml() {
1322         global $post;
1323         echo mysql2date('Y-m-d', $post->post_date, false);
1324 }
1325
1326 /**
1327  * Display or Retrieve the date the current $post was written (once per date)
1328  *
1329  * Will only output the date if the current post's date is different from the
1330  * previous one output.
1331  *
1332  * i.e. Only one date listing will show per day worth of posts shown in the loop, even if the
1333  * function is called several times for each post.
1334  *
1335  * HTML output can be filtered with 'the_date'.
1336  * Date string output can be filtered with 'get_the_date'.
1337  *
1338  * @since 0.71
1339  * @uses get_the_date()
1340  * @param string $d Optional. PHP date format defaults to the date_format option if not specified.
1341  * @param string $before Optional. Output before the date.
1342  * @param string $after Optional. Output after the date.
1343  * @param bool $echo Optional, default is display. Whether to echo the date or return it.
1344  * @return string|null Null if displaying, string if retrieving.
1345  */
1346 function the_date( $d = '', $before = '', $after = '', $echo = true ) {
1347         global $currentday, $previousday;
1348         $the_date = '';
1349         if ( $currentday != $previousday ) {
1350                 $the_date .= $before;
1351                 $the_date .= get_the_date( $d );
1352                 $the_date .= $after;
1353                 $previousday = $currentday;
1354
1355                 $the_date = apply_filters('the_date', $the_date, $d, $before, $after);
1356
1357                 if ( $echo )
1358                         echo $the_date;
1359                 else
1360                         return $the_date;
1361         }
1362
1363         return null;
1364 }
1365
1366 /**
1367  * Retrieve the date the current $post was written.
1368  *
1369  * Unlike the_date() this function will always return the date.
1370  * Modify output with 'get_the_date' filter.
1371  *
1372  * @since 3.0.0
1373  *
1374  * @param string $d Optional. PHP date format defaults to the date_format option if not specified.
1375  * @return string|null Null if displaying, string if retrieving.
1376  */
1377 function get_the_date( $d = '' ) {
1378         global $post;
1379         $the_date = '';
1380
1381         if ( '' == $d )
1382                 $the_date .= mysql2date(get_option('date_format'), $post->post_date);
1383         else
1384                 $the_date .= mysql2date($d, $post->post_date);
1385
1386         return apply_filters('get_the_date', $the_date, $d);
1387 }
1388
1389 /**
1390  * Display the date on which the post was last modified.
1391  *
1392  * @since 2.1.0
1393  *
1394  * @param string $d Optional. PHP date format defaults to the date_format option if not specified.
1395  * @param string $before Optional. Output before the date.
1396  * @param string $after Optional. Output after the date.
1397  * @param bool $echo Optional, default is display. Whether to echo the date or return it.
1398  * @return string|null Null if displaying, string if retrieving.
1399  */
1400 function the_modified_date($d = '', $before='', $after='', $echo = true) {
1401
1402         $the_modified_date = $before . get_the_modified_date($d) . $after;
1403         $the_modified_date = apply_filters('the_modified_date', $the_modified_date, $d, $before, $after);
1404
1405         if ( $echo )
1406                 echo $the_modified_date;
1407         else
1408                 return $the_modified_date;
1409
1410 }
1411
1412 /**
1413  * Retrieve the date on which the post was last modified.
1414  *
1415  * @since 2.1.0
1416  *
1417  * @param string $d Optional. PHP date format. Defaults to the "date_format" option
1418  * @return string
1419  */
1420 function get_the_modified_date($d = '') {
1421         if ( '' == $d )
1422                 $the_time = get_post_modified_time(get_option('date_format'), null, null, true);
1423         else
1424                 $the_time = get_post_modified_time($d, null, null, true);
1425         return apply_filters('get_the_modified_date', $the_time, $d);
1426 }
1427
1428 /**
1429  * Display the time at which the post was written.
1430  *
1431  * @since 0.71
1432  *
1433  * @param string $d Either 'G', 'U', or php date format.
1434  */
1435 function the_time( $d = '' ) {
1436         echo apply_filters('the_time', get_the_time( $d ), $d);
1437 }
1438
1439 /**
1440  * Retrieve the time at which the post was written.
1441  *
1442  * @since 1.5.0
1443  *
1444  * @param string $d Optional Either 'G', 'U', or php date format defaults to the value specified in the time_format option.
1445  * @param int|object $post Optional post ID or object. Default is global $post object.
1446  * @return string
1447  */
1448 function get_the_time( $d = '', $post = null ) {
1449         $post = get_post($post);
1450
1451         if ( '' == $d )
1452                 $the_time = get_post_time(get_option('time_format'), false, $post, true);
1453         else
1454                 $the_time = get_post_time($d, false, $post, true);
1455         return apply_filters('get_the_time', $the_time, $d, $post);
1456 }
1457
1458 /**
1459  * Retrieve the time at which the post was written.
1460  *
1461  * @since 2.0.0
1462  *
1463  * @param string $d Optional Either 'G', 'U', or php date format.
1464  * @param bool $gmt Optional, default is false. Whether to return the gmt time.
1465  * @param int|object $post Optional post ID or object. Default is global $post object.
1466  * @param bool $translate Whether to translate the time string
1467  * @return string
1468  */
1469 function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false ) { // returns timestamp
1470         $post = get_post($post);
1471
1472         if ( $gmt )
1473                 $time = $post->post_date_gmt;
1474         else
1475                 $time = $post->post_date;
1476
1477         $time = mysql2date($d, $time, $translate);
1478         return apply_filters('get_post_time', $time, $d, $gmt);
1479 }
1480
1481 /**
1482  * Display the time at which the post was last modified.
1483  *
1484  * @since 2.0.0
1485  *
1486  * @param string $d Optional Either 'G', 'U', or php date format defaults to the value specified in the time_format option.
1487  */
1488 function the_modified_time($d = '') {
1489         echo apply_filters('the_modified_time', get_the_modified_time($d), $d);
1490 }
1491
1492 /**
1493  * Retrieve the time at which the post was last modified.
1494  *
1495  * @since 2.0.0
1496  *
1497  * @param string $d Optional Either 'G', 'U', or php date format defaults to the value specified in the time_format option.
1498  * @return string
1499  */
1500 function get_the_modified_time($d = '') {
1501         if ( '' == $d )
1502                 $the_time = get_post_modified_time(get_option('time_format'), null, null, true);
1503         else
1504                 $the_time = get_post_modified_time($d, null, null, true);
1505         return apply_filters('get_the_modified_time', $the_time, $d);
1506 }
1507
1508 /**
1509  * Retrieve the time at which the post was last modified.
1510  *
1511  * @since 2.0.0
1512  *
1513  * @param string $d Optional, default is 'U'. Either 'G', 'U', or php date format.
1514  * @param bool $gmt Optional, default is false. Whether to return the gmt time.
1515  * @param int|object $post Optional, default is global post object. A post_id or post object
1516  * @param bool $translate Optional, default is false. Whether to translate the result
1517  * @return string Returns timestamp
1518  */
1519 function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translate = false ) {
1520         $post = get_post($post);
1521
1522         if ( $gmt )
1523                 $time = $post->post_modified_gmt;
1524         else
1525                 $time = $post->post_modified;
1526         $time = mysql2date($d, $time, $translate);
1527
1528         return apply_filters('get_post_modified_time', $time, $d, $gmt);
1529 }
1530
1531 /**
1532  * Display the weekday on which the post was written.
1533  *
1534  * @since 0.71
1535  * @uses $wp_locale
1536  * @uses $post
1537  */
1538 function the_weekday() {
1539         global $wp_locale, $post;
1540         $the_weekday = $wp_locale->get_weekday(mysql2date('w', $post->post_date, false));
1541         $the_weekday = apply_filters('the_weekday', $the_weekday);
1542         echo $the_weekday;
1543 }
1544
1545 /**
1546  * Display the weekday on which the post was written.
1547  *
1548  * Will only output the weekday if the current post's weekday is different from
1549  * the previous one output.
1550  *
1551  * @since 0.71
1552  *
1553  * @param string $before Optional Output before the date.
1554  * @param string $after Optional Output after the date.
1555  */
1556 function the_weekday_date($before='',$after='') {
1557         global $wp_locale, $post, $day, $previousweekday;
1558         $the_weekday_date = '';
1559         if ( $currentday != $previousweekday ) {
1560                 $the_weekday_date .= $before;
1561                 $the_weekday_date .= $wp_locale->get_weekday(mysql2date('w', $post->post_date, false));
1562                 $the_weekday_date .= $after;
1563                 $previousweekday = $currentday;
1564         }
1565         $the_weekday_date = apply_filters('the_weekday_date', $the_weekday_date, $before, $after);
1566         echo $the_weekday_date;
1567 }
1568
1569 /**
1570  * Fire the wp_head action
1571  *
1572  * @since 1.2.0
1573  * @uses do_action() Calls 'wp_head' hook.
1574  */
1575 function wp_head() {
1576         do_action('wp_head');
1577 }
1578
1579 /**
1580  * Fire the wp_footer action
1581  *
1582  * @since 1.5.1
1583  * @uses do_action() Calls 'wp_footer' hook.
1584  */
1585 function wp_footer() {
1586         do_action('wp_footer');
1587 }
1588
1589 /**
1590  * Display the links to the general feeds.
1591  *
1592  * @since 2.8.0
1593  *
1594  * @param array $args Optional arguments.
1595  */
1596 function feed_links( $args = array() ) {
1597         if ( !current_theme_supports('automatic-feed-links') )
1598                 return;
1599
1600         $defaults = array(
1601                 /* translators: Separator between blog name and feed type in feed links */
1602                 'separator'     => _x('&raquo;', 'feed link'),
1603                 /* translators: 1: blog title, 2: separator (raquo) */
1604                 'feedtitle'     => __('%1$s %2$s Feed'),
1605                 /* translators: %s: blog title, 2: separator (raquo) */
1606                 'comstitle'     => __('%1$s %2$s Comments Feed'),
1607         );
1608
1609         $args = wp_parse_args( $args, $defaults );
1610
1611         echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['feedtitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link() . "\" />\n";
1612         echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['comstitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link( 'comments_' . get_default_feed() ) . "\" />\n";
1613 }
1614
1615 /**
1616  * Display the links to the extra feeds such as category feeds.
1617  *
1618  * @since 2.8.0
1619  *
1620  * @param array $args Optional arguments.
1621  */
1622 function feed_links_extra( $args = array() ) {
1623         $defaults = array(
1624                 /* translators: Separator between blog name and feed type in feed links */
1625                 'separator'   => _x('&raquo;', 'feed link'),
1626                 /* translators: 1: blog name, 2: separator(raquo), 3: post title */
1627                 'singletitle' => __('%1$s %2$s %3$s Comments Feed'),
1628                 /* translators: 1: blog name, 2: separator(raquo), 3: category name */
1629                 'cattitle'    => __('%1$s %2$s %3$s Category Feed'),
1630                 /* translators: 1: blog name, 2: separator(raquo), 3: tag name */
1631                 'tagtitle'    => __('%1$s %2$s %3$s Tag Feed'),
1632                 /* translators: 1: blog name, 2: separator(raquo), 3: author name  */
1633                 'authortitle' => __('%1$s %2$s Posts by %3$s Feed'),
1634                 /* translators: 1: blog name, 2: separator(raquo), 3: search phrase */
1635                 'searchtitle' => __('%1$s %2$s Search Results for &#8220;%3$s&#8221; Feed'),
1636         );
1637
1638         $args = wp_parse_args( $args, $defaults );
1639
1640         if ( is_single() || is_page() ) {
1641                 $id = 0;
1642                 $post = &get_post( $id );
1643
1644                 if ( comments_open() || pings_open() || $post->comment_count > 0 ) {
1645                         $title = sprintf( $args['singletitle'], get_bloginfo('name'), $args['separator'], esc_html( get_the_title() ) );
1646                         $href = get_post_comments_feed_link( $post->ID );
1647                 }
1648         } elseif ( is_category() ) {
1649                 $term = get_queried_object();
1650
1651                 $title = sprintf( $args['cattitle'], get_bloginfo('name'), $args['separator'], $term->name );
1652                 $href = get_category_feed_link( $term->term_id );
1653         } elseif ( is_tag() ) {
1654                 $term = get_queried_object();
1655
1656                 $title = sprintf( $args['tagtitle'], get_bloginfo('name'), $args['separator'], $term->name );
1657                 $href = get_tag_feed_link( $term->term_id );
1658         } elseif ( is_author() ) {
1659                 $author_id = intval( get_query_var('author') );
1660
1661                 $title = sprintf( $args['authortitle'], get_bloginfo('name'), $args['separator'], get_the_author_meta( 'display_name', $author_id ) );
1662                 $href = get_author_feed_link( $author_id );
1663         } elseif ( is_search() ) {
1664                 $title = sprintf( $args['searchtitle'], get_bloginfo('name'), $args['separator'], get_search_query( false ) );
1665                 $href = get_search_feed_link();
1666         }
1667
1668         if ( isset($title) && isset($href) )
1669                 echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
1670 }
1671
1672 /**
1673  * Display the link to the Really Simple Discovery service endpoint.
1674  *
1675  * @link http://archipelago.phrasewise.com/rsd
1676  * @since 2.0.0
1677  */
1678 function rsd_link() {
1679         echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . get_bloginfo('wpurl') . "/xmlrpc.php?rsd\" />\n";
1680 }
1681
1682 /**
1683  * Display the link to the Windows Live Writer manifest file.
1684  *
1685  * @link http://msdn.microsoft.com/en-us/library/bb463265.aspx
1686  * @since 2.3.1
1687  */
1688 function wlwmanifest_link() {
1689         echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="'
1690                 . get_bloginfo('wpurl') . '/wp-includes/wlwmanifest.xml" /> ' . "\n";
1691 }
1692
1693 /**
1694  * Display a noindex meta tag if required by the blog configuration.
1695  *
1696  * If a blog is marked as not being public then the noindex meta tag will be
1697  * output to tell web robots not to index the page content. Add this to the wp_head action.
1698  * Typical usage is as a wp_head callback. add_action( 'wp_head', 'noindex' );
1699  *
1700  * @see wp_no_robots
1701  *
1702  * @since 2.1.0
1703  */
1704 function noindex() {
1705         // If the blog is not public, tell robots to go away.
1706         if ( '0' == get_option('blog_public') )
1707                 wp_no_robots();
1708 }
1709
1710 /**
1711  * Display a noindex meta tag.
1712  *
1713  * Outputs a noindex meta tag that tells web robots not to index the page content.
1714  * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_no_robots' );
1715  *
1716  * @since 3.3.0
1717  */
1718 function wp_no_robots() {
1719         echo "<meta name='robots' content='noindex,nofollow' />\n";
1720 }
1721
1722 /**
1723  * Determine if TinyMCE is available.
1724  *
1725  * Checks to see if the user has deleted the tinymce files to slim down there WordPress install.
1726  *
1727  * @since 2.1.0
1728  *
1729  * @return bool Whether TinyMCE exists.
1730  */
1731 function rich_edit_exists() {
1732         global $wp_rich_edit_exists;
1733         if ( !isset($wp_rich_edit_exists) )
1734                 $wp_rich_edit_exists = file_exists(ABSPATH . WPINC . '/js/tinymce/tiny_mce.js');
1735         return $wp_rich_edit_exists;
1736 }
1737
1738 /**
1739  * Whether the user should have a WYSIWIG editor.
1740  *
1741  * Checks that the user requires a WYSIWIG editor and that the editor is
1742  * supported in the users browser.
1743  *
1744  * @since 2.0.0
1745  *
1746  * @return bool
1747  */
1748 function user_can_richedit() {
1749         global $wp_rich_edit, $is_gecko, $is_opera, $is_safari, $is_chrome, $is_IE;
1750
1751         if ( !isset($wp_rich_edit) ) {
1752                 $wp_rich_edit = false;
1753
1754                 if ( get_user_option( 'rich_editing' ) == 'true' || ! is_user_logged_in() ) { // default to 'true' for logged out users
1755                         if ( $is_safari ) {
1756                                 $wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && intval( $match[1] ) >= 534 );
1757                         } elseif ( $is_gecko || $is_opera || $is_chrome || $is_IE ) {
1758                                 $wp_rich_edit = true;
1759                         }
1760                 }
1761         }
1762
1763         return apply_filters('user_can_richedit', $wp_rich_edit);
1764 }
1765
1766 /**
1767  * Find out which editor should be displayed by default.
1768  *
1769  * Works out which of the two editors to display as the current editor for a
1770  * user.
1771  *
1772  * @since 2.5.0
1773  *
1774  * @return string Either 'tinymce', or 'html', or 'test'
1775  */
1776 function wp_default_editor() {
1777         $r = user_can_richedit() ? 'tinymce' : 'html'; // defaults
1778         if ( $user = wp_get_current_user() ) { // look for cookie
1779                 $ed = get_user_setting('editor', 'tinymce');
1780                 $r = ( in_array($ed, array('tinymce', 'html', 'test') ) ) ? $ed : $r;
1781         }
1782         return apply_filters( 'wp_default_editor', $r ); // filter
1783 }
1784
1785 /**
1786  * Renders an editor.
1787  *
1788  * Using this function is the proper way to output all needed components for both TinyMCE and Quicktags.
1789  * _WP_Editors should not be used directly. See http://core.trac.wordpress.org/ticket/17144.
1790  *
1791  * NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason
1792  * running wp_editor() inside of a metabox is not a good idea unless only Quicktags is used.
1793  * On the post edit screen several actions can be used to include additional editors
1794  * containing TinyMCE: 'edit_page_form', 'edit_form_advanced' and 'dbx_post_sidebar'.
1795  * See http://core.trac.wordpress.org/ticket/19173 for more information.
1796  *
1797  * @see wp-includes/class-wp-editor.php
1798  * @since 3.3.0
1799  *
1800  * @param string $content Initial content for the editor.
1801  * @param string $editor_id HTML ID attribute value for the textarea and TinyMCE. Can only be /[a-z]+/.
1802  * @param array $settings See _WP_Editors::editor().
1803  */
1804 function wp_editor( $content, $editor_id, $settings = array() ) {
1805         if ( ! class_exists( '_WP_Editors' ) )
1806                 require( ABSPATH . WPINC . '/class-wp-editor.php' );
1807
1808         _WP_Editors::editor($content, $editor_id, $settings);
1809 }
1810
1811 /**
1812  * Retrieve the contents of the search WordPress query variable.
1813  *
1814  * The search query string is passed through {@link esc_attr()}
1815  * to ensure that it is safe for placing in an html attribute.
1816  *
1817  * @since 2.3.0
1818  * @uses esc_attr()
1819  *
1820  * @param bool $escaped Whether the result is escaped. Default true.
1821  *      Only use when you are later escaping it. Do not use unescaped.
1822  * @return string
1823  */
1824 function get_search_query( $escaped = true ) {
1825         $query = apply_filters( 'get_search_query', get_query_var( 's' ) );
1826         if ( $escaped )
1827                 $query = esc_attr( $query );
1828         return $query;
1829 }
1830
1831 /**
1832  * Display the contents of the search query variable.
1833  *
1834  * The search query string is passed through {@link esc_attr()}
1835  * to ensure that it is safe for placing in an html attribute.
1836  *
1837  * @uses esc_attr()
1838  * @since 2.1.0
1839  */
1840 function the_search_query() {
1841         echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
1842 }
1843
1844 /**
1845  * Display the language attributes for the html tag.
1846  *
1847  * Builds up a set of html attributes containing the text direction and language
1848  * information for the page.
1849  *
1850  * @since 2.1.0
1851  *
1852  * @param string $doctype The type of html document (xhtml|html).
1853  */
1854 function language_attributes($doctype = 'html') {
1855         $attributes = array();
1856         $output = '';
1857
1858         if ( function_exists( 'is_rtl' ) )
1859                 $attributes[] = 'dir="' . ( is_rtl() ? 'rtl' : 'ltr' ) . '"';
1860
1861         if ( $lang = get_bloginfo('language') ) {
1862                 if ( get_option('html_type') == 'text/html' || $doctype == 'html' )
1863                         $attributes[] = "lang=\"$lang\"";
1864
1865                 if ( get_option('html_type') != 'text/html' || $doctype == 'xhtml' )
1866                         $attributes[] = "xml:lang=\"$lang\"";
1867         }
1868
1869         $output = implode(' ', $attributes);
1870         $output = apply_filters('language_attributes', $output);
1871         echo $output;
1872 }
1873
1874 /**
1875  * Retrieve paginated link for archive post pages.
1876  *
1877  * Technically, the function can be used to create paginated link list for any
1878  * area. The 'base' argument is used to reference the url, which will be used to
1879  * create the paginated links. The 'format' argument is then used for replacing
1880  * the page number. It is however, most likely and by default, to be used on the
1881  * archive post pages.
1882  *
1883  * The 'type' argument controls format of the returned value. The default is
1884  * 'plain', which is just a string with the links separated by a newline
1885  * character. The other possible values are either 'array' or 'list'. The
1886  * 'array' value will return an array of the paginated link list to offer full
1887  * control of display. The 'list' value will place all of the paginated links in
1888  * an unordered HTML list.
1889  *
1890  * The 'total' argument is the total amount of pages and is an integer. The
1891  * 'current' argument is the current page number and is also an integer.
1892  *
1893  * An example of the 'base' argument is "http://example.com/all_posts.php%_%"
1894  * and the '%_%' is required. The '%_%' will be replaced by the contents of in
1895  * the 'format' argument. An example for the 'format' argument is "?page=%#%"
1896  * and the '%#%' is also required. The '%#%' will be replaced with the page
1897  * number.
1898  *
1899  * You can include the previous and next links in the list by setting the
1900  * 'prev_next' argument to true, which it is by default. You can set the
1901  * previous text, by using the 'prev_text' argument. You can set the next text
1902  * by setting the 'next_text' argument.
1903  *
1904  * If the 'show_all' argument is set to true, then it will show all of the pages
1905  * instead of a short list of the pages near the current page. By default, the
1906  * 'show_all' is set to false and controlled by the 'end_size' and 'mid_size'
1907  * arguments. The 'end_size' argument is how many numbers on either the start
1908  * and the end list edges, by default is 1. The 'mid_size' argument is how many
1909  * numbers to either side of current page, but not including current page.
1910  *
1911  * It is possible to add query vars to the link by using the 'add_args' argument
1912  * and see {@link add_query_arg()} for more information.
1913  *
1914  * @since 2.1.0
1915  *
1916  * @param string|array $args Optional. Override defaults.
1917  * @return array|string String of page links or array of page links.
1918  */
1919 function paginate_links( $args = '' ) {
1920         $defaults = array(
1921                 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
1922                 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
1923                 'total' => 1,
1924                 'current' => 0,
1925                 'show_all' => false,
1926                 'prev_next' => true,
1927                 'prev_text' => __('&laquo; Previous'),
1928                 'next_text' => __('Next &raquo;'),
1929                 'end_size' => 1,
1930                 'mid_size' => 2,
1931                 'type' => 'plain',
1932                 'add_args' => false, // array of query args to add
1933                 'add_fragment' => ''
1934         );
1935
1936         $args = wp_parse_args( $args, $defaults );
1937         extract($args, EXTR_SKIP);
1938
1939         // Who knows what else people pass in $args
1940         $total = (int) $total;
1941         if ( $total < 2 )
1942                 return;
1943         $current  = (int) $current;
1944         $end_size = 0  < (int) $end_size ? (int) $end_size : 1; // Out of bounds?  Make it the default.
1945         $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
1946         $add_args = is_array($add_args) ? $add_args : false;
1947         $r = '';
1948         $page_links = array();
1949         $n = 0;
1950         $dots = false;
1951
1952         if ( $prev_next && $current && 1 < $current ) :
1953                 $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
1954                 $link = str_replace('%#%', $current - 1, $link);
1955                 if ( $add_args )
1956                         $link = add_query_arg( $add_args, $link );
1957                 $link .= $add_fragment;
1958                 $page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $prev_text . '</a>';
1959         endif;
1960         for ( $n = 1; $n <= $total; $n++ ) :
1961                 $n_display = number_format_i18n($n);
1962                 if ( $n == $current ) :
1963                         $page_links[] = "<span class='page-numbers current'>$n_display</span>";
1964                         $dots = true;
1965                 else :
1966                         if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
1967                                 $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
1968                                 $link = str_replace('%#%', $n, $link);
1969                                 if ( $add_args )
1970                                         $link = add_query_arg( $add_args, $link );
1971                                 $link .= $add_fragment;
1972                                 $page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>$n_display</a>";
1973                                 $dots = true;
1974                         elseif ( $dots && !$show_all ) :
1975                                 $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
1976                                 $dots = false;
1977                         endif;
1978                 endif;
1979         endfor;
1980         if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
1981                 $link = str_replace('%_%', $format, $base);
1982                 $link = str_replace('%#%', $current + 1, $link);
1983                 if ( $add_args )
1984                         $link = add_query_arg( $add_args, $link );
1985                 $link .= $add_fragment;
1986                 $page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
1987         endif;
1988         switch ( $type ) :
1989                 case 'array' :
1990                         return $page_links;
1991                         break;
1992                 case 'list' :
1993                         $r .= "<ul class='page-numbers'>\n\t<li>";
1994                         $r .= join("</li>\n\t<li>", $page_links);
1995                         $r .= "</li>\n</ul>\n";
1996                         break;
1997                 default :
1998                         $r = join("\n", $page_links);
1999                         break;
2000         endswitch;
2001         return $r;
2002 }
2003
2004 /**
2005  * Registers an admin colour scheme css file.
2006  *
2007  * Allows a plugin to register a new admin colour scheme. For example:
2008  * <code>
2009  * wp_admin_css_color('classic', __('Classic'), admin_url("css/colors-classic.css"),
2010  * array('#07273E', '#14568A', '#D54E21', '#2683AE'));
2011  * </code>
2012  *
2013  * @since 2.5.0
2014  *
2015  * @param string $key The unique key for this theme.
2016  * @param string $name The name of the theme.
2017  * @param string $url The url of the css file containing the colour scheme.
2018  * @param array $colors Optional An array of CSS color definitions which are used to give the user a feel for the theme.
2019  */
2020 function wp_admin_css_color($key, $name, $url, $colors = array()) {
2021         global $_wp_admin_css_colors;
2022
2023         if ( !isset($_wp_admin_css_colors) )
2024                 $_wp_admin_css_colors = array();
2025
2026         $_wp_admin_css_colors[$key] = (object) array('name' => $name, 'url' => $url, 'colors' => $colors);
2027 }
2028
2029 /**
2030  * Registers the default Admin color schemes
2031  *
2032  * @since 3.0.0
2033  */
2034 function register_admin_color_schemes() {
2035         wp_admin_css_color( 'classic', _x( 'Blue', 'admin color scheme' ), admin_url( 'css/colors-classic.css' ),
2036                 array( '#5589aa', '#cfdfe9', '#d1e5ee', '#eff8ff' ) );
2037         wp_admin_css_color( 'fresh', _x( 'Gray', 'admin color scheme' ), admin_url( 'css/colors-fresh.css' ),
2038                 array( '#555', '#a0a0a0', '#ccc', '#f1f1f1' ) );
2039 }
2040
2041 /**
2042  * Display the URL of a WordPress admin CSS file.
2043  *
2044  * @see WP_Styles::_css_href and its style_loader_src filter.
2045  *
2046  * @since 2.3.0
2047  *
2048  * @param string $file file relative to wp-admin/ without its ".css" extension.
2049  */
2050 function wp_admin_css_uri( $file = 'wp-admin' ) {
2051         if ( defined('WP_INSTALLING') ) {
2052                 $_file = "./$file.css";
2053         } else {
2054                 $_file = admin_url("$file.css");
2055         }
2056         $_file = add_query_arg( 'version', get_bloginfo( 'version' ),  $_file );
2057
2058         return apply_filters( 'wp_admin_css_uri', $_file, $file );
2059 }
2060
2061 /**
2062  * Enqueues or directly prints a stylesheet link to the specified CSS file.
2063  *
2064  * "Intelligently" decides to enqueue or to print the CSS file. If the
2065  * 'wp_print_styles' action has *not* yet been called, the CSS file will be
2066  * enqueued. If the wp_print_styles action *has* been called, the CSS link will
2067  * be printed. Printing may be forced by passing true as the $force_echo
2068  * (second) parameter.
2069  *
2070  * For backward compatibility with WordPress 2.3 calling method: If the $file
2071  * (first) parameter does not correspond to a registered CSS file, we assume
2072  * $file is a file relative to wp-admin/ without its ".css" extension. A
2073  * stylesheet link to that generated URL is printed.
2074  *
2075  * @package WordPress
2076  * @since 2.3.0
2077  * @uses $wp_styles WordPress Styles Object
2078  *
2079  * @param string $file Optional. Style handle name or file name (without ".css" extension) relative
2080  *       to wp-admin/. Defaults to 'wp-admin'.
2081  * @param bool $force_echo Optional. Force the stylesheet link to be printed rather than enqueued.
2082  */
2083 function wp_admin_css( $file = 'wp-admin', $force_echo = false ) {
2084         global $wp_styles;
2085         if ( !is_a($wp_styles, 'WP_Styles') )
2086                 $wp_styles = new WP_Styles();
2087
2088         // For backward compatibility
2089         $handle = 0 === strpos( $file, 'css/' ) ? substr( $file, 4 ) : $file;
2090
2091         if ( $wp_styles->query( $handle ) ) {
2092                 if ( $force_echo || did_action( 'wp_print_styles' ) ) // we already printed the style queue. Print this one immediately
2093                         wp_print_styles( $handle );
2094                 else // Add to style queue
2095                         wp_enqueue_style( $handle );
2096                 return;
2097         }
2098
2099         echo apply_filters( 'wp_admin_css', "<link rel='stylesheet' href='" . esc_url( wp_admin_css_uri( $file ) ) . "' type='text/css' />\n", $file );
2100         if ( function_exists( 'is_rtl' ) && is_rtl() )
2101                 echo apply_filters( 'wp_admin_css', "<link rel='stylesheet' href='" . esc_url( wp_admin_css_uri( "$file-rtl" ) ) . "' type='text/css' />\n", "$file-rtl" );
2102 }
2103
2104 /**
2105  * Enqueues the default ThickBox js and css.
2106  *
2107  * If any of the settings need to be changed, this can be done with another js
2108  * file similar to media-upload.js. That file should
2109  * require array('thickbox') to ensure it is loaded after.
2110  *
2111  * @since 2.5.0
2112  */
2113 function add_thickbox() {
2114         wp_enqueue_script( 'thickbox' );
2115         wp_enqueue_style( 'thickbox' );
2116
2117         if ( is_network_admin() )
2118                 add_action( 'admin_head', '_thickbox_path_admin_subfolder' );
2119 }
2120
2121 /**
2122  * Display the XHTML generator that is generated on the wp_head hook.
2123  *
2124  * @since 2.5.0
2125  */
2126 function wp_generator() {
2127         the_generator( apply_filters( 'wp_generator_type', 'xhtml' ) );
2128 }
2129
2130 /**
2131  * Display the generator XML or Comment for RSS, ATOM, etc.
2132  *
2133  * Returns the correct generator type for the requested output format. Allows
2134  * for a plugin to filter generators overall the the_generator filter.
2135  *
2136  * @since 2.5.0
2137  * @uses apply_filters() Calls 'the_generator' hook.
2138  *
2139  * @param string $type The type of generator to output - (html|xhtml|atom|rss2|rdf|comment|export).
2140  */
2141 function the_generator( $type ) {
2142         echo apply_filters('the_generator', get_the_generator($type), $type) . "\n";
2143 }
2144
2145 /**
2146  * Creates the generator XML or Comment for RSS, ATOM, etc.
2147  *
2148  * Returns the correct generator type for the requested output format. Allows
2149  * for a plugin to filter generators on an individual basis using the
2150  * 'get_the_generator_{$type}' filter.
2151  *
2152  * @since 2.5.0
2153  * @uses apply_filters() Calls 'get_the_generator_$type' hook.
2154  *
2155  * @param string $type The type of generator to return - (html|xhtml|atom|rss2|rdf|comment|export).
2156  * @return string The HTML content for the generator.
2157  */
2158 function get_the_generator( $type = '' ) {
2159         if ( empty( $type ) ) {
2160
2161                 $current_filter = current_filter();
2162                 if ( empty( $current_filter ) )
2163                         return;
2164
2165                 switch ( $current_filter ) {
2166                         case 'rss2_head' :
2167                         case 'commentsrss2_head' :
2168                                 $type = 'rss2';
2169                                 break;
2170                         case 'rss_head' :
2171                         case 'opml_head' :
2172                                 $type = 'comment';
2173                                 break;
2174                         case 'rdf_header' :
2175                                 $type = 'rdf';
2176                                 break;
2177                         case 'atom_head' :
2178                         case 'comments_atom_head' :
2179                         case 'app_head' :
2180                                 $type = 'atom';
2181                                 break;
2182                 }
2183         }
2184
2185         switch ( $type ) {
2186                 case 'html':
2187                         $gen = '<meta name="generator" content="WordPress ' . get_bloginfo( 'version' ) . '">';
2188                         break;
2189                 case 'xhtml':
2190                         $gen = '<meta name="generator" content="WordPress ' . get_bloginfo( 'version' ) . '" />';
2191                         break;
2192                 case 'atom':
2193                         $gen = '<generator uri="http://wordpress.org/" version="' . get_bloginfo_rss( 'version' ) . '">WordPress</generator>';
2194                         break;
2195                 case 'rss2':
2196                         $gen = '<generator>http://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) . '</generator>';
2197                         break;
2198                 case 'rdf':
2199                         $gen = '<admin:generatorAgent rdf:resource="http://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) . '" />';
2200                         break;
2201                 case 'comment':
2202                         $gen = '<!-- generator="WordPress/' . get_bloginfo( 'version' ) . '" -->';
2203                         break;
2204                 case 'export':
2205                         $gen = '<!-- generator="WordPress/' . get_bloginfo_rss('version') . '" created="'. date('Y-m-d H:i') . '" -->';
2206                         break;
2207         }
2208         return apply_filters( "get_the_generator_{$type}", $gen, $type );
2209 }
2210
2211 /**
2212  * Outputs the html checked attribute.
2213  *
2214  * Compares the first two arguments and if identical marks as checked
2215  *
2216  * @since 1.0.0
2217  *
2218  * @param mixed $checked One of the values to compare
2219  * @param mixed $current (true) The other value to compare if not just true
2220  * @param bool $echo Whether to echo or just return the string
2221  * @return string html attribute or empty string
2222  */
2223 function checked( $checked, $current = true, $echo = true ) {
2224         return __checked_selected_helper( $checked, $current, $echo, 'checked' );
2225 }
2226
2227 /**
2228  * Outputs the html selected attribute.
2229  *
2230  * Compares the first two arguments and if identical marks as selected
2231  *
2232  * @since 1.0.0
2233  *
2234  * @param mixed $selected One of the values to compare
2235  * @param mixed $current (true) The other value to compare if not just true
2236  * @param bool $echo Whether to echo or just return the string
2237  * @return string html attribute or empty string
2238  */
2239 function selected( $selected, $current = true, $echo = true ) {
2240         return __checked_selected_helper( $selected, $current, $echo, 'selected' );
2241 }
2242
2243 /**
2244  * Outputs the html disabled attribute.
2245  *
2246  * Compares the first two arguments and if identical marks as disabled
2247  *
2248  * @since 3.0.0
2249  *
2250  * @param mixed $disabled One of the values to compare
2251  * @param mixed $current (true) The other value to compare if not just true
2252  * @param bool $echo Whether to echo or just return the string
2253  * @return string html attribute or empty string
2254  */
2255 function disabled( $disabled, $current = true, $echo = true ) {
2256         return __checked_selected_helper( $disabled, $current, $echo, 'disabled' );
2257 }
2258
2259 /**
2260  * Private helper function for checked, selected, and disabled.
2261  *
2262  * Compares the first two arguments and if identical marks as $type
2263  *
2264  * @since 2.8.0
2265  * @access private
2266  *
2267  * @param any $helper One of the values to compare
2268  * @param any $current (true) The other value to compare if not just true
2269  * @param bool $echo Whether to echo or just return the string
2270  * @param string $type The type of checked|selected|disabled we are doing
2271  * @return string html attribute or empty string
2272  */
2273 function __checked_selected_helper( $helper, $current, $echo, $type ) {
2274         if ( (string) $helper === (string) $current )
2275                 $result = " $type='$type'";
2276         else
2277                 $result = '';
2278
2279         if ( $echo )
2280                 echo $result;
2281
2282         return $result;
2283 }