PageRenderTime 38ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/general-template.php

https://github.com/markjaquith/WordPress
PHP | 4920 lines | 2340 code | 456 blank | 2124 comment | 391 complexity | 5e0b9f128daa38abbd91cd422109d0b2 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. * @since 1.5.0
  18. * @since 5.5.0 A return value was added.
  19. * @since 5.5.0 The `$args` parameter was added.
  20. *
  21. * @param string $name The name of the specialised header.
  22. * @param array $args Optional. Additional arguments passed to the header template.
  23. * Default empty array.
  24. * @return void|false Void on success, false if the template does not exist.
  25. */
  26. function get_header( $name = null, $args = array() ) {
  27. /**
  28. * Fires before the header template file is loaded.
  29. *
  30. * @since 2.1.0
  31. * @since 2.8.0 The `$name` parameter was added.
  32. * @since 5.5.0 The `$args` parameter was added.
  33. *
  34. * @param string|null $name Name of the specific header file to use. Null for the default header.
  35. * @param array $args Additional arguments passed to the header template.
  36. */
  37. do_action( 'get_header', $name, $args );
  38. $templates = array();
  39. $name = (string) $name;
  40. if ( '' !== $name ) {
  41. $templates[] = "header-{$name}.php";
  42. }
  43. $templates[] = 'header.php';
  44. if ( ! locate_template( $templates, true, true, $args ) ) {
  45. return false;
  46. }
  47. }
  48. /**
  49. * Load footer template.
  50. *
  51. * Includes the footer template for a theme or if a name is specified then a
  52. * specialised footer will be included.
  53. *
  54. * For the parameter, if the file is called "footer-special.php" then specify
  55. * "special".
  56. *
  57. * @since 1.5.0
  58. * @since 5.5.0 A return value was added.
  59. * @since 5.5.0 The `$args` parameter was added.
  60. *
  61. * @param string $name The name of the specialised footer.
  62. * @param array $args Optional. Additional arguments passed to the footer template.
  63. * Default empty array.
  64. * @return void|false Void on success, false if the template does not exist.
  65. */
  66. function get_footer( $name = null, $args = array() ) {
  67. /**
  68. * Fires before the footer template file is loaded.
  69. *
  70. * @since 2.1.0
  71. * @since 2.8.0 The `$name` parameter was added.
  72. * @since 5.5.0 The `$args` parameter was added.
  73. *
  74. * @param string|null $name Name of the specific footer file to use. Null for the default footer.
  75. * @param array $args Additional arguments passed to the footer template.
  76. */
  77. do_action( 'get_footer', $name, $args );
  78. $templates = array();
  79. $name = (string) $name;
  80. if ( '' !== $name ) {
  81. $templates[] = "footer-{$name}.php";
  82. }
  83. $templates[] = 'footer.php';
  84. if ( ! locate_template( $templates, true, true, $args ) ) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Load sidebar template.
  90. *
  91. * Includes the sidebar template for a theme or if a name is specified then a
  92. * specialised sidebar will be included.
  93. *
  94. * For the parameter, if the file is called "sidebar-special.php" then specify
  95. * "special".
  96. *
  97. * @since 1.5.0
  98. * @since 5.5.0 A return value was added.
  99. * @since 5.5.0 The `$args` parameter was added.
  100. *
  101. * @param string $name The name of the specialised sidebar.
  102. * @param array $args Optional. Additional arguments passed to the sidebar template.
  103. * Default empty array.
  104. * @return void|false Void on success, false if the template does not exist.
  105. */
  106. function get_sidebar( $name = null, $args = array() ) {
  107. /**
  108. * Fires before the sidebar template file is loaded.
  109. *
  110. * @since 2.2.0
  111. * @since 2.8.0 The `$name` parameter was added.
  112. * @since 5.5.0 The `$args` parameter was added.
  113. *
  114. * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
  115. * @param array $args Additional arguments passed to the sidebar template.
  116. */
  117. do_action( 'get_sidebar', $name, $args );
  118. $templates = array();
  119. $name = (string) $name;
  120. if ( '' !== $name ) {
  121. $templates[] = "sidebar-{$name}.php";
  122. }
  123. $templates[] = 'sidebar.php';
  124. if ( ! locate_template( $templates, true, true, $args ) ) {
  125. return false;
  126. }
  127. }
  128. /**
  129. * Loads a template part into a template.
  130. *
  131. * Provides a simple mechanism for child themes to overload reusable sections of code
  132. * in the theme.
  133. *
  134. * Includes the named template part for a theme or if a name is specified then a
  135. * specialised part will be included. If the theme contains no {slug}.php file
  136. * then no template will be included.
  137. *
  138. * The template is included using require, not require_once, so you may include the
  139. * same template part multiple times.
  140. *
  141. * For the $name parameter, if the file is called "{slug}-special.php" then specify
  142. * "special".
  143. *
  144. * @since 3.0.0
  145. * @since 5.5.0 A return value was added.
  146. * @since 5.5.0 The `$args` parameter was added.
  147. *
  148. * @param string $slug The slug name for the generic template.
  149. * @param string $name The name of the specialised template.
  150. * @param array $args Optional. Additional arguments passed to the template.
  151. * Default empty array.
  152. * @return void|false Void on success, false if the template does not exist.
  153. */
  154. function get_template_part( $slug, $name = null, $args = array() ) {
  155. /**
  156. * Fires before the specified template part file is loaded.
  157. *
  158. * The dynamic portion of the hook name, `$slug`, refers to the slug name
  159. * for the generic template part.
  160. *
  161. * @since 3.0.0
  162. * @since 5.5.0 The `$args` parameter was added.
  163. *
  164. * @param string $slug The slug name for the generic template.
  165. * @param string|null $name The name of the specialized template.
  166. * @param array $args Additional arguments passed to the template.
  167. */
  168. do_action( "get_template_part_{$slug}", $slug, $name, $args );
  169. $templates = array();
  170. $name = (string) $name;
  171. if ( '' !== $name ) {
  172. $templates[] = "{$slug}-{$name}.php";
  173. }
  174. $templates[] = "{$slug}.php";
  175. /**
  176. * Fires before an attempt is made to locate and load a template part.
  177. *
  178. * @since 5.2.0
  179. * @since 5.5.0 The `$args` parameter was added.
  180. *
  181. * @param string $slug The slug name for the generic template.
  182. * @param string $name The name of the specialized template.
  183. * @param string[] $templates Array of template files to search for, in order.
  184. * @param array $args Additional arguments passed to the template.
  185. */
  186. do_action( 'get_template_part', $slug, $name, $templates, $args );
  187. if ( ! locate_template( $templates, true, false, $args ) ) {
  188. return false;
  189. }
  190. }
  191. /**
  192. * Display search form.
  193. *
  194. * Will first attempt to locate the searchform.php file in either the child or
  195. * the parent, then load it. If it doesn't exist, then the default search form
  196. * will be displayed. The default search form is HTML, which will be displayed.
  197. * There is a filter applied to the search form HTML in order to edit or replace
  198. * it. The filter is {@see 'get_search_form'}.
  199. *
  200. * This function is primarily used by themes which want to hardcode the search
  201. * form into the sidebar and also by the search widget in WordPress.
  202. *
  203. * There is also an action that is called whenever the function is run called,
  204. * {@see 'pre_get_search_form'}. This can be useful for outputting JavaScript that the
  205. * search relies on or various formatting that applies to the beginning of the
  206. * search. To give a few examples of what it can be used for.
  207. *
  208. * @since 2.7.0
  209. * @since 5.2.0 The `$args` array parameter was added in place of an `$echo` boolean flag.
  210. *
  211. * @param array $args {
  212. * Optional. Array of display arguments.
  213. *
  214. * @type bool $echo Whether to echo or return the form. Default true.
  215. * @type string $aria_label ARIA label for the search form. Useful to distinguish
  216. * multiple search forms on the same page and improve
  217. * accessibility. Default empty.
  218. * }
  219. * @return void|string Void if 'echo' argument is true, search form HTML if 'echo' is false.
  220. */
  221. function get_search_form( $args = array() ) {
  222. /**
  223. * Fires before the search form is retrieved, at the start of get_search_form().
  224. *
  225. * @since 2.7.0 as 'get_search_form' action.
  226. * @since 3.6.0
  227. * @since 5.5.0 The `$args` parameter was added.
  228. *
  229. * @link https://core.trac.wordpress.org/ticket/19321
  230. *
  231. * @param array $args The array of arguments for building the search form.
  232. * See get_search_form() for information on accepted arguments.
  233. */
  234. do_action( 'pre_get_search_form', $args );
  235. $echo = true;
  236. if ( ! is_array( $args ) ) {
  237. /*
  238. * Back compat: to ensure previous uses of get_search_form() continue to
  239. * function as expected, we handle a value for the boolean $echo param removed
  240. * in 5.2.0. Then we deal with the $args array and cast its defaults.
  241. */
  242. $echo = (bool) $args;
  243. // Set an empty array and allow default arguments to take over.
  244. $args = array();
  245. }
  246. // Defaults are to echo and to output no custom label on the form.
  247. $defaults = array(
  248. 'echo' => $echo,
  249. 'aria_label' => '',
  250. );
  251. $args = wp_parse_args( $args, $defaults );
  252. /**
  253. * Filters the array of arguments used when generating the search form.
  254. *
  255. * @since 5.2.0
  256. *
  257. * @param array $args The array of arguments for building the search form.
  258. * See get_search_form() for information on accepted arguments.
  259. */
  260. $args = apply_filters( 'search_form_args', $args );
  261. // Ensure that the filtered arguments contain all required default values.
  262. $args = array_merge( $defaults, $args );
  263. $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
  264. /**
  265. * Filters the HTML format of the search form.
  266. *
  267. * @since 3.6.0
  268. * @since 5.5.0 The `$args` parameter was added.
  269. *
  270. * @param string $format The type of markup to use in the search form.
  271. * Accepts 'html5', 'xhtml'.
  272. * @param array $args The array of arguments for building the search form.
  273. * See get_search_form() for information on accepted arguments.
  274. */
  275. $format = apply_filters( 'search_form_format', $format, $args );
  276. $search_form_template = locate_template( 'searchform.php' );
  277. if ( '' !== $search_form_template ) {
  278. ob_start();
  279. require $search_form_template;
  280. $form = ob_get_clean();
  281. } else {
  282. // Build a string containing an aria-label to use for the search form.
  283. if ( $args['aria_label'] ) {
  284. $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
  285. } else {
  286. /*
  287. * If there's no custom aria-label, we can set a default here. At the
  288. * moment it's empty as there's uncertainty about what the default should be.
  289. */
  290. $aria_label = '';
  291. }
  292. if ( 'html5' === $format ) {
  293. $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
  294. <label>
  295. <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
  296. <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
  297. </label>
  298. <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  299. </form>';
  300. } else {
  301. $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
  302. <div>
  303. <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
  304. <input type="text" value="' . get_search_query() . '" name="s" id="s" />
  305. <input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  306. </div>
  307. </form>';
  308. }
  309. }
  310. /**
  311. * Filters the HTML output of the search form.
  312. *
  313. * @since 2.7.0
  314. * @since 5.5.0 The `$args` parameter was added.
  315. *
  316. * @param string $form The search form HTML output.
  317. * @param array $args The array of arguments for building the search form.
  318. * See get_search_form() for information on accepted arguments.
  319. */
  320. $result = apply_filters( 'get_search_form', $form, $args );
  321. if ( null === $result ) {
  322. $result = $form;
  323. }
  324. if ( $args['echo'] ) {
  325. echo $result;
  326. } else {
  327. return $result;
  328. }
  329. }
  330. /**
  331. * Display the Log In/Out link.
  332. *
  333. * Displays a link, which allows users to navigate to the Log In page to log in
  334. * or log out depending on whether they are currently logged in.
  335. *
  336. * @since 1.5.0
  337. *
  338. * @param string $redirect Optional path to redirect to on login/logout.
  339. * @param bool $echo Default to echo and not return the link.
  340. * @return void|string Void if `$echo` argument is true, log in/out link if `$echo` is false.
  341. */
  342. function wp_loginout( $redirect = '', $echo = true ) {
  343. if ( ! is_user_logged_in() ) {
  344. $link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __( 'Log in' ) . '</a>';
  345. } else {
  346. $link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
  347. }
  348. if ( $echo ) {
  349. /**
  350. * Filters the HTML output for the Log In/Log Out link.
  351. *
  352. * @since 1.5.0
  353. *
  354. * @param string $link The HTML link content.
  355. */
  356. echo apply_filters( 'loginout', $link );
  357. } else {
  358. /** This filter is documented in wp-includes/general-template.php */
  359. return apply_filters( 'loginout', $link );
  360. }
  361. }
  362. /**
  363. * Retrieves the logout URL.
  364. *
  365. * Returns the URL that allows the user to log out of the site.
  366. *
  367. * @since 2.7.0
  368. *
  369. * @param string $redirect Path to redirect to on logout.
  370. * @return string The logout URL. Note: HTML-encoded via esc_html() in wp_nonce_url().
  371. */
  372. function wp_logout_url( $redirect = '' ) {
  373. $args = array();
  374. if ( ! empty( $redirect ) ) {
  375. $args['redirect_to'] = urlencode( $redirect );
  376. }
  377. $logout_url = add_query_arg( $args, site_url( 'wp-login.php?action=logout', 'login' ) );
  378. $logout_url = wp_nonce_url( $logout_url, 'log-out' );
  379. /**
  380. * Filters the logout URL.
  381. *
  382. * @since 2.8.0
  383. *
  384. * @param string $logout_url The HTML-encoded logout URL.
  385. * @param string $redirect Path to redirect to on logout.
  386. */
  387. return apply_filters( 'logout_url', $logout_url, $redirect );
  388. }
  389. /**
  390. * Retrieves the login URL.
  391. *
  392. * @since 2.7.0
  393. *
  394. * @param string $redirect Path to redirect to on log in.
  395. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  396. * Default false.
  397. * @return string The login URL. Not HTML-encoded.
  398. */
  399. function wp_login_url( $redirect = '', $force_reauth = false ) {
  400. $login_url = site_url( 'wp-login.php', 'login' );
  401. if ( ! empty( $redirect ) ) {
  402. $login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
  403. }
  404. if ( $force_reauth ) {
  405. $login_url = add_query_arg( 'reauth', '1', $login_url );
  406. }
  407. /**
  408. * Filters the login URL.
  409. *
  410. * @since 2.8.0
  411. * @since 4.2.0 The `$force_reauth` parameter was added.
  412. *
  413. * @param string $login_url The login URL. Not HTML-encoded.
  414. * @param string $redirect The path to redirect to on login, if supplied.
  415. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  416. */
  417. return apply_filters( 'login_url', $login_url, $redirect, $force_reauth );
  418. }
  419. /**
  420. * Returns the URL that allows the user to register on the site.
  421. *
  422. * @since 3.6.0
  423. *
  424. * @return string User registration URL.
  425. */
  426. function wp_registration_url() {
  427. /**
  428. * Filters the user registration URL.
  429. *
  430. * @since 3.6.0
  431. *
  432. * @param string $register The user registration URL.
  433. */
  434. return apply_filters( 'register_url', site_url( 'wp-login.php?action=register', 'login' ) );
  435. }
  436. /**
  437. * Provides a simple login form for use anywhere within WordPress.
  438. *
  439. * The login form HTML is echoed by default. Pass a false value for `$echo` to return it instead.
  440. *
  441. * @since 3.0.0
  442. *
  443. * @param array $args {
  444. * Optional. Array of options to control the form output. Default empty array.
  445. *
  446. * @type bool $echo Whether to display the login form or return the form HTML code.
  447. * Default true (echo).
  448. * @type string $redirect URL to redirect to. Must be absolute, as in "https://example.com/mypage/".
  449. * Default is to redirect back to the request URI.
  450. * @type string $form_id ID attribute value for the form. Default 'loginform'.
  451. * @type string $label_username Label for the username or email address field. Default 'Username or Email Address'.
  452. * @type string $label_password Label for the password field. Default 'Password'.
  453. * @type string $label_remember Label for the remember field. Default 'Remember Me'.
  454. * @type string $label_log_in Label for the submit button. Default 'Log In'.
  455. * @type string $id_username ID attribute value for the username field. Default 'user_login'.
  456. * @type string $id_password ID attribute value for the password field. Default 'user_pass'.
  457. * @type string $id_remember ID attribute value for the remember field. Default 'rememberme'.
  458. * @type string $id_submit ID attribute value for the submit button. Default 'wp-submit'.
  459. * @type bool $remember Whether to display the "rememberme" checkbox in the form.
  460. * @type string $value_username Default value for the username field. Default empty.
  461. * @type bool $value_remember Whether the "Remember Me" checkbox should be checked by default.
  462. * Default false (unchecked).
  463. *
  464. * }
  465. * @return void|string Void if 'echo' argument is true, login form HTML if 'echo' is false.
  466. */
  467. function wp_login_form( $args = array() ) {
  468. $defaults = array(
  469. 'echo' => true,
  470. // Default 'redirect' value takes the user back to the request URI.
  471. 'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
  472. 'form_id' => 'loginform',
  473. 'label_username' => __( 'Username or Email Address' ),
  474. 'label_password' => __( 'Password' ),
  475. 'label_remember' => __( 'Remember Me' ),
  476. 'label_log_in' => __( 'Log In' ),
  477. 'id_username' => 'user_login',
  478. 'id_password' => 'user_pass',
  479. 'id_remember' => 'rememberme',
  480. 'id_submit' => 'wp-submit',
  481. 'remember' => true,
  482. 'value_username' => '',
  483. // Set 'value_remember' to true to default the "Remember me" checkbox to checked.
  484. 'value_remember' => false,
  485. );
  486. /**
  487. * Filters the default login form output arguments.
  488. *
  489. * @since 3.0.0
  490. *
  491. * @see wp_login_form()
  492. *
  493. * @param array $defaults An array of default login form arguments.
  494. */
  495. $args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );
  496. /**
  497. * Filters content to display at the top of the login form.
  498. *
  499. * The filter evaluates just following the opening form tag element.
  500. *
  501. * @since 3.0.0
  502. *
  503. * @param string $content Content to display. Default empty.
  504. * @param array $args Array of login form arguments.
  505. */
  506. $login_form_top = apply_filters( 'login_form_top', '', $args );
  507. /**
  508. * Filters content to display in the middle of the login form.
  509. *
  510. * The filter evaluates just following the location where the 'login-password'
  511. * field is displayed.
  512. *
  513. * @since 3.0.0
  514. *
  515. * @param string $content Content to display. Default empty.
  516. * @param array $args Array of login form arguments.
  517. */
  518. $login_form_middle = apply_filters( 'login_form_middle', '', $args );
  519. /**
  520. * Filters content to display at the bottom of the login form.
  521. *
  522. * The filter evaluates just preceding the closing form tag element.
  523. *
  524. * @since 3.0.0
  525. *
  526. * @param string $content Content to display. Default empty.
  527. * @param array $args Array of login form arguments.
  528. */
  529. $login_form_bottom = apply_filters( 'login_form_bottom', '', $args );
  530. $form =
  531. sprintf(
  532. '<form name="%1$s" id="%1$s" action="%2$s" method="post">',
  533. esc_attr( $args['form_id'] ),
  534. esc_url( site_url( 'wp-login.php', 'login_post' ) )
  535. ) .
  536. $login_form_top .
  537. sprintf(
  538. '<p class="login-username">
  539. <label for="%1$s">%2$s</label>
  540. <input type="text" name="log" id="%1$s" class="input" value="%3$s" size="20" />
  541. </p>',
  542. esc_attr( $args['id_username'] ),
  543. esc_html( $args['label_username'] ),
  544. esc_attr( $args['value_username'] )
  545. ) .
  546. sprintf(
  547. '<p class="login-password">
  548. <label for="%1$s">%2$s</label>
  549. <input type="password" name="pwd" id="%1$s" class="input" value="" size="20" />
  550. </p>',
  551. esc_attr( $args['id_password'] ),
  552. esc_html( $args['label_password'] )
  553. ) .
  554. $login_form_middle .
  555. ( $args['remember'] ?
  556. sprintf(
  557. '<p class="login-remember"><label><input name="rememberme" type="checkbox" id="%1$s" value="forever"%2$s /> %3$s</label></p>',
  558. esc_attr( $args['id_remember'] ),
  559. ( $args['value_remember'] ? ' checked="checked"' : '' ),
  560. esc_html( $args['label_remember'] )
  561. ) : ''
  562. ) .
  563. sprintf(
  564. '<p class="login-submit">
  565. <input type="submit" name="wp-submit" id="%1$s" class="button button-primary" value="%2$s" />
  566. <input type="hidden" name="redirect_to" value="%3$s" />
  567. </p>',
  568. esc_attr( $args['id_submit'] ),
  569. esc_attr( $args['label_log_in'] ),
  570. esc_url( $args['redirect'] )
  571. ) .
  572. $login_form_bottom .
  573. '</form>';
  574. if ( $args['echo'] ) {
  575. echo $form;
  576. } else {
  577. return $form;
  578. }
  579. }
  580. /**
  581. * Returns the URL that allows the user to retrieve the lost password
  582. *
  583. * @since 2.8.0
  584. *
  585. * @param string $redirect Path to redirect to on login.
  586. * @return string Lost password URL.
  587. */
  588. function wp_lostpassword_url( $redirect = '' ) {
  589. $args = array(
  590. 'action' => 'lostpassword',
  591. );
  592. if ( ! empty( $redirect ) ) {
  593. $args['redirect_to'] = urlencode( $redirect );
  594. }
  595. if ( is_multisite() ) {
  596. $blog_details = get_blog_details();
  597. $wp_login_path = $blog_details->path . 'wp-login.php';
  598. } else {
  599. $wp_login_path = 'wp-login.php';
  600. }
  601. $lostpassword_url = add_query_arg( $args, network_site_url( $wp_login_path, 'login' ) );
  602. /**
  603. * Filters the Lost Password URL.
  604. *
  605. * @since 2.8.0
  606. *
  607. * @param string $lostpassword_url The lost password page URL.
  608. * @param string $redirect The path to redirect to on login.
  609. */
  610. return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
  611. }
  612. /**
  613. * Display the Registration or Admin link.
  614. *
  615. * Display a link which allows the user to navigate to the registration page if
  616. * not logged in and registration is enabled or to the dashboard if logged in.
  617. *
  618. * @since 1.5.0
  619. *
  620. * @param string $before Text to output before the link. Default `<li>`.
  621. * @param string $after Text to output after the link. Default `</li>`.
  622. * @param bool $echo Default to echo and not return the link.
  623. * @return void|string Void if `$echo` argument is true, registration or admin link
  624. * if `$echo` is false.
  625. */
  626. function wp_register( $before = '<li>', $after = '</li>', $echo = true ) {
  627. if ( ! is_user_logged_in() ) {
  628. if ( get_option( 'users_can_register' ) ) {
  629. $link = $before . '<a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a>' . $after;
  630. } else {
  631. $link = '';
  632. }
  633. } elseif ( current_user_can( 'read' ) ) {
  634. $link = $before . '<a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a>' . $after;
  635. } else {
  636. $link = '';
  637. }
  638. /**
  639. * Filters the HTML link to the Registration or Admin page.
  640. *
  641. * Users are sent to the admin page if logged-in, or the registration page
  642. * if enabled and logged-out.
  643. *
  644. * @since 1.5.0
  645. *
  646. * @param string $link The HTML code for the link to the Registration or Admin page.
  647. */
  648. $link = apply_filters( 'register', $link );
  649. if ( $echo ) {
  650. echo $link;
  651. } else {
  652. return $link;
  653. }
  654. }
  655. /**
  656. * Theme container function for the 'wp_meta' action.
  657. *
  658. * The {@see 'wp_meta'} action can have several purposes, depending on how you use it,
  659. * but one purpose might have been to allow for theme switching.
  660. *
  661. * @since 1.5.0
  662. *
  663. * @link https://core.trac.wordpress.org/ticket/1458 Explanation of 'wp_meta' action.
  664. */
  665. function wp_meta() {
  666. /**
  667. * Fires before displaying echoed content in the sidebar.
  668. *
  669. * @since 1.5.0
  670. */
  671. do_action( 'wp_meta' );
  672. }
  673. /**
  674. * Displays information about the current site.
  675. *
  676. * @since 0.71
  677. *
  678. * @see get_bloginfo() For possible `$show` values
  679. *
  680. * @param string $show Optional. Site information to display. Default empty.
  681. */
  682. function bloginfo( $show = '' ) {
  683. echo get_bloginfo( $show, 'display' );
  684. }
  685. /**
  686. * Retrieves information about the current site.
  687. *
  688. * Possible values for `$show` include:
  689. *
  690. * - 'name' - Site title (set in Settings > General)
  691. * - 'description' - Site tagline (set in Settings > General)
  692. * - 'wpurl' - The WordPress address (URL) (set in Settings > General)
  693. * - 'url' - The Site address (URL) (set in Settings > General)
  694. * - 'admin_email' - Admin email (set in Settings > General)
  695. * - 'charset' - The "Encoding for pages and feeds" (set in Settings > Reading)
  696. * - 'version' - The current WordPress version
  697. * - 'html_type' - The content-type (default: "text/html"). Themes and plugins
  698. * can override the default value using the {@see 'pre_option_html_type'} filter
  699. * - 'text_direction' - The text direction determined by the site's language. is_rtl()
  700. * should be used instead
  701. * - 'language' - Language code for the current site
  702. * - 'stylesheet_url' - URL to the stylesheet for the active theme. An active child theme
  703. * will take precedence over this value
  704. * - 'stylesheet_directory' - Directory path for the active theme. An active child theme
  705. * will take precedence over this value
  706. * - 'template_url' / 'template_directory' - URL of the active theme's directory. An active
  707. * child theme will NOT take precedence over this value
  708. * - 'pingback_url' - The pingback XML-RPC file URL (xmlrpc.php)
  709. * - 'atom_url' - The Atom feed URL (/feed/atom)
  710. * - 'rdf_url' - The RDF/RSS 1.0 feed URL (/feed/rdf)
  711. * - 'rss_url' - The RSS 0.92 feed URL (/feed/rss)
  712. * - 'rss2_url' - The RSS 2.0 feed URL (/feed)
  713. * - 'comments_atom_url' - The comments Atom feed URL (/comments/feed)
  714. * - 'comments_rss2_url' - The comments RSS 2.0 feed URL (/comments/feed)
  715. *
  716. * Some `$show` values are deprecated and will be removed in future versions.
  717. * These options will trigger the _deprecated_argument() function.
  718. *
  719. * Deprecated arguments include:
  720. *
  721. * - 'siteurl' - Use 'url' instead
  722. * - 'home' - Use 'url' instead
  723. *
  724. * @since 0.71
  725. *
  726. * @global string $wp_version The WordPress version string.
  727. *
  728. * @param string $show Optional. Site info to retrieve. Default empty (site name).
  729. * @param string $filter Optional. How to filter what is retrieved. Default 'raw'.
  730. * @return string Mostly string values, might be empty.
  731. */
  732. function get_bloginfo( $show = '', $filter = 'raw' ) {
  733. switch ( $show ) {
  734. case 'home': // Deprecated.
  735. case 'siteurl': // Deprecated.
  736. _deprecated_argument(
  737. __FUNCTION__,
  738. '2.2.0',
  739. sprintf(
  740. /* translators: 1: 'siteurl'/'home' argument, 2: bloginfo() function name, 3: 'url' argument. */
  741. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s option instead.' ),
  742. '<code>' . $show . '</code>',
  743. '<code>bloginfo()</code>',
  744. '<code>url</code>'
  745. )
  746. );
  747. // Intentional fall-through to be handled by the 'url' case.
  748. case 'url':
  749. $output = home_url();
  750. break;
  751. case 'wpurl':
  752. $output = site_url();
  753. break;
  754. case 'description':
  755. $output = get_option( 'blogdescription' );
  756. break;
  757. case 'rdf_url':
  758. $output = get_feed_link( 'rdf' );
  759. break;
  760. case 'rss_url':
  761. $output = get_feed_link( 'rss' );
  762. break;
  763. case 'rss2_url':
  764. $output = get_feed_link( 'rss2' );
  765. break;
  766. case 'atom_url':
  767. $output = get_feed_link( 'atom' );
  768. break;
  769. case 'comments_atom_url':
  770. $output = get_feed_link( 'comments_atom' );
  771. break;
  772. case 'comments_rss2_url':
  773. $output = get_feed_link( 'comments_rss2' );
  774. break;
  775. case 'pingback_url':
  776. $output = site_url( 'xmlrpc.php' );
  777. break;
  778. case 'stylesheet_url':
  779. $output = get_stylesheet_uri();
  780. break;
  781. case 'stylesheet_directory':
  782. $output = get_stylesheet_directory_uri();
  783. break;
  784. case 'template_directory':
  785. case 'template_url':
  786. $output = get_template_directory_uri();
  787. break;
  788. case 'admin_email':
  789. $output = get_option( 'admin_email' );
  790. break;
  791. case 'charset':
  792. $output = get_option( 'blog_charset' );
  793. if ( '' === $output ) {
  794. $output = 'UTF-8';
  795. }
  796. break;
  797. case 'html_type':
  798. $output = get_option( 'html_type' );
  799. break;
  800. case 'version':
  801. global $wp_version;
  802. $output = $wp_version;
  803. break;
  804. case 'language':
  805. /*
  806. * translators: Translate this to the correct language tag for your locale,
  807. * see https://www.w3.org/International/articles/language-tags/ for reference.
  808. * Do not translate into your own language.
  809. */
  810. $output = __( 'html_lang_attribute' );
  811. if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
  812. $output = determine_locale();
  813. $output = str_replace( '_', '-', $output );
  814. }
  815. break;
  816. case 'text_direction':
  817. _deprecated_argument(
  818. __FUNCTION__,
  819. '2.2.0',
  820. sprintf(
  821. /* translators: 1: 'text_direction' argument, 2: bloginfo() function name, 3: is_rtl() function name. */
  822. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s function instead.' ),
  823. '<code>' . $show . '</code>',
  824. '<code>bloginfo()</code>',
  825. '<code>is_rtl()</code>'
  826. )
  827. );
  828. if ( function_exists( 'is_rtl' ) ) {
  829. $output = is_rtl() ? 'rtl' : 'ltr';
  830. } else {
  831. $output = 'ltr';
  832. }
  833. break;
  834. case 'name':
  835. default:
  836. $output = get_option( 'blogname' );
  837. break;
  838. }
  839. $url = true;
  840. if ( strpos( $show, 'url' ) === false &&
  841. strpos( $show, 'directory' ) === false &&
  842. strpos( $show, 'home' ) === false ) {
  843. $url = false;
  844. }
  845. if ( 'display' === $filter ) {
  846. if ( $url ) {
  847. /**
  848. * Filters the URL returned by get_bloginfo().
  849. *
  850. * @since 2.0.5
  851. *
  852. * @param string $output The URL returned by bloginfo().
  853. * @param string $show Type of information requested.
  854. */
  855. $output = apply_filters( 'bloginfo_url', $output, $show );
  856. } else {
  857. /**
  858. * Filters the site information returned by get_bloginfo().
  859. *
  860. * @since 0.71
  861. *
  862. * @param mixed $output The requested non-URL site information.
  863. * @param string $show Type of information requested.
  864. */
  865. $output = apply_filters( 'bloginfo', $output, $show );
  866. }
  867. }
  868. return $output;
  869. }
  870. /**
  871. * Returns the Site Icon URL.
  872. *
  873. * @since 4.3.0
  874. *
  875. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  876. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  877. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  878. * @return string Site Icon URL.
  879. */
  880. function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  881. $switched_blog = false;
  882. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  883. switch_to_blog( $blog_id );
  884. $switched_blog = true;
  885. }
  886. $site_icon_id = get_option( 'site_icon' );
  887. if ( $site_icon_id ) {
  888. if ( $size >= 512 ) {
  889. $size_data = 'full';
  890. } else {
  891. $size_data = array( $size, $size );
  892. }
  893. $url = wp_get_attachment_image_url( $site_icon_id, $size_data );
  894. }
  895. if ( $switched_blog ) {
  896. restore_current_blog();
  897. }
  898. /**
  899. * Filters the site icon URL.
  900. *
  901. * @since 4.4.0
  902. *
  903. * @param string $url Site icon URL.
  904. * @param int $size Size of the site icon.
  905. * @param int $blog_id ID of the blog to get the site icon for.
  906. */
  907. return apply_filters( 'get_site_icon_url', $url, $size, $blog_id );
  908. }
  909. /**
  910. * Displays the Site Icon URL.
  911. *
  912. * @since 4.3.0
  913. *
  914. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  915. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  916. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  917. */
  918. function site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  919. echo esc_url( get_site_icon_url( $size, $url, $blog_id ) );
  920. }
  921. /**
  922. * Whether the site has a Site Icon.
  923. *
  924. * @since 4.3.0
  925. *
  926. * @param int $blog_id Optional. ID of the blog in question. Default current blog.
  927. * @return bool Whether the site has a site icon or not.
  928. */
  929. function has_site_icon( $blog_id = 0 ) {
  930. return (bool) get_site_icon_url( 512, '', $blog_id );
  931. }
  932. /**
  933. * Determines whether the site has a custom logo.
  934. *
  935. * @since 4.5.0
  936. *
  937. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  938. * @return bool Whether the site has a custom logo or not.
  939. */
  940. function has_custom_logo( $blog_id = 0 ) {
  941. $switched_blog = false;
  942. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  943. switch_to_blog( $blog_id );
  944. $switched_blog = true;
  945. }
  946. $custom_logo_id = get_theme_mod( 'custom_logo' );
  947. if ( $switched_blog ) {
  948. restore_current_blog();
  949. }
  950. return (bool) $custom_logo_id;
  951. }
  952. /**
  953. * Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
  954. *
  955. * @since 4.5.0
  956. * @since 5.5.0 Added option to remove the link on the home page with `unlink-homepage-logo` theme support
  957. * for the `custom-logo` theme feature.
  958. * @since 5.5.1 Disabled lazy-loading by default.
  959. *
  960. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  961. * @return string Custom logo markup.
  962. */
  963. function get_custom_logo( $blog_id = 0 ) {
  964. $html = '';
  965. $switched_blog = false;
  966. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  967. switch_to_blog( $blog_id );
  968. $switched_blog = true;
  969. }
  970. $custom_logo_id = get_theme_mod( 'custom_logo' );
  971. // We have a logo. Logo is go.
  972. if ( $custom_logo_id ) {
  973. $custom_logo_attr = array(
  974. 'class' => 'custom-logo',
  975. 'loading' => false,
  976. );
  977. $unlink_homepage_logo = (bool) get_theme_support( 'custom-logo', 'unlink-homepage-logo' );
  978. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  979. /*
  980. * If on the home page, set the logo alt attribute to an empty string,
  981. * as the image is decorative and doesn't need its purpose to be described.
  982. */
  983. $custom_logo_attr['alt'] = '';
  984. } else {
  985. /*
  986. * If the logo alt attribute is empty, get the site title and explicitly pass it
  987. * to the attributes used by wp_get_attachment_image().
  988. */
  989. $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
  990. if ( empty( $image_alt ) ) {
  991. $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
  992. }
  993. }
  994. /**
  995. * Filters the list of custom logo image attributes.
  996. *
  997. * @since 5.5.0
  998. *
  999. * @param array $custom_logo_attr Custom logo image attributes.
  1000. * @param int $custom_logo_id Custom logo attachment ID.
  1001. * @param int $blog_id ID of the blog to get the custom logo for.
  1002. */
  1003. $custom_logo_attr = apply_filters( 'get_custom_logo_image_attributes', $custom_logo_attr, $custom_logo_id, $blog_id );
  1004. /*
  1005. * If the alt attribute is not empty, there's no need to explicitly pass it
  1006. * because wp_get_attachment_image() already adds the alt attribute.
  1007. */
  1008. $image = wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr );
  1009. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  1010. // If on the home page, don't link the logo to home.
  1011. $html = sprintf(
  1012. '<span class="custom-logo-link">%1$s</span>',
  1013. $image
  1014. );
  1015. } else {
  1016. $aria_current = is_front_page() && ! is_paged() ? ' aria-current="page"' : '';
  1017. $html = sprintf(
  1018. '<a href="%1$s" class="custom-logo-link" rel="home"%2$s>%3$s</a>',
  1019. esc_url( home_url( '/' ) ),
  1020. $aria_current,
  1021. $image
  1022. );
  1023. }
  1024. } elseif ( is_customize_preview() ) {
  1025. // If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
  1026. $html = sprintf(
  1027. '<a href="%1$s" class="custom-logo-link" style="display:none;"><img class="custom-logo" alt="" /></a>',
  1028. esc_url( home_url( '/' ) )
  1029. );
  1030. }
  1031. if ( $switched_blog ) {
  1032. restore_current_blog();
  1033. }
  1034. /**
  1035. * Filters the custom logo output.
  1036. *
  1037. * @since 4.5.0
  1038. * @since 4.6.0 Added the `$blog_id` parameter.
  1039. *
  1040. * @param string $html Custom logo HTML output.
  1041. * @param int $blog_id ID of the blog to get the custom logo for.
  1042. */
  1043. return apply_filters( 'get_custom_logo', $html, $blog_id );
  1044. }
  1045. /**
  1046. * Displays a custom logo, linked to home unless the theme supports removing the link on the home page.
  1047. *
  1048. * @since 4.5.0
  1049. *
  1050. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  1051. */
  1052. function the_custom_logo( $blog_id = 0 ) {
  1053. echo get_custom_logo( $blog_id );
  1054. }
  1055. /**
  1056. * Returns document title for the current page.
  1057. *
  1058. * @since 4.4.0
  1059. *
  1060. * @global int $page Page number of a single post.
  1061. * @global int $paged Page number of a list of posts.
  1062. *
  1063. * @return string Tag with the document title.
  1064. */
  1065. function wp_get_document_title() {
  1066. /**
  1067. * Filters the document title before it is generated.
  1068. *
  1069. * Passing a non-empty value will short-circuit wp_get_document_title(),
  1070. * returning that value instead.
  1071. *
  1072. * @since 4.4.0
  1073. *
  1074. * @param string $title The document title. Default empty string.
  1075. */
  1076. $title = apply_filters( 'pre_get_document_title', '' );
  1077. if ( ! empty( $title ) ) {
  1078. return $title;
  1079. }
  1080. global $page, $paged;
  1081. $title = array(
  1082. 'title' => '',
  1083. );
  1084. // If it's a 404 page, use a "Page not found" title.
  1085. if ( is_404() ) {
  1086. $title['title'] = __( 'Page not found' );
  1087. // If it's a search, use a dynamic search results title.
  1088. } elseif ( is_search() ) {
  1089. /* translators: %s: Search query. */
  1090. $title['title'] = sprintf( __( 'Search Results for &#8220;%s&#8221;' ), get_search_query() );
  1091. // If on the front page, use the site title.
  1092. } elseif ( is_front_page() ) {
  1093. $title['title'] = get_bloginfo( 'name', 'display' );
  1094. // If on a post type archive, use the post type archive title.
  1095. } elseif ( is_post_type_archive() ) {
  1096. $title['title'] = post_type_archive_title( '', false );
  1097. // If on a taxonomy archive, use the term title.
  1098. } elseif ( is_tax() ) {
  1099. $title['title'] = single_term_title( '', false );
  1100. /*
  1101. * If we're on the blog page that is not the homepage
  1102. * or a single post of any post type, use the post title.
  1103. */
  1104. } elseif ( is_home() || is_singular() ) {
  1105. $title['title'] = single_post_title( '', false );
  1106. // If on a category or tag archive, use the term title.
  1107. } elseif ( is_category() || is_tag() ) {
  1108. $title['title'] = single_term_title( '', false );
  1109. // If on an author archive, use the author's display name.
  1110. } elseif ( is_author() && get_queried_object() ) {
  1111. $author = get_queried_object();
  1112. $title['title'] = $author->display_name;
  1113. // If it's a date archive, use the date as the title.
  1114. } elseif ( is_year() ) {
  1115. $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
  1116. } elseif ( is_month() ) {
  1117. $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
  1118. } elseif ( is_day() ) {
  1119. $title['title'] = get_the_date();
  1120. }
  1121. // Add a page number if necessary.
  1122. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  1123. /* translators: %s: Page number. */
  1124. $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
  1125. }
  1126. // Append the description or site title to give context.
  1127. if ( is_front_page() ) {
  1128. $title['tagline'] = get_bloginfo( 'description', 'display' );
  1129. } else {
  1130. $title['site'] = get_bloginfo( 'name', 'display' );
  1131. }
  1132. /**
  1133. * Filters the separator for the document title.
  1134. *
  1135. * @since 4.4.0
  1136. *
  1137. * @param string $sep Document title separator. Default '-'.
  1138. */
  1139. $sep = apply_filters( 'document_title_separator', '-' );
  1140. /**
  1141. * Filters the parts of the document title.
  1142. *
  1143. * @since 4.4.0
  1144. *
  1145. * @param array $title {
  1146. * The document title parts.
  1147. *
  1148. * @type string $title Title of the viewed page.
  1149. * @type string $page Optional. Page number if paginated.
  1150. * @type string $tagline Optional. Site description when on home page.
  1151. * @type string $site Optional. Site title when not on home page.
  1152. * }
  1153. */
  1154. $title = apply_filters( 'document_title_parts', $title );
  1155. $title = implode( " $sep ", array_filter( $title ) );
  1156. /**
  1157. * Filters the document title.
  1158. *
  1159. * @since 5.8.0
  1160. *
  1161. * @param string $title Document title.
  1162. */
  1163. $title = apply_filters( 'document_title', $title );
  1164. return $title;
  1165. }
  1166. /**
  1167. * Displays title tag with content.
  1168. *
  1169. * @ignore
  1170. * @since 4.1.0
  1171. * @since 4.4.0 Improved title output replaced `wp_title()`.
  1172. * @access private
  1173. */
  1174. function _wp_render_title_tag() {
  1175. if ( ! current_theme_supports( 'title-tag' ) ) {
  1176. return;
  1177. }
  1178. echo '<title>' . wp_get_document_title() . '</title>' . "\n";
  1179. }
  1180. /**
  1181. * Display or retrieve page title for all areas of blog.
  1182. *
  1183. * By default, the page title will display the separator before the page title,
  1184. * so that the blog title will be before the page title. This is not good for
  1185. * title display, since the blog title shows up on most tabs and not what is
  1186. * important, which is the page that the user is looking at.
  1187. *
  1188. * There are also SEO benefits to having the blog title after or to the 'right'
  1189. * of the page title. However, it is mostly common sense to have the blog title
  1190. * to the right with most browsers supporting tabs. You can achieve this by
  1191. * using the seplocation parameter and setting the value to 'right'. This change
  1192. * was introduced around 2.5.0, in case backward compatibility of themes is
  1193. * important.
  1194. *
  1195. * @since 1.0.0
  1196. *
  1197. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1198. *
  1199. * @param string $sep Optional. How to separate the various items within the page title.
  1200. * Default '&raquo;'.
  1201. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1202. * @param string $seplocation Optional. Location of the separator ('left' or 'right').
  1203. * @return string|void String when `$display` is true, nothing otherwise.
  1204. */
  1205. function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
  1206. global $wp_locale;
  1207. $m = get_query_var( 'm' );
  1208. $year = get_query_var( 'year' );
  1209. $monthnum = get_query_var( 'monthnum' );
  1210. $day = get_query_var( 'day' );
  1211. $search = get_query_var( 's' );
  1212. $title = '';
  1213. $t_sep = '%WP_TITLE_SEP%'; // Temporary separator, for accurate flipping, if necessary.
  1214. // If there is a post.
  1215. if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
  1216. $title = single_post_title( '', false );
  1217. }
  1218. // If there's a post type archive.
  1219. if ( is_post_type_archive() ) {
  1220. $post_type = get_query_var( 'post_type' );
  1221. if ( is_array( $post_type ) ) {
  1222. $post_type = reset( $post_type );
  1223. }
  1224. $post_type_object = get_post_type_object( $post_type );
  1225. if ( ! $post_type_object->has_archive ) {
  1226. $title = post_type_archive_title( '', false );
  1227. }
  1228. }
  1229. // If there's a category or tag.
  1230. if ( is_category() || is_tag() ) {
  1231. $title = single_term_title( '', false );
  1232. }
  1233. // If there's a taxonomy.
  1234. if ( is_tax() ) {
  1235. $term = get_queried_object();
  1236. if ( $term ) {
  1237. $tax = get_taxonomy( $term->taxonomy );
  1238. $title = single_term_title( $tax->labels->name . $t_sep, false );
  1239. }
  1240. }
  1241. // If there's an author.
  1242. if ( is_author() && ! is_post_type_archive() ) {
  1243. $author = get_queried_object();
  1244. if ( $author ) {
  1245. $title = $author->display_name;
  1246. }
  1247. }
  1248. // Post type archives with has_archive should override terms.
  1249. if ( is_post_type_archive() && $post_type_object->has_archive ) {
  1250. $title = post_type_archive_title( '', false );
  1251. }
  1252. // If there's a month.
  1253. if ( is_archive() && ! empty( $m ) ) {
  1254. $my_year = substr( $m, 0, 4 );
  1255. $my_month = substr( $m, 4, 2 );
  1256. $my_day = (int) substr( $m, 6, 2 );
  1257. $title = $my_year .
  1258. ( $my_month ? $t_sep . $wp_locale->get_month( $my_month ) : '' ) .
  1259. ( $my_day ? $t_sep . $my_day : '' );
  1260. }
  1261. // If there's a year.
  1262. if ( is_archive() && ! empty( $year ) ) {
  1263. $title = $year;
  1264. if ( ! empty( $monthnum ) ) {
  1265. $title .= $t_sep . $wp_locale->get_month( $monthnum );
  1266. }
  1267. if ( ! empty( $day ) ) {
  1268. $title .= $t_sep . zeroise( $day, 2 );
  1269. }
  1270. }
  1271. // If it's a search.
  1272. if ( is_search() ) {
  1273. /* translators: 1: Separator, 2: Search query. */
  1274. $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
  1275. }
  1276. // If it's a 404 page.
  1277. if ( is_404() ) {
  1278. $title = __( 'Page not found' );
  1279. }
  1280. $prefix = '';
  1281. if ( ! empty( $title ) ) {
  1282. $prefix = " $sep ";
  1283. }
  1284. /**
  1285. * Filters the parts of the page title.
  1286. *
  1287. * @since 4.0.0
  1288. *
  1289. * @param string[] $title_array Array of parts of the page title.
  1290. */
  1291. $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
  1292. // Determines position of the separator and direction of the breadcrumb.
  1293. if ( 'right' === $seplocation ) { // Separator on right, so reverse the order.
  1294. $title_array = array_reverse( $title_array );
  1295. $title = implode( " $sep ", $title_array ) . $prefix;
  1296. } else {
  1297. $title = $prefix . implode( " $sep ", $title_array );
  1298. }
  1299. /**
  1300. * Filters the text of the page title.
  1301. *
  1302. * @since 2.0.0
  1303. *
  1304. * @param string $title Page title.
  1305. * @param string $sep Title separator.
  1306. * @param string $seplocation Location of the separator ('left' or 'right').
  1307. */
  1308. $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
  1309. // Send it out.
  1310. if ( $display ) {
  1311. echo $title;
  1312. } else {
  1313. return $title;
  1314. }
  1315. }
  1316. /**
  1317. * Display or retrieve page title for post.
  1318. *
  1319. * This is optimized for single.php template file for displaying the post title.
  1320. *
  1321. * It does not support placing the separator after the title, but by leaving the
  1322. * prefix parameter empty, you can set the title separator manually. The prefix
  1323. * does not automatically place a space between the prefix, so if there should
  1324. * be a space, the parameter value will need to have it at the end.
  1325. *
  1326. * @since 0.71
  1327. *
  1328. * @param string $prefix Optional. What to display before the title.
  1329. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1330. * @return string|void Title when retrieving.
  1331. */
  1332. function single_post_title( $prefix = '', $display = true ) {
  1333. $_post = get_queried_object();
  1334. if ( ! isset( $_post->post_title ) ) {
  1335. return;
  1336. }
  1337. /**
  1338. * Filters the page title for a single post.
  1339. *
  1340. * @since 0.71
  1341. *
  1342. * @param string $_post_title The single post page title.
  1343. * @param WP_Post $_post The current post.
  1344. */
  1345. $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
  1346. if ( $display ) {
  1347. echo $prefix . $title;
  1348. } else {
  1349. return $prefix . $title;
  1350. }
  1351. }
  1352. /**
  1353. * Display or retrieve title for a post type archive.
  1354. *
  1355. * This is optimized for archive.php and archive-{$post_type}.php template files
  1356. * for displaying the title of the post type.
  1357. *
  1358. * @since 3.1.0
  1359. *
  1360. * @param string $prefix Optional. What to display before the title.
  1361. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1362. * @return string|void Title when retrieving, null when displaying or failure.
  1363. */
  1364. function post_type_archive_title( $prefix = '', $display = true ) {
  1365. if ( ! is_post_type_archive() ) {
  1366. return;
  1367. }
  1368. $post_type = get_query_var( 'post_type' );
  1369. if ( is_array( $post_type ) ) {
  1370. $post_type = reset( $post_type );
  1371. }
  1372. $post_type_obj = get_post_type_object( $post_type );
  1373. /**
  1374. * Filters the post type archive title.
  1375. *
  1376. * @since 3.1.0
  1377. *
  1378. * @param string $post_type_name Post type 'name' label.
  1379. * @param string $post_type Post type.
  1380. */
  1381. $title = apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );
  1382. if ( $display ) {
  1383. echo $prefix . $title;
  1384. } else {
  1385. return $prefix . $title;
  1386. }
  1387. }
  1388. /**
  1389. * Display or retrieve page title for category archive.
  1390. *
  1391. * Useful for category template files for displaying the category page title.
  1392. * The prefix does not automatically place a space between the prefix, so if
  1393. * there should be a space, the parameter value will need to have it at the end.
  1394. *
  1395. * @since 0.71
  1396. *
  1397. * @param string $prefix Optional. What to display before the title.
  1398. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1399. * @return string|void Title when retrieving.
  1400. */
  1401. function single_cat_title( $prefix = '', $display = true ) {
  1402. return single_term_title( $prefix, $display );
  1403. }
  1404. /**
  1405. * Display or retrieve page title for tag post archive.
  1406. *
  1407. * Useful for tag template files for displaying the tag page title. The prefix
  1408. * does not automatically place a space between the prefix, so if there should
  1409. * be a space, the parameter value will need to have it at the end.
  1410. *
  1411. * @since 2.3.0
  1412. *
  1413. * @param string $prefix Optional. What to display before the title.
  1414. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1415. * @return string|void Title when retrieving.
  1416. */
  1417. function single_tag_title( $prefix = '', $display = true ) {
  1418. return single_term_title( $prefix, $display );
  1419. }
  1420. /**
  1421. * Display or retrieve page title for taxonomy term archive.
  1422. *
  1423. * Useful for taxonomy term template files for displaying the taxonomy term page title.
  1424. * The prefix does not automatically place a space between the prefix, so if there should
  1425. * be a space, the parameter value will need to have it at the end.
  1426. *
  1427. * @since 3.1.0
  1428. *
  1429. * @param string $prefix Optional. What to display before the title.
  1430. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1431. * @return string|void Title when retrieving.
  1432. */
  1433. function single_term_title( $prefix = '', $display = true ) {
  1434. $term = get_queried_object();
  1435. if ( ! $term ) {
  1436. return;
  1437. }
  1438. if ( is_category() ) {
  1439. /**
  1440. * Filters the category archive page title.
  1441. *
  1442. * @since 2.0.10
  1443. *
  1444. * @param string $term_name Category name for archive being displayed.
  1445. */
  1446. $term_name = apply_filters( 'single_cat_title', $term->name );
  1447. } elseif ( is_tag() ) {
  1448. /**
  1449. * Filters the tag archive page title.
  1450. *
  1451. * @since 2.3.0
  1452. *
  1453. * @param string $term_name Tag name for archive being displayed.
  1454. */
  1455. $term_name = apply_filters( 'single_tag_title', $term->name );
  1456. } elseif ( is_tax() ) {
  1457. /**
  1458. * Filters the custom taxonomy archive page title.
  1459. *
  1460. * @since 3.1.0
  1461. *
  1462. * @param string $term_name Term name for archive being displayed.
  1463. */
  1464. $term_name = apply_filters( 'single_term_title', $term->name );
  1465. } else {
  1466. return;
  1467. }
  1468. if ( empty( $term_name ) ) {
  1469. return;
  1470. }
  1471. if ( $display ) {
  1472. echo $prefix . $term_name;
  1473. } else {
  1474. return $prefix . $term_name;
  1475. }
  1476. }
  1477. /**
  1478. * Display or retrieve page title for post archive based on date.
  1479. *
  1480. * Useful for when the template only needs to display the month and year,
  1481. * if either are available. The prefix does not automatically place a space
  1482. * between the prefix, so if there should be a space, the parameter value
  1483. * will need to have it at the end.
  1484. *
  1485. * @since 0.71
  1486. *
  1487. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1488. *
  1489. * @param string $prefix Optional. What to display before the title.
  1490. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1491. * @return string|void Title when retrieving.
  1492. */
  1493. function single_month_title( $prefix = '', $display = true ) {
  1494. global $wp_locale;
  1495. $m = get_query_var( 'm' );
  1496. $year = get_query_var( 'year' );
  1497. $monthnum = get_query_var( 'monthnum' );
  1498. if ( ! empty( $monthnum ) && ! empty( $year ) ) {
  1499. $my_year = $year;
  1500. $my_month = $wp_locale->get_month( $monthnum );
  1501. } elseif ( ! empty( $m ) ) {
  1502. $my_year = substr( $m, 0, 4 );
  1503. $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
  1504. }
  1505. if ( empty( $my_month ) ) {
  1506. return false;
  1507. }
  1508. $result = $prefix . $my_month . $prefix . $my_year;
  1509. if ( ! $display ) {
  1510. return $result;
  1511. }
  1512. echo $result;
  1513. }
  1514. /**
  1515. * Display the archive title based on the queried object.
  1516. *
  1517. * @since 4.1.0
  1518. *
  1519. * @see get_the_archive_title()
  1520. *
  1521. * @param string $before Optional. Content to prepend to the title. Default empty.
  1522. * @param string $after Optional. Content to append to the title. Default empty.
  1523. */
  1524. function the_archive_title( $before = '', $after = '' ) {
  1525. $title = get_the_archive_title();
  1526. if ( ! empty( $title ) ) {
  1527. echo $before . $title . $after;
  1528. }
  1529. }
  1530. /**
  1531. * Retrieve the archive title based on the queried object.
  1532. *
  1533. * @since 4.1.0
  1534. * @since 5.5.0 The title part is wrapped in a `<span>` element.
  1535. *
  1536. * @return string Archive title.
  1537. */
  1538. function get_the_archive_title() {
  1539. $title = __( 'Archives' );
  1540. $prefix = '';
  1541. if ( is_category() ) {
  1542. $title = single_cat_title( '', false );
  1543. $prefix = _x( 'Category:', 'category archive title prefix' );
  1544. } elseif ( is_tag() ) {
  1545. $title = single_tag_title( '', false );
  1546. $prefix = _x( 'Tag:', 'tag archive title prefix' );
  1547. } elseif ( is_author() ) {
  1548. $title = get_the_author();
  1549. $prefix = _x( 'Author:', 'author archive title prefix' );
  1550. } elseif ( is_year() ) {
  1551. $title = get_the_date( _x( 'Y', 'yearly archives date format' ) );
  1552. $prefix = _x( 'Year:', 'date archive title prefix' );
  1553. } elseif ( is_month() ) {
  1554. $title = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
  1555. $prefix = _x( 'Month:', 'date archive title prefix' );
  1556. } elseif ( is_day() ) {
  1557. $title = get_the_date( _x( 'F j, Y', 'daily archives date format' ) );
  1558. $prefix = _x( 'Day:', 'date archive title prefix' );
  1559. } elseif ( is_tax( 'post_format' ) ) {
  1560. if ( is_tax( 'post_format', 'post-format-aside' ) ) {
  1561. $title = _x( 'Asides', 'post format archive title' );
  1562. } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
  1563. $title = _x( 'Galleries', 'post format archive title' );
  1564. } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
  1565. $title = _x( 'Images', 'post format archive title' );
  1566. } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
  1567. $title = _x( 'Videos', 'post format archive title' );
  1568. } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
  1569. $title = _x( 'Quotes', 'post format archive title' );
  1570. } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
  1571. $title = _x( 'Links', 'post format archive title' );
  1572. } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
  1573. $title = _x( 'Statuses', 'post format archive title' );
  1574. } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
  1575. $title = _x( 'Audio', 'post format archive title' );
  1576. } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
  1577. $title = _x( 'Chats', 'post format archive title' );
  1578. }
  1579. } elseif ( is_post_type_archive() ) {
  1580. $title = post_type_archive_title( '', false );
  1581. $prefix = _x( 'Archives:', 'post type archive title prefix' );
  1582. } elseif ( is_tax() ) {
  1583. $queried_object = get_queried_object();
  1584. if ( $queried_object ) {
  1585. $tax = get_taxonomy( $queried_object->taxonomy );
  1586. $title = single_term_title( '', false );
  1587. $prefix = sprintf(
  1588. /* translators: %s: Taxonomy singular name. */
  1589. _x( '%s:', 'taxonomy term archive title prefix' ),
  1590. $tax->labels->singular_name
  1591. );
  1592. }
  1593. }
  1594. $original_title = $title;
  1595. /**
  1596. * Filters the archive title prefix.
  1597. *
  1598. * @since 5.5.0
  1599. *
  1600. * @param string $prefix Archive title prefix.
  1601. */
  1602. $prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
  1603. if ( $prefix ) {
  1604. $title = sprintf(
  1605. /* translators: 1: Title prefix. 2: Title. */
  1606. _x( '%1$s %2$s', 'archive title' ),
  1607. $prefix,
  1608. '<span>' . $title . '</span>'
  1609. );
  1610. }
  1611. /**
  1612. * Filters the archive title.
  1613. *
  1614. * @since 4.1.0
  1615. * @since 5.5.0 Added the `$prefix` and `$original_title` parameters.
  1616. *
  1617. * @param string $title Archive title to be displayed.
  1618. * @param string $original_title Archive title without prefix.
  1619. * @param string $prefix Archive title prefix.
  1620. */
  1621. return apply_filters( 'get_the_archive_title', $title, $original_title, $prefix );
  1622. }
  1623. /**
  1624. * Display category, tag, term, or author description.
  1625. *
  1626. * @since 4.1.0
  1627. *
  1628. * @see get_the_archive_description()
  1629. *
  1630. * @param string $before Optional. Content to prepend to the description. Default empty.
  1631. * @param string $after Optional. Content to append to the description. Default empty.
  1632. */
  1633. function the_archive_description( $before = '', $after = '' ) {
  1634. $description = get_the_archive_description();
  1635. if ( $description ) {
  1636. echo $before . $description . $after;
  1637. }
  1638. }
  1639. /**
  1640. * Retrieves the description for an author, post type, or term archive.
  1641. *
  1642. * @since 4.1.0
  1643. * @since 4.7.0 Added support for author archives.
  1644. * @since 4.9.0 Added support for post type archives.
  1645. *
  1646. * @see term_description()
  1647. *
  1648. * @return string Archive description.
  1649. */
  1650. function get_the_archive_description() {
  1651. if ( is_author() ) {
  1652. $description = get_the_author_meta( 'description' );
  1653. } elseif ( is_post_type_archive() ) {
  1654. $description = get_the_post_type_description();
  1655. } else {
  1656. $description = term_description();
  1657. }
  1658. /**
  1659. * Filters the archive description.
  1660. *
  1661. * @since 4.1.0
  1662. *
  1663. * @param string $description Archive description to be displayed.
  1664. */
  1665. return apply_filters( 'get_the_archive_description', $description );
  1666. }
  1667. /**
  1668. * Retrieves the description for a post type archive.
  1669. *
  1670. * @since 4.9.0
  1671. *
  1672. * @return string The post type description.
  1673. */
  1674. function get_the_post_type_description() {
  1675. $post_type = get_query_var( 'post_type' );
  1676. if ( is_array( $post_type ) ) {
  1677. $post_type = reset( $post_type );
  1678. }
  1679. $post_type_obj = get_post_type_object( $post_type );
  1680. // Check if a description is set.
  1681. if ( isset( $post_type_obj->description ) ) {
  1682. $description = $post_type_obj->description;
  1683. } else {
  1684. $description = '';
  1685. }
  1686. /**
  1687. * Filters the description for a post type archive.
  1688. *
  1689. * @since 4.9.0
  1690. *
  1691. * @param string $description The post type description.
  1692. * @param WP_Post_Type $post_type_obj The post type object.
  1693. */
  1694. return apply_filters( 'get_the_post_type_description', $description, $post_type_obj );
  1695. }
  1696. /**
  1697. * Retrieve archive link content based on predefined or custom code.
  1698. *
  1699. * The format can be one of four styles. The 'link' for head element, 'option'
  1700. * for use in the select element, 'html' for use in list (either ol or ul HTML
  1701. * elements). Custom content is also supported using the before and after
  1702. * parameters.
  1703. *
  1704. * The 'link' format uses the `<link>` HTML element with the **archives**
  1705. * relationship. The before and after parameters are not used. The text
  1706. * parameter is used to describe the link.
  1707. *
  1708. * The 'option' format uses the option HTML element for use in select element.
  1709. * The value is the url parameter and the before and after parameters are used
  1710. * between the text description.
  1711. *
  1712. * The 'html' format, which is the default, uses the li HTML element for use in
  1713. * the list HTML elements. The before parameter is before the link and the after
  1714. * parameter is after the closing link.
  1715. *
  1716. * The custom format uses the before parameter before the link ('a' HTML
  1717. * element) and the after parameter after the closing link tag. If the above
  1718. * three values for the format are not used, then custom format is assumed.
  1719. *
  1720. * @since 1.0.0
  1721. * @since 5.2.0 Added the `$selected` parameter.
  1722. *
  1723. * @param string $url URL to archive.
  1724. * @param string $text Archive text description.
  1725. * @param string $format Optional. Can be 'link', 'option', 'html', or custom. Default 'html'.
  1726. * @param string $before Optional. Content to prepend to the description. Default empty.
  1727. * @param string $after Optional. Content to append to the description. Default empty.
  1728. * @param bool $selected Optional. Set to true if the current page is the selected archive page.
  1729. * @return string HTML link content for archive.
  1730. */
  1731. function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
  1732. $text = wptexturize( $text );
  1733. $url = esc_url( $url );
  1734. $aria_current = $selected ? ' aria-current="page"' : '';
  1735. if ( 'link' === $format ) {
  1736. $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
  1737. } elseif ( 'option' === $format ) {
  1738. $selected_attr = $selected ? " selected='selected'" : '';
  1739. $link_html = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
  1740. } elseif ( 'html' === $format ) {
  1741. $link_html = "\t<li>$before<a href='$url'$aria_current>$text</a>$after</li>\n";
  1742. } else { // Custom.
  1743. $link_html = "\t$before<a href='$url'$aria_current>$text</a>$after\n";
  1744. }
  1745. /**
  1746. * Filters the archive link content.
  1747. *
  1748. * @since 2.6.0
  1749. * @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters.
  1750. * @since 5.2.0 Added the `$selected` parameter.
  1751. *
  1752. * @param string $link_html The archive HTML link content.
  1753. * @param string $url URL to archive.
  1754. * @param string $text Archive text description.
  1755. * @param string $format Link format. Can be 'link', 'option', 'html', or custom.
  1756. * @param string $before Content to prepend to the description.
  1757. * @param string $after Content to append to the description.
  1758. * @param bool $selected True if the current page is the selected archive.
  1759. */
  1760. return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
  1761. }
  1762. /**
  1763. * Display archive links based on type and format.
  1764. *
  1765. * @since 1.2.0
  1766. * @since 4.4.0 The `$post_type` argument was added.
  1767. * @since 5.2.0 The `$year`, `$monthnum`, `$day`, and `$w` arguments were added.
  1768. *
  1769. * @see get_archives_link()
  1770. *
  1771. * @global wpdb $wpdb WordPress database abstraction object.
  1772. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1773. *
  1774. * @param string|array $args {
  1775. * Default archive links arguments. Optional.
  1776. *
  1777. * @type string $type Type of archive to retrieve. Accepts 'daily', 'weekly', 'monthly',
  1778. * 'yearly', 'postbypost', or 'alpha'. Both 'postbypost' and 'alpha'
  1779. * display the same archive link list as well as post titles instead
  1780. * of displaying dates. The difference between the two is that 'alpha'
  1781. * will order by post title and 'postbypost' will order by post date.
  1782. * Default 'monthly'.
  1783. * @type string|int $limit Number of links to limit the query to. Default empty (no limit).
  1784. * @type string $format Format each link should take using the $before and $after args.
  1785. * Accepts 'link' (`<link>` tag), 'option' (`<option>` tag), 'html'
  1786. * (`<li>` tag), or a custom format, which generates a link anchor
  1787. * with $before preceding and $after succeeding. Default 'html'.
  1788. * @type string $before Markup to prepend to the beginning of each link. Default empty.
  1789. * @type string $after Markup to append to the end of each link. Default empty.
  1790. * @type bool $show_post_count Whether to display the post count alongside the link. Default false.
  1791. * @type bool|int $echo Whether to echo or return the links list. Default 1|true to echo.
  1792. * @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.
  1793. * Default 'DESC'.
  1794. * @type string $post_type Post type. Default 'post'.
  1795. * @type string $year Year. Default current year.
  1796. * @type string $monthnum Month number. Default current month number.
  1797. * @type string $day Day. Default current day.
  1798. * @type string $w Week. Default current week.
  1799. * }
  1800. * @return void|string Void if 'echo' argument is true, archive links if 'echo' is false.
  1801. */
  1802. function wp_get_archives( $args = '' ) {
  1803. global $wpdb, $wp_locale;
  1804. $defaults = array(
  1805. 'type' => 'monthly',
  1806. 'limit' => '',
  1807. 'format' => 'html',
  1808. 'before' => '',
  1809. 'after' => '',
  1810. 'show_post_count' => false,
  1811. 'echo' => 1,
  1812. 'order' => 'DESC',
  1813. 'post_type' => 'post',
  1814. 'year' => get_query_var( 'year' ),
  1815. 'monthnum' => get_query_var( 'monthnum' ),
  1816. 'day' => get_query_var( 'day' ),
  1817. 'w' => get_query_var( 'w' ),
  1818. );
  1819. $parsed_args = wp_parse_args( $args, $defaults );
  1820. $post_type_object = get_post_type_object( $parsed_args['post_type'] );
  1821. if ( ! is_post_type_viewable( $post_type_object ) ) {
  1822. return;
  1823. }
  1824. $parsed_args['post_type'] = $post_type_object->name;
  1825. if ( '' === $parsed_args['type'] ) {
  1826. $parsed_args['type'] = 'monthly';
  1827. }
  1828. if ( ! empty( $parsed_args['limit'] ) ) {
  1829. $parsed_args['limit'] = absint( $parsed_args['limit'] );
  1830. $parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit'];
  1831. }
  1832. $order = strtoupper( $parsed_args['order'] );
  1833. if ( 'ASC' !== $order ) {
  1834. $order = 'DESC';
  1835. }
  1836. // This is what will separate dates on weekly archive links.
  1837. $archive_week_separator = '&#8211;';
  1838. $sql_where = $wpdb->prepare( "WHERE post_type = %s AND post_status = 'publish'", $parsed_args['post_type'] );
  1839. /**
  1840. * Filters the SQL WHERE clause for retrieving archives.
  1841. *
  1842. * @since 2.2.0
  1843. *
  1844. * @param string $sql_where Portion of SQL query containing the WHERE clause.
  1845. * @param array $parsed_args An array of default arguments.
  1846. */
  1847. $where = apply_filters( 'getarchives_where', $sql_where, $parsed_args );
  1848. /**
  1849. * Filters the SQL JOIN clause for retrieving archives.
  1850. *
  1851. * @since 2.2.0
  1852. *
  1853. * @param string $sql_join Portion of SQL query containing JOIN clause.
  1854. * @param array $parsed_args An array of default arguments.
  1855. */
  1856. $join = apply_filters( 'getarchives_join', '', $parsed_args );
  1857. $output = '';
  1858. $last_changed = wp_cache_get_last_changed( 'posts' );
  1859. $limit = $parsed_args['limit'];
  1860. if ( 'monthly' === $parsed_args['type'] ) {
  1861. $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";
  1862. $key = md5( $query );
  1863. $key = "wp_get_archives:$key:$last_changed";
  1864. $results = wp_cache_get( $key, 'posts' );
  1865. if ( ! $results ) {
  1866. $results = $wpdb->get_results( $query );
  1867. wp_cache_set( $key, $results, 'posts' );
  1868. }
  1869. if ( $results ) {
  1870. $after = $parsed_args['after'];
  1871. foreach ( (array) $results as $result ) {
  1872. $url = get_month_link( $result->year, $result->month );
  1873. if ( 'post' !== $parsed_args['post_type'] ) {
  1874. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1875. }
  1876. /* translators: 1: Month name, 2: 4-digit year. */
  1877. $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
  1878. if ( $parsed_args['show_post_count'] ) {
  1879. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1880. }
  1881. $selected = is_archive() && (string) $parsed_args['year'] === $result->year && (string) $parsed_args['monthnum'] === $result->month;
  1882. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1883. }
  1884. }
  1885. } elseif ( 'yearly' === $parsed_args['type'] ) {
  1886. $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";
  1887. $key = md5( $query );
  1888. $key = "wp_get_archives:$key:$last_changed";
  1889. $results = wp_cache_get( $key, 'posts' );
  1890. if ( ! $results ) {
  1891. $results = $wpdb->get_results( $query );
  1892. wp_cache_set( $key, $results, 'posts' );
  1893. }
  1894. if ( $results ) {
  1895. $after = $parsed_args['after'];
  1896. foreach ( (array) $results as $result ) {
  1897. $url = get_year_link( $result->year );
  1898. if ( 'post' !== $parsed_args['post_type'] ) {
  1899. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1900. }
  1901. $text = sprintf( '%d', $result->year );
  1902. if ( $parsed_args['show_post_count'] ) {
  1903. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1904. }
  1905. $selected = is_archive() && (string) $parsed_args['year'] === $result->year;
  1906. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1907. }
  1908. }
  1909. } elseif ( 'daily' === $parsed_args['type'] ) {
  1910. $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";
  1911. $key = md5( $query );
  1912. $key = "wp_get_archives:$key:$last_changed";
  1913. $results = wp_cache_get( $key, 'posts' );
  1914. if ( ! $results ) {
  1915. $results = $wpdb->get_results( $query );
  1916. wp_cache_set( $key, $results, 'posts' );
  1917. }
  1918. if ( $results ) {
  1919. $after = $parsed_args['after'];
  1920. foreach ( (array) $results as $result ) {
  1921. $url = get_day_link( $result->year, $result->month, $result->dayofmonth );
  1922. if ( 'post' !== $parsed_args['post_type'] ) {
  1923. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1924. }
  1925. $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
  1926. $text = mysql2date( get_option( 'date_format' ), $date );
  1927. if ( $parsed_args['show_post_count'] ) {
  1928. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1929. }
  1930. $selected = is_archive() && (string) $parsed_args['year'] === $result->year && (string) $parsed_args['monthnum'] === $result->month && (string) $parsed_args['day'] === $result->dayofmonth;
  1931. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1932. }
  1933. }
  1934. } elseif ( 'weekly' === $parsed_args['type'] ) {
  1935. $week = _wp_mysql_week( '`post_date`' );
  1936. $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";
  1937. $key = md5( $query );
  1938. $key = "wp_get_archives:$key:$last_changed";
  1939. $results = wp_cache_get( $key, 'posts' );
  1940. if ( ! $results ) {
  1941. $results = $wpdb->get_results( $query );
  1942. wp_cache_set( $key, $results, 'posts' );
  1943. }
  1944. $arc_w_last = '';
  1945. if ( $results ) {
  1946. $after = $parsed_args['after'];
  1947. foreach ( (array) $results as $result ) {
  1948. if ( $result->week != $arc_w_last ) {
  1949. $arc_year = $result->yr;
  1950. $arc_w_last = $result->week;
  1951. $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
  1952. $arc_week_start = date_i18n( get_option( 'date_format' ), $arc_week['start'] );
  1953. $arc_week_end = date_i18n( get_option( 'date_format' ), $arc_week['end'] );
  1954. $url = add_query_arg(
  1955. array(
  1956. 'm' => $arc_year,
  1957. 'w' => $result->week,
  1958. ),
  1959. home_url( '/' )
  1960. );
  1961. if ( 'post' !== $parsed_args['post_type'] ) {
  1962. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1963. }
  1964. $text = $arc_week_start . $archive_week_separator . $arc_week_end;
  1965. if ( $parsed_args['show_post_count'] ) {
  1966. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1967. }
  1968. $selected = is_archive() && (string) $parsed_args['year'] === $result->yr && (string) $parsed_args['w'] === $result->week;
  1969. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1970. }
  1971. }
  1972. }
  1973. } elseif ( ( 'postbypost' === $parsed_args['type'] ) || ( 'alpha' === $parsed_args['type'] ) ) {
  1974. $orderby = ( 'alpha' === $parsed_args['type'] ) ? 'post_title ASC ' : 'post_date DESC, ID DESC ';
  1975. $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
  1976. $key = md5( $query );
  1977. $key = "wp_get_archives:$key:$last_changed";
  1978. $results = wp_cache_get( $key, 'posts' );
  1979. if ( ! $results ) {
  1980. $results = $wpdb->get_results( $query );
  1981. wp_cache_set( $key, $results, 'posts' );
  1982. }
  1983. if ( $results ) {
  1984. foreach ( (array) $results as $result ) {
  1985. if ( '0000-00-00 00:00:00' !== $result->post_date ) {
  1986. $url = get_permalink( $result );
  1987. if ( $result->post_title ) {
  1988. /** This filter is documented in wp-includes/post-template.php */
  1989. $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
  1990. } else {
  1991. $text = $result->ID;
  1992. }
  1993. $selected = get_the_ID() === $result->ID;
  1994. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1995. }
  1996. }
  1997. }
  1998. }
  1999. if ( $parsed_args['echo'] ) {
  2000. echo $output;
  2001. } else {
  2002. return $output;
  2003. }
  2004. }
  2005. /**
  2006. * Get number of days since the start of the week.
  2007. *
  2008. * @since 1.5.0
  2009. *
  2010. * @param int $num Number of day.
  2011. * @return float Days since the start of the week.
  2012. */
  2013. function calendar_week_mod( $num ) {
  2014. $base = 7;
  2015. return ( $num - $base * floor( $num / $base ) );
  2016. }
  2017. /**
  2018. * Display calendar with days that have posts as links.
  2019. *
  2020. * The calendar is cached, which will be retrieved, if it exists. If there are
  2021. * no posts for the month, then it will not be displayed.
  2022. *
  2023. * @since 1.0.0
  2024. *
  2025. * @global wpdb $wpdb WordPress database abstraction object.
  2026. * @global int $m
  2027. * @global int $monthnum
  2028. * @global int $year
  2029. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2030. * @global array $posts
  2031. *
  2032. * @param bool $initial Optional. Whether to use initial calendar names. Default true.
  2033. * @param bool $echo Optional. Whether to display the calendar output. Default true.
  2034. * @return void|string Void if `$echo` argument is true, calendar HTML if `$echo` is false.
  2035. */
  2036. function get_calendar( $initial = true, $echo = true ) {
  2037. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  2038. $key = md5( $m . $monthnum . $year );
  2039. $cache = wp_cache_get( 'get_calendar', 'calendar' );
  2040. if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
  2041. /** This filter is documented in wp-includes/general-template.php */
  2042. $output = apply_filters( 'get_calendar', $cache[ $key ] );
  2043. if ( $echo ) {
  2044. echo $output;
  2045. return;
  2046. }
  2047. return $output;
  2048. }
  2049. if ( ! is_array( $cache ) ) {
  2050. $cache = array();
  2051. }
  2052. // Quick check. If we have no posts at all, abort!
  2053. if ( ! $posts ) {
  2054. $gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
  2055. if ( ! $gotsome ) {
  2056. $cache[ $key ] = '';
  2057. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  2058. return;
  2059. }
  2060. }
  2061. if ( isset( $_GET['w'] ) ) {
  2062. $w = (int) $_GET['w'];
  2063. }
  2064. // week_begins = 0 stands for Sunday.
  2065. $week_begins = (int) get_option( 'start_of_week' );
  2066. // Let's figure out when we are.
  2067. if ( ! empty( $monthnum ) && ! empty( $year ) ) {
  2068. $thismonth = zeroise( (int) $monthnum, 2 );
  2069. $thisyear = (int) $year;
  2070. } elseif ( ! empty( $w ) ) {
  2071. // We need to get the month from MySQL.
  2072. $thisyear = (int) substr( $m, 0, 4 );
  2073. // It seems MySQL's weeks disagree with PHP's.
  2074. $d = ( ( $w - 1 ) * 7 ) + 6;
  2075. $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" );
  2076. } elseif ( ! empty( $m ) ) {
  2077. $thisyear = (int) substr( $m, 0, 4 );
  2078. if ( strlen( $m ) < 6 ) {
  2079. $thismonth = '01';
  2080. } else {
  2081. $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
  2082. }
  2083. } else {
  2084. $thisyear = current_time( 'Y' );
  2085. $thismonth = current_time( 'm' );
  2086. }
  2087. $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear );
  2088. $last_day = gmdate( 't', $unixmonth );
  2089. // Get the next and previous month and year with at least one post.
  2090. $previous = $wpdb->get_row(
  2091. "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
  2092. FROM $wpdb->posts
  2093. WHERE post_date < '$thisyear-$thismonth-01'
  2094. AND post_type = 'post' AND post_status = 'publish'
  2095. ORDER BY post_date DESC
  2096. LIMIT 1"
  2097. );
  2098. $next = $wpdb->get_row(
  2099. "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
  2100. FROM $wpdb->posts
  2101. WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
  2102. AND post_type = 'post' AND post_status = 'publish'
  2103. ORDER BY post_date ASC
  2104. LIMIT 1"
  2105. );
  2106. /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */
  2107. $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
  2108. $calendar_output = '<table id="wp-calendar" class="wp-calendar-table">
  2109. <caption>' . sprintf(
  2110. $calendar_caption,
  2111. $wp_locale->get_month( $thismonth ),
  2112. gmdate( 'Y', $unixmonth )
  2113. ) . '</caption>
  2114. <thead>
  2115. <tr>';
  2116. $myweek = array();
  2117. for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
  2118. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
  2119. }
  2120. foreach ( $myweek as $wd ) {
  2121. $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
  2122. $wd = esc_attr( $wd );
  2123. $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
  2124. }
  2125. $calendar_output .= '
  2126. </tr>
  2127. </thead>
  2128. <tbody>
  2129. <tr>';
  2130. $daywithpost = array();
  2131. // Get days with posts.
  2132. $dayswithposts = $wpdb->get_results(
  2133. "SELECT DISTINCT DAYOFMONTH(post_date)
  2134. FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
  2135. AND post_type = 'post' AND post_status = 'publish'
  2136. AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'",
  2137. ARRAY_N
  2138. );
  2139. if ( $dayswithposts ) {
  2140. foreach ( (array) $dayswithposts as $daywith ) {
  2141. $daywithpost[] = (int) $daywith[0];
  2142. }
  2143. }
  2144. // See how much we should pad in the beginning.
  2145. $pad = calendar_week_mod( gmdate( 'w', $unixmonth ) - $week_begins );
  2146. if ( 0 != $pad ) {
  2147. $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad">&nbsp;</td>';
  2148. }
  2149. $newrow = false;
  2150. $daysinmonth = (int) gmdate( 't', $unixmonth );
  2151. for ( $day = 1; $day <= $daysinmonth; ++$day ) {
  2152. if ( isset( $newrow ) && $newrow ) {
  2153. $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
  2154. }
  2155. $newrow = false;
  2156. if ( current_time( 'j' ) == $day &&
  2157. current_time( 'm' ) == $thismonth &&
  2158. current_time( 'Y' ) == $thisyear ) {
  2159. $calendar_output .= '<td id="today">';
  2160. } else {
  2161. $calendar_output .= '<td>';
  2162. }
  2163. if ( in_array( $day, $daywithpost, true ) ) {
  2164. // Any posts today?
  2165. $date_format = gmdate( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
  2166. /* translators: Post calendar label. %s: Date. */
  2167. $label = sprintf( __( 'Posts published on %s' ), $date_format );
  2168. $calendar_output .= sprintf(
  2169. '<a href="%s" aria-label="%s">%s</a>',
  2170. get_day_link( $thisyear, $thismonth, $day ),
  2171. esc_attr( $label ),
  2172. $day
  2173. );
  2174. } else {
  2175. $calendar_output .= $day;
  2176. }
  2177. $calendar_output .= '</td>';
  2178. if ( 6 == calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
  2179. $newrow = true;
  2180. }
  2181. }
  2182. $pad = 7 - calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins );
  2183. if ( 0 != $pad && 7 != $pad ) {
  2184. $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '">&nbsp;</td>';
  2185. }
  2186. $calendar_output .= "\n\t</tr>\n\t</tbody>";
  2187. $calendar_output .= "\n\t</table>";
  2188. $calendar_output .= '<nav aria-label="' . __( 'Previous and next months' ) . '" class="wp-calendar-nav">';
  2189. if ( $previous ) {
  2190. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
  2191. $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
  2192. '</a></span>';
  2193. } else {
  2194. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev">&nbsp;</span>';
  2195. }
  2196. $calendar_output .= "\n\t\t" . '<span class="pad">&nbsp;</span>';
  2197. if ( $next ) {
  2198. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
  2199. $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
  2200. ' &raquo;</a></span>';
  2201. } else {
  2202. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next">&nbsp;</span>';
  2203. }
  2204. $calendar_output .= '
  2205. </nav>';
  2206. $cache[ $key ] = $calendar_output;
  2207. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  2208. if ( $echo ) {
  2209. /**
  2210. * Filters the HTML calendar output.
  2211. *
  2212. * @since 3.0.0
  2213. *
  2214. * @param string $calendar_output HTML output of the calendar.
  2215. */
  2216. echo apply_filters( 'get_calendar', $calendar_output );
  2217. return;
  2218. }
  2219. /** This filter is documented in wp-includes/general-template.php */
  2220. return apply_filters( 'get_calendar', $calendar_output );
  2221. }
  2222. /**
  2223. * Purge the cached results of get_calendar.
  2224. *
  2225. * @see get_calendar()
  2226. * @since 2.1.0
  2227. */
  2228. function delete_get_calendar_cache() {
  2229. wp_cache_delete( 'get_calendar', 'calendar' );
  2230. }
  2231. /**
  2232. * Display all of the allowed tags in HTML format with attributes.
  2233. *
  2234. * This is useful for displaying in the comment area, which elements and
  2235. * attributes are supported. As well as any plugins which want to display it.
  2236. *
  2237. * @since 1.0.1
  2238. *
  2239. * @global array $allowedtags
  2240. *
  2241. * @return string HTML allowed tags entity encoded.
  2242. */
  2243. function allowed_tags() {
  2244. global $allowedtags;
  2245. $allowed = '';
  2246. foreach ( (array) $allowedtags as $tag => $attributes ) {
  2247. $allowed .= '<' . $tag;
  2248. if ( 0 < count( $attributes ) ) {
  2249. foreach ( $attributes as $attribute => $limits ) {
  2250. $allowed .= ' ' . $attribute . '=""';
  2251. }
  2252. }
  2253. $allowed .= '> ';
  2254. }
  2255. return htmlentities( $allowed );
  2256. }
  2257. /***** Date/Time tags */
  2258. /**
  2259. * Outputs the date in iso8601 format for xml files.
  2260. *
  2261. * @since 1.0.0
  2262. */
  2263. function the_date_xml() {
  2264. echo mysql2date( 'Y-m-d', get_post()->post_date, false );
  2265. }
  2266. /**
  2267. * Display or Retrieve the date the current post was written (once per date)
  2268. *
  2269. * Will only output the date if the current post's date is different from the
  2270. * previous one output.
  2271. *
  2272. * i.e. Only one date listing will show per day worth of posts shown in the loop, even if the
  2273. * function is called several times for each post.
  2274. *
  2275. * HTML output can be filtered with 'the_date'.
  2276. * Date string output can be filtered with 'get_the_date'.
  2277. *
  2278. * @since 0.71
  2279. *
  2280. * @global string $currentday The day of the current post in the loop.
  2281. * @global string $previousday The day of the previous post in the loop.
  2282. *
  2283. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2284. * @param string $before Optional. Output before the date. Default empty.
  2285. * @param string $after Optional. Output after the date. Default empty.
  2286. * @param bool $echo Optional. Whether to echo the date or return it. Default true.
  2287. * @return string|void String if retrieving.
  2288. */
  2289. function the_date( $format = '', $before = '', $after = '', $echo = true ) {
  2290. global $currentday, $previousday;
  2291. $the_date = '';
  2292. if ( is_new_day() ) {
  2293. $the_date = $before . get_the_date( $format ) . $after;
  2294. $previousday = $currentday;
  2295. }
  2296. /**
  2297. * Filters the date a post was published for display.
  2298. *
  2299. * @since 0.71
  2300. *
  2301. * @param string $the_date The formatted date string.
  2302. * @param string $format PHP date format.
  2303. * @param string $before HTML output before the date.
  2304. * @param string $after HTML output after the date.
  2305. */
  2306. $the_date = apply_filters( 'the_date', $the_date, $format, $before, $after );
  2307. if ( $echo ) {
  2308. echo $the_date;
  2309. } else {
  2310. return $the_date;
  2311. }
  2312. }
  2313. /**
  2314. * Retrieve the date on which the post was written.
  2315. *
  2316. * Unlike the_date() this function will always return the date.
  2317. * Modify output with the {@see 'get_the_date'} filter.
  2318. *
  2319. * @since 3.0.0
  2320. *
  2321. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2322. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2323. * @return string|false Date the current post was written. False on failure.
  2324. */
  2325. function get_the_date( $format = '', $post = null ) {
  2326. $post = get_post( $post );
  2327. if ( ! $post ) {
  2328. return false;
  2329. }
  2330. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  2331. $the_date = get_post_time( $_format, false, $post, true );
  2332. /**
  2333. * Filters the date a post was published.
  2334. *
  2335. * @since 3.0.0
  2336. *
  2337. * @param string $the_date The formatted date.
  2338. * @param string $format PHP date format.
  2339. * @param int|WP_Post $post The post object or ID.
  2340. */
  2341. return apply_filters( 'get_the_date', $the_date, $format, $post );
  2342. }
  2343. /**
  2344. * Display the date on which the post was last modified.
  2345. *
  2346. * @since 2.1.0
  2347. *
  2348. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2349. * @param string $before Optional. Output before the date. Default empty.
  2350. * @param string $after Optional. Output after the date. Default empty.
  2351. * @param bool $echo Optional. Whether to echo the date or return it. Default true.
  2352. * @return string|void String if retrieving.
  2353. */
  2354. function the_modified_date( $format = '', $before = '', $after = '', $echo = true ) {
  2355. $the_modified_date = $before . get_the_modified_date( $format ) . $after;
  2356. /**
  2357. * Filters the date a post was last modified for display.
  2358. *
  2359. * @since 2.1.0
  2360. *
  2361. * @param string|false $the_modified_date The last modified date or false if no post is found.
  2362. * @param string $format PHP date format.
  2363. * @param string $before HTML output before the date.
  2364. * @param string $after HTML output after the date.
  2365. */
  2366. $the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $format, $before, $after );
  2367. if ( $echo ) {
  2368. echo $the_modified_date;
  2369. } else {
  2370. return $the_modified_date;
  2371. }
  2372. }
  2373. /**
  2374. * Retrieve the date on which the post was last modified.
  2375. *
  2376. * @since 2.1.0
  2377. * @since 4.6.0 Added the `$post` parameter.
  2378. *
  2379. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2380. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2381. * @return string|int|false Date the current post was modified. False on failure.
  2382. */
  2383. function get_the_modified_date( $format = '', $post = null ) {
  2384. $post = get_post( $post );
  2385. if ( ! $post ) {
  2386. // For backward compatibility, failures go through the filter below.
  2387. $the_time = false;
  2388. } else {
  2389. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  2390. $the_time = get_post_modified_time( $_format, false, $post, true );
  2391. }
  2392. /**
  2393. * Filters the date a post was last modified.
  2394. *
  2395. * @since 2.1.0
  2396. * @since 4.6.0 Added the `$post` parameter.
  2397. *
  2398. * @param string|int|false $the_time The formatted date or false if no post is found.
  2399. * @param string $format PHP date format.
  2400. * @param WP_Post|null $post WP_Post object or null if no post is found.
  2401. */
  2402. return apply_filters( 'get_the_modified_date', $the_time, $format, $post );
  2403. }
  2404. /**
  2405. * Display the time at which the post was written.
  2406. *
  2407. * @since 0.71
  2408. *
  2409. * @param string $format Optional. Format to use for retrieving the time the post
  2410. * was written. Accepts 'G', 'U', or PHP date format.
  2411. * Defaults to the 'time_format' option.
  2412. */
  2413. function the_time( $format = '' ) {
  2414. /**
  2415. * Filters the time a post was written for display.
  2416. *
  2417. * @since 0.71
  2418. *
  2419. * @param string $get_the_time The formatted time.
  2420. * @param string $format Format to use for retrieving the time the post
  2421. * was written. Accepts 'G', 'U', or PHP date format.
  2422. */
  2423. echo apply_filters( 'the_time', get_the_time( $format ), $format );
  2424. }
  2425. /**
  2426. * Retrieve the time at which the post was written.
  2427. *
  2428. * @since 1.5.0
  2429. *
  2430. * @param string $format Optional. Format to use for retrieving the time the post
  2431. * was written. Accepts 'G', 'U', or PHP date format.
  2432. * Defaults to the 'time_format' option.
  2433. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2434. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2435. * False on failure.
  2436. */
  2437. function get_the_time( $format = '', $post = null ) {
  2438. $post = get_post( $post );
  2439. if ( ! $post ) {
  2440. return false;
  2441. }
  2442. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  2443. $the_time = get_post_time( $_format, false, $post, true );
  2444. /**
  2445. * Filters the time a post was written.
  2446. *
  2447. * @since 1.5.0
  2448. *
  2449. * @param string $the_time The formatted time.
  2450. * @param string $format Format to use for retrieving the time the post
  2451. * was written. Accepts 'G', 'U', or PHP date format.
  2452. * @param int|WP_Post $post WP_Post object or ID.
  2453. */
  2454. return apply_filters( 'get_the_time', $the_time, $format, $post );
  2455. }
  2456. /**
  2457. * Retrieve the time at which the post was written.
  2458. *
  2459. * @since 2.0.0
  2460. *
  2461. * @param string $format Optional. Format to use for retrieving the time the post
  2462. * was written. Accepts 'G', 'U', or PHP date format. Default 'U'.
  2463. * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
  2464. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2465. * @param bool $translate Whether to translate the time string. Default false.
  2466. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2467. * False on failure.
  2468. */
  2469. function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
  2470. $post = get_post( $post );
  2471. if ( ! $post ) {
  2472. return false;
  2473. }
  2474. $source = ( $gmt ) ? 'gmt' : 'local';
  2475. $datetime = get_post_datetime( $post, 'date', $source );
  2476. if ( false === $datetime ) {
  2477. return false;
  2478. }
  2479. if ( 'U' === $format || 'G' === $format ) {
  2480. $time = $datetime->getTimestamp();
  2481. // Returns a sum of timestamp with timezone offset. Ideally should never be used.
  2482. if ( ! $gmt ) {
  2483. $time += $datetime->getOffset();
  2484. }
  2485. } elseif ( $translate ) {
  2486. $time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
  2487. } else {
  2488. if ( $gmt ) {
  2489. $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
  2490. }
  2491. $time = $datetime->format( $format );
  2492. }
  2493. /**
  2494. * Filters the localized time a post was written.
  2495. *
  2496. * @since 2.6.0
  2497. *
  2498. * @param string $time The formatted time.
  2499. * @param string $format Format to use for retrieving the time the post was written.
  2500. * Accepts 'G', 'U', or PHP date format. Default 'U'.
  2501. * @param bool $gmt Whether to retrieve the GMT time. Default false.
  2502. */
  2503. return apply_filters( 'get_post_time', $time, $format, $gmt );
  2504. }
  2505. /**
  2506. * Retrieve post published or modified time as a `DateTimeImmutable` object instance.
  2507. *
  2508. * The object will be set to the timezone from WordPress settings.
  2509. *
  2510. * For legacy reasons, this function allows to choose to instantiate from local or UTC time in database.
  2511. * Normally this should make no difference to the result. However, the values might get out of sync in database,
  2512. * typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards
  2513. * compatible behaviors in such cases.
  2514. *
  2515. * @since 5.3.0
  2516. *
  2517. * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
  2518. * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
  2519. * Default 'date'.
  2520. * @param string $source Optional. Local or UTC time to use from database. Accepts 'local' or 'gmt'.
  2521. * Default 'local'.
  2522. * @return DateTimeImmutable|false Time object on success, false on failure.
  2523. */
  2524. function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
  2525. $post = get_post( $post );
  2526. if ( ! $post ) {
  2527. return false;
  2528. }
  2529. $wp_timezone = wp_timezone();
  2530. if ( 'gmt' === $source ) {
  2531. $time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
  2532. $timezone = new DateTimeZone( 'UTC' );
  2533. } else {
  2534. $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
  2535. $timezone = $wp_timezone;
  2536. }
  2537. if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
  2538. return false;
  2539. }
  2540. $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );
  2541. if ( false === $datetime ) {
  2542. return false;
  2543. }
  2544. return $datetime->setTimezone( $wp_timezone );
  2545. }
  2546. /**
  2547. * Retrieve post published or modified time as a Unix timestamp.
  2548. *
  2549. * Note that this function returns a true Unix timestamp, not summed with timezone offset
  2550. * like older WP functions.
  2551. *
  2552. * @since 5.3.0
  2553. *
  2554. * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
  2555. * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
  2556. * Default 'date'.
  2557. * @return int|false Unix timestamp on success, false on failure.
  2558. */
  2559. function get_post_timestamp( $post = null, $field = 'date' ) {
  2560. $datetime = get_post_datetime( $post, $field );
  2561. if ( false === $datetime ) {
  2562. return false;
  2563. }
  2564. return $datetime->getTimestamp();
  2565. }
  2566. /**
  2567. * Display the time at which the post was last modified.
  2568. *
  2569. * @since 2.0.0
  2570. *
  2571. * @param string $format Optional. Format to use for retrieving the time the post
  2572. * was modified. Accepts 'G', 'U', or PHP date format.
  2573. * Defaults to the 'time_format' option.
  2574. */
  2575. function the_modified_time( $format = '' ) {
  2576. /**
  2577. * Filters the localized time a post was last modified, for display.
  2578. *
  2579. * @since 2.0.0
  2580. *
  2581. * @param string|false $get_the_modified_time The formatted time or false if no post is found.
  2582. * @param string $format Format to use for retrieving the time the post
  2583. * was modified. Accepts 'G', 'U', or PHP date format.
  2584. */
  2585. echo apply_filters( 'the_modified_time', get_the_modified_time( $format ), $format );
  2586. }
  2587. /**
  2588. * Retrieve the time at which the post was last modified.
  2589. *
  2590. * @since 2.0.0
  2591. * @since 4.6.0 Added the `$post` parameter.
  2592. *
  2593. * @param string $format Optional. Format to use for retrieving the time the post
  2594. * was modified. Accepts 'G', 'U', or PHP date format.
  2595. * Defaults to the 'time_format' option.
  2596. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2597. * @return string|int|false Formatted date string or Unix timestamp. False on failure.
  2598. */
  2599. function get_the_modified_time( $format = '', $post = null ) {
  2600. $post = get_post( $post );
  2601. if ( ! $post ) {
  2602. // For backward compatibility, failures go through the filter below.
  2603. $the_time = false;
  2604. } else {
  2605. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  2606. $the_time = get_post_modified_time( $_format, false, $post, true );
  2607. }
  2608. /**
  2609. * Filters the localized time a post was last modified.
  2610. *
  2611. * @since 2.0.0
  2612. * @since 4.6.0 Added the `$post` parameter.
  2613. *
  2614. * @param string|int|false $the_time The formatted time or false if no post is found.
  2615. * @param string $format Format to use for retrieving the time the post
  2616. * was modified. Accepts 'G', 'U', or PHP date format.
  2617. * @param WP_Post|null $post WP_Post object or null if no post is found.
  2618. */
  2619. return apply_filters( 'get_the_modified_time', $the_time, $format, $post );
  2620. }
  2621. /**
  2622. * Retrieve the time at which the post was last modified.
  2623. *
  2624. * @since 2.0.0
  2625. *
  2626. * @param string $format Optional. Format to use for retrieving the time the post
  2627. * was modified. Accepts 'G', 'U', or PHP date format. Default 'U'.
  2628. * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
  2629. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2630. * @param bool $translate Whether to translate the time string. Default false.
  2631. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2632. * False on failure.
  2633. */
  2634. function get_post_modified_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
  2635. $post = get_post( $post );
  2636. if ( ! $post ) {
  2637. return false;
  2638. }
  2639. $source = ( $gmt ) ? 'gmt' : 'local';
  2640. $datetime = get_post_datetime( $post, 'modified', $source );
  2641. if ( false === $datetime ) {
  2642. return false;
  2643. }
  2644. if ( 'U' === $format || 'G' === $format ) {
  2645. $time = $datetime->getTimestamp();
  2646. // Returns a sum of timestamp with timezone offset. Ideally should never be used.
  2647. if ( ! $gmt ) {
  2648. $time += $datetime->getOffset();
  2649. }
  2650. } elseif ( $translate ) {
  2651. $time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
  2652. } else {
  2653. if ( $gmt ) {
  2654. $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
  2655. }
  2656. $time = $datetime->format( $format );
  2657. }
  2658. /**
  2659. * Filters the localized time a post was last modified.
  2660. *
  2661. * @since 2.8.0
  2662. *
  2663. * @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2664. * @param string $format Format to use for retrieving the time the post was modified.
  2665. * Accepts 'G', 'U', or PHP date format. Default 'U'.
  2666. * @param bool $gmt Whether to retrieve the GMT time. Default false.
  2667. */
  2668. return apply_filters( 'get_post_modified_time', $time, $format, $gmt );
  2669. }
  2670. /**
  2671. * Display the weekday on which the post was written.
  2672. *
  2673. * @since 0.71
  2674. *
  2675. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2676. */
  2677. function the_weekday() {
  2678. global $wp_locale;
  2679. $post = get_post();
  2680. if ( ! $post ) {
  2681. return;
  2682. }
  2683. $the_weekday = $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
  2684. /**
  2685. * Filters the weekday on which the post was written, for display.
  2686. *
  2687. * @since 0.71
  2688. *
  2689. * @param string $the_weekday
  2690. */
  2691. echo apply_filters( 'the_weekday', $the_weekday );
  2692. }
  2693. /**
  2694. * Display the weekday on which the post was written.
  2695. *
  2696. * Will only output the weekday if the current post's weekday is different from
  2697. * the previous one output.
  2698. *
  2699. * @since 0.71
  2700. *
  2701. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2702. * @global string $currentday The day of the current post in the loop.
  2703. * @global string $previousweekday The day of the previous post in the loop.
  2704. *
  2705. * @param string $before Optional. Output before the date. Default empty.
  2706. * @param string $after Optional. Output after the date. Default empty.
  2707. */
  2708. function the_weekday_date( $before = '', $after = '' ) {
  2709. global $wp_locale, $currentday, $previousweekday;
  2710. $post = get_post();
  2711. if ( ! $post ) {
  2712. return;
  2713. }
  2714. $the_weekday_date = '';
  2715. if ( $currentday !== $previousweekday ) {
  2716. $the_weekday_date .= $before;
  2717. $the_weekday_date .= $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
  2718. $the_weekday_date .= $after;
  2719. $previousweekday = $currentday;
  2720. }
  2721. /**
  2722. * Filters the localized date on which the post was written, for display.
  2723. *
  2724. * @since 0.71
  2725. *
  2726. * @param string $the_weekday_date The weekday on which the post was written.
  2727. * @param string $before The HTML to output before the date.
  2728. * @param string $after The HTML to output after the date.
  2729. */
  2730. echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
  2731. }
  2732. /**
  2733. * Fire the wp_head action.
  2734. *
  2735. * See {@see 'wp_head'}.
  2736. *
  2737. * @since 1.2.0
  2738. */
  2739. function wp_head() {
  2740. /**
  2741. * Prints scripts or data in the head tag on the front end.
  2742. *
  2743. * @since 1.5.0
  2744. */
  2745. do_action( 'wp_head' );
  2746. }
  2747. /**
  2748. * Fire the wp_footer action.
  2749. *
  2750. * See {@see 'wp_footer'}.
  2751. *
  2752. * @since 1.5.1
  2753. */
  2754. function wp_footer() {
  2755. /**
  2756. * Prints scripts or data before the closing body tag on the front end.
  2757. *
  2758. * @since 1.5.1
  2759. */
  2760. do_action( 'wp_footer' );
  2761. }
  2762. /**
  2763. * Fire the wp_body_open action.
  2764. *
  2765. * See {@see 'wp_body_open'}.
  2766. *
  2767. * @since 5.2.0
  2768. */
  2769. function wp_body_open() {
  2770. /**
  2771. * Triggered after the opening body tag.
  2772. *
  2773. * @since 5.2.0
  2774. */
  2775. do_action( 'wp_body_open' );
  2776. }
  2777. /**
  2778. * Display the links to the general feeds.
  2779. *
  2780. * @since 2.8.0
  2781. *
  2782. * @param array $args Optional arguments.
  2783. */
  2784. function feed_links( $args = array() ) {
  2785. if ( ! current_theme_supports( 'automatic-feed-links' ) ) {
  2786. return;
  2787. }
  2788. $defaults = array(
  2789. /* translators: Separator between blog name and feed type in feed links. */
  2790. 'separator' => _x( '&raquo;', 'feed link' ),
  2791. /* translators: 1: Blog title, 2: Separator (raquo). */
  2792. 'feedtitle' => __( '%1$s %2$s Feed' ),
  2793. /* translators: 1: Blog title, 2: Separator (raquo). */
  2794. 'comstitle' => __( '%1$s %2$s Comments Feed' ),
  2795. );
  2796. $args = wp_parse_args( $args, $defaults );
  2797. /**
  2798. * Filters whether to display the posts feed link.
  2799. *
  2800. * @since 4.4.0
  2801. *
  2802. * @param bool $show Whether to display the posts feed link. Default true.
  2803. */
  2804. if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
  2805. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link() ) . "\" />\n";
  2806. }
  2807. /**
  2808. * Filters whether to display the comments feed link.
  2809. *
  2810. * @since 4.4.0
  2811. *
  2812. * @param bool $show Whether to display the comments feed link. Default true.
  2813. */
  2814. if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
  2815. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link( 'comments_' . get_default_feed() ) ) . "\" />\n";
  2816. }
  2817. }
  2818. /**
  2819. * Display the links to the extra feeds such as category feeds.
  2820. *
  2821. * @since 2.8.0
  2822. *
  2823. * @param array $args Optional arguments.
  2824. */
  2825. function feed_links_extra( $args = array() ) {
  2826. $defaults = array(
  2827. /* translators: Separator between blog name and feed type in feed links. */
  2828. 'separator' => _x( '&raquo;', 'feed link' ),
  2829. /* translators: 1: Blog name, 2: Separator (raquo), 3: Post title. */
  2830. 'singletitle' => __( '%1$s %2$s %3$s Comments Feed' ),
  2831. /* translators: 1: Blog name, 2: Separator (raquo), 3: Category name. */
  2832. 'cattitle' => __( '%1$s %2$s %3$s Category Feed' ),
  2833. /* translators: 1: Blog name, 2: Separator (raquo), 3: Tag name. */
  2834. 'tagtitle' => __( '%1$s %2$s %3$s Tag Feed' ),
  2835. /* translators: 1: Blog name, 2: Separator (raquo), 3: Term name, 4: Taxonomy singular name. */
  2836. 'taxtitle' => __( '%1$s %2$s %3$s %4$s Feed' ),
  2837. /* translators: 1: Blog name, 2: Separator (raquo), 3: Author name. */
  2838. 'authortitle' => __( '%1$s %2$s Posts by %3$s Feed' ),
  2839. /* translators: 1: Blog name, 2: Separator (raquo), 3: Search query. */
  2840. 'searchtitle' => __( '%1$s %2$s Search Results for &#8220;%3$s&#8221; Feed' ),
  2841. /* translators: 1: Blog name, 2: Separator (raquo), 3: Post type name. */
  2842. 'posttypetitle' => __( '%1$s %2$s %3$s Feed' ),
  2843. );
  2844. $args = wp_parse_args( $args, $defaults );
  2845. if ( is_singular() ) {
  2846. $id = 0;
  2847. $post = get_post( $id );
  2848. if ( comments_open() || pings_open() || $post->comment_count > 0 ) {
  2849. $title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
  2850. $href = get_post_comments_feed_link( $post->ID );
  2851. }
  2852. } elseif ( is_post_type_archive() ) {
  2853. $post_type = get_query_var( 'post_type' );
  2854. if ( is_array( $post_type ) ) {
  2855. $post_type = reset( $post_type );
  2856. }
  2857. $post_type_obj = get_post_type_object( $post_type );
  2858. $title = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name );
  2859. $href = get_post_type_archive_feed_link( $post_type_obj->name );
  2860. } elseif ( is_category() ) {
  2861. $term = get_queried_object();
  2862. if ( $term ) {
  2863. $title = sprintf( $args['cattitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
  2864. $href = get_category_feed_link( $term->term_id );
  2865. }
  2866. } elseif ( is_tag() ) {
  2867. $term = get_queried_object();
  2868. if ( $term ) {
  2869. $title = sprintf( $args['tagtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
  2870. $href = get_tag_feed_link( $term->term_id );
  2871. }
  2872. } elseif ( is_tax() ) {
  2873. $term = get_queried_object();
  2874. if ( $term ) {
  2875. $tax = get_taxonomy( $term->taxonomy );
  2876. $title = sprintf( $args['taxtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name, $tax->labels->singular_name );
  2877. $href = get_term_feed_link( $term->term_id, $term->taxonomy );
  2878. }
  2879. } elseif ( is_author() ) {
  2880. $author_id = (int) get_query_var( 'author' );
  2881. $title = sprintf( $args['authortitle'], get_bloginfo( 'name' ), $args['separator'], get_the_author_meta( 'display_name', $author_id ) );
  2882. $href = get_author_feed_link( $author_id );
  2883. } elseif ( is_search() ) {
  2884. $title = sprintf( $args['searchtitle'], get_bloginfo( 'name' ), $args['separator'], get_search_query( false ) );
  2885. $href = get_search_feed_link();
  2886. }
  2887. if ( isset( $title ) && isset( $href ) ) {
  2888. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
  2889. }
  2890. }
  2891. /**
  2892. * Display the link to the Really Simple Discovery service endpoint.
  2893. *
  2894. * @link http://archipelago.phrasewise.com/rsd
  2895. * @since 2.0.0
  2896. */
  2897. function rsd_link() {
  2898. echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) . '" />' . "\n";
  2899. }
  2900. /**
  2901. * Display the link to the Windows Live Writer manifest file.
  2902. *
  2903. * @link https://msdn.microsoft.com/en-us/library/bb463265.aspx
  2904. * @since 2.3.1
  2905. */
  2906. function wlwmanifest_link() {
  2907. echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="' . includes_url( 'wlwmanifest.xml' ) . '" /> ' . "\n";
  2908. }
  2909. /**
  2910. * Displays a referrer strict-origin-when-cross-origin meta tag.
  2911. *
  2912. * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
  2913. * url as a referrer to other sites when cross-origin assets are loaded.
  2914. *
  2915. * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
  2916. *
  2917. * @since 5.7.0
  2918. */
  2919. function wp_strict_cross_origin_referrer() {
  2920. ?>
  2921. <meta name='referrer' content='strict-origin-when-cross-origin' />
  2922. <?php
  2923. }
  2924. /**
  2925. * Display site icon meta tags.
  2926. *
  2927. * @since 4.3.0
  2928. *
  2929. * @link https://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon HTML5 specification link icon.
  2930. */
  2931. function wp_site_icon() {
  2932. if ( ! has_site_icon() && ! is_customize_preview() ) {
  2933. return;
  2934. }
  2935. $meta_tags = array();
  2936. $icon_32 = get_site_icon_url( 32 );
  2937. if ( empty( $icon_32 ) && is_customize_preview() ) {
  2938. $icon_32 = '/favicon.ico'; // Serve default favicon URL in customizer so element can be updated for preview.
  2939. }
  2940. if ( $icon_32 ) {
  2941. $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( $icon_32 ) );
  2942. }
  2943. $icon_192 = get_site_icon_url( 192 );
  2944. if ( $icon_192 ) {
  2945. $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( $icon_192 ) );
  2946. }
  2947. $icon_180 = get_site_icon_url( 180 );
  2948. if ( $icon_180 ) {
  2949. $meta_tags[] = sprintf( '<link rel="apple-touch-icon" href="%s" />', esc_url( $icon_180 ) );
  2950. }
  2951. $icon_270 = get_site_icon_url( 270 );
  2952. if ( $icon_270 ) {
  2953. $meta_tags[] = sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( $icon_270 ) );
  2954. }
  2955. /**
  2956. * Filters the site icon meta tags, so plugins can add their own.
  2957. *
  2958. * @since 4.3.0
  2959. *
  2960. * @param string[] $meta_tags Array of Site Icon meta tags.
  2961. */
  2962. $meta_tags = apply_filters( 'site_icon_meta_tags', $meta_tags );
  2963. $meta_tags = array_filter( $meta_tags );
  2964. foreach ( $meta_tags as $meta_tag ) {
  2965. echo "$meta_tag\n";
  2966. }
  2967. }
  2968. /**
  2969. * Prints resource hints to browsers for pre-fetching, pre-rendering
  2970. * and pre-connecting to web sites.
  2971. *
  2972. * Gives hints to browsers to prefetch specific pages or render them
  2973. * in the background, to perform DNS lookups or to begin the connection
  2974. * handshake (DNS, TCP, TLS) in the background.
  2975. *
  2976. * These performance improving indicators work by using `<link rel"…">`.
  2977. *
  2978. * @since 4.6.0
  2979. */
  2980. function wp_resource_hints() {
  2981. $hints = array(
  2982. 'dns-prefetch' => wp_dependencies_unique_hosts(),
  2983. 'preconnect' => array(),
  2984. 'prefetch' => array(),
  2985. 'prerender' => array(),
  2986. );
  2987. /*
  2988. * Add DNS prefetch for the Emoji CDN.
  2989. * The path is removed in the foreach loop below.
  2990. */
  2991. /** This filter is documented in wp-includes/formatting.php */
  2992. $hints['dns-prefetch'][] = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/13.0.0/svg/' );
  2993. foreach ( $hints as $relation_type => $urls ) {
  2994. $unique_urls = array();
  2995. /**
  2996. * Filters domains and URLs for resource hints of relation type.
  2997. *
  2998. * @since 4.6.0
  2999. * @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes
  3000. * as its child elements.
  3001. *
  3002. * @param array $urls {
  3003. * Array of resources and their attributes, or URLs to print for resource hints.
  3004. *
  3005. * @type array|string ...$0 {
  3006. * Array of resource attributes, or a URL string.
  3007. *
  3008. * @type string $href URL to include in resource hints. Required.
  3009. * @type string $as How the browser should treat the resource
  3010. * (`script`, `style`, `image`, `document`, etc).
  3011. * @type string $crossorigin Indicates the CORS policy of the specified resource.
  3012. * @type float $pr Expected probability that the resource hint will be used.
  3013. * @type string $type Type of the resource (`text/html`, `text/css`, etc).
  3014. * }
  3015. * }
  3016. * @param string $relation_type The relation type the URLs are printed for,
  3017. * e.g. 'preconnect' or 'prerender'.
  3018. */
  3019. $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
  3020. foreach ( $urls as $key => $url ) {
  3021. $atts = array();
  3022. if ( is_array( $url ) ) {
  3023. if ( isset( $url['href'] ) ) {
  3024. $atts = $url;
  3025. $url = $url['href'];
  3026. } else {
  3027. continue;
  3028. }
  3029. }
  3030. $url = esc_url( $url, array( 'http', 'https' ) );
  3031. if ( ! $url ) {
  3032. continue;
  3033. }
  3034. if ( isset( $unique_urls[ $url ] ) ) {
  3035. continue;
  3036. }
  3037. if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) {
  3038. $parsed = wp_parse_url( $url );
  3039. if ( empty( $parsed['host'] ) ) {
  3040. continue;
  3041. }
  3042. if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
  3043. $url = $parsed['scheme'] . '://' . $parsed['host'];
  3044. } else {
  3045. // Use protocol-relative URLs for dns-prefetch or if scheme is missing.
  3046. $url = '//' . $parsed['host'];
  3047. }
  3048. }
  3049. $atts['rel'] = $relation_type;
  3050. $atts['href'] = $url;
  3051. $unique_urls[ $url ] = $atts;
  3052. }
  3053. foreach ( $unique_urls as $atts ) {
  3054. $html = '';
  3055. foreach ( $atts as $attr => $value ) {
  3056. if ( ! is_scalar( $value )
  3057. || ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) )
  3058. ) {
  3059. continue;
  3060. }
  3061. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  3062. if ( ! is_string( $attr ) ) {
  3063. $html .= " $value";
  3064. } else {
  3065. $html .= " $attr='$value'";
  3066. }
  3067. }
  3068. $html = trim( $html );
  3069. echo "<link $html />\n";
  3070. }
  3071. }
  3072. }
  3073. /**
  3074. * Retrieves a list of unique hosts of all enqueued scripts and styles.
  3075. *
  3076. * @since 4.6.0
  3077. *
  3078. * @return string[] A list of unique hosts of enqueued scripts and styles.
  3079. */
  3080. function wp_dependencies_unique_hosts() {
  3081. global $wp_scripts, $wp_styles;
  3082. $unique_hosts = array();
  3083. foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) {
  3084. if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) {
  3085. foreach ( $dependencies->queue as $handle ) {
  3086. if ( ! isset( $dependencies->registered[ $handle ] ) ) {
  3087. continue;
  3088. }
  3089. /* @var _WP_Dependency $dependency */
  3090. $dependency = $dependencies->registered[ $handle ];
  3091. $parsed = wp_parse_url( $dependency->src );
  3092. if ( ! empty( $parsed['host'] )
  3093. && ! in_array( $parsed['host'], $unique_hosts, true ) && $parsed['host'] !== $_SERVER['SERVER_NAME']
  3094. ) {
  3095. $unique_hosts[] = $parsed['host'];
  3096. }
  3097. }
  3098. }
  3099. }
  3100. return $unique_hosts;
  3101. }
  3102. /**
  3103. * Whether the user can access the visual editor.
  3104. *
  3105. * Checks if the user can access the visual editor and that it's supported by the user's browser.
  3106. *
  3107. * @since 2.0.0
  3108. *
  3109. * @global bool $wp_rich_edit Whether the user can access the visual editor.
  3110. * @global bool $is_gecko Whether the browser is Gecko-based.
  3111. * @global bool $is_opera Whether the browser is Opera.
  3112. * @global bool $is_safari Whether the browser is Safari.
  3113. * @global bool $is_chrome Whether the browser is Chrome.
  3114. * @global bool $is_IE Whether the browser is Internet Explorer.
  3115. * @global bool $is_edge Whether the browser is Microsoft Edge.
  3116. *
  3117. * @return bool True if the user can access the visual editor, false otherwise.
  3118. */
  3119. function user_can_richedit() {
  3120. global $wp_rich_edit, $is_gecko, $is_opera, $is_safari, $is_chrome, $is_IE, $is_edge;
  3121. if ( ! isset( $wp_rich_edit ) ) {
  3122. $wp_rich_edit = false;
  3123. if ( 'true' === get_user_option( 'rich_editing' ) || ! is_user_logged_in() ) { // Default to 'true' for logged out users.
  3124. if ( $is_safari ) {
  3125. $wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && (int) $match[1] >= 534 );
  3126. } elseif ( $is_IE ) {
  3127. $wp_rich_edit = ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' ) !== false );
  3128. } elseif ( $is_gecko || $is_chrome || $is_edge || ( $is_opera && ! wp_is_mobile() ) ) {
  3129. $wp_rich_edit = true;
  3130. }
  3131. }
  3132. }
  3133. /**
  3134. * Filters whether the user can access the visual editor.
  3135. *
  3136. * @since 2.1.0
  3137. *
  3138. * @param bool $wp_rich_edit Whether the user can access the visual editor.
  3139. */
  3140. return apply_filters( 'user_can_richedit', $wp_rich_edit );
  3141. }
  3142. /**
  3143. * Find out which editor should be displayed by default.
  3144. *
  3145. * Works out which of the two editors to display as the current editor for a
  3146. * user. The 'html' setting is for the "Text" editor tab.
  3147. *
  3148. * @since 2.5.0
  3149. *
  3150. * @return string Either 'tinymce', or 'html', or 'test'
  3151. */
  3152. function wp_default_editor() {
  3153. $r = user_can_richedit() ? 'tinymce' : 'html'; // Defaults.
  3154. if ( wp_get_current_user() ) { // Look for cookie.
  3155. $ed = get_user_setting( 'editor', 'tinymce' );
  3156. $r = ( in_array( $ed, array( 'tinymce', 'html', 'test' ), true ) ) ? $ed : $r;
  3157. }
  3158. /**
  3159. * Filters which editor should be displayed by default.
  3160. *
  3161. * @since 2.5.0
  3162. *
  3163. * @param string $r Which editor should be displayed by default. Either 'tinymce', 'html', or 'test'.
  3164. */
  3165. return apply_filters( 'wp_default_editor', $r );
  3166. }
  3167. /**
  3168. * Renders an editor.
  3169. *
  3170. * Using this function is the proper way to output all needed components for both TinyMCE and Quicktags.
  3171. * _WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144.
  3172. *
  3173. * NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason
  3174. * running wp_editor() inside of a meta box is not a good idea unless only Quicktags is used.
  3175. * On the post edit screen several actions can be used to include additional editors
  3176. * containing TinyMCE: 'edit_page_form', 'edit_form_advanced' and 'dbx_post_sidebar'.
  3177. * See https://core.trac.wordpress.org/ticket/19173 for more information.
  3178. *
  3179. * @see _WP_Editors::editor()
  3180. * @see _WP_Editors::parse_settings()
  3181. * @since 3.3.0
  3182. *
  3183. * @param string $content Initial content for the editor.
  3184. * @param string $editor_id HTML ID attribute value for the textarea and TinyMCE.
  3185. * Should not contain square brackets.
  3186. * @param array $settings See _WP_Editors::parse_settings() for description.
  3187. */
  3188. function wp_editor( $content, $editor_id, $settings = array() ) {
  3189. if ( ! class_exists( '_WP_Editors', false ) ) {
  3190. require ABSPATH . WPINC . '/class-wp-editor.php';
  3191. }
  3192. _WP_Editors::editor( $content, $editor_id, $settings );
  3193. }
  3194. /**
  3195. * Outputs the editor scripts, stylesheets, and default settings.
  3196. *
  3197. * The editor can be initialized when needed after page load.
  3198. * See wp.editor.initialize() in wp-admin/js/editor.js for initialization options.
  3199. *
  3200. * @uses _WP_Editors
  3201. * @since 4.8.0
  3202. */
  3203. function wp_enqueue_editor() {
  3204. if ( ! class_exists( '_WP_Editors', false ) ) {
  3205. require ABSPATH . WPINC . '/class-wp-editor.php';
  3206. }
  3207. _WP_Editors::enqueue_default_editor();
  3208. }
  3209. /**
  3210. * Enqueue assets needed by the code editor for the given settings.
  3211. *
  3212. * @since 4.9.0
  3213. *
  3214. * @see wp_enqueue_editor()
  3215. * @see wp_get_code_editor_settings();
  3216. * @see _WP_Editors::parse_settings()
  3217. *
  3218. * @param array $args {
  3219. * Args.
  3220. *
  3221. * @type string $type The MIME type of the file to be edited.
  3222. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
  3223. * @type WP_Theme $theme Theme being edited when on the theme file editor.
  3224. * @type string $plugin Plugin being edited when on the plugin file editor.
  3225. * @type array $codemirror Additional CodeMirror setting overrides.
  3226. * @type array $csslint CSSLint rule overrides.
  3227. * @type array $jshint JSHint rule overrides.
  3228. * @type array $htmlhint HTMLHint rule overrides.
  3229. * }
  3230. * @return array|false Settings for the enqueued code editor, or false if the editor was not enqueued.
  3231. */
  3232. function wp_enqueue_code_editor( $args ) {
  3233. if ( is_user_logged_in() && 'false' === wp_get_current_user()->syntax_highlighting ) {
  3234. return false;
  3235. }
  3236. $settings = wp_get_code_editor_settings( $args );
  3237. if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
  3238. return false;
  3239. }
  3240. wp_enqueue_script( 'code-editor' );
  3241. wp_enqueue_style( 'code-editor' );
  3242. if ( isset( $settings['codemirror']['mode'] ) ) {
  3243. $mode = $settings['codemirror']['mode'];
  3244. if ( is_string( $mode ) ) {
  3245. $mode = array(
  3246. 'name' => $mode,
  3247. );
  3248. }
  3249. if ( ! empty( $settings['codemirror']['lint'] ) ) {
  3250. switch ( $mode['name'] ) {
  3251. case 'css':
  3252. case 'text/css':
  3253. case 'text/x-scss':
  3254. case 'text/x-less':
  3255. wp_enqueue_script( 'csslint' );
  3256. break;
  3257. case 'htmlmixed':
  3258. case 'text/html':
  3259. case 'php':
  3260. case 'application/x-httpd-php':
  3261. case 'text/x-php':
  3262. wp_enqueue_script( 'htmlhint' );
  3263. wp_enqueue_script( 'csslint' );
  3264. wp_enqueue_script( 'jshint' );
  3265. if ( ! current_user_can( 'unfiltered_html' ) ) {
  3266. wp_enqueue_script( 'htmlhint-kses' );
  3267. }
  3268. break;
  3269. case 'javascript':
  3270. case 'application/ecmascript':
  3271. case 'application/json':
  3272. case 'application/javascript':
  3273. case 'application/ld+json':
  3274. case 'text/typescript':
  3275. case 'application/typescript':
  3276. wp_enqueue_script( 'jshint' );
  3277. wp_enqueue_script( 'jsonlint' );
  3278. break;
  3279. }
  3280. }
  3281. }
  3282. wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
  3283. /**
  3284. * Fires when scripts and styles are enqueued for the code editor.
  3285. *
  3286. * @since 4.9.0
  3287. *
  3288. * @param array $settings Settings for the enqueued code editor.
  3289. */
  3290. do_action( 'wp_enqueue_code_editor', $settings );
  3291. return $settings;
  3292. }
  3293. /**
  3294. * Generate and return code editor settings.
  3295. *
  3296. * @since 5.0.0
  3297. *
  3298. * @see wp_enqueue_code_editor()
  3299. *
  3300. * @param array $args {
  3301. * Args.
  3302. *
  3303. * @type string $type The MIME type of the file to be edited.
  3304. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
  3305. * @type WP_Theme $theme Theme being edited when on the theme file editor.
  3306. * @type string $plugin Plugin being edited when on the plugin file editor.
  3307. * @type array $codemirror Additional CodeMirror setting overrides.
  3308. * @type array $csslint CSSLint rule overrides.
  3309. * @type array $jshint JSHint rule overrides.
  3310. * @type array $htmlhint HTMLHint rule overrides.
  3311. * }
  3312. * @return array|false Settings for the code editor.
  3313. */
  3314. function wp_get_code_editor_settings( $args ) {
  3315. $settings = array(
  3316. 'codemirror' => array(
  3317. 'indentUnit' => 4,
  3318. 'indentWithTabs' => true,
  3319. 'inputStyle' => 'contenteditable',
  3320. 'lineNumbers' => true,
  3321. 'lineWrapping' => true,
  3322. 'styleActiveLine' => true,
  3323. 'continueComments' => true,
  3324. 'extraKeys' => array(
  3325. 'Ctrl-Space' => 'autocomplete',
  3326. 'Ctrl-/' => 'toggleComment',
  3327. 'Cmd-/' => 'toggleComment',
  3328. 'Alt-F' => 'findPersistent',
  3329. 'Ctrl-F' => 'findPersistent',
  3330. 'Cmd-F' => 'findPersistent',
  3331. ),
  3332. 'direction' => 'ltr', // Code is shown in LTR even in RTL languages.
  3333. 'gutters' => array(),
  3334. ),
  3335. 'csslint' => array(
  3336. 'errors' => true, // Parsing errors.
  3337. 'box-model' => true,
  3338. 'display-property-grouping' => true,
  3339. 'duplicate-properties' => true,
  3340. 'known-properties' => true,
  3341. 'outline-none' => true,
  3342. ),
  3343. 'jshint' => array(
  3344. // The following are copied from <https://github.com/WordPress/wordpress-develop/blob/4.8.1/.jshintrc>.
  3345. 'boss' => true,
  3346. 'curly' => true,
  3347. 'eqeqeq' => true,
  3348. 'eqnull' => true,
  3349. 'es3' => true,
  3350. 'expr' => true,
  3351. 'immed' => true,
  3352. 'noarg' => true,
  3353. 'nonbsp' => true,
  3354. 'onevar' => true,
  3355. 'quotmark' => 'single',
  3356. 'trailing' => true,
  3357. 'undef' => true,
  3358. 'unused' => true,
  3359. 'browser' => true,
  3360. 'globals' => array(
  3361. '_' => false,
  3362. 'Backbone' => false,
  3363. 'jQuery' => false,
  3364. 'JSON' => false,
  3365. 'wp' => false,
  3366. ),
  3367. ),
  3368. 'htmlhint' => array(
  3369. 'tagname-lowercase' => true,
  3370. 'attr-lowercase' => true,
  3371. 'attr-value-double-quotes' => false,
  3372. 'doctype-first' => false,
  3373. 'tag-pair' => true,
  3374. 'spec-char-escape' => true,
  3375. 'id-unique' => true,
  3376. 'src-not-empty' => true,
  3377. 'attr-no-duplication' => true,
  3378. 'alt-require' => true,
  3379. 'space-tab-mixed-disabled' => 'tab',
  3380. 'attr-unsafe-chars' => true,
  3381. ),
  3382. );
  3383. $type = '';
  3384. if ( isset( $args['type'] ) ) {
  3385. $type = $args['type'];
  3386. // Remap MIME types to ones that CodeMirror modes will recognize.
  3387. if ( 'application/x-patch' === $type || 'text/x-patch' === $type ) {
  3388. $type = 'text/x-diff';
  3389. }
  3390. } elseif ( isset( $args['file'] ) && false !== strpos( basename( $args['file'] ), '.' ) ) {
  3391. $extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) );
  3392. foreach ( wp_get_mime_types() as $exts => $mime ) {
  3393. if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
  3394. $type = $mime;
  3395. break;
  3396. }
  3397. }
  3398. // Supply any types that are not matched by wp_get_mime_types().
  3399. if ( empty( $type ) ) {
  3400. switch ( $extension ) {
  3401. case 'conf':
  3402. $type = 'text/nginx';
  3403. break;
  3404. case 'css':
  3405. $type = 'text/css';
  3406. break;
  3407. case 'diff':
  3408. case 'patch':
  3409. $type = 'text/x-diff';
  3410. break;
  3411. case 'html':
  3412. case 'htm':
  3413. $type = 'text/html';
  3414. break;
  3415. case 'http':
  3416. $type = 'message/http';
  3417. break;
  3418. case 'js':
  3419. $type = 'text/javascript';
  3420. break;
  3421. case 'json':
  3422. $type = 'application/json';
  3423. break;
  3424. case 'jsx':
  3425. $type = 'text/jsx';
  3426. break;
  3427. case 'less':
  3428. $type = 'text/x-less';
  3429. break;
  3430. case 'md':
  3431. $type = 'text/x-gfm';
  3432. break;
  3433. case 'php':
  3434. case 'phtml':
  3435. case 'php3':
  3436. case 'php4':
  3437. case 'php5':
  3438. case 'php7':
  3439. case 'phps':
  3440. $type = 'application/x-httpd-php';
  3441. break;
  3442. case 'scss':
  3443. $type = 'text/x-scss';
  3444. break;
  3445. case 'sass':
  3446. $type = 'text/x-sass';
  3447. break;
  3448. case 'sh':
  3449. case 'bash':
  3450. $type = 'text/x-sh';
  3451. break;
  3452. case 'sql':
  3453. $type = 'text/x-sql';
  3454. break;
  3455. case 'svg':
  3456. $type = 'application/svg+xml';
  3457. break;
  3458. case 'xml':
  3459. $type = 'text/xml';
  3460. break;
  3461. case 'yml':
  3462. case 'yaml':
  3463. $type = 'text/x-yaml';
  3464. break;
  3465. case 'txt':
  3466. default:
  3467. $type = 'text/plain';
  3468. break;
  3469. }
  3470. }
  3471. }
  3472. if ( in_array( $type, array( 'text/css', 'text/x-scss', 'text/x-less', 'text/x-sass' ), true ) ) {
  3473. $settings['codemirror'] = array_merge(
  3474. $settings['codemirror'],
  3475. array(
  3476. 'mode' => $type,
  3477. 'lint' => false,
  3478. 'autoCloseBrackets' => true,
  3479. 'matchBrackets' => true,
  3480. )
  3481. );
  3482. } elseif ( 'text/x-diff' === $type ) {
  3483. $settings['codemirror'] = array_merge(
  3484. $settings['codemirror'],
  3485. array(
  3486. 'mode' => 'diff',
  3487. )
  3488. );
  3489. } elseif ( 'text/html' === $type ) {
  3490. $settings['codemirror'] = array_merge(
  3491. $settings['codemirror'],
  3492. array(
  3493. 'mode' => 'htmlmixed',
  3494. 'lint' => true,
  3495. 'autoCloseBrackets' => true,
  3496. 'autoCloseTags' => true,
  3497. 'matchTags' => array(
  3498. 'bothTags' => true,
  3499. ),
  3500. )
  3501. );
  3502. if ( ! current_user_can( 'unfiltered_html' ) ) {
  3503. $settings['htmlhint']['kses'] = wp_kses_allowed_html( 'post' );
  3504. }
  3505. } elseif ( 'text/x-gfm' === $type ) {
  3506. $settings['codemirror'] = array_merge(
  3507. $settings['codemirror'],
  3508. array(
  3509. 'mode' => 'gfm',
  3510. 'highlightFormatting' => true,
  3511. )
  3512. );
  3513. } elseif ( 'application/javascript' === $type || 'text/javascript' === $type ) {
  3514. $settings['codemirror'] = array_merge(
  3515. $settings['codemirror'],
  3516. array(
  3517. 'mode' => 'javascript',
  3518. 'lint' => true,
  3519. 'autoCloseBrackets' => true,
  3520. 'matchBrackets' => true,
  3521. )
  3522. );
  3523. } elseif ( false !== strpos( $type, 'json' ) ) {
  3524. $settings['codemirror'] = array_merge(
  3525. $settings['codemirror'],
  3526. array(
  3527. 'mode' => array(
  3528. 'name' => 'javascript',
  3529. ),
  3530. 'lint' => true,
  3531. 'autoCloseBrackets' => true,
  3532. 'matchBrackets' => true,
  3533. )
  3534. );
  3535. if ( 'application/ld+json' === $type ) {
  3536. $settings['codemirror']['mode']['jsonld'] = true;
  3537. } else {
  3538. $settings['codemirror']['mode']['json'] = true;
  3539. }
  3540. } elseif ( false !== strpos( $type, 'jsx' ) ) {
  3541. $settings['codemirror'] = array_merge(
  3542. $settings['codemirror'],
  3543. array(
  3544. 'mode' => 'jsx',
  3545. 'autoCloseBrackets' => true,
  3546. 'matchBrackets' => true,
  3547. )
  3548. );
  3549. } elseif ( 'text/x-markdown' === $type ) {
  3550. $settings['codemirror'] = array_merge(
  3551. $settings['codemirror'],
  3552. array(
  3553. 'mode' => 'markdown',
  3554. 'highlightFormatting' => true,
  3555. )
  3556. );
  3557. } elseif ( 'text/nginx' === $type ) {
  3558. $settings['codemirror'] = array_merge(
  3559. $settings['codemirror'],
  3560. array(
  3561. 'mode' => 'nginx',
  3562. )
  3563. );
  3564. } elseif ( 'application/x-httpd-php' === $type ) {
  3565. $settings['codemirror'] = array_merge(
  3566. $settings['codemirror'],
  3567. array(
  3568. 'mode' => 'php',
  3569. 'autoCloseBrackets' => true,
  3570. 'autoCloseTags' => true,
  3571. 'matchBrackets' => true,
  3572. 'matchTags' => array(
  3573. 'bothTags' => true,
  3574. ),
  3575. )
  3576. );
  3577. } elseif ( 'text/x-sql' === $type || 'text/x-mysql' === $type ) {
  3578. $settings['codemirror'] = array_merge(
  3579. $settings['codemirror'],
  3580. array(
  3581. 'mode' => 'sql',
  3582. 'autoCloseBrackets' => true,
  3583. 'matchBrackets' => true,
  3584. )
  3585. );
  3586. } elseif ( false !== strpos( $type, 'xml' ) ) {
  3587. $settings['codemirror'] = array_merge(
  3588. $settings['codemirror'],
  3589. array(
  3590. 'mode' => 'xml',
  3591. 'autoCloseBrackets' => true,
  3592. 'autoCloseTags' => true,
  3593. 'matchTags' => array(
  3594. 'bothTags' => true,
  3595. ),
  3596. )
  3597. );
  3598. } elseif ( 'text/x-yaml' === $type ) {
  3599. $settings['codemirror'] = array_merge(
  3600. $settings['codemirror'],
  3601. array(
  3602. 'mode' => 'yaml',
  3603. )
  3604. );
  3605. } else {
  3606. $settings['codemirror']['mode'] = $type;
  3607. }
  3608. if ( ! empty( $settings['codemirror']['lint'] ) ) {
  3609. $settings['codemirror']['gutters'][] = 'CodeMirror-lint-markers';
  3610. }
  3611. // Let settings supplied via args override any defaults.
  3612. foreach ( wp_array_slice_assoc( $args, array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ) ) as $key => $value ) {
  3613. $settings[ $key ] = array_merge(
  3614. $settings[ $key ],
  3615. $value
  3616. );
  3617. }
  3618. /**
  3619. * Filters settings that are passed into the code editor.
  3620. *
  3621. * Returning a falsey value will disable the syntax-highlighting code editor.
  3622. *
  3623. * @since 4.9.0
  3624. *
  3625. * @param array $settings The array of settings passed to the code editor.
  3626. * A falsey value disables the editor.
  3627. * @param array $args {
  3628. * Args passed when calling `get_code_editor_settings()`.
  3629. *
  3630. * @type string $type The MIME type of the file to be edited.
  3631. * @type string $file Filename being edited.
  3632. * @type WP_Theme $theme Theme being edited when on the theme file editor.
  3633. * @type string $plugin Plugin being edited when on the plugin file editor.
  3634. * @type array $codemirror Additional CodeMirror setting overrides.
  3635. * @type array $csslint CSSLint rule overrides.
  3636. * @type array $jshint JSHint rule overrides.
  3637. * @type array $htmlhint HTMLHint rule overrides.
  3638. * }
  3639. */
  3640. return apply_filters( 'wp_code_editor_settings', $settings, $args );
  3641. }
  3642. /**
  3643. * Retrieves the contents of the search WordPress query variable.
  3644. *
  3645. * The search query string is passed through esc_attr() to ensure that it is safe
  3646. * for placing in an HTML attribute.
  3647. *
  3648. * @since 2.3.0
  3649. *
  3650. * @param bool $escaped Whether the result is escaped. Default true.
  3651. * Only use when you are later escaping it. Do not use unescaped.
  3652. * @return string
  3653. */
  3654. function get_search_query( $escaped = true ) {
  3655. /**
  3656. * Filters the contents of the search query variable.
  3657. *
  3658. * @since 2.3.0
  3659. *
  3660. * @param mixed $search Contents of the search query variable.
  3661. */
  3662. $query = apply_filters( 'get_search_query', get_query_var( 's' ) );
  3663. if ( $escaped ) {
  3664. $query = esc_attr( $query );
  3665. }
  3666. return $query;
  3667. }
  3668. /**
  3669. * Displays the contents of the search query variable.
  3670. *
  3671. * The search query string is passed through esc_attr() to ensure that it is safe
  3672. * for placing in an HTML attribute.
  3673. *
  3674. * @since 2.1.0
  3675. */
  3676. function the_search_query() {
  3677. /**
  3678. * Filters the contents of the search query variable for display.
  3679. *
  3680. * @since 2.3.0
  3681. *
  3682. * @param mixed $search Contents of the search query variable.
  3683. */
  3684. echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
  3685. }
  3686. /**
  3687. * Gets the language attributes for the 'html' tag.
  3688. *
  3689. * Builds up a set of HTML attributes containing the text direction and language
  3690. * information for the page.
  3691. *
  3692. * @since 4.3.0
  3693. *
  3694. * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'.
  3695. */
  3696. function get_language_attributes( $doctype = 'html' ) {
  3697. $attributes = array();
  3698. if ( function_exists( 'is_rtl' ) && is_rtl() ) {
  3699. $attributes[] = 'dir="rtl"';
  3700. }
  3701. $lang = get_bloginfo( 'language' );
  3702. if ( $lang ) {
  3703. if ( 'text/html' === get_option( 'html_type' ) || 'html' === $doctype ) {
  3704. $attributes[] = 'lang="' . esc_attr( $lang ) . '"';
  3705. }
  3706. if ( 'text/html' !== get_option( 'html_type' ) || 'xhtml' === $doctype ) {
  3707. $attributes[] = 'xml:lang="' . esc_attr( $lang ) . '"';
  3708. }
  3709. }
  3710. $output = implode( ' ', $attributes );
  3711. /**
  3712. * Filters the language attributes for display in the 'html' tag.
  3713. *
  3714. * @since 2.5.0
  3715. * @since 4.3.0 Added the `$doctype` parameter.
  3716. *
  3717. * @param string $output A space-separated list of language attributes.
  3718. * @param string $doctype The type of HTML document (xhtml|html).
  3719. */
  3720. return apply_filters( 'language_attributes', $output, $doctype );
  3721. }
  3722. /**
  3723. * Displays the language attributes for the 'html' tag.
  3724. *
  3725. * Builds up a set of HTML attributes containing the text direction and language
  3726. * information for the page.
  3727. *
  3728. * @since 2.1.0
  3729. * @since 4.3.0 Converted into a wrapper for get_language_attributes().
  3730. *
  3731. * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'.
  3732. */
  3733. function language_attributes( $doctype = 'html' ) {
  3734. echo get_language_attributes( $doctype );
  3735. }
  3736. /**
  3737. * Retrieves paginated links for archive post pages.
  3738. *
  3739. * Technically, the function can be used to create paginated link list for any
  3740. * area. The 'base' argument is used to reference the url, which will be used to
  3741. * create the paginated links. The 'format' argument is then used for replacing
  3742. * the page number. It is however, most likely and by default, to be used on the
  3743. * archive post pages.
  3744. *
  3745. * The 'type' argument controls format of the returned value. The default is
  3746. * 'plain', which is just a string with the links separated by a newline
  3747. * character. The other possible values are either 'array' or 'list'. The
  3748. * 'array' value will return an array of the paginated link list to offer full
  3749. * control of display. The 'list' value will place all of the paginated links in
  3750. * an unordered HTML list.
  3751. *
  3752. * The 'total' argument is the total amount of pages and is an integer. The
  3753. * 'current' argument is the current page number and is also an integer.
  3754. *
  3755. * An example of the 'base' argument is "http://example.com/all_posts.php%_%"
  3756. * and the '%_%' is required. The '%_%' will be replaced by the contents of in
  3757. * the 'format' argument. An example for the 'format' argument is "?page=%#%"
  3758. * and the '%#%' is also required. The '%#%' will be replaced with the page
  3759. * number.
  3760. *
  3761. * You can include the previous and next links in the list by setting the
  3762. * 'prev_next' argument to true, which it is by default. You can set the
  3763. * previous text, by using the 'prev_text' argument. You can set the next text
  3764. * by setting the 'next_text' argument.
  3765. *
  3766. * If the 'show_all' argument is set to true, then it will show all of the pages
  3767. * instead of a short list of the pages near the current page. By default, the
  3768. * 'show_all' is set to false and controlled by the 'end_size' and 'mid_size'
  3769. * arguments. The 'end_size' argument is how many numbers on either the start
  3770. * and the end list edges, by default is 1. The 'mid_size' argument is how many
  3771. * numbers to either side of current page, but not including current page.
  3772. *
  3773. * It is possible to add query vars to the link by using the 'add_args' argument
  3774. * and see add_query_arg() for more information.
  3775. *
  3776. * The 'before_page_number' and 'after_page_number' arguments allow users to
  3777. * augment the links themselves. Typically this might be to add context to the
  3778. * numbered links so that screen reader users understand what the links are for.
  3779. * The text strings are added before and after the page number - within the
  3780. * anchor tag.
  3781. *
  3782. * @since 2.1.0
  3783. * @since 4.9.0 Added the `aria_current` argument.
  3784. *
  3785. * @global WP_Query $wp_query WordPress Query object.
  3786. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  3787. *
  3788. * @param string|array $args {
  3789. * Optional. Array or string of arguments for generating paginated links for archives.
  3790. *
  3791. * @type string $base Base of the paginated url. Default empty.
  3792. * @type string $format Format for the pagination structure. Default empty.
  3793. * @type int $total The total amount of pages. Default is the value WP_Query's
  3794. * `max_num_pages` or 1.
  3795. * @type int $current The current page number. Default is 'paged' query var or 1.
  3796. * @type string $aria_current The value for the aria-current attribute. Possible values are 'page',
  3797. * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'.
  3798. * @type bool $show_all Whether to show all pages. Default false.
  3799. * @type int $end_size How many numbers on either the start and the end list edges.
  3800. * Default 1.
  3801. * @type int $mid_size How many numbers to either side of the current pages. Default 2.
  3802. * @type bool $prev_next Whether to include the previous and next links in the list. Default true.
  3803. * @type bool $prev_text The previous page text. Default '&laquo; Previous'.
  3804. * @type bool $next_text The next page text. Default 'Next &raquo;'.
  3805. * @type string $type Controls format of the returned value. Possible values are 'plain',
  3806. * 'array' and 'list'. Default is 'plain'.
  3807. * @type array $add_args An array of query args to add. Default false.
  3808. * @type string $add_fragment A string to append to each link. Default empty.
  3809. * @type string $before_page_number A string to appear before the page number. Default empty.
  3810. * @type string $after_page_number A string to append after the page number. Default empty.
  3811. * }
  3812. * @return string|array|void String of page links or array of page links, depending on 'type' argument.
  3813. * Void if total number of pages is less than 2.
  3814. */
  3815. function paginate_links( $args = '' ) {
  3816. global $wp_query, $wp_rewrite;
  3817. // Setting up default values based on the current URL.
  3818. $pagenum_link = html_entity_decode( get_pagenum_link() );
  3819. $url_parts = explode( '?', $pagenum_link );
  3820. // Get max pages and current page out of the current query, if available.
  3821. $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
  3822. $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
  3823. // Append the format placeholder to the base URL.
  3824. $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
  3825. // URL base depends on permalink settings.
  3826. $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
  3827. $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
  3828. $defaults = array(
  3829. 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below).
  3830. 'format' => $format, // ?page=%#% : %#% is replaced by the page number.
  3831. 'total' => $total,
  3832. 'current' => $current,
  3833. 'aria_current' => 'page',
  3834. 'show_all' => false,
  3835. 'prev_next' => true,
  3836. 'prev_text' => __( '&laquo; Previous' ),
  3837. 'next_text' => __( 'Next &raquo;' ),
  3838. 'end_size' => 1,
  3839. 'mid_size' => 2,
  3840. 'type' => 'plain',
  3841. 'add_args' => array(), // Array of query args to add.
  3842. 'add_fragment' => '',
  3843. 'before_page_number' => '',
  3844. 'after_page_number' => '',
  3845. );
  3846. $args = wp_parse_args( $args, $defaults );
  3847. if ( ! is_array( $args['add_args'] ) ) {
  3848. $args['add_args'] = array();
  3849. }
  3850. // Merge additional query vars found in the original URL into 'add_args' array.
  3851. if ( isset( $url_parts[1] ) ) {
  3852. // Find the format argument.
  3853. $format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
  3854. $format_query = isset( $format[1] ) ? $format[1] : '';
  3855. wp_parse_str( $format_query, $format_args );
  3856. // Find the query args of the requested URL.
  3857. wp_parse_str( $url_parts[1], $url_query_args );
  3858. // Remove the format argument from the array of query arguments, to avoid overwriting custom format.
  3859. foreach ( $format_args as $format_arg => $format_arg_value ) {
  3860. unset( $url_query_args[ $format_arg ] );
  3861. }
  3862. $args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) );
  3863. }
  3864. // Who knows what else people pass in $args.
  3865. $total = (int) $args['total'];
  3866. if ( $total < 2 ) {
  3867. return;
  3868. }
  3869. $current = (int) $args['current'];
  3870. $end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
  3871. if ( $end_size < 1 ) {
  3872. $end_size = 1;
  3873. }
  3874. $mid_size = (int) $args['mid_size'];
  3875. if ( $mid_size < 0 ) {
  3876. $mid_size = 2;
  3877. }
  3878. $add_args = $args['add_args'];
  3879. $r = '';
  3880. $page_links = array();
  3881. $dots = false;
  3882. if ( $args['prev_next'] && $current && 1 < $current ) :
  3883. $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
  3884. $link = str_replace( '%#%', $current - 1, $link );
  3885. if ( $add_args ) {
  3886. $link = add_query_arg( $add_args, $link );
  3887. }
  3888. $link .= $args['add_fragment'];
  3889. $page_links[] = sprintf(
  3890. '<a class="prev page-numbers" href="%s">%s</a>',
  3891. /**
  3892. * Filters the paginated links for the given archive pages.
  3893. *
  3894. * @since 3.0.0
  3895. *
  3896. * @param string $link The paginated link URL.
  3897. */
  3898. esc_url( apply_filters( 'paginate_links', $link ) ),
  3899. $args['prev_text']
  3900. );
  3901. endif;
  3902. for ( $n = 1; $n <= $total; $n++ ) :
  3903. if ( $n == $current ) :
  3904. $page_links[] = sprintf(
  3905. '<span aria-current="%s" class="page-numbers current">%s</span>',
  3906. esc_attr( $args['aria_current'] ),
  3907. $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
  3908. );
  3909. $dots = true;
  3910. else :
  3911. if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
  3912. $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
  3913. $link = str_replace( '%#%', $n, $link );
  3914. if ( $add_args ) {
  3915. $link = add_query_arg( $add_args, $link );
  3916. }
  3917. $link .= $args['add_fragment'];
  3918. $page_links[] = sprintf(
  3919. '<a class="page-numbers" href="%s">%s</a>',
  3920. /** This filter is documented in wp-includes/general-template.php */
  3921. esc_url( apply_filters( 'paginate_links', $link ) ),
  3922. $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
  3923. );
  3924. $dots = true;
  3925. elseif ( $dots && ! $args['show_all'] ) :
  3926. $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
  3927. $dots = false;
  3928. endif;
  3929. endif;
  3930. endfor;
  3931. if ( $args['prev_next'] && $current && $current < $total ) :
  3932. $link = str_replace( '%_%', $args['format'], $args['base'] );
  3933. $link = str_replace( '%#%', $current + 1, $link );
  3934. if ( $add_args ) {
  3935. $link = add_query_arg( $add_args, $link );
  3936. }
  3937. $link .= $args['add_fragment'];
  3938. $page_links[] = sprintf(
  3939. '<a class="next page-numbers" href="%s">%s</a>',
  3940. /** This filter is documented in wp-includes/general-template.php */
  3941. esc_url( apply_filters( 'paginate_links', $link ) ),
  3942. $args['next_text']
  3943. );
  3944. endif;
  3945. switch ( $args['type'] ) {
  3946. case 'array':
  3947. return $page_links;
  3948. case 'list':
  3949. $r .= "<ul class='page-numbers'>\n\t<li>";
  3950. $r .= implode( "</li>\n\t<li>", $page_links );
  3951. $r .= "</li>\n</ul>\n";
  3952. break;
  3953. default:
  3954. $r = implode( "\n", $page_links );
  3955. break;
  3956. }
  3957. /**
  3958. * Filters the HTML output of paginated links for archives.
  3959. *
  3960. * @since 5.7.0
  3961. *
  3962. * @param string $r HTML output.
  3963. * @param array $args An array of arguments. See paginate_links()
  3964. * for information on accepted arguments.
  3965. */
  3966. $r = apply_filters( 'paginate_links_output', $r, $args );
  3967. return $r;
  3968. }
  3969. /**
  3970. * Registers an admin color scheme css file.
  3971. *
  3972. * Allows a plugin to register a new admin color scheme. For example:
  3973. *
  3974. * wp_admin_css_color( 'classic', __( 'Classic' ), admin_url( "css/colors-classic.css" ), array(
  3975. * '#07273E', '#14568A', '#D54E21', '#2683AE'
  3976. * ) );
  3977. *
  3978. * @since 2.5.0
  3979. *
  3980. * @global array $_wp_admin_css_colors
  3981. *
  3982. * @param string $key The unique key for this theme.
  3983. * @param string $name The name of the theme.
  3984. * @param string $url The URL of the CSS file containing the color scheme.
  3985. * @param array $colors Optional. An array of CSS color definition strings which are used
  3986. * to give the user a feel for the theme.
  3987. * @param array $icons {
  3988. * Optional. CSS color definitions used to color any SVG icons.
  3989. *
  3990. * @type string $base SVG icon base color.
  3991. * @type string $focus SVG icon color on focus.
  3992. * @type string $current SVG icon color of current admin menu link.
  3993. * }
  3994. */
  3995. function wp_admin_css_color( $key, $name, $url, $colors = array(), $icons = array() ) {
  3996. global $_wp_admin_css_colors;
  3997. if ( ! isset( $_wp_admin_css_colors ) ) {
  3998. $_wp_admin_css_colors = array();
  3999. }
  4000. $_wp_admin_css_colors[ $key ] = (object) array(
  4001. 'name' => $name,
  4002. 'url' => $url,
  4003. 'colors' => $colors,
  4004. 'icon_colors' => $icons,
  4005. );
  4006. }
  4007. /**
  4008. * Registers the default admin color schemes.
  4009. *
  4010. * Registers the initial set of eight color schemes in the Profile section
  4011. * of the dashboard which allows for styling the admin menu and toolbar.
  4012. *
  4013. * @see wp_admin_css_color()
  4014. *
  4015. * @since 3.0.0
  4016. */
  4017. function register_admin_color_schemes() {
  4018. $suffix = is_rtl() ? '-rtl' : '';
  4019. $suffix .= SCRIPT_DEBUG ? '' : '.min';
  4020. wp_admin_css_color(
  4021. 'fresh',
  4022. _x( 'Default', 'admin color scheme' ),
  4023. false,
  4024. array( '#1d2327', '#2c3338', '#2271b1', '#72aee6' ),
  4025. array(
  4026. 'base' => '#a7aaad',
  4027. 'focus' => '#72aee6',
  4028. 'current' => '#fff',
  4029. )
  4030. );
  4031. wp_admin_css_color(
  4032. 'light',
  4033. _x( 'Light', 'admin color scheme' ),
  4034. admin_url( "css/colors/light/colors$suffix.css" ),
  4035. array( '#e5e5e5', '#999', '#d64e07', '#04a4cc' ),
  4036. array(
  4037. 'base' => '#999',
  4038. 'focus' => '#ccc',
  4039. 'current' => '#ccc',
  4040. )
  4041. );
  4042. wp_admin_css_color(
  4043. 'modern',
  4044. _x( 'Modern', 'admin color scheme' ),
  4045. admin_url( "css/colors/modern/colors$suffix.css" ),
  4046. array( '#1e1e1e', '#3858e9', '#33f078' ),
  4047. array(
  4048. 'base' => '#f3f1f1',
  4049. 'focus' => '#fff',
  4050. 'current' => '#fff',
  4051. )
  4052. );
  4053. wp_admin_css_color(
  4054. 'blue',
  4055. _x( 'Blue', 'admin color scheme' ),
  4056. admin_url( "css/colors/blue/colors$suffix.css" ),
  4057. array( '#096484', '#4796b3', '#52accc', '#74B6CE' ),
  4058. array(
  4059. 'base' => '#e5f8ff',
  4060. 'focus' => '#fff',
  4061. 'current' => '#fff',
  4062. )
  4063. );
  4064. wp_admin_css_color(
  4065. 'midnight',
  4066. _x( 'Midnight', 'admin color scheme' ),
  4067. admin_url( "css/colors/midnight/colors$suffix.css" ),
  4068. array( '#25282b', '#363b3f', '#69a8bb', '#e14d43' ),
  4069. array(
  4070. 'base' => '#f1f2f3',
  4071. 'focus' => '#fff',
  4072. 'current' => '#fff',
  4073. )
  4074. );
  4075. wp_admin_css_color(
  4076. 'sunrise',
  4077. _x( 'Sunrise', 'admin color scheme' ),
  4078. admin_url( "css/colors/sunrise/colors$suffix.css" ),
  4079. array( '#b43c38', '#cf4944', '#dd823b', '#ccaf0b' ),
  4080. array(
  4081. 'base' => '#f3f1f1',
  4082. 'focus' => '#fff',
  4083. 'current' => '#fff',
  4084. )
  4085. );
  4086. wp_admin_css_color(
  4087. 'ectoplasm',
  4088. _x( 'Ectoplasm', 'admin color scheme' ),
  4089. admin_url( "css/colors/ectoplasm/colors$suffix.css" ),
  4090. array( '#413256', '#523f6d', '#a3b745', '#d46f15' ),
  4091. array(
  4092. 'base' => '#ece6f6',
  4093. 'focus' => '#fff',
  4094. 'current' => '#fff',
  4095. )
  4096. );
  4097. wp_admin_css_color(
  4098. 'ocean',
  4099. _x( 'Ocean', 'admin color scheme' ),
  4100. admin_url( "css/colors/ocean/colors$suffix.css" ),
  4101. array( '#627c83', '#738e96', '#9ebaa0', '#aa9d88' ),
  4102. array(
  4103. 'base' => '#f2fcff',
  4104. 'focus' => '#fff',
  4105. 'current' => '#fff',
  4106. )
  4107. );
  4108. wp_admin_css_color(
  4109. 'coffee',
  4110. _x( 'Coffee', 'admin color scheme' ),
  4111. admin_url( "css/colors/coffee/colors$suffix.css" ),
  4112. array( '#46403c', '#59524c', '#c7a589', '#9ea476' ),
  4113. array(
  4114. 'base' => '#f3f2f1',
  4115. 'focus' => '#fff',
  4116. 'current' => '#fff',
  4117. )
  4118. );
  4119. }
  4120. /**
  4121. * Displays the URL of a WordPress admin CSS file.
  4122. *
  4123. * @see WP_Styles::_css_href and its {@see 'style_loader_src'} filter.
  4124. *
  4125. * @since 2.3.0
  4126. *
  4127. * @param string $file file relative to wp-admin/ without its ".css" extension.
  4128. * @return string
  4129. */
  4130. function wp_admin_css_uri( $file = 'wp-admin' ) {
  4131. if ( defined( 'WP_INSTALLING' ) ) {
  4132. $_file = "./$file.css";
  4133. } else {
  4134. $_file = admin_url( "$file.css" );
  4135. }
  4136. $_file = add_query_arg( 'version', get_bloginfo( 'version' ), $_file );
  4137. /**
  4138. * Filters the URI of a WordPress admin CSS file.
  4139. *
  4140. * @since 2.3.0
  4141. *
  4142. * @param string $_file Relative path to the file with query arguments attached.
  4143. * @param string $file Relative path to the file, minus its ".css" extension.
  4144. */
  4145. return apply_filters( 'wp_admin_css_uri', $_file, $file );
  4146. }
  4147. /**
  4148. * Enqueues or directly prints a stylesheet link to the specified CSS file.
  4149. *
  4150. * "Intelligently" decides to enqueue or to print the CSS file. If the
  4151. * {@see 'wp_print_styles'} action has *not* yet been called, the CSS file will be
  4152. * enqueued. If the {@see 'wp_print_styles'} action has been called, the CSS link will
  4153. * be printed. Printing may be forced by passing true as the $force_echo
  4154. * (second) parameter.
  4155. *
  4156. * For backward compatibility with WordPress 2.3 calling method: If the $file
  4157. * (first) parameter does not correspond to a registered CSS file, we assume
  4158. * $file is a file relative to wp-admin/ without its ".css" extension. A
  4159. * stylesheet link to that generated URL is printed.
  4160. *
  4161. * @since 2.3.0
  4162. *
  4163. * @param string $file Optional. Style handle name or file name (without ".css" extension) relative
  4164. * to wp-admin/. Defaults to 'wp-admin'.
  4165. * @param bool $force_echo Optional. Force the stylesheet link to be printed rather than enqueued.
  4166. */
  4167. function wp_admin_css( $file = 'wp-admin', $force_echo = false ) {
  4168. // For backward compatibility.
  4169. $handle = 0 === strpos( $file, 'css/' ) ? substr( $file, 4 ) : $file;
  4170. if ( wp_styles()->query( $handle ) ) {
  4171. if ( $force_echo || did_action( 'wp_print_styles' ) ) {
  4172. // We already printed the style queue. Print this one immediately.
  4173. wp_print_styles( $handle );
  4174. } else {
  4175. // Add to style queue.
  4176. wp_enqueue_style( $handle );
  4177. }
  4178. return;
  4179. }
  4180. $stylesheet_link = sprintf(
  4181. "<link rel='stylesheet' href='%s' type='text/css' />\n",
  4182. esc_url( wp_admin_css_uri( $file ) )
  4183. );
  4184. /**
  4185. * Filters the stylesheet link to the specified CSS file.
  4186. *
  4187. * If the site is set to display right-to-left, the RTL stylesheet link
  4188. * will be used instead.
  4189. *
  4190. * @since 2.3.0
  4191. * @param string $stylesheet_link HTML link element for the stylesheet.
  4192. * @param string $file Style handle name or filename (without ".css" extension)
  4193. * relative to wp-admin/. Defaults to 'wp-admin'.
  4194. */
  4195. echo apply_filters( 'wp_admin_css', $stylesheet_link, $file );
  4196. if ( function_exists( 'is_rtl' ) && is_rtl() ) {
  4197. $rtl_stylesheet_link = sprintf(
  4198. "<link rel='stylesheet' href='%s' type='text/css' />\n",
  4199. esc_url( wp_admin_css_uri( "$file-rtl" ) )
  4200. );
  4201. /** This filter is documented in wp-includes/general-template.php */
  4202. echo apply_filters( 'wp_admin_css', $rtl_stylesheet_link, "$file-rtl" );
  4203. }
  4204. }
  4205. /**
  4206. * Enqueues the default ThickBox js and css.
  4207. *
  4208. * If any of the settings need to be changed, this can be done with another js
  4209. * file similar to media-upload.js. That file should
  4210. * require array('thickbox') to ensure it is loaded after.
  4211. *
  4212. * @since 2.5.0
  4213. */
  4214. function add_thickbox() {
  4215. wp_enqueue_script( 'thickbox' );
  4216. wp_enqueue_style( 'thickbox' );
  4217. if ( is_network_admin() ) {
  4218. add_action( 'admin_head', '_thickbox_path_admin_subfolder' );
  4219. }
  4220. }
  4221. /**
  4222. * Displays the XHTML generator that is generated on the wp_head hook.
  4223. *
  4224. * See {@see 'wp_head'}.
  4225. *
  4226. * @since 2.5.0
  4227. */
  4228. function wp_generator() {
  4229. /**
  4230. * Filters the output of the XHTML generator tag.
  4231. *
  4232. * @since 2.5.0
  4233. *
  4234. * @param string $generator_type The XHTML generator.
  4235. */
  4236. the_generator( apply_filters( 'wp_generator_type', 'xhtml' ) );
  4237. }
  4238. /**
  4239. * Display the generator XML or Comment for RSS, ATOM, etc.
  4240. *
  4241. * Returns the correct generator type for the requested output format. Allows
  4242. * for a plugin to filter generators overall the {@see 'the_generator'} filter.
  4243. *
  4244. * @since 2.5.0
  4245. *
  4246. * @param string $type The type of generator to output - (html|xhtml|atom|rss2|rdf|comment|export).
  4247. */
  4248. function the_generator( $type ) {
  4249. /**
  4250. * Filters the output of the XHTML generator tag for display.
  4251. *
  4252. * @since 2.5.0
  4253. *
  4254. * @param string $generator_type The generator output.
  4255. * @param string $type The type of generator to output. Accepts 'html',
  4256. * 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export'.
  4257. */
  4258. echo apply_filters( 'the_generator', get_the_generator( $type ), $type ) . "\n";
  4259. }
  4260. /**
  4261. * Creates the generator XML or Comment for RSS, ATOM, etc.
  4262. *
  4263. * Returns the correct generator type for the requested output format. Allows
  4264. * for a plugin to filter generators on an individual basis using the
  4265. * {@see 'get_the_generator_$type'} filter.
  4266. *
  4267. * @since 2.5.0
  4268. *
  4269. * @param string $type The type of generator to return - (html|xhtml|atom|rss2|rdf|comment|export).
  4270. * @return string|void The HTML content for the generator.
  4271. */
  4272. function get_the_generator( $type = '' ) {
  4273. if ( empty( $type ) ) {
  4274. $current_filter = current_filter();
  4275. if ( empty( $current_filter ) ) {
  4276. return;
  4277. }
  4278. switch ( $current_filter ) {
  4279. case 'rss2_head':
  4280. case 'commentsrss2_head':
  4281. $type = 'rss2';
  4282. break;
  4283. case 'rss_head':
  4284. case 'opml_head':
  4285. $type = 'comment';
  4286. break;
  4287. case 'rdf_header':
  4288. $type = 'rdf';
  4289. break;
  4290. case 'atom_head':
  4291. case 'comments_atom_head':
  4292. case 'app_head':
  4293. $type = 'atom';
  4294. break;
  4295. }
  4296. }
  4297. switch ( $type ) {
  4298. case 'html':
  4299. $gen = '<meta name="generator" content="WordPress ' . esc_attr( get_bloginfo( 'version' ) ) . '">';
  4300. break;
  4301. case 'xhtml':
  4302. $gen = '<meta name="generator" content="WordPress ' . esc_attr( get_bloginfo( 'version' ) ) . '" />';
  4303. break;
  4304. case 'atom':
  4305. $gen = '<generator uri="https://wordpress.org/" version="' . esc_attr( get_bloginfo_rss( 'version' ) ) . '">WordPress</generator>';
  4306. break;
  4307. case 'rss2':
  4308. $gen = '<generator>' . esc_url_raw( 'https://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) ) . '</generator>';
  4309. break;
  4310. case 'rdf':
  4311. $gen = '<admin:generatorAgent rdf:resource="' . esc_url_raw( 'https://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) ) . '" />';
  4312. break;
  4313. case 'comment':
  4314. $gen = '<!-- generator="WordPress/' . esc_attr( get_bloginfo( 'version' ) ) . '" -->';
  4315. break;
  4316. case 'export':
  4317. $gen = '<!-- generator="WordPress/' . esc_attr( get_bloginfo_rss( 'version' ) ) . '" created="' . gmdate( 'Y-m-d H:i' ) . '" -->';
  4318. break;
  4319. }
  4320. /**
  4321. * Filters the HTML for the retrieved generator type.
  4322. *
  4323. * The dynamic portion of the hook name, `$type`, refers to the generator type.
  4324. *
  4325. * Possible hook names include:
  4326. *
  4327. * - `get_the_generator_atom`
  4328. * - `get_the_generator_comment`
  4329. * - `get_the_generator_export`
  4330. * - `get_the_generator_html`
  4331. * - `get_the_generator_rdf`
  4332. * - `get_the_generator_rss2`
  4333. * - `get_the_generator_xhtml`
  4334. *
  4335. * @since 2.5.0
  4336. *
  4337. * @param string $gen The HTML markup output to wp_head().
  4338. * @param string $type The type of generator. Accepts 'html', 'xhtml', 'atom',
  4339. * 'rss2', 'rdf', 'comment', 'export'.
  4340. */
  4341. return apply_filters( "get_the_generator_{$type}", $gen, $type );
  4342. }
  4343. /**
  4344. * Outputs the HTML checked attribute.
  4345. *
  4346. * Compares the first two arguments and if identical marks as checked.
  4347. *
  4348. * @since 1.0.0
  4349. *
  4350. * @param mixed $checked One of the values to compare.
  4351. * @param mixed $current Optional. The other value to compare if not just true.
  4352. * Default true.
  4353. * @param bool $echo Optional. Whether to echo or just return the string.
  4354. * Default true.
  4355. * @return string HTML attribute or empty string.
  4356. */
  4357. function checked( $checked, $current = true, $echo = true ) {
  4358. return __checked_selected_helper( $checked, $current, $echo, 'checked' );
  4359. }
  4360. /**
  4361. * Outputs the HTML selected attribute.
  4362. *
  4363. * Compares the first two arguments and if identical marks as selected.
  4364. *
  4365. * @since 1.0.0
  4366. *
  4367. * @param mixed $selected One of the values to compare.
  4368. * @param mixed $current Optional. The other value to compare if not just true.
  4369. * Default true.
  4370. * @param bool $echo Optional. Whether to echo or just return the string.
  4371. * Default true.
  4372. * @return string HTML attribute or empty string.
  4373. */
  4374. function selected( $selected, $current = true, $echo = true ) {
  4375. return __checked_selected_helper( $selected, $current, $echo, 'selected' );
  4376. }
  4377. /**
  4378. * Outputs the HTML disabled attribute.
  4379. *
  4380. * Compares the first two arguments and if identical marks as disabled.
  4381. *
  4382. * @since 3.0.0
  4383. *
  4384. * @param mixed $disabled One of the values to compare.
  4385. * @param mixed $current Optional. The other value to compare if not just true.
  4386. * Default true.
  4387. * @param bool $echo Optional. Whether to echo or just return the string.
  4388. * Default true.
  4389. * @return string HTML attribute or empty string.
  4390. */
  4391. function disabled( $disabled, $current = true, $echo = true ) {
  4392. return __checked_selected_helper( $disabled, $current, $echo, 'disabled' );
  4393. }
  4394. /**
  4395. * Outputs the HTML readonly attribute.
  4396. *
  4397. * Compares the first two arguments and if identical marks as readonly.
  4398. *
  4399. * @since 5.9.0
  4400. *
  4401. * @param mixed $readonly One of the values to compare.
  4402. * @param mixed $current Optional. The other value to compare if not just true.
  4403. * Default true.
  4404. * @param bool $echo Optional. Whether to echo or just return the string.
  4405. * Default true.
  4406. * @return string HTML attribute or empty string.
  4407. */
  4408. function wp_readonly( $readonly, $current = true, $echo = true ) {
  4409. return __checked_selected_helper( $readonly, $current, $echo, 'readonly' );
  4410. }
  4411. /*
  4412. * Include a compat `readonly()` function on PHP < 8.1. Since PHP 8.1,
  4413. * `readonly` is a reserved keyword and cannot be used as a function name.
  4414. * In order to avoid PHP parser errors, this function was extracted
  4415. * to a separate file and is only included conditionally on PHP < 8.1.
  4416. */
  4417. if ( PHP_VERSION_ID < 80100 ) {
  4418. require_once __DIR__ . '/php-compat/readonly.php';
  4419. }
  4420. /**
  4421. * Private helper function for checked, selected, disabled and readonly.
  4422. *
  4423. * Compares the first two arguments and if identical marks as `$type`.
  4424. *
  4425. * @since 2.8.0
  4426. * @access private
  4427. *
  4428. * @param mixed $helper One of the values to compare.
  4429. * @param mixed $current The other value to compare if not just true.
  4430. * @param bool $echo Whether to echo or just return the string.
  4431. * @param string $type The type of checked|selected|disabled|readonly we are doing.
  4432. * @return string HTML attribute or empty string.
  4433. */
  4434. function __checked_selected_helper( $helper, $current, $echo, $type ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
  4435. if ( (string) $helper === (string) $current ) {
  4436. $result = " $type='$type'";
  4437. } else {
  4438. $result = '';
  4439. }
  4440. if ( $echo ) {
  4441. echo $result;
  4442. }
  4443. return $result;
  4444. }
  4445. /**
  4446. * Default settings for heartbeat
  4447. *
  4448. * Outputs the nonce used in the heartbeat XHR
  4449. *
  4450. * @since 3.6.0
  4451. *
  4452. * @param array $settings
  4453. * @return array Heartbeat settings.
  4454. */
  4455. function wp_heartbeat_settings( $settings ) {
  4456. if ( ! is_admin() ) {
  4457. $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' );
  4458. }
  4459. if ( is_user_logged_in() ) {
  4460. $settings['nonce'] = wp_create_nonce( 'heartbeat-nonce' );
  4461. }
  4462. return $settings;
  4463. }