You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1818 lines
57 KiB

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