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