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