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

/wp-includes/post-template.php

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