PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/general-template.php

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