PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post-template.php

http://github.com/wordpress/wordpress
PHP | 1951 lines | 1345 code | 143 blank | 463 comment | 152 complexity | 02fcbdd8202b36ea40760127eefb83db MD5 | raw file
Possible License(s): 0BSD

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress Post Template Functions.
  4. *
  5. * Gets content for the current post in the loop.
  6. *
  7. * @package WordPress
  8. * @subpackage Template
  9. */
  10. /**
  11. * Display the ID of the current item in the WordPress Loop.
  12. *
  13. * @since 0.71
  14. */
  15. function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  16. echo get_the_ID();
  17. }
  18. /**
  19. * Retrieve the ID of the current item in the WordPress Loop.
  20. *
  21. * @since 2.1.0
  22. *
  23. * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set.
  24. */
  25. function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  26. $post = get_post();
  27. return ! empty( $post ) ? $post->ID : false;
  28. }
  29. /**
  30. * Display or retrieve the current post title with optional markup.
  31. *
  32. * @since 0.71
  33. *
  34. * @param string $before Optional. Markup to prepend to the title. Default empty.
  35. * @param string $after Optional. Markup to append to the title. Default empty.
  36. * @param bool $echo Optional. Whether to echo or return the title. Default true for echo.
  37. * @return void|string Void if `$echo` argument is true, current post title if `$echo` is false.
  38. */
  39. function the_title( $before = '', $after = '', $echo = true ) {
  40. $title = get_the_title();
  41. if ( strlen( $title ) == 0 ) {
  42. return;
  43. }
  44. $title = $before . $title . $after;
  45. if ( $echo ) {
  46. echo $title;
  47. } else {
  48. return $title;
  49. }
  50. }
  51. /**
  52. * Sanitize the current title when retrieving or displaying.
  53. *
  54. * Works like the_title(), except the parameters can be in a string or
  55. * an array. See the function for what can be override in the $args parameter.
  56. *
  57. * The title before it is displayed will have the tags stripped and esc_attr()
  58. * before it is passed to the user or displayed. The default as with the_title(),
  59. * is to display the title.
  60. *
  61. * @since 2.3.0
  62. *
  63. * @param string|array $args {
  64. * Title attribute arguments. Optional.
  65. *
  66. * @type string $before Markup to prepend to the title. Default empty.
  67. * @type string $after Markup to append to the title. Default empty.
  68. * @type bool $echo Whether to echo or return the title. Default true for echo.
  69. * @type WP_Post $post Current post object to retrieve the title for.
  70. * }
  71. * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
  72. */
  73. function the_title_attribute( $args = '' ) {
  74. $defaults = array(
  75. 'before' => '',
  76. 'after' => '',
  77. 'echo' => true,
  78. 'post' => get_post(),
  79. );
  80. $parsed_args = wp_parse_args( $args, $defaults );
  81. $title = get_the_title( $parsed_args['post'] );
  82. if ( strlen( $title ) == 0 ) {
  83. return;
  84. }
  85. $title = $parsed_args['before'] . $title . $parsed_args['after'];
  86. $title = esc_attr( strip_tags( $title ) );
  87. if ( $parsed_args['echo'] ) {
  88. echo $title;
  89. } else {
  90. return $title;
  91. }
  92. }
  93. /**
  94. * Retrieve post title.
  95. *
  96. * If the post is protected and the visitor is not an admin, then "Protected"
  97. * will be displayed before the post title. If the post is private, then
  98. * "Private" will be located before the post title.
  99. *
  100. * @since 0.71
  101. *
  102. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  103. * @return string
  104. */
  105. function get_the_title( $post = 0 ) {
  106. $post = get_post( $post );
  107. $title = isset( $post->post_title ) ? $post->post_title : '';
  108. $id = isset( $post->ID ) ? $post->ID : 0;
  109. if ( ! is_admin() ) {
  110. if ( ! empty( $post->post_password ) ) {
  111. /* translators: %s: Protected post title. */
  112. $prepend = __( 'Protected: %s' );
  113. /**
  114. * Filters the text prepended to the post title for protected posts.
  115. *
  116. * The filter is only applied on the front end.
  117. *
  118. * @since 2.8.0
  119. *
  120. * @param string $prepend Text displayed before the post title.
  121. * Default 'Protected: %s'.
  122. * @param WP_Post $post Current post object.
  123. */
  124. $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );
  125. $title = sprintf( $protected_title_format, $title );
  126. } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
  127. /* translators: %s: Private post title. */
  128. $prepend = __( 'Private: %s' );
  129. /**
  130. * Filters the text prepended to the post title of private posts.
  131. *
  132. * The filter is only applied on the front end.
  133. *
  134. * @since 2.8.0
  135. *
  136. * @param string $prepend Text displayed before the post title.
  137. * Default 'Private: %s'.
  138. * @param WP_Post $post Current post object.
  139. */
  140. $private_title_format = apply_filters( 'private_title_format', $prepend, $post );
  141. $title = sprintf( $private_title_format, $title );
  142. }
  143. }
  144. /**
  145. * Filters the post title.
  146. *
  147. * @since 0.71
  148. *
  149. * @param string $title The post title.
  150. * @param int $id The post ID.
  151. */
  152. return apply_filters( 'the_title', $title, $id );
  153. }
  154. /**
  155. * Display the Post Global Unique Identifier (guid).
  156. *
  157. * The guid will appear to be a link, but should not be used as a link to the
  158. * post. The reason you should not use it as a link, is because of moving the
  159. * blog across domains.
  160. *
  161. * URL is escaped to make it XML-safe.
  162. *
  163. * @since 1.5.0
  164. *
  165. * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
  166. */
  167. function the_guid( $post = 0 ) {
  168. $post = get_post( $post );
  169. $guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
  170. $id = isset( $post->ID ) ? $post->ID : 0;
  171. /**
  172. * Filters the escaped Global Unique Identifier (guid) of the post.
  173. *
  174. * @since 4.2.0
  175. *
  176. * @see get_the_guid()
  177. *
  178. * @param string $guid Escaped Global Unique Identifier (guid) of the post.
  179. * @param int $id The post ID.
  180. */
  181. echo apply_filters( 'the_guid', $guid, $id );
  182. }
  183. /**
  184. * Retrieve the Post Global Unique Identifier (guid).
  185. *
  186. * The guid will appear to be a link, but should not be used as an link to the
  187. * post. The reason you should not use it as a link, is because of moving the
  188. * blog across domains.
  189. *
  190. * @since 1.5.0
  191. *
  192. * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
  193. * @return string
  194. */
  195. function get_the_guid( $post = 0 ) {
  196. $post = get_post( $post );
  197. $guid = isset( $post->guid ) ? $post->guid : '';
  198. $id = isset( $post->ID ) ? $post->ID : 0;
  199. /**
  200. * Filters the Global Unique Identifier (guid) of the post.
  201. *
  202. * @since 1.5.0
  203. *
  204. * @param string $guid Global Unique Identifier (guid) of the post.
  205. * @param int $id The post ID.
  206. */
  207. return apply_filters( 'get_the_guid', $guid, $id );
  208. }
  209. /**
  210. * Display the post content.
  211. *
  212. * @since 0.71
  213. *
  214. * @param string $more_link_text Optional. Content for when there is more text.
  215. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
  216. */
  217. function the_content( $more_link_text = null, $strip_teaser = false ) {
  218. $content = get_the_content( $more_link_text, $strip_teaser );
  219. /**
  220. * Filters the post content.
  221. *
  222. * @since 0.71
  223. *
  224. * @param string $content Content of the current post.
  225. */
  226. $content = apply_filters( 'the_content', $content );
  227. $content = str_replace( ']]>', ']]&gt;', $content );
  228. echo $content;
  229. }
  230. /**
  231. * Retrieve the post content.
  232. *
  233. * @since 0.71
  234. * @since 5.2.0 Added the `$post` parameter.
  235. *
  236. * @global int $page Page number of a single post/page.
  237. * @global int $more Boolean indicator for whether single post/page is being viewed.
  238. * @global bool $preview Whether post/page is in preview mode.
  239. * @global array $pages Array of all pages in post/page. Each array element contains
  240. * part of the content separated by the `<!--nextpage-->` tag.
  241. * @global int $multipage Boolean indicator for whether multiple pages are in play.
  242. *
  243. * @param string $more_link_text Optional. Content for when there is more text.
  244. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
  245. * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default is null.
  246. * @return string
  247. */
  248. function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) {
  249. global $page, $more, $preview, $pages, $multipage;
  250. $_post = get_post( $post );
  251. if ( ! ( $_post instanceof WP_Post ) ) {
  252. return '';
  253. }
  254. if ( null === $post ) {
  255. $elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
  256. } else {
  257. $elements = generate_postdata( $_post );
  258. }
  259. if ( null === $more_link_text ) {
  260. $more_link_text = sprintf(
  261. '<span aria-label="%1$s">%2$s</span>',
  262. sprintf(
  263. /* translators: %s: Post title. */
  264. __( 'Continue reading %s' ),
  265. the_title_attribute(
  266. array(
  267. 'echo' => false,
  268. 'post' => $_post,
  269. )
  270. )
  271. ),
  272. __( '(more&hellip;)' )
  273. );
  274. }
  275. $output = '';
  276. $has_teaser = false;
  277. // If post password required and it doesn't match the cookie.
  278. if ( post_password_required( $_post ) ) {
  279. return get_the_password_form( $_post );
  280. }
  281. // If the requested page doesn't exist.
  282. if ( $elements['page'] > count( $elements['pages'] ) ) {
  283. // Give them the highest numbered page that DOES exist.
  284. $elements['page'] = count( $elements['pages'] );
  285. }
  286. $page_no = $elements['page'];
  287. $content = $elements['pages'][ $page_no - 1 ];
  288. if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
  289. if ( has_block( 'more', $content ) ) {
  290. // Remove the core/more block delimiters. They will be left over after $content is split up.
  291. $content = preg_replace( '/<!-- \/?wp:more(.*?) -->/', '', $content );
  292. }
  293. $content = explode( $matches[0], $content, 2 );
  294. if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
  295. $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
  296. }
  297. $has_teaser = true;
  298. } else {
  299. $content = array( $content );
  300. }
  301. if ( false !== strpos( $_post->post_content, '<!--noteaser-->' ) && ( ! $elements['multipage'] || 1 == $elements['page'] ) ) {
  302. $strip_teaser = true;
  303. }
  304. $teaser = $content[0];
  305. if ( $elements['more'] && $strip_teaser && $has_teaser ) {
  306. $teaser = '';
  307. }
  308. $output .= $teaser;
  309. if ( count( $content ) > 1 ) {
  310. if ( $elements['more'] ) {
  311. $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1];
  312. } else {
  313. if ( ! empty( $more_link_text ) ) {
  314. /**
  315. * Filters the Read More link text.
  316. *
  317. * @since 2.8.0
  318. *
  319. * @param string $more_link_element Read More link element.
  320. * @param string $more_link_text Read More text.
  321. */
  322. $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
  323. }
  324. $output = force_balance_tags( $output );
  325. }
  326. }
  327. return $output;
  328. }
  329. /**
  330. * Display the post excerpt.
  331. *
  332. * @since 0.71
  333. */
  334. function the_excerpt() {
  335. /**
  336. * Filters the displayed post excerpt.
  337. *
  338. * @since 0.71
  339. *
  340. * @see get_the_excerpt()
  341. *
  342. * @param string $post_excerpt The post excerpt.
  343. */
  344. echo apply_filters( 'the_excerpt', get_the_excerpt() );
  345. }
  346. /**
  347. * Retrieves the post excerpt.
  348. *
  349. * @since 0.71
  350. * @since 4.5.0 Introduced the `$post` parameter.
  351. *
  352. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  353. * @return string Post excerpt.
  354. */
  355. function get_the_excerpt( $post = null ) {
  356. if ( is_bool( $post ) ) {
  357. _deprecated_argument( __FUNCTION__, '2.3.0' );
  358. }
  359. $post = get_post( $post );
  360. if ( empty( $post ) ) {
  361. return '';
  362. }
  363. if ( post_password_required( $post ) ) {
  364. return __( 'There is no excerpt because this is a protected post.' );
  365. }
  366. /**
  367. * Filters the retrieved post excerpt.
  368. *
  369. * @since 1.2.0
  370. * @since 4.5.0 Introduced the `$post` parameter.
  371. *
  372. * @param string $post_excerpt The post excerpt.
  373. * @param WP_Post $post Post object.
  374. */
  375. return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
  376. }
  377. /**
  378. * Determines whether the post has a custom excerpt.
  379. *
  380. * For more information on this and similar theme functions, check out
  381. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  382. * Conditional Tags} article in the Theme Developer Handbook.
  383. *
  384. * @since 2.3.0
  385. *
  386. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  387. * @return bool True if the post has a custom excerpt, false otherwise.
  388. */
  389. function has_excerpt( $post = 0 ) {
  390. $post = get_post( $post );
  391. return ( ! empty( $post->post_excerpt ) );
  392. }
  393. /**
  394. * Displays the classes for the post container element.
  395. *
  396. * @since 2.7.0
  397. *
  398. * @param string|array $class One or more classes to add to the class list.
  399. * @param int|WP_Post $post_id Optional. Post ID or post object. Defaults to the global `$post`.
  400. */
  401. function post_class( $class = '', $post_id = null ) {
  402. // Separates classes with a single space, collates classes for post DIV.
  403. echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
  404. }
  405. /**
  406. * Retrieves an array of the class names for the post container element.
  407. *
  408. * The class names are many. If the post is a sticky, then the 'sticky'
  409. * class name. The class 'hentry' is always added to each post. If the post has a
  410. * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
  411. * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
  412. * eg 'category-foo' or 'my_custom_taxonomy-bar'.
  413. *
  414. * The 'post_tag' taxonomy is a special
  415. * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
  416. * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
  417. * $class parameter value, with the post ID as the last parameter.
  418. *
  419. * @since 2.7.0
  420. * @since 4.2.0 Custom taxonomy class names were added.
  421. *
  422. * @param string|string[] $class Space-separated string or array of class names to add to the class list.
  423. * @param int|WP_Post $post_id Optional. Post ID or post object.
  424. * @return string[] Array of class names.
  425. */
  426. function get_post_class( $class = '', $post_id = null ) {
  427. $post = get_post( $post_id );
  428. $classes = array();
  429. if ( $class ) {
  430. if ( ! is_array( $class ) ) {
  431. $class = preg_split( '#\s+#', $class );
  432. }
  433. $classes = array_map( 'esc_attr', $class );
  434. } else {
  435. // Ensure that we always coerce class to being an array.
  436. $class = array();
  437. }
  438. if ( ! $post ) {
  439. return $classes;
  440. }
  441. $classes[] = 'post-' . $post->ID;
  442. if ( ! is_admin() ) {
  443. $classes[] = $post->post_type;
  444. }
  445. $classes[] = 'type-' . $post->post_type;
  446. $classes[] = 'status-' . $post->post_status;
  447. // Post Format.
  448. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  449. $post_format = get_post_format( $post->ID );
  450. if ( $post_format && ! is_wp_error( $post_format ) ) {
  451. $classes[] = 'format-' . sanitize_html_class( $post_format );
  452. } else {
  453. $classes[] = 'format-standard';
  454. }
  455. }
  456. $post_password_required = post_password_required( $post->ID );
  457. // Post requires password.
  458. if ( $post_password_required ) {
  459. $classes[] = 'post-password-required';
  460. } elseif ( ! empty( $post->post_password ) ) {
  461. $classes[] = 'post-password-protected';
  462. }
  463. // Post thumbnails.
  464. if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
  465. $classes[] = 'has-post-thumbnail';
  466. }
  467. // Sticky for Sticky Posts.
  468. if ( is_sticky( $post->ID ) ) {
  469. if ( is_home() && ! is_paged() ) {
  470. $classes[] = 'sticky';
  471. } elseif ( is_admin() ) {
  472. $classes[] = 'status-sticky';
  473. }
  474. }
  475. // hentry for hAtom compliance.
  476. $classes[] = 'hentry';
  477. // All public taxonomies.
  478. $taxonomies = get_taxonomies( array( 'public' => true ) );
  479. foreach ( (array) $taxonomies as $taxonomy ) {
  480. if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
  481. foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
  482. if ( empty( $term->slug ) ) {
  483. continue;
  484. }
  485. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  486. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  487. $term_class = $term->term_id;
  488. }
  489. // 'post_tag' uses the 'tag' prefix for backward compatibility.
  490. if ( 'post_tag' == $taxonomy ) {
  491. $classes[] = 'tag-' . $term_class;
  492. } else {
  493. $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
  494. }
  495. }
  496. }
  497. }
  498. $classes = array_map( 'esc_attr', $classes );
  499. /**
  500. * Filters the list of CSS class names for the current post.
  501. *
  502. * @since 2.7.0
  503. *
  504. * @param string[] $classes An array of post class names.
  505. * @param string[] $class An array of additional class names added to the post.
  506. * @param int $post_id The post ID.
  507. */
  508. $classes = apply_filters( 'post_class', $classes, $class, $post->ID );
  509. return array_unique( $classes );
  510. }
  511. /**
  512. * Displays the class names for the body element.
  513. *
  514. * @since 2.8.0
  515. *
  516. * @param string|string[] $class Space-separated string or array of class names to add to the class list.
  517. */
  518. function body_class( $class = '' ) {
  519. // Separates class names with a single space, collates class names for body element.
  520. echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
  521. }
  522. /**
  523. * Retrieves an array of the class names for the body element.
  524. *
  525. * @since 2.8.0
  526. *
  527. * @global WP_Query $wp_query WordPress Query object.
  528. *
  529. * @param string|string[] $class Space-separated string or array of class names to add to the class list.
  530. * @return string[] Array of class names.
  531. */
  532. function get_body_class( $class = '' ) {
  533. global $wp_query;
  534. $classes = array();
  535. if ( is_rtl() ) {
  536. $classes[] = 'rtl';
  537. }
  538. if ( is_front_page() ) {
  539. $classes[] = 'home';
  540. }
  541. if ( is_home() ) {
  542. $classes[] = 'blog';
  543. }
  544. if ( is_privacy_policy() ) {
  545. $classes[] = 'privacy-policy';
  546. }
  547. if ( is_archive() ) {
  548. $classes[] = 'archive';
  549. }
  550. if ( is_date() ) {
  551. $classes[] = 'date';
  552. }
  553. if ( is_search() ) {
  554. $classes[] = 'search';
  555. $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
  556. }
  557. if ( is_paged() ) {
  558. $classes[] = 'paged';
  559. }
  560. if ( is_attachment() ) {
  561. $classes[] = 'attachment';
  562. }
  563. if ( is_404() ) {
  564. $classes[] = 'error404';
  565. }
  566. if ( is_singular() ) {
  567. $post_id = $wp_query->get_queried_object_id();
  568. $post = $wp_query->get_queried_object();
  569. $post_type = $post->post_type;
  570. if ( is_page_template() ) {
  571. $classes[] = "{$post_type}-template";
  572. $template_slug = get_page_template_slug( $post_id );
  573. $template_parts = explode( '/', $template_slug );
  574. foreach ( $template_parts as $part ) {
  575. $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
  576. }
  577. $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
  578. } else {
  579. $classes[] = "{$post_type}-template-default";
  580. }
  581. if ( is_single() ) {
  582. $classes[] = 'single';
  583. if ( isset( $post->post_type ) ) {
  584. $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
  585. $classes[] = 'postid-' . $post_id;
  586. // Post Format.
  587. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  588. $post_format = get_post_format( $post->ID );
  589. if ( $post_format && ! is_wp_error( $post_format ) ) {
  590. $classes[] = 'single-format-' . sanitize_html_class( $post_format );
  591. } else {
  592. $classes[] = 'single-format-standard';
  593. }
  594. }
  595. }
  596. }
  597. if ( is_attachment() ) {
  598. $mime_type = get_post_mime_type( $post_id );
  599. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
  600. $classes[] = 'attachmentid-' . $post_id;
  601. $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
  602. } elseif ( is_page() ) {
  603. $classes[] = 'page';
  604. $page_id = $wp_query->get_queried_object_id();
  605. $post = get_post( $page_id );
  606. $classes[] = 'page-id-' . $page_id;
  607. if ( get_pages(
  608. array(
  609. 'parent' => $page_id,
  610. 'number' => 1,
  611. )
  612. ) ) {
  613. $classes[] = 'page-parent';
  614. }
  615. if ( $post->post_parent ) {
  616. $classes[] = 'page-child';
  617. $classes[] = 'parent-pageid-' . $post->post_parent;
  618. }
  619. }
  620. } elseif ( is_archive() ) {
  621. if ( is_post_type_archive() ) {
  622. $classes[] = 'post-type-archive';
  623. $post_type = get_query_var( 'post_type' );
  624. if ( is_array( $post_type ) ) {
  625. $post_type = reset( $post_type );
  626. }
  627. $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
  628. } elseif ( is_author() ) {
  629. $author = $wp_query->get_queried_object();
  630. $classes[] = 'author';
  631. if ( isset( $author->user_nicename ) ) {
  632. $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
  633. $classes[] = 'author-' . $author->ID;
  634. }
  635. } elseif ( is_category() ) {
  636. $cat = $wp_query->get_queried_object();
  637. $classes[] = 'category';
  638. if ( isset( $cat->term_id ) ) {
  639. $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
  640. if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
  641. $cat_class = $cat->term_id;
  642. }
  643. $classes[] = 'category-' . $cat_class;
  644. $classes[] = 'category-' . $cat->term_id;
  645. }
  646. } elseif ( is_tag() ) {
  647. $tag = $wp_query->get_queried_object();
  648. $classes[] = 'tag';
  649. if ( isset( $tag->term_id ) ) {
  650. $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
  651. if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
  652. $tag_class = $tag->term_id;
  653. }
  654. $classes[] = 'tag-' . $tag_class;
  655. $classes[] = 'tag-' . $tag->term_id;
  656. }
  657. } elseif ( is_tax() ) {
  658. $term = $wp_query->get_queried_object();
  659. if ( isset( $term->term_id ) ) {
  660. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  661. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  662. $term_class = $term->term_id;
  663. }
  664. $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
  665. $classes[] = 'term-' . $term_class;
  666. $classes[] = 'term-' . $term->term_id;
  667. }
  668. }
  669. }
  670. if ( is_user_logged_in() ) {
  671. $classes[] = 'logged-in';
  672. }
  673. if ( is_admin_bar_showing() ) {
  674. $classes[] = 'admin-bar';
  675. $classes[] = 'no-customize-support';
  676. }
  677. if ( current_theme_supports( 'custom-background' )
  678. && ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) {
  679. $classes[] = 'custom-background';
  680. }
  681. if ( has_custom_logo() ) {
  682. $classes[] = 'wp-custom-logo';
  683. }
  684. if ( current_theme_supports( 'responsive-embeds' ) ) {
  685. $classes[] = 'wp-embed-responsive';
  686. }
  687. $page = $wp_query->get( 'page' );
  688. if ( ! $page || $page < 2 ) {
  689. $page = $wp_query->get( 'paged' );
  690. }
  691. if ( $page && $page > 1 && ! is_404() ) {
  692. $classes[] = 'paged-' . $page;
  693. if ( is_single() ) {
  694. $classes[] = 'single-paged-' . $page;
  695. } elseif ( is_page() ) {
  696. $classes[] = 'page-paged-' . $page;
  697. } elseif ( is_category() ) {
  698. $classes[] = 'category-paged-' . $page;
  699. } elseif ( is_tag() ) {
  700. $classes[] = 'tag-paged-' . $page;
  701. } elseif ( is_date() ) {
  702. $classes[] = 'date-paged-' . $page;
  703. } elseif ( is_author() ) {
  704. $classes[] = 'author-paged-' . $page;
  705. } elseif ( is_search() ) {
  706. $classes[] = 'search-paged-' . $page;
  707. } elseif ( is_post_type_archive() ) {
  708. $classes[] = 'post-type-paged-' . $page;
  709. }
  710. }
  711. if ( ! empty( $class ) ) {
  712. if ( ! is_array( $class ) ) {
  713. $class = preg_split( '#\s+#', $class );
  714. }
  715. $classes = array_merge( $classes, $class );
  716. } else {
  717. // Ensure that we always coerce class to being an array.
  718. $class = array();
  719. }
  720. $classes = array_map( 'esc_attr', $classes );
  721. /**
  722. * Filters the list of CSS body class names for the current post or page.
  723. *
  724. * @since 2.8.0
  725. *
  726. * @param string[] $classes An array of body class names.
  727. * @param string[] $class An array of additional class names added to the body.
  728. */
  729. $classes = apply_filters( 'body_class', $classes, $class );
  730. return array_unique( $classes );
  731. }
  732. /**
  733. * Whether post requires password and correct password has been provided.
  734. *
  735. * @since 2.7.0
  736. *
  737. * @param int|WP_Post|null $post An optional post. Global $post used if not provided.
  738. * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
  739. */
  740. function post_password_required( $post = null ) {
  741. $post = get_post( $post );
  742. if ( empty( $post->post_password ) ) {
  743. /** This filter is documented in wp-includes/post-template.php */
  744. return apply_filters( 'post_password_required', false, $post );
  745. }
  746. if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
  747. /** This filter is documented in wp-includes/post-template.php */
  748. return apply_filters( 'post_password_required', true, $post );
  749. }
  750. require_once ABSPATH . WPINC . '/class-phpass.php';
  751. $hasher = new PasswordHash( 8, true );
  752. $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
  753. if ( 0 !== strpos( $hash, '$P$B' ) ) {
  754. $required = true;
  755. } else {
  756. $required = ! $hasher->CheckPassword( $post->post_password, $hash );
  757. }
  758. /**
  759. * Filters whether a post requires the user to supply a password.
  760. *
  761. * @since 4.7.0
  762. *
  763. * @param bool $required Whether the user needs to supply a password. True if password has not been
  764. * provided or is incorrect, false if password has been supplied or is not required.
  765. * @param WP_Post $post Post data.
  766. */
  767. return apply_filters( 'post_password_required', $required, $post );
  768. }
  769. //
  770. // Page Template Functions for usage in Themes.
  771. //
  772. /**
  773. * The formatted output of a list of pages.
  774. *
  775. * Displays page links for paginated posts (i.e. including the `<!--nextpage-->`
  776. * Quicktag one or more times). This tag must be within The Loop.
  777. *
  778. * @since 1.2.0
  779. * @since 5.1.0 Added the `aria_current` argument.
  780. *
  781. * @global int $page
  782. * @global int $numpages
  783. * @global int $multipage
  784. * @global int $more
  785. *
  786. * @param string|array $args {
  787. * Optional. Array or string of default arguments.
  788. *
  789. * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`.
  790. * @type string $after HTML or text to append to each link. Default is `</p>`.
  791. * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag.
  792. * Also prepended to the current item, which is not linked. Default empty.
  793. * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag.
  794. * Also appended to the current item, which is not linked. Default empty.
  795. * @type string $aria_current The value for the aria-current attribute. Possible values are 'page',
  796. * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'.
  797. * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number
  798. * and next. Default is 'number'.
  799. * @type string $separator Text between pagination links. Default is ' '.
  800. * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'.
  801. * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
  802. * @type string $pagelink Format string for page numbers. The % in the parameter string will be
  803. * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
  804. * Defaults to '%', just the page number.
  805. * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
  806. * }
  807. * @return string Formatted output in HTML.
  808. */
  809. function wp_link_pages( $args = '' ) {
  810. global $page, $numpages, $multipage, $more;
  811. $defaults = array(
  812. 'before' => '<p class="post-nav-links">' . __( 'Pages:' ),
  813. 'after' => '</p>',
  814. 'link_before' => '',
  815. 'link_after' => '',
  816. 'aria_current' => 'page',
  817. 'next_or_number' => 'number',
  818. 'separator' => ' ',
  819. 'nextpagelink' => __( 'Next page' ),
  820. 'previouspagelink' => __( 'Previous page' ),
  821. 'pagelink' => '%',
  822. 'echo' => 1,
  823. );
  824. $parsed_args = wp_parse_args( $args, $defaults );
  825. /**
  826. * Filters the arguments used in retrieving page links for paginated posts.
  827. *
  828. * @since 3.0.0
  829. *
  830. * @param array $parsed_args An array of arguments for page links for paginated posts.
  831. */
  832. $parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args );
  833. $output = '';
  834. if ( $multipage ) {
  835. if ( 'number' == $parsed_args['next_or_number'] ) {
  836. $output .= $parsed_args['before'];
  837. for ( $i = 1; $i <= $numpages; $i++ ) {
  838. $link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after'];
  839. if ( $i != $page || ! $more && 1 == $page ) {
  840. $link = _wp_link_page( $i ) . $link . '</a>';
  841. } elseif ( $i === $page ) {
  842. $link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>';
  843. }
  844. /**
  845. * Filters the HTML output of individual page number links.
  846. *
  847. * @since 3.6.0
  848. *
  849. * @param string $link The page number HTML output.
  850. * @param int $i Page number for paginated posts' page links.
  851. */
  852. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  853. // Use the custom links separator beginning with the second link.
  854. $output .= ( 1 === $i ) ? ' ' : $parsed_args['separator'];
  855. $output .= $link;
  856. }
  857. $output .= $parsed_args['after'];
  858. } elseif ( $more ) {
  859. $output .= $parsed_args['before'];
  860. $prev = $page - 1;
  861. if ( $prev > 0 ) {
  862. $link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>';
  863. /** This filter is documented in wp-includes/post-template.php */
  864. $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
  865. }
  866. $next = $page + 1;
  867. if ( $next <= $numpages ) {
  868. if ( $prev ) {
  869. $output .= $parsed_args['separator'];
  870. }
  871. $link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>';
  872. /** This filter is documented in wp-includes/post-template.php */
  873. $output .= apply_filters( 'wp_link_pages_link', $link, $next );
  874. }
  875. $output .= $parsed_args['after'];
  876. }
  877. }
  878. /**
  879. * Filters the HTML output of page links for paginated posts.
  880. *
  881. * @since 3.6.0
  882. *
  883. * @param string $output HTML output of paginated posts' page links.
  884. * @param array $args An array of arguments.
  885. */
  886. $html = apply_filters( 'wp_link_pages', $output, $args );
  887. if ( $parsed_args['echo'] ) {
  888. echo $html;
  889. }
  890. return $html;
  891. }
  892. /**
  893. * Helper function for wp_link_pages().
  894. *
  895. * @since 3.1.0
  896. * @access private
  897. *
  898. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  899. *
  900. * @param int $i Page number.
  901. * @return string Link.
  902. */
  903. function _wp_link_page( $i ) {
  904. global $wp_rewrite;
  905. $post = get_post();
  906. $query_args = array();
  907. if ( 1 == $i ) {
  908. $url = get_permalink();
  909. } else {
  910. if ( '' == get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) {
  911. $url = add_query_arg( 'page', $i, get_permalink() );
  912. } elseif ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) {
  913. $url = trailingslashit( get_permalink() ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' );
  914. } else {
  915. $url = trailingslashit( get_permalink() ) . user_trailingslashit( $i, 'single_paged' );
  916. }
  917. }
  918. if ( is_preview() ) {
  919. if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
  920. $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
  921. $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
  922. }
  923. $url = get_preview_post_link( $post, $query_args, $url );
  924. }
  925. return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">';
  926. }
  927. //
  928. // Post-meta: Custom per-post fields.
  929. //
  930. /**
  931. * Retrieve post custom meta data field.
  932. *
  933. * @since 1.5.0
  934. *
  935. * @param string $key Meta data key name.
  936. * @return array|string|false Array of values, or single value if only one element exists.
  937. * False if the key does not exist.
  938. */
  939. function post_custom( $key = '' ) {
  940. $custom = get_post_custom();
  941. if ( ! isset( $custom[ $key ] ) ) {
  942. return false;
  943. } elseif ( 1 == count( $custom[ $key ] ) ) {
  944. return $custom[ $key ][0];
  945. } else {
  946. return $custom[ $key ];
  947. }
  948. }
  949. /**
  950. * Display a list of post custom fields.
  951. *
  952. * @since 1.2.0
  953. *
  954. * @internal This will probably change at some point...
  955. */
  956. function the_meta() {
  957. $keys = get_post_custom_keys();
  958. if ( $keys ) {
  959. $li_html = '';
  960. foreach ( (array) $keys as $key ) {
  961. $keyt = trim( $key );
  962. if ( is_protected_meta( $keyt, 'post' ) ) {
  963. continue;
  964. }
  965. $values = array_map( 'trim', get_post_custom_values( $key ) );
  966. $value = implode( ', ', $values );
  967. $html = sprintf(
  968. "<li><span class='post-meta-key'>%s</span> %s</li>\n",
  969. /* translators: %s: Post custom field name. */
  970. sprintf( _x( '%s:', 'Post custom field name' ), $key ),
  971. $value
  972. );
  973. /**
  974. * Filters the HTML output of the li element in the post custom fields list.
  975. *
  976. * @since 2.2.0
  977. *
  978. * @param string $html The HTML output for the li element.
  979. * @param string $key Meta key.
  980. * @param string $value Meta value.
  981. */
  982. $li_html .= apply_filters( 'the_meta_key', $html, $key, $value );
  983. }
  984. if ( $li_html ) {
  985. echo "<ul class='post-meta'>\n{$li_html}</ul>\n";
  986. }
  987. }
  988. }
  989. //
  990. // Pages.
  991. //
  992. /**
  993. * Retrieve or display a list of pages as a dropdown (select list).
  994. *
  995. * @since 2.1.0
  996. * @since 4.2.0 The `$value_field` argument was added.
  997. * @since 4.3.0 The `$class` argument was added.
  998. *
  999. * @see get_pages()
  1000. *
  1001. * @param array|string $args {
  1002. * Optional. Array or string of arguments to generate a page dropdown. See `get_pages()` for additional arguments.
  1003. *
  1004. * @type int $depth Maximum depth. Default 0.
  1005. * @type int $child_of Page ID to retrieve child pages of. Default 0.
  1006. * @type int|string $selected Value of the option that should be selected. Default 0.
  1007. * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1,
  1008. * or their bool equivalents. Default 1.
  1009. * @type string $name Value for the 'name' attribute of the select element.
  1010. * Default 'page_id'.
  1011. * @type string $id Value for the 'id' attribute of the select element.
  1012. * @type string $class Value for the 'class' attribute of the select element. Default: none.
  1013. * Defaults to the value of `$name`.
  1014. * @type string $show_option_none Text to display for showing no pages. Default empty (does not display).
  1015. * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display).
  1016. * @type string $option_none_value Value to use when no page is selected. Default empty.
  1017. * @type string $value_field Post field used to populate the 'value' attribute of the option
  1018. * elements. Accepts any valid post field. Default 'ID'.
  1019. * }
  1020. * @return string HTML dropdown list of pages.
  1021. */
  1022. function wp_dropdown_pages( $args = '' ) {
  1023. $defaults = array(
  1024. 'depth' => 0,
  1025. 'child_of' => 0,
  1026. 'selected' => 0,
  1027. 'echo' => 1,
  1028. 'name' => 'page_id',
  1029. 'id' => '',
  1030. 'class' => '',
  1031. 'show_option_none' => '',
  1032. 'show_option_no_change' => '',
  1033. 'option_none_value' => '',
  1034. 'value_field' => 'ID',
  1035. );
  1036. $parsed_args = wp_parse_args( $args, $defaults );
  1037. $pages = get_pages( $parsed_args );
  1038. $output = '';
  1039. // Back-compat with old system where both id and name were based on $name argument.
  1040. if ( empty( $parsed_args['id'] ) ) {
  1041. $parsed_args['id'] = $parsed_args['name'];
  1042. }
  1043. if ( ! empty( $pages ) ) {
  1044. $class = '';
  1045. if ( ! empty( $parsed_args['class'] ) ) {
  1046. $class = " class='" . esc_attr( $parsed_args['class'] ) . "'";
  1047. }
  1048. $output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";
  1049. if ( $parsed_args['show_option_no_change'] ) {
  1050. $output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n";
  1051. }
  1052. if ( $parsed_args['show_option_none'] ) {
  1053. $output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";
  1054. }
  1055. $output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );
  1056. $output .= "</select>\n";
  1057. }
  1058. /**
  1059. * Filters the HTML output of a list of pages as a drop down.
  1060. *
  1061. * @since 2.1.0
  1062. * @since 4.4.0 `$parsed_args` and `$pages` added as arguments.
  1063. *
  1064. * @param string $output HTML output for drop down list of pages.
  1065. * @param array $parsed_args The parsed arguments array.
  1066. * @param WP_Post[] $pages Array of the page objects.
  1067. */
  1068. $html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages );
  1069. if ( $parsed_args['echo'] ) {
  1070. echo $html;
  1071. }
  1072. return $html;
  1073. }
  1074. /**
  1075. * Retrieve or display a list of pages (or hierarchical post type items) in list (li) format.
  1076. *
  1077. * @since 1.5.0
  1078. * @since 4.7.0 Added the `item_spacing` argument.
  1079. *
  1080. * @see get_pages()
  1081. *
  1082. * @global WP_Query $wp_query WordPress Query object.
  1083. *
  1084. * @param array|string $args {
  1085. * Optional. Array or string of arguments to generate a list of pages. See `get_pages()` for additional arguments.
  1086. *
  1087. * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
  1088. * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
  1089. * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
  1090. * Default is the value of 'date_format' option.
  1091. * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
  1092. * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
  1093. * the given n depth). Default 0.
  1094. * @type bool $echo Whether or not to echo the list of pages. Default true.
  1095. * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
  1096. * @type array $include Comma-separated list of page IDs to include. Default empty.
  1097. * @type string $link_after Text or HTML to follow the page link label. Default null.
  1098. * @type string $link_before Text or HTML to precede the page link label. Default null.
  1099. * @type string $post_type Post type to query for. Default 'page'.
  1100. * @type string|array $post_status Comma-separated list or array of post statuses to include. Default 'publish'.
  1101. * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
  1102. * 'modified' or any other value. An empty value hides the date. Default empty.
  1103. * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
  1104. * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
  1105. * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
  1106. * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
  1107. * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
  1108. * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
  1109. * Default 'preserve'.
  1110. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
  1111. * }
  1112. * @return void|string Void if 'echo' argument is true, HTML list of pages if 'echo' is false.
  1113. */
  1114. function wp_list_pages( $args = '' ) {
  1115. $defaults = array(
  1116. 'depth' => 0,
  1117. 'show_date' => '',
  1118. 'date_format' => get_option( 'date_format' ),
  1119. 'child_of' => 0,
  1120. 'exclude' => '',
  1121. 'title_li' => __( 'Pages' ),
  1122. 'echo' => 1,
  1123. 'authors' => '',
  1124. 'sort_column' => 'menu_order, post_title',
  1125. 'link_before' => '',
  1126. 'link_after' => '',
  1127. 'item_spacing' => 'preserve',
  1128. 'walker' => '',
  1129. );
  1130. $parsed_args = wp_parse_args( $args, $defaults );
  1131. if ( ! in_array( $parsed_args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
  1132. // Invalid value, fall back to default.
  1133. $parsed_args['item_spacing'] = $defaults['item_spacing'];
  1134. }
  1135. $output = '';
  1136. $current_page = 0;
  1137. // Sanitize, mostly to keep spaces out.
  1138. $parsed_args['exclude'] = preg_replace( '/[^0-9,]/', '', $parsed_args['exclude'] );
  1139. // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array).
  1140. $exclude_array = ( $parsed_args['exclude'] ) ? explode( ',', $parsed_args['exclude'] ) : array();
  1141. /**
  1142. * Filters the array of pages to exclude from the pages list.
  1143. *
  1144. * @since 2.1.0
  1145. *
  1146. * @param string[] $exclude_array An array of page IDs to exclude.
  1147. */
  1148. $parsed_args['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
  1149. $parsed_args['hierarchical'] = 0;
  1150. // Query pages.
  1151. $pages = get_pages( $parsed_args );
  1152. if ( ! empty( $pages ) ) {
  1153. if ( $parsed_args['title_li'] ) {
  1154. $output .= '<li class="pagenav">' . $parsed_args['title_li'] . '<ul>';
  1155. }
  1156. global $wp_query;
  1157. if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
  1158. $current_page = get_queried_object_id();
  1159. } elseif ( is_singular() ) {
  1160. $queried_object = get_queried_object();
  1161. if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
  1162. $current_page = $queried_object->ID;
  1163. }
  1164. }
  1165. $output .= walk_page_tree( $pages, $parsed_args['depth'], $current_page, $parsed_args );
  1166. if ( $parsed_args['title_li'] ) {
  1167. $output .= '</ul></li>';
  1168. }
  1169. }
  1170. /**
  1171. * Filters the HTML output of the pages to list.
  1172. *
  1173. * @since 1.5.1
  1174. * @since 4.4.0 `$pages` added as arguments.
  1175. *
  1176. * @see wp_list_pages()
  1177. *
  1178. * @param string $output HTML output of the pages list.
  1179. * @param array $parsed_args An array of page-listing arguments.
  1180. * @param WP_Post[] $pages Array of the page objects.
  1181. */
  1182. $html = apply_filters( 'wp_list_pages', $output, $parsed_args, $pages );
  1183. if ( $parsed_args['echo'] ) {
  1184. echo $html;
  1185. } else {
  1186. return $html;
  1187. }
  1188. }
  1189. /**
  1190. * Displays or retrieves a list of pages with an optional home link.
  1191. *
  1192. * The arguments are listed below and part of the arguments are for wp_list_pages() function.
  1193. * Check that function for more info on those arguments.
  1194. *
  1195. * @since 2.7.0
  1196. * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
  1197. * @since 4.7.0 Added the `item_spacing` argument.
  1198. *
  1199. * @param array|string $args {
  1200. * Optional. Array or string of arguments to generate a page menu. See `wp_list_pages()` for additional arguments.
  1201. *
  1202. * @type string $sort_column How to sort the list of pages. Accepts post column names.
  1203. * Default 'menu_order, post_title'.
  1204. * @type string $menu_id ID for the div containing the page list. Default is empty string.
  1205. * @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
  1206. * @type string $container Element to use for the element containing the page list. Default 'div'.
  1207. * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
  1208. * Default true.
  1209. * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
  1210. * you'd like shown for the home link. 1|true defaults to 'Home'.
  1211. * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
  1212. * @type string $link_after The HTML or text to append to $show_home text. Default empty.
  1213. * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
  1214. * @type string $after The HTML or text to append to the menu. Default is '</ul>'.
  1215. * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve'
  1216. * or 'discard'. Default 'discard'.
  1217. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
  1218. * }
  1219. * @return void|string Void if 'echo' argument is true, HTML menu if 'echo' is false.
  1220. */
  1221. function wp_page_menu( $args = array() ) {
  1222. $defaults = array(
  1223. 'sort_column' => 'menu_order, post_title',
  1224. 'menu_id' => '',
  1225. 'menu_class' => 'menu',
  1226. 'container' => 'div',
  1227. 'echo' => true,
  1228. 'link_before' => '',
  1229. 'link_after' => '',
  1230. 'before' => '<ul>',
  1231. 'after' => '</ul>',
  1232. 'item_spacing' => 'discard',
  1233. 'walker' => '',
  1234. );
  1235. $args = wp_parse_args( $args, $defaults );
  1236. if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
  1237. // Invalid value, fall back to default.
  1238. $args['item_spacing'] = $defaults['item_spacing'];
  1239. }
  1240. if ( 'preserve' === $args['item_spacing'] ) {
  1241. $t = "\t";
  1242. $n = "\n";
  1243. } else {
  1244. $t = '';
  1245. $n = '';
  1246. }
  1247. /**
  1248. * Filters the arguments used to generate a page-based menu.
  1249. *
  1250. * @since 2.7.0
  1251. *
  1252. * @see wp_page_menu()
  1253. *
  1254. * @param array $args An array of page menu arguments.
  1255. */
  1256. $args = apply_filters( 'wp_page_menu_args', $args );
  1257. $menu = '';
  1258. $list_args = $args;
  1259. // Show Home in the menu.
  1260. if ( ! empty( $args['show_home'] ) ) {
  1261. if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) {
  1262. $text = __( 'Home' );
  1263. } else {
  1264. $text = $args['show_home'];
  1265. }
  1266. $class = '';
  1267. if ( is_front_page() && ! is_paged() ) {
  1268. $class = 'class="current_page_item"';
  1269. }
  1270. $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
  1271. // If the front page is a page, add it to the exclude list.
  1272. if ( get_option( 'show_on_front' ) == 'page' ) {
  1273. if ( ! empty( $list_args['exclude'] ) ) {
  1274. $list_args['exclude'] .= ',';
  1275. } else {
  1276. $list_args['exclude'] = '';
  1277. }
  1278. $list_args['exclude'] .= get_option( 'page_on_front' );
  1279. }
  1280. }
  1281. $list_args['echo'] = false;
  1282. $list_args['title_li'] = '';
  1283. $menu .= wp_list_pages( $list_args );
  1284. $container = sanitize_text_field( $args['container'] );
  1285. // Fallback in case `wp_nav_menu()` was called without a container.
  1286. if ( empty( $container ) ) {
  1287. $container = 'div';
  1288. }
  1289. if ( $menu ) {
  1290. // wp_nav_menu() doesn't set before and after.
  1291. if ( isset( $args['fallback_cb'] ) &&
  1292. 'wp_page_menu' === $args['fallback_cb'] &&
  1293. 'ul' !== $container ) {
  1294. $args['before'] = "<ul>{$n}";
  1295. $args['after'] = '</ul>';
  1296. }
  1297. $menu = $args['before'] . $menu . $args['after'];
  1298. }
  1299. $attrs = '';
  1300. if ( ! empty( $args['menu_id'] ) ) {
  1301. $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
  1302. }
  1303. if ( ! empty( $args['menu_class'] ) ) {
  1304. $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
  1305. }
  1306. $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
  1307. /**
  1308. * Filters the HTML output of a page-based menu.
  1309. *
  1310. * @since 2.7.0
  1311. *
  1312. * @see wp_page_menu()
  1313. *
  1314. * @param string $menu The HTML output.
  1315. * @param array $args An array of arguments.
  1316. */
  1317. $menu = apply_filters( 'wp_page_menu', $menu, $args );
  1318. if ( $args['echo'] ) {
  1319. echo $menu;
  1320. } else {
  1321. return $menu;
  1322. }
  1323. }
  1324. //
  1325. // Page helpers.
  1326. //
  1327. /**
  1328. * Retrieve HTML list content for page list.
  1329. *
  1330. * @uses Walker_Page to create HTML list content.
  1331. * @since 2.1.0
  1332. *
  1333. * @param array $pages
  1334. * @param int $depth
  1335. * @param int $current_page
  1336. * @param array $r
  1337. * @return string
  1338. */
  1339. function walk_page_tree( $pages, $depth, $current_page, $r ) {
  1340. if ( empty( $r['walker'] ) ) {
  1341. $walker = new Walker_Page;
  1342. } else {
  1343. $walker = $r['walker'];
  1344. }
  1345. foreach ( (array) $pages as $page ) {
  1346. if ( $page->post_parent ) {
  1347. $r['pages_with_children'][ $page->post_parent ] = true;
  1348. }
  1349. }
  1350. return $walker->walk( $pages, $depth, $r, $current_page );
  1351. }
  1352. /**
  1353. * Retrieve HTML dropdown (select) content for page list.
  1354. *
  1355. * @since 2.1.0
  1356. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  1357. * to the function signature.
  1358. *
  1359. * @uses Walker_PageDropdown to create HTML dropdown content.
  1360. * @see Walker_PageDropdown::walk() for parameters and return description.
  1361. *
  1362. * @return string
  1363. */
  1364. function walk_page_dropdown_tree( ...$args ) {
  1365. if ( empty( $args[2]['walker'] ) ) { // The user's options are the third parameter.
  1366. $walker = new Walker_PageDropdown;
  1367. } else {
  1368. $walker = $args[2]['walker'];
  1369. }
  1370. return $walker->walk( ...$args );
  1371. }
  1372. //
  1373. // Attachments.
  1374. //
  1375. /**
  1376. * Display an attachment page link using an image or icon.
  1377. *
  1378. * @since 2.0.0

Large files files are truncated, but you can click here to view the full file