PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post-template.php

https://gitlab.com/webkod3r/tripolis
PHP | 1582 lines | 1008 code | 134 blank | 440 comment | 138 complexity | ae119eaed4a856b7a661c9e1897556ff MD5 | raw 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() {
  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 content.
  31. *
  32. * @since 0.71
  33. *
  34. * @param string $before Optional. Content to prepend to the title.
  35. * @param string $after Optional. Content to append to the title.
  36. * @param bool $echo Optional, default to true.Whether to display or return.
  37. * @return string|void String if $echo parameter 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 {@link 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 {@link
  56. * esc_attr()} before it is passed to the user or displayed. The default
  57. * as with {@link the_title()}, 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. * Filter 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. * Filter 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. * Filter 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. * Filter 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. * Filter 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. * Filter 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
  225. * @global int $more
  226. * @global bool $preview
  227. * @global array $pages
  228. * @global int $multipage
  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 = __( '(more&hellip;)' );
  239. $output = '';
  240. $has_teaser = false;
  241. // If post password required and it doesn't match the cookie.
  242. if ( post_password_required( $post ) )
  243. return get_the_password_form( $post );
  244. if ( $page > count( $pages ) ) // if the requested page doesn't exist
  245. $page = count( $pages ); // give them the highest numbered page that DOES exist
  246. $content = $pages[$page - 1];
  247. if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
  248. $content = explode( $matches[0], $content, 2 );
  249. if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
  250. $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
  251. $has_teaser = true;
  252. } else {
  253. $content = array( $content );
  254. }
  255. if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
  256. $strip_teaser = true;
  257. $teaser = $content[0];
  258. if ( $more && $strip_teaser && $has_teaser )
  259. $teaser = '';
  260. $output .= $teaser;
  261. if ( count( $content ) > 1 ) {
  262. if ( $more ) {
  263. $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
  264. } else {
  265. if ( ! empty( $more_link_text ) )
  266. /**
  267. * Filter the Read More link text.
  268. *
  269. * @since 2.8.0
  270. *
  271. * @param string $more_link_element Read More link element.
  272. * @param string $more_link_text Read More text.
  273. */
  274. $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
  275. $output = force_balance_tags( $output );
  276. }
  277. }
  278. if ( $preview ) // Preview fix for JavaScript bug with foreign languages.
  279. $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
  280. return $output;
  281. }
  282. /**
  283. * Preview fix for JavaScript bug with foreign languages.
  284. *
  285. * @since 3.1.0
  286. * @access private
  287. *
  288. * @param array $match Match array from preg_replace_callback.
  289. * @return string
  290. */
  291. function _convert_urlencoded_to_entities( $match ) {
  292. return '&#' . base_convert( $match[1], 16, 10 ) . ';';
  293. }
  294. /**
  295. * Display the post excerpt.
  296. *
  297. * @since 0.71
  298. */
  299. function the_excerpt() {
  300. /**
  301. * Filter the displayed post excerpt.
  302. *
  303. * @since 0.71
  304. *
  305. * @see get_the_excerpt()
  306. *
  307. * @param string $post_excerpt The post excerpt.
  308. */
  309. echo apply_filters( 'the_excerpt', get_the_excerpt() );
  310. }
  311. /**
  312. * Retrieves the post excerpt.
  313. *
  314. * @since 0.71
  315. * @since 4.5.0 Introduced the `$post` parameter.
  316. *
  317. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  318. * @return string Post excerpt.
  319. */
  320. function get_the_excerpt( $post = null ) {
  321. if ( is_bool( $post ) ) {
  322. _deprecated_argument( __FUNCTION__, '2.3' );
  323. }
  324. $post = get_post( $post );
  325. if ( empty( $post ) ) {
  326. return '';
  327. }
  328. if ( post_password_required( $post ) ) {
  329. return __( 'There is no excerpt because this is a protected post.' );
  330. }
  331. /**
  332. * Filter the retrieved post excerpt.
  333. *
  334. * @since 1.2.0
  335. * @since 4.5.0 Introduced the `$post` parameter.
  336. *
  337. * @param string $post_excerpt The post excerpt.
  338. * @param WP_Post $post Post object.
  339. */
  340. return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
  341. }
  342. /**
  343. * Whether post has excerpt.
  344. *
  345. * @since 2.3.0
  346. *
  347. * @param int|WP_Post $id Optional. Post ID or post object.
  348. * @return bool
  349. */
  350. function has_excerpt( $id = 0 ) {
  351. $post = get_post( $id );
  352. return ( !empty( $post->post_excerpt ) );
  353. }
  354. /**
  355. * Display the classes for the post div.
  356. *
  357. * @since 2.7.0
  358. *
  359. * @param string|array $class One or more classes to add to the class list.
  360. * @param int|WP_Post $post_id Optional. Post ID or post object. Defaults to the global `$post`.
  361. */
  362. function post_class( $class = '', $post_id = null ) {
  363. // Separates classes with a single space, collates classes for post DIV
  364. echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
  365. }
  366. /**
  367. * Retrieve the classes for the post div as an array.
  368. *
  369. * The class names are many. If the post is a sticky, then the 'sticky'
  370. * class name. The class 'hentry' is always added to each post. If the post has a
  371. * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
  372. * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
  373. * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
  374. * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
  375. * passed through the filter, 'post_class' with the list of classes, followed by
  376. * $class parameter value, with the post ID as the last parameter.
  377. *
  378. * @since 2.7.0
  379. * @since 4.2.0 Custom taxonomy classes were added.
  380. *
  381. * @param string|array $class One or more classes to add to the class list.
  382. * @param int|WP_Post $post_id Optional. Post ID or post object.
  383. * @return array Array of classes.
  384. */
  385. function get_post_class( $class = '', $post_id = null ) {
  386. $post = get_post( $post_id );
  387. $classes = array();
  388. if ( $class ) {
  389. if ( ! is_array( $class ) ) {
  390. $class = preg_split( '#\s+#', $class );
  391. }
  392. $classes = array_map( 'esc_attr', $class );
  393. } else {
  394. // Ensure that we always coerce class to being an array.
  395. $class = array();
  396. }
  397. if ( ! $post ) {
  398. return $classes;
  399. }
  400. $classes[] = 'post-' . $post->ID;
  401. if ( ! is_admin() )
  402. $classes[] = $post->post_type;
  403. $classes[] = 'type-' . $post->post_type;
  404. $classes[] = 'status-' . $post->post_status;
  405. // Post Format
  406. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  407. $post_format = get_post_format( $post->ID );
  408. if ( $post_format && !is_wp_error($post_format) )
  409. $classes[] = 'format-' . sanitize_html_class( $post_format );
  410. else
  411. $classes[] = 'format-standard';
  412. }
  413. $post_password_required = post_password_required( $post->ID );
  414. // Post requires password.
  415. if ( $post_password_required ) {
  416. $classes[] = 'post-password-required';
  417. } elseif ( ! empty( $post->post_password ) ) {
  418. $classes[] = 'post-password-protected';
  419. }
  420. // Post thumbnails.
  421. if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
  422. $classes[] = 'has-post-thumbnail';
  423. }
  424. // sticky for Sticky Posts
  425. if ( is_sticky( $post->ID ) ) {
  426. if ( is_home() && ! is_paged() ) {
  427. $classes[] = 'sticky';
  428. } elseif ( is_admin() ) {
  429. $classes[] = 'status-sticky';
  430. }
  431. }
  432. // hentry for hAtom compliance
  433. $classes[] = 'hentry';
  434. // All public taxonomies
  435. $taxonomies = get_taxonomies( array( 'public' => true ) );
  436. foreach ( (array) $taxonomies as $taxonomy ) {
  437. if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
  438. foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
  439. if ( empty( $term->slug ) ) {
  440. continue;
  441. }
  442. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  443. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  444. $term_class = $term->term_id;
  445. }
  446. // 'post_tag' uses the 'tag' prefix for backward compatibility.
  447. if ( 'post_tag' == $taxonomy ) {
  448. $classes[] = 'tag-' . $term_class;
  449. } else {
  450. $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
  451. }
  452. }
  453. }
  454. }
  455. $classes = array_map( 'esc_attr', $classes );
  456. /**
  457. * Filter the list of CSS classes for the current post.
  458. *
  459. * @since 2.7.0
  460. *
  461. * @param array $classes An array of post classes.
  462. * @param array $class An array of additional classes added to the post.
  463. * @param int $post_id The post ID.
  464. */
  465. $classes = apply_filters( 'post_class', $classes, $class, $post->ID );
  466. return array_unique( $classes );
  467. }
  468. /**
  469. * Display the classes for the body element.
  470. *
  471. * @since 2.8.0
  472. *
  473. * @param string|array $class One or more classes to add to the class list.
  474. */
  475. function body_class( $class = '' ) {
  476. // Separates classes with a single space, collates classes for body element
  477. echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
  478. }
  479. /**
  480. * Retrieve the classes for the body element as an array.
  481. *
  482. * @since 2.8.0
  483. *
  484. * @global WP_Query $wp_query
  485. *
  486. * @param string|array $class One or more classes to add to the class list.
  487. * @return array Array of classes.
  488. */
  489. function get_body_class( $class = '' ) {
  490. global $wp_query;
  491. $classes = array();
  492. if ( is_rtl() )
  493. $classes[] = 'rtl';
  494. if ( is_front_page() )
  495. $classes[] = 'home';
  496. if ( is_home() )
  497. $classes[] = 'blog';
  498. if ( is_archive() )
  499. $classes[] = 'archive';
  500. if ( is_date() )
  501. $classes[] = 'date';
  502. if ( is_search() ) {
  503. $classes[] = 'search';
  504. $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
  505. }
  506. if ( is_paged() )
  507. $classes[] = 'paged';
  508. if ( is_attachment() )
  509. $classes[] = 'attachment';
  510. if ( is_404() )
  511. $classes[] = 'error404';
  512. if ( is_single() ) {
  513. $post_id = $wp_query->get_queried_object_id();
  514. $post = $wp_query->get_queried_object();
  515. $classes[] = 'single';
  516. if ( isset( $post->post_type ) ) {
  517. $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
  518. $classes[] = 'postid-' . $post_id;
  519. // Post Format
  520. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  521. $post_format = get_post_format( $post->ID );
  522. if ( $post_format && !is_wp_error($post_format) )
  523. $classes[] = 'single-format-' . sanitize_html_class( $post_format );
  524. else
  525. $classes[] = 'single-format-standard';
  526. }
  527. }
  528. if ( is_attachment() ) {
  529. $mime_type = get_post_mime_type($post_id);
  530. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
  531. $classes[] = 'attachmentid-' . $post_id;
  532. $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
  533. }
  534. } elseif ( is_archive() ) {
  535. if ( is_post_type_archive() ) {
  536. $classes[] = 'post-type-archive';
  537. $post_type = get_query_var( 'post_type' );
  538. if ( is_array( $post_type ) )
  539. $post_type = reset( $post_type );
  540. $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
  541. } elseif ( is_author() ) {
  542. $author = $wp_query->get_queried_object();
  543. $classes[] = 'author';
  544. if ( isset( $author->user_nicename ) ) {
  545. $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
  546. $classes[] = 'author-' . $author->ID;
  547. }
  548. } elseif ( is_category() ) {
  549. $cat = $wp_query->get_queried_object();
  550. $classes[] = 'category';
  551. if ( isset( $cat->term_id ) ) {
  552. $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
  553. if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
  554. $cat_class = $cat->term_id;
  555. }
  556. $classes[] = 'category-' . $cat_class;
  557. $classes[] = 'category-' . $cat->term_id;
  558. }
  559. } elseif ( is_tag() ) {
  560. $tag = $wp_query->get_queried_object();
  561. $classes[] = 'tag';
  562. if ( isset( $tag->term_id ) ) {
  563. $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
  564. if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
  565. $tag_class = $tag->term_id;
  566. }
  567. $classes[] = 'tag-' . $tag_class;
  568. $classes[] = 'tag-' . $tag->term_id;
  569. }
  570. } elseif ( is_tax() ) {
  571. $term = $wp_query->get_queried_object();
  572. if ( isset( $term->term_id ) ) {
  573. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  574. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  575. $term_class = $term->term_id;
  576. }
  577. $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
  578. $classes[] = 'term-' . $term_class;
  579. $classes[] = 'term-' . $term->term_id;
  580. }
  581. }
  582. } elseif ( is_page() ) {
  583. $classes[] = 'page';
  584. $page_id = $wp_query->get_queried_object_id();
  585. $post = get_post($page_id);
  586. $classes[] = 'page-id-' . $page_id;
  587. if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
  588. $classes[] = 'page-parent';
  589. }
  590. if ( $post->post_parent ) {
  591. $classes[] = 'page-child';
  592. $classes[] = 'parent-pageid-' . $post->post_parent;
  593. }
  594. if ( is_page_template() ) {
  595. $classes[] = 'page-template';
  596. $template_slug = get_page_template_slug( $page_id );
  597. $template_parts = explode( '/', $template_slug );
  598. foreach ( $template_parts as $part ) {
  599. $classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
  600. }
  601. $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
  602. } else {
  603. $classes[] = 'page-template-default';
  604. }
  605. }
  606. if ( is_user_logged_in() )
  607. $classes[] = 'logged-in';
  608. if ( is_admin_bar_showing() ) {
  609. $classes[] = 'admin-bar';
  610. $classes[] = 'no-customize-support';
  611. }
  612. if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() )
  613. $classes[] = 'custom-background';
  614. if ( has_custom_logo() ) {
  615. $classes[] = 'wp-custom-logo';
  616. }
  617. $page = $wp_query->get( 'page' );
  618. if ( ! $page || $page < 2 )
  619. $page = $wp_query->get( 'paged' );
  620. if ( $page && $page > 1 && ! is_404() ) {
  621. $classes[] = 'paged-' . $page;
  622. if ( is_single() )
  623. $classes[] = 'single-paged-' . $page;
  624. elseif ( is_page() )
  625. $classes[] = 'page-paged-' . $page;
  626. elseif ( is_category() )
  627. $classes[] = 'category-paged-' . $page;
  628. elseif ( is_tag() )
  629. $classes[] = 'tag-paged-' . $page;
  630. elseif ( is_date() )
  631. $classes[] = 'date-paged-' . $page;
  632. elseif ( is_author() )
  633. $classes[] = 'author-paged-' . $page;
  634. elseif ( is_search() )
  635. $classes[] = 'search-paged-' . $page;
  636. elseif ( is_post_type_archive() )
  637. $classes[] = 'post-type-paged-' . $page;
  638. }
  639. if ( ! empty( $class ) ) {
  640. if ( !is_array( $class ) )
  641. $class = preg_split( '#\s+#', $class );
  642. $classes = array_merge( $classes, $class );
  643. } else {
  644. // Ensure that we always coerce class to being an array.
  645. $class = array();
  646. }
  647. $classes = array_map( 'esc_attr', $classes );
  648. /**
  649. * Filter the list of CSS body classes for the current post or page.
  650. *
  651. * @since 2.8.0
  652. *
  653. * @param array $classes An array of body classes.
  654. * @param array $class An array of additional classes added to the body.
  655. */
  656. $classes = apply_filters( 'body_class', $classes, $class );
  657. return array_unique( $classes );
  658. }
  659. /**
  660. * Whether post requires password and correct password has been provided.
  661. *
  662. * @since 2.7.0
  663. *
  664. * @param int|WP_Post|null $post An optional post. Global $post used if not provided.
  665. * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
  666. */
  667. function post_password_required( $post = null ) {
  668. $post = get_post($post);
  669. if ( empty( $post->post_password ) )
  670. return false;
  671. if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
  672. return true;
  673. require_once ABSPATH . WPINC . '/class-phpass.php';
  674. $hasher = new PasswordHash( 8, true );
  675. $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
  676. if ( 0 !== strpos( $hash, '$P$B' ) )
  677. return true;
  678. return ! $hasher->CheckPassword( $post->post_password, $hash );
  679. }
  680. //
  681. // Page Template Functions for usage in Themes
  682. //
  683. /**
  684. * The formatted output of a list of pages.
  685. *
  686. * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
  687. * Quicktag one or more times). This tag must be within The Loop.
  688. *
  689. * @since 1.2.0
  690. *
  691. * @global int $page
  692. * @global int $numpages
  693. * @global int $multipage
  694. * @global int $more
  695. *
  696. * @param string|array $args {
  697. * Optional. Array or string of default arguments.
  698. *
  699. * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`.
  700. * @type string $after HTML or text to append to each link. Default is `</p>`.
  701. * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag.
  702. * Also prepended to the current item, which is not linked. Default empty.
  703. * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag.
  704. * Also appended to the current item, which is not linked. Default empty.
  705. * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number
  706. * and next. Default is 'number'.
  707. * @type string $separator Text between pagination links. Default is ' '.
  708. * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'.
  709. * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
  710. * @type string $pagelink Format string for page numbers. The % in the parameter string will be
  711. * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
  712. * Defaults to '%', just the page number.
  713. * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
  714. * }
  715. * @return string Formatted output in HTML.
  716. */
  717. function wp_link_pages( $args = '' ) {
  718. global $page, $numpages, $multipage, $more;
  719. $defaults = array(
  720. 'before' => '<p>' . __( 'Pages:' ),
  721. 'after' => '</p>',
  722. 'link_before' => '',
  723. 'link_after' => '',
  724. 'next_or_number' => 'number',
  725. 'separator' => ' ',
  726. 'nextpagelink' => __( 'Next page' ),
  727. 'previouspagelink' => __( 'Previous page' ),
  728. 'pagelink' => '%',
  729. 'echo' => 1
  730. );
  731. $params = wp_parse_args( $args, $defaults );
  732. /**
  733. * Filter the arguments used in retrieving page links for paginated posts.
  734. *
  735. * @since 3.0.0
  736. *
  737. * @param array $params An array of arguments for page links for paginated posts.
  738. */
  739. $r = apply_filters( 'wp_link_pages_args', $params );
  740. $output = '';
  741. if ( $multipage ) {
  742. if ( 'number' == $r['next_or_number'] ) {
  743. $output .= $r['before'];
  744. for ( $i = 1; $i <= $numpages; $i++ ) {
  745. $link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after'];
  746. if ( $i != $page || ! $more && 1 == $page ) {
  747. $link = _wp_link_page( $i ) . $link . '</a>';
  748. }
  749. /**
  750. * Filter the HTML output of individual page number links.
  751. *
  752. * @since 3.6.0
  753. *
  754. * @param string $link The page number HTML output.
  755. * @param int $i Page number for paginated posts' page links.
  756. */
  757. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  758. // Use the custom links separator beginning with the second link.
  759. $output .= ( 1 === $i ) ? ' ' : $r['separator'];
  760. $output .= $link;
  761. }
  762. $output .= $r['after'];
  763. } elseif ( $more ) {
  764. $output .= $r['before'];
  765. $prev = $page - 1;
  766. if ( $prev > 0 ) {
  767. $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>';
  768. /** This filter is documented in wp-includes/post-template.php */
  769. $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
  770. }
  771. $next = $page + 1;
  772. if ( $next <= $numpages ) {
  773. if ( $prev ) {
  774. $output .= $r['separator'];
  775. }
  776. $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';
  777. /** This filter is documented in wp-includes/post-template.php */
  778. $output .= apply_filters( 'wp_link_pages_link', $link, $next );
  779. }
  780. $output .= $r['after'];
  781. }
  782. }
  783. /**
  784. * Filter the HTML output of page links for paginated posts.
  785. *
  786. * @since 3.6.0
  787. *
  788. * @param string $output HTML output of paginated posts' page links.
  789. * @param array $args An array of arguments.
  790. */
  791. $html = apply_filters( 'wp_link_pages', $output, $args );
  792. if ( $r['echo'] ) {
  793. echo $html;
  794. }
  795. return $html;
  796. }
  797. /**
  798. * Helper function for wp_link_pages().
  799. *
  800. * @since 3.1.0
  801. * @access private
  802. *
  803. * @global WP_Rewrite $wp_rewrite
  804. *
  805. * @param int $i Page number.
  806. * @return string Link.
  807. */
  808. function _wp_link_page( $i ) {
  809. global $wp_rewrite;
  810. $post = get_post();
  811. $query_args = array();
  812. if ( 1 == $i ) {
  813. $url = get_permalink();
  814. } else {
  815. if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
  816. $url = add_query_arg( 'page', $i, get_permalink() );
  817. elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
  818. $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
  819. else
  820. $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
  821. }
  822. if ( is_preview() ) {
  823. if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
  824. $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
  825. $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
  826. }
  827. $url = get_preview_post_link( $post, $query_args, $url );
  828. }
  829. return '<a href="' . esc_url( $url ) . '">';
  830. }
  831. //
  832. // Post-meta: Custom per-post fields.
  833. //
  834. /**
  835. * Retrieve post custom meta data field.
  836. *
  837. * @since 1.5.0
  838. *
  839. * @param string $key Meta data key name.
  840. * @return false|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
  841. */
  842. function post_custom( $key = '' ) {
  843. $custom = get_post_custom();
  844. if ( !isset( $custom[$key] ) )
  845. return false;
  846. elseif ( 1 == count($custom[$key]) )
  847. return $custom[$key][0];
  848. else
  849. return $custom[$key];
  850. }
  851. /**
  852. * Display list of post custom fields.
  853. *
  854. * @since 1.2.0
  855. *
  856. * @internal This will probably change at some point...
  857. *
  858. */
  859. function the_meta() {
  860. if ( $keys = get_post_custom_keys() ) {
  861. echo "<ul class='post-meta'>\n";
  862. foreach ( (array) $keys as $key ) {
  863. $keyt = trim($key);
  864. if ( is_protected_meta( $keyt, 'post' ) )
  865. continue;
  866. $values = array_map('trim', get_post_custom_values($key));
  867. $value = implode($values,', ');
  868. /**
  869. * Filter the HTML output of the li element in the post custom fields list.
  870. *
  871. * @since 2.2.0
  872. *
  873. * @param string $html The HTML output for the li element.
  874. * @param string $key Meta key.
  875. * @param string $value Meta value.
  876. */
  877. echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
  878. }
  879. echo "</ul>\n";
  880. }
  881. }
  882. //
  883. // Pages
  884. //
  885. /**
  886. * Retrieve or display list of pages as a dropdown (select list).
  887. *
  888. * @since 2.1.0
  889. * @since 4.2.0 The `$value_field` argument was added.
  890. * @since 4.3.0 The `$class` argument was added.
  891. *
  892. * @param array|string $args {
  893. * Optional. Array or string of arguments to generate a pages drop-down element.
  894. *
  895. * @type int $depth Maximum depth. Default 0.
  896. * @type int $child_of Page ID to retrieve child pages of. Default 0.
  897. * @type int|string $selected Value of the option that should be selected. Default 0.
  898. * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1,
  899. * or their bool equivalents. Default 1.
  900. * @type string $name Value for the 'name' attribute of the select element.
  901. * Default 'page_id'.
  902. * @type string $id Value for the 'id' attribute of the select element.
  903. * @type string $class Value for the 'class' attribute of the select element. Default: none.
  904. * Defaults to the value of `$name`.
  905. * @type string $show_option_none Text to display for showing no pages. Default empty (does not display).
  906. * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display).
  907. * @type string $option_none_value Value to use when no page is selected. Default empty.
  908. * @type string $value_field Post field used to populate the 'value' attribute of the option
  909. * elements. Accepts any valid post field. Default 'ID'.
  910. * }
  911. * @return string HTML content, if not displaying.
  912. */
  913. function wp_dropdown_pages( $args = '' ) {
  914. $defaults = array(
  915. 'depth' => 0, 'child_of' => 0,
  916. 'selected' => 0, 'echo' => 1,
  917. 'name' => 'page_id', 'id' => '',
  918. 'class' => '',
  919. 'show_option_none' => '', 'show_option_no_change' => '',
  920. 'option_none_value' => '',
  921. 'value_field' => 'ID',
  922. );
  923. $r = wp_parse_args( $args, $defaults );
  924. $pages = get_pages( $r );
  925. $output = '';
  926. // Back-compat with old system where both id and name were based on $name argument
  927. if ( empty( $r['id'] ) ) {
  928. $r['id'] = $r['name'];
  929. }
  930. if ( ! empty( $pages ) ) {
  931. $class = '';
  932. if ( ! empty( $r['class'] ) ) {
  933. $class = " class='" . esc_attr( $r['class'] ) . "'";
  934. }
  935. $output = "<select name='" . esc_attr( $r['name'] ) . "'" . $class . " id='" . esc_attr( $r['id'] ) . "'>\n";
  936. if ( $r['show_option_no_change'] ) {
  937. $output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n";
  938. }
  939. if ( $r['show_option_none'] ) {
  940. $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '">' . $r['show_option_none'] . "</option>\n";
  941. }
  942. $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r );
  943. $output .= "</select>\n";
  944. }
  945. /**
  946. * Filter the HTML output of a list of pages as a drop down.
  947. *
  948. * @since 2.1.0
  949. * @since 4.4.0 `$r` and `$pages` added as arguments.
  950. *
  951. * @param string $output HTML output for drop down list of pages.
  952. * @param array $r The parsed arguments array.
  953. * @param array $pages List of WP_Post objects returned by `get_pages()`
  954. */
  955. $html = apply_filters( 'wp_dropdown_pages', $output, $r, $pages );
  956. if ( $r['echo'] ) {
  957. echo $html;
  958. }
  959. return $html;
  960. }
  961. /**
  962. * Retrieve or display list of pages in list (li) format.
  963. *
  964. * @since 1.5.0
  965. *
  966. * @see get_pages()
  967. *
  968. * @global WP_Query $wp_query
  969. *
  970. * @param array|string $args {
  971. * Array or string of arguments. Optional.
  972. *
  973. * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
  974. * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
  975. * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
  976. * Default is the value of 'date_format' option.
  977. * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
  978. * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
  979. * the given n depth). Default 0.
  980. * @type bool $echo Whether or not to echo the list of pages. Default true.
  981. * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
  982. * @type array $include Comma-separated list of page IDs to include. Default empty.
  983. * @type string $link_after Text or HTML to follow the page link label. Default null.
  984. * @type string $link_before Text or HTML to precede the page link label. Default null.
  985. * @type string $post_type Post type to query for. Default 'page'.
  986. * @type string $post_status Comma-separated list of post statuses to include. Default 'publish'.
  987. * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
  988. * 'modified' or any other value. An empty value hides the date. Default empty.
  989. * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
  990. * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
  991. * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
  992. * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
  993. * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
  994. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
  995. * }
  996. * @return string|void HTML list of pages.
  997. */
  998. function wp_list_pages( $args = '' ) {
  999. $defaults = array(
  1000. 'depth' => 0, 'show_date' => '',
  1001. 'date_format' => get_option( 'date_format' ),
  1002. 'child_of' => 0, 'exclude' => '',
  1003. 'title_li' => __( 'Pages' ), 'echo' => 1,
  1004. 'authors' => '', 'sort_column' => 'menu_order, post_title',
  1005. 'link_before' => '', 'link_after' => '', 'walker' => '',
  1006. );
  1007. $r = wp_parse_args( $args, $defaults );
  1008. $output = '';
  1009. $current_page = 0;
  1010. // sanitize, mostly to keep spaces out
  1011. $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );
  1012. // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
  1013. $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();
  1014. /**
  1015. * Filter the array of pages to exclude from the pages list.
  1016. *
  1017. * @since 2.1.0
  1018. *
  1019. * @param array $exclude_array An array of page IDs to exclude.
  1020. */
  1021. $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
  1022. // Query pages.
  1023. $r['hierarchical'] = 0;
  1024. $pages = get_pages( $r );
  1025. if ( ! empty( $pages ) ) {
  1026. if ( $r['title_li'] ) {
  1027. $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
  1028. }
  1029. global $wp_query;
  1030. if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
  1031. $current_page = get_queried_object_id();
  1032. } elseif ( is_singular() ) {
  1033. $queried_object = get_queried_object();
  1034. if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
  1035. $current_page = $queried_object->ID;
  1036. }
  1037. }
  1038. $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r );
  1039. if ( $r['title_li'] ) {
  1040. $output .= '</ul></li>';
  1041. }
  1042. }
  1043. /**
  1044. * Filter the HTML output of the pages to list.
  1045. *
  1046. * @since 1.5.1
  1047. * @since 4.4.0 `$pages` added as arguments.
  1048. *
  1049. * @see wp_list_pages()
  1050. *
  1051. * @param string $output HTML output of the pages list.
  1052. * @param array $r An array of page-listing arguments.
  1053. * @param array $pages List of WP_Post objects returned by `get_pages()`
  1054. */
  1055. $html = apply_filters( 'wp_list_pages', $output, $r, $pages );
  1056. if ( $r['echo'] ) {
  1057. echo $html;
  1058. } else {
  1059. return $html;
  1060. }
  1061. }
  1062. /**
  1063. * Display or retrieve list of pages with optional home link.
  1064. *
  1065. * The arguments are listed below and part of the arguments are for {@link
  1066. * wp_list_pages()} function. Check that function for more info on those
  1067. * arguments.
  1068. *
  1069. * @since 2.7.0
  1070. * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
  1071. *
  1072. * @param array|string $args {
  1073. * Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
  1074. *
  1075. * @type string $sort_column How to short the list of pages. Accepts post column names.
  1076. * Default 'menu_order, post_title'.
  1077. * @type string $menu_id ID for the div containing the page list. Default is empty string.
  1078. * @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
  1079. * @type string $container Element to use for the element containing the page list. Default 'div'.
  1080. * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
  1081. * Default true.
  1082. * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
  1083. * you'd like shown for the home link. 1|true defaults to 'Home'.
  1084. * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
  1085. * @type string $link_after The HTML or text to append to $show_home text. Default empty.
  1086. * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
  1087. * @type string $after The HTML or text to append to the menu. Default is '</ul>'.
  1088. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
  1089. * }
  1090. * @return string|void HTML menu
  1091. */
  1092. function wp_page_menu( $args = array() ) {
  1093. $defaults = array(
  1094. 'sort_column' => 'menu_order, post_title',
  1095. 'menu_id' => '',
  1096. 'menu_class' => 'menu',
  1097. 'container' => 'div',
  1098. 'echo' => true,
  1099. 'link_before' => '',
  1100. 'link_after' => '',
  1101. 'before' => '<ul>',
  1102. 'after' => '</ul>',
  1103. 'walker' => '',
  1104. );
  1105. $args = wp_parse_args( $args, $defaults );
  1106. /**
  1107. * Filter the arguments used to generate a page-based menu.
  1108. *
  1109. * @since 2.7.0
  1110. *
  1111. * @see wp_page_menu()
  1112. *
  1113. * @param array $args An array of page menu arguments.
  1114. */
  1115. $args = apply_filters( 'wp_page_menu_args', $args );
  1116. $menu = '';
  1117. $list_args = $args;
  1118. // Show Home in the menu
  1119. if ( ! empty($args['show_home']) ) {
  1120. if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
  1121. $text = __('Home');
  1122. else
  1123. $text = $args['show_home'];
  1124. $class = '';
  1125. if ( is_front_page() && !is_paged() )
  1126. $class = 'class="current_page_item"';
  1127. $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
  1128. // If the front page is a page, add it to the exclude list
  1129. if (get_option('show_on_front') == 'page') {
  1130. if ( !empty( $list_args['exclude'] ) ) {
  1131. $list_args['exclude'] .= ',';
  1132. } else {
  1133. $list_args['exclude'] = '';
  1134. }
  1135. $list_args['exclude'] .= get_option('page_on_front');
  1136. }
  1137. }
  1138. $list_args['echo'] = false;
  1139. $list_args['title_li'] = '';
  1140. $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
  1141. $container = sanitize_text_field( $args['container'] );
  1142. // Fallback in case `wp_nav_menu()` was called without a container.
  1143. if ( empty( $container ) ) {
  1144. $container = 'div';
  1145. }
  1146. if ( $menu ) {
  1147. // wp_nav_menu doesn't set before and after
  1148. if ( isset( $args['fallback_cb'] ) &&
  1149. 'wp_page_menu' === $args['fallback_cb'] &&
  1150. 'ul' !== $container ) {
  1151. $args['before'] = '<ul>';
  1152. $args['after'] = '</ul>';
  1153. }
  1154. $menu = $args['before'] . $menu . $args['after'];
  1155. }
  1156. $attrs = '';
  1157. if ( ! empty( $args['menu_id'] ) ) {
  1158. $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
  1159. }
  1160. if ( ! empty( $args['menu_class'] ) ) {
  1161. $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
  1162. }
  1163. $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>\n";
  1164. /**
  1165. * Filter the HTML output of a page-based menu.
  1166. *
  1167. * @since 2.7.0
  1168. *
  1169. * @see wp_page_menu()
  1170. *
  1171. * @param string $menu The HTML output.
  1172. * @param array $args An array of arguments.
  1173. */
  1174. $menu = apply_filters( 'wp_page_menu', $menu, $args );
  1175. if ( $args['echo'] )
  1176. echo $menu;
  1177. else
  1178. return $menu;
  1179. }
  1180. //
  1181. // Page helpers
  1182. //
  1183. /**
  1184. * Retrieve HTML list content for page list.
  1185. *
  1186. * @uses Walker_Page to create HTML list content.
  1187. * @since 2.1.0
  1188. *
  1189. * @param array $pages
  1190. * @param int $depth
  1191. * @param int $current_page
  1192. * @param array $r
  1193. * @return string
  1194. */
  1195. function walk_page_tree( $pages, $depth, $current_page, $r ) {
  1196. if ( empty($r['walker']) )
  1197. $walker = new Walker_Page;
  1198. else
  1199. $walker = $r['walker'];
  1200. foreach ( (array) $pages as $page ) {
  1201. if ( $page->post_parent )
  1202. $r['pages_with_children'][ $page->post_parent ] = true;
  1203. }
  1204. $args = array($pages, $depth, $r, $current_page);
  1205. return call_user_func_array(array($walker, 'walk'), $args);
  1206. }
  1207. /**
  1208. * Retrieve HTML dropdown (select) content for page list.
  1209. *
  1210. * @uses Walker_PageDropdown to create HTML dropdown content.
  1211. * @since 2.1.0
  1212. * @see Walker_PageDropdown::walk() for parameters and return description.
  1213. *
  1214. * @return string
  1215. */
  1216. function walk_page_dropdown_tree() {
  1217. $args = func_get_args();
  1218. if ( empty($args[2]['walker']) ) // the user's options are the third parameter
  1219. $walker = new Walker_PageDropdown;
  1220. else
  1221. $walker = $args[2]['walker'];
  1222. return call_user_func_array(array($walker, 'walk'), $args);
  1223. }
  1224. //
  1225. // Attachments
  1226. //
  1227. /**
  1228. * Display an attachment page link using an image or icon.
  1229. *
  1230. * @since 2.0.0
  1231. *
  1232. * @param int|WP_Post $id Optional. Post ID or post object.
  1233. * @param bool $fullsize Optional, default is false. Whether to use full size.
  1234. * @param bool $deprecated Deprecated. Not used.
  1235. * @param bool $permalink Optional, default is false. Whether to include permalink.
  1236. */
  1237. function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
  1238. if ( !empty( $deprecated ) )
  1239. _deprecated_argument( __FUNCTION__, '2.5' );
  1240. if ( $fullsize )
  1241. echo wp_get_attachment_link($id, 'full', $permalink);
  1242. else
  1243. echo wp_get_attachment_link($id, 'thumbnail', $permalink);
  1244. }
  1245. /**
  1246. * Retrieve an attachment page link using an image or icon, if possible.
  1247. *
  1248. * @since 2.5.0
  1249. * @since 4.4.0 The `$id` parameter can now accept either a post ID or `WP_Post` object.
  1250. *
  1251. * @param int|WP_Post $id Optional. Post ID or post object.
  1252. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array
  1253. * of width and height values in pixels (in that order).
  1254. * Default 'thumbnail'.
  1255. * @param bool $permalink Optional, Whether to add permalink to image. Default false.
  1256. * @param bool $icon Optional. Whether the attachment is an icon. Default false.
  1257. * @param string|false $text Optional. Link text to use. Activated by passing a string, false otherwise.
  1258. * Default false.
  1259. * @param array|string $attr Optional. Array or string of attributes. Default empty.
  1260. * @return string HTML content.
  1261. */
  1262. function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
  1263. $_post = get_post( $id );
  1264. if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
  1265. return __( 'Missing Attachment' );
  1266. if ( $permalink )
  1267. $url = get_attachment_link( $_post->ID );
  1268. if ( $text ) {
  1269. $link_text = $text;
  1270. } elseif ( $size && 'none' != $size ) {
  1271. $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
  1272. } else {
  1273. $link_text = '';
  1274. }
  1275. if ( trim( $link_text ) == '' )
  1276. $link_text = $_post->post_title;
  1277. /**
  1278. * Filter a retrieved attachment page link.
  1279. *
  1280. * @since 2.7.0
  1281. *
  1282. * @param string $link_html The page link HTML output.
  1283. * @param int $id Post ID.
  1284. * @param string|array $size Size of the image. Image size or array of width and height values (in that order).
  1285. * Default 'thumbnail'.
  1286. * @param bool $permalink Whether to add permalink to image. Default false.
  1287. * @param bool $icon Whether to include an icon. Default false.
  1288. * @param string|bool $text If string, will be link text. Default false.
  1289. */
  1290. return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );
  1291. }
  1292. /**
  1293. * Wrap attachment in paragraph tag before content.
  1294. *
  1295. * @since 2.0.0
  1296. *
  1297. * @param string $content
  1298. * @return string
  1299. */
  1300. function prepend_attachment($content) {
  1301. $post = get_post();
  1302. if ( empty($post->post_type) || $post->post_type != 'attachment' )
  1303. return $content;
  1304. if ( wp_attachment_is( 'video', $post ) ) {
  1305. $meta = wp_get_attachment_metadata( get_the_ID() );
  1306. $atts = array( 'src' => wp_get_attachment_url() );
  1307. if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
  1308. $atts['width'] = (int) $meta['width'];
  1309. $atts['height'] = (int) $meta['height'];
  1310. }
  1311. if ( has_post_thumbnail() ) {
  1312. $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
  1313. }
  1314. $p = wp_video_shortcode( $atts );
  1315. } elseif ( wp_attachment_is( 'audio', $post ) ) {
  1316. $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
  1317. } else {
  1318. $p = '<p class="attachment">';
  1319. // show the medium sized image representation of the attachment if available, and link to the raw file
  1320. $p .= wp_get_attachment_link(0, 'medium', false);
  1321. $p .= '</p>';
  1322. }
  1323. /**
  1324. * Filter the attachment markup to be prepended to the post content.
  1325. *
  1326. * @since 2.0.0
  1327. *
  1328. * @see prepend_attachment()
  1329. *
  1330. * @param string $p The attachment HTML output.
  1331. */
  1332. $p = apply_filters( 'prepend_attachment', $p );
  1333. return "$p\n$content";
  1334. }
  1335. //
  1336. // Misc
  1337. //
  1338. /**
  1339. * Retrieve protected post password form content.
  1340. *
  1341. * @since 1.0.0
  1342. *
  1343. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  1344. * @return string HTML content for password form for password protected post.
  1345. */
  1346. function get_the_password_form( $post = 0 ) {
  1347. $post = get_post( $post );
  1348. $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
  1349. $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
  1350. <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
  1351. <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>
  1352. ';
  1353. /**
  1354. * Filter the HTML output for the protected post password form.
  1355. *
  1356. * If modifying the password field, please note that the core database schema
  1357. * limits the password field to 20 characters regardless of the value of the
  1358. * size attribute in the form input.
  1359. *
  1360. * @since 2.7.0
  1361. *
  1362. * @param string $output The password form HTML output.
  1363. */
  1364. return apply_filters( 'the_password_form', $output );
  1365. }
  1366. /**
  1367. * Whether currently in a page template.
  1368. *
  1369. * This template tag allows you to determine if you are in a page template.
  1370. * You can optionally provide a template name or array of template names
  1371. * and then the check will be specific to that template.
  1372. *
  1373. * @since 2.5.0
  1374. * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
  1375. *
  1376. * @param string|array $template The specific template name or array of templates to match.
  1377. * @return bool True on success, false on failure.
  1378. */
  1379. function is_page_template( $template = '' ) {
  1380. if ( ! is_page() )
  1381. return false;
  1382. $page_template = get_page_template_slug( get_queried_object_id() );
  1383. if ( empty( $template ) )
  1384. return (bool) $page_template;
  1385. if ( $template == $page_template )
  1386. return true;
  1387. if ( is_array( $template ) ) {
  1388. if ( ( in_array( 'default', $template, true ) && ! $page_template )
  1389. || in_array( $page_template, $template, true )
  1390. ) {
  1391. return t