PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/post-template.php

https://gitlab.com/Gashler/dp
PHP | 1432 lines | 710 code | 196 blank | 526 comment | 202 complexity | b3a5e6b017178b1116b6d0c3f153a276 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. * @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 Optional. Override the defaults.
  62. * @return string|null Null on failure or display. String when echo is false.
  63. */
  64. function the_title_attribute( $args = '' ) {
  65. $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post() );
  66. $r = wp_parse_args($args, $defaults);
  67. extract( $r, EXTR_SKIP );
  68. $title = get_the_title( $post );
  69. if ( strlen($title) == 0 )
  70. return;
  71. $title = $before . $title . $after;
  72. $title = esc_attr(strip_tags($title));
  73. if ( $echo )
  74. echo $title;
  75. else
  76. return $title;
  77. }
  78. /**
  79. * Retrieve post title.
  80. *
  81. * If the post is protected and the visitor is not an admin, then "Protected"
  82. * will be displayed before the post title. If the post is private, then
  83. * "Private" will be located before the post title.
  84. *
  85. * @since 0.71
  86. *
  87. * @param int|object $post Optional. Post ID or object.
  88. * @return string
  89. */
  90. function get_the_title( $post = 0 ) {
  91. $post = get_post( $post );
  92. $title = isset( $post->post_title ) ? $post->post_title : '';
  93. $id = isset( $post->ID ) ? $post->ID : 0;
  94. if ( ! is_admin() ) {
  95. if ( ! empty( $post->post_password ) ) {
  96. $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) );
  97. $title = sprintf( $protected_title_format, $title );
  98. } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) {
  99. $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) );
  100. $title = sprintf( $private_title_format, $title );
  101. }
  102. }
  103. return apply_filters( 'the_title', $title, $id );
  104. }
  105. /**
  106. * Display the Post Global Unique Identifier (guid).
  107. *
  108. * The guid will appear to be a link, but should not be used as an link to the
  109. * post. The reason you should not use it as a link, is because of moving the
  110. * blog across domains.
  111. *
  112. * Url is escaped to make it xml safe
  113. *
  114. * @since 1.5.0
  115. *
  116. * @param int $id Optional. Post ID.
  117. */
  118. function the_guid( $id = 0 ) {
  119. echo esc_url( get_the_guid( $id ) );
  120. }
  121. /**
  122. * Retrieve the Post Global Unique Identifier (guid).
  123. *
  124. * The guid will appear to be a link, but should not be used as an link to the
  125. * post. The reason you should not use it as a link, is because of moving the
  126. * blog across domains.
  127. *
  128. * @since 1.5.0
  129. *
  130. * @param int $id Optional. Post ID.
  131. * @return string
  132. */
  133. function get_the_guid( $id = 0 ) {
  134. $post = get_post($id);
  135. return apply_filters('get_the_guid', $post->guid);
  136. }
  137. /**
  138. * Display the post content.
  139. *
  140. * @since 0.71
  141. *
  142. * @param string $more_link_text Optional. Content for when there is more text.
  143. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
  144. */
  145. function the_content( $more_link_text = null, $strip_teaser = false) {
  146. $content = get_the_content( $more_link_text, $strip_teaser );
  147. $content = apply_filters( 'the_content', $content );
  148. $content = str_replace( ']]>', ']]&gt;', $content );
  149. echo $content;
  150. }
  151. /**
  152. * Retrieve the post content.
  153. *
  154. * @since 0.71
  155. *
  156. * @param string $more_link_text Optional. Content for when there is more text.
  157. * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
  158. * @return string
  159. */
  160. function get_the_content( $more_link_text = null, $strip_teaser = false ) {
  161. global $page, $more, $preview, $pages, $multipage;
  162. $post = get_post();
  163. if ( null === $more_link_text )
  164. $more_link_text = __( '(more&hellip;)' );
  165. $output = '';
  166. $has_teaser = false;
  167. // If post password required and it doesn't match the cookie.
  168. if ( post_password_required( $post ) )
  169. return get_the_password_form( $post );
  170. if ( $page > count( $pages ) ) // if the requested page doesn't exist
  171. $page = count( $pages ); // give them the highest numbered page that DOES exist
  172. $content = $pages[$page - 1];
  173. if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
  174. $content = explode( $matches[0], $content, 2 );
  175. if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
  176. $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
  177. $has_teaser = true;
  178. } else {
  179. $content = array( $content );
  180. }
  181. if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
  182. $strip_teaser = true;
  183. $teaser = $content[0];
  184. if ( $more && $strip_teaser && $has_teaser )
  185. $teaser = '';
  186. $output .= $teaser;
  187. if ( count( $content ) > 1 ) {
  188. if ( $more ) {
  189. $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
  190. } else {
  191. if ( ! empty( $more_link_text ) )
  192. $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
  193. $output = force_balance_tags( $output );
  194. }
  195. }
  196. if ( $preview ) // preview fix for javascript bug with foreign languages
  197. $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
  198. return $output;
  199. }
  200. /**
  201. * Preview fix for javascript bug with foreign languages
  202. *
  203. * @since 3.1.0
  204. * @access private
  205. * @param array $match Match array from preg_replace_callback
  206. * @return string
  207. */
  208. function _convert_urlencoded_to_entities( $match ) {
  209. return '&#' . base_convert( $match[1], 16, 10 ) . ';';
  210. }
  211. /**
  212. * Display the post excerpt.
  213. *
  214. * @since 0.71
  215. * @uses apply_filters() Calls 'the_excerpt' hook on post excerpt.
  216. */
  217. function the_excerpt() {
  218. echo apply_filters('the_excerpt', get_the_excerpt());
  219. }
  220. /**
  221. * Retrieve the post excerpt.
  222. *
  223. * @since 0.71
  224. *
  225. * @param mixed $deprecated Not used.
  226. * @return string
  227. */
  228. function get_the_excerpt( $deprecated = '' ) {
  229. if ( !empty( $deprecated ) )
  230. _deprecated_argument( __FUNCTION__, '2.3' );
  231. $post = get_post();
  232. if ( post_password_required() ) {
  233. return __( 'There is no excerpt because this is a protected post.' );
  234. }
  235. return apply_filters( 'get_the_excerpt', $post->post_excerpt );
  236. }
  237. /**
  238. * Whether post has excerpt.
  239. *
  240. * @since 2.3.0
  241. *
  242. * @param int $id Optional. Post ID.
  243. * @return bool
  244. */
  245. function has_excerpt( $id = 0 ) {
  246. $post = get_post( $id );
  247. return ( !empty( $post->post_excerpt ) );
  248. }
  249. /**
  250. * Display the classes for the post div.
  251. *
  252. * @since 2.7.0
  253. *
  254. * @param string|array $class One or more classes to add to the class list.
  255. * @param int $post_id An optional post ID.
  256. */
  257. function post_class( $class = '', $post_id = null ) {
  258. // Separates classes with a single space, collates classes for post DIV
  259. echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
  260. }
  261. /**
  262. * Retrieve the classes for the post div as an array.
  263. *
  264. * The class names are add are many. If the post is a sticky, then the 'sticky'
  265. * class name. The class 'hentry' is always added to each post. For each
  266. * category, the class will be added with 'category-' with category slug is
  267. * added. The tags are the same way as the categories with 'tag-' before the tag
  268. * slug. All classes are passed through the filter, 'post_class' with the list
  269. * of classes, followed by $class parameter value, with the post ID as the last
  270. * parameter.
  271. *
  272. * @since 2.7.0
  273. *
  274. * @param string|array $class One or more classes to add to the class list.
  275. * @param int $post_id An optional post ID.
  276. * @return array Array of classes.
  277. */
  278. function get_post_class( $class = '', $post_id = null ) {
  279. $post = get_post($post_id);
  280. $classes = array();
  281. if ( empty($post) )
  282. return $classes;
  283. $classes[] = 'post-' . $post->ID;
  284. if ( ! is_admin() )
  285. $classes[] = $post->post_type;
  286. $classes[] = 'type-' . $post->post_type;
  287. $classes[] = 'status-' . $post->post_status;
  288. // Post Format
  289. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  290. $post_format = get_post_format( $post->ID );
  291. if ( $post_format && !is_wp_error($post_format) )
  292. $classes[] = 'format-' . sanitize_html_class( $post_format );
  293. else
  294. $classes[] = 'format-standard';
  295. }
  296. // post requires password
  297. if ( post_password_required($post->ID) )
  298. $classes[] = 'post-password-required';
  299. // sticky for Sticky Posts
  300. if ( is_sticky($post->ID) && is_home() && !is_paged() )
  301. $classes[] = 'sticky';
  302. // hentry for hAtom compliance
  303. $classes[] = 'hentry';
  304. // Categories
  305. if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
  306. foreach ( (array) get_the_category($post->ID) as $cat ) {
  307. if ( empty($cat->slug ) )
  308. continue;
  309. $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
  310. }
  311. }
  312. // Tags
  313. if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
  314. foreach ( (array) get_the_tags($post->ID) as $tag ) {
  315. if ( empty($tag->slug ) )
  316. continue;
  317. $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
  318. }
  319. }
  320. if ( !empty($class) ) {
  321. if ( !is_array( $class ) )
  322. $class = preg_split('#\s+#', $class);
  323. $classes = array_merge($classes, $class);
  324. }
  325. $classes = array_map('esc_attr', $classes);
  326. return apply_filters('post_class', $classes, $class, $post->ID);
  327. }
  328. /**
  329. * Display the classes for the body element.
  330. *
  331. * @since 2.8.0
  332. *
  333. * @param string|array $class One or more classes to add to the class list.
  334. */
  335. function body_class( $class = '' ) {
  336. // Separates classes with a single space, collates classes for body element
  337. echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
  338. }
  339. /**
  340. * Retrieve the classes for the body element as an array.
  341. *
  342. * @since 2.8.0
  343. *
  344. * @param string|array $class One or more classes to add to the class list.
  345. * @return array Array of classes.
  346. */
  347. function get_body_class( $class = '' ) {
  348. global $wp_query, $wpdb;
  349. $classes = array();
  350. if ( is_rtl() )
  351. $classes[] = 'rtl';
  352. if ( is_front_page() )
  353. $classes[] = 'home';
  354. if ( is_home() )
  355. $classes[] = 'blog';
  356. if ( is_archive() )
  357. $classes[] = 'archive';
  358. if ( is_date() )
  359. $classes[] = 'date';
  360. if ( is_search() ) {
  361. $classes[] = 'search';
  362. $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
  363. }
  364. if ( is_paged() )
  365. $classes[] = 'paged';
  366. if ( is_attachment() )
  367. $classes[] = 'attachment';
  368. if ( is_404() )
  369. $classes[] = 'error404';
  370. if ( is_single() ) {
  371. $post_id = $wp_query->get_queried_object_id();
  372. $post = $wp_query->get_queried_object();
  373. $classes[] = 'single';
  374. if ( isset( $post->post_type ) ) {
  375. $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
  376. $classes[] = 'postid-' . $post_id;
  377. // Post Format
  378. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  379. $post_format = get_post_format( $post->ID );
  380. if ( $post_format && !is_wp_error($post_format) )
  381. $classes[] = 'single-format-' . sanitize_html_class( $post_format );
  382. else
  383. $classes[] = 'single-format-standard';
  384. }
  385. }
  386. if ( is_attachment() ) {
  387. $mime_type = get_post_mime_type($post_id);
  388. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
  389. $classes[] = 'attachmentid-' . $post_id;
  390. $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
  391. }
  392. } elseif ( is_archive() ) {
  393. if ( is_post_type_archive() ) {
  394. $classes[] = 'post-type-archive';
  395. $classes[] = 'post-type-archive-' . sanitize_html_class( get_query_var( 'post_type' ) );
  396. } else if ( is_author() ) {
  397. $author = $wp_query->get_queried_object();
  398. $classes[] = 'author';
  399. if ( isset( $author->user_nicename ) ) {
  400. $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
  401. $classes[] = 'author-' . $author->ID;
  402. }
  403. } elseif ( is_category() ) {
  404. $cat = $wp_query->get_queried_object();
  405. $classes[] = 'category';
  406. if ( isset( $cat->term_id ) ) {
  407. $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
  408. $classes[] = 'category-' . $cat->term_id;
  409. }
  410. } elseif ( is_tag() ) {
  411. $tags = $wp_query->get_queried_object();
  412. $classes[] = 'tag';
  413. if ( isset( $tags->term_id ) ) {
  414. $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
  415. $classes[] = 'tag-' . $tags->term_id;
  416. }
  417. } elseif ( is_tax() ) {
  418. $term = $wp_query->get_queried_object();
  419. if ( isset( $term->term_id ) ) {
  420. $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
  421. $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
  422. $classes[] = 'term-' . $term->term_id;
  423. }
  424. }
  425. } elseif ( is_page() ) {
  426. $classes[] = 'page';
  427. $page_id = $wp_query->get_queried_object_id();
  428. $post = get_post($page_id);
  429. $classes[] = 'page-id-' . $page_id;
  430. if ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $page_id) ) )
  431. $classes[] = 'page-parent';
  432. if ( $post->post_parent ) {
  433. $classes[] = 'page-child';
  434. $classes[] = 'parent-pageid-' . $post->post_parent;
  435. }
  436. if ( is_page_template() ) {
  437. $classes[] = 'page-template';
  438. $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
  439. } else {
  440. $classes[] = 'page-template-default';
  441. }
  442. }
  443. if ( is_user_logged_in() )
  444. $classes[] = 'logged-in';
  445. if ( is_admin_bar_showing() ) {
  446. $classes[] = 'admin-bar';
  447. $classes[] = 'no-customize-support';
  448. }
  449. if ( get_theme_mod( 'background_color' ) || get_background_image() )
  450. $classes[] = 'custom-background';
  451. $page = $wp_query->get( 'page' );
  452. if ( !$page || $page < 2)
  453. $page = $wp_query->get( 'paged' );
  454. if ( $page && $page > 1 ) {
  455. $classes[] = 'paged-' . $page;
  456. if ( is_single() )
  457. $classes[] = 'single-paged-' . $page;
  458. elseif ( is_page() )
  459. $classes[] = 'page-paged-' . $page;
  460. elseif ( is_category() )
  461. $classes[] = 'category-paged-' . $page;
  462. elseif ( is_tag() )
  463. $classes[] = 'tag-paged-' . $page;
  464. elseif ( is_date() )
  465. $classes[] = 'date-paged-' . $page;
  466. elseif ( is_author() )
  467. $classes[] = 'author-paged-' . $page;
  468. elseif ( is_search() )
  469. $classes[] = 'search-paged-' . $page;
  470. elseif ( is_post_type_archive() )
  471. $classes[] = 'post-type-paged-' . $page;
  472. }
  473. if ( ! empty( $class ) ) {
  474. if ( !is_array( $class ) )
  475. $class = preg_split( '#\s+#', $class );
  476. $classes = array_merge( $classes, $class );
  477. } else {
  478. // Ensure that we always coerce class to being an array.
  479. $class = array();
  480. }
  481. $classes = array_map( 'esc_attr', $classes );
  482. return apply_filters( 'body_class', $classes, $class );
  483. }
  484. /**
  485. * Whether post requires password and correct password has been provided.
  486. *
  487. * @since 2.7.0
  488. *
  489. * @param int|WP_Post $post An optional post. Global $post used if not provided.
  490. * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
  491. */
  492. function post_password_required( $post = null ) {
  493. $post = get_post($post);
  494. if ( empty( $post->post_password ) )
  495. return false;
  496. if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
  497. return true;
  498. require_once ABSPATH . 'wp-includes/class-phpass.php';
  499. $hasher = new PasswordHash( 8, true );
  500. $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
  501. if ( 0 !== strpos( $hash, '$P$B' ) )
  502. return true;
  503. return ! $hasher->CheckPassword( $post->post_password, $hash );
  504. }
  505. /**
  506. * Page Template Functions for usage in Themes
  507. *
  508. * @package WordPress
  509. * @subpackage Template
  510. */
  511. /**
  512. * The formatted output of a list of pages.
  513. *
  514. * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
  515. * Quicktag one or more times). This tag must be within The Loop.
  516. *
  517. * The defaults for overwriting are:
  518. * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
  519. * each bookmarks.
  520. * 'after' - Default is '</p>' (string). The html or text to append to each
  521. * bookmarks.
  522. * 'link_before' - Default is '' (string). The html or text to prepend to each
  523. * Pages link inside the <a> tag. Also prepended to the current item, which
  524. * is not linked.
  525. * 'link_after' - Default is '' (string). The html or text to append to each
  526. * Pages link inside the <a> tag. Also appended to the current item, which
  527. * is not linked.
  528. * 'next_or_number' - Default is 'number' (string). Indicates whether page
  529. * numbers should be used. Valid values are number and next.
  530. * 'separator' - Default is ' ' (string). Text used between pagination links.
  531. * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
  532. * of the bookmark.
  533. * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
  534. * previous page, if available.
  535. * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
  536. * the parameter string will be replaced with the page number, so Page %
  537. * generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
  538. * 'echo' - Default is 1 (integer). When not 0, this triggers the HTML to be
  539. * echoed and then returned.
  540. *
  541. * @since 1.2.0
  542. *
  543. * @param string|array $args Optional. Overwrite the defaults.
  544. * @return string Formatted output in HTML.
  545. */
  546. function wp_link_pages( $args = '' ) {
  547. $defaults = array(
  548. 'before' => '<p>' . __( 'Pages:' ),
  549. 'after' => '</p>',
  550. 'link_before' => '',
  551. 'link_after' => '',
  552. 'next_or_number' => 'number',
  553. 'separator' => ' ',
  554. 'nextpagelink' => __( 'Next page' ),
  555. 'previouspagelink' => __( 'Previous page' ),
  556. 'pagelink' => '%',
  557. 'echo' => 1
  558. );
  559. $r = wp_parse_args( $args, $defaults );
  560. $r = apply_filters( 'wp_link_pages_args', $r );
  561. extract( $r, EXTR_SKIP );
  562. global $page, $numpages, $multipage, $more, $pagenow;
  563. $output = '';
  564. if ( $multipage ) {
  565. if ( 'number' == $next_or_number ) {
  566. $output .= $before;
  567. for ( $i = 1; $i <= $numpages; $i++ ) {
  568. $link = $link_before . str_replace( '%', $i, $pagelink ) . $link_after;
  569. if ( $i != $page || ! $more && 1 == $page )
  570. $link = _wp_link_page( $i ) . $link . '</a>';
  571. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  572. $output .= $separator . $link;
  573. }
  574. $output .= $after;
  575. } elseif ( $more ) {
  576. $output .= $before;
  577. $i = $page - 1;
  578. if ( $i ) {
  579. $link = _wp_link_page( $i ) . $link_before . $previouspagelink . $link_after . '</a>';
  580. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  581. $output .= $separator . $link;
  582. }
  583. $i = $page + 1;
  584. if ( $i <= $numpages ) {
  585. $link = _wp_link_page( $i ) . $link_before . $nextpagelink . $link_after . '</a>';
  586. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  587. $output .= $separator . $link;
  588. }
  589. $output .= $after;
  590. }
  591. }
  592. $output = apply_filters( 'wp_link_pages', $output, $args );
  593. if ( $echo )
  594. echo $output;
  595. return $output;
  596. }
  597. /**
  598. * Helper function for wp_link_pages().
  599. *
  600. * @since 3.1.0
  601. * @access private
  602. *
  603. * @param int $i Page number.
  604. * @return string Link.
  605. */
  606. function _wp_link_page( $i ) {
  607. global $wp_rewrite;
  608. $post = get_post();
  609. if ( 1 == $i ) {
  610. $url = get_permalink();
  611. } else {
  612. if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
  613. $url = add_query_arg( 'page', $i, get_permalink() );
  614. elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
  615. $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
  616. else
  617. $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
  618. }
  619. return '<a href="' . esc_url( $url ) . '">';
  620. }
  621. //
  622. // Post-meta: Custom per-post fields.
  623. //
  624. /**
  625. * Retrieve post custom meta data field.
  626. *
  627. * @since 1.5.0
  628. *
  629. * @param string $key Meta data key name.
  630. * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
  631. */
  632. function post_custom( $key = '' ) {
  633. $custom = get_post_custom();
  634. if ( !isset( $custom[$key] ) )
  635. return false;
  636. elseif ( 1 == count($custom[$key]) )
  637. return $custom[$key][0];
  638. else
  639. return $custom[$key];
  640. }
  641. /**
  642. * Display list of post custom fields.
  643. *
  644. * @internal This will probably change at some point...
  645. * @since 1.2.0
  646. * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
  647. */
  648. function the_meta() {
  649. if ( $keys = get_post_custom_keys() ) {
  650. echo "<ul class='post-meta'>\n";
  651. foreach ( (array) $keys as $key ) {
  652. $keyt = trim($key);
  653. if ( is_protected_meta( $keyt, 'post' ) )
  654. continue;
  655. $values = array_map('trim', get_post_custom_values($key));
  656. $value = implode($values,', ');
  657. echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
  658. }
  659. echo "</ul>\n";
  660. }
  661. }
  662. //
  663. // Pages
  664. //
  665. /**
  666. * Retrieve or display list of pages as a dropdown (select list).
  667. *
  668. * @since 2.1.0
  669. *
  670. * @param array|string $args Optional. Override default arguments.
  671. * @return string HTML content, if not displaying.
  672. */
  673. function wp_dropdown_pages($args = '') {
  674. $defaults = array(
  675. 'depth' => 0, 'child_of' => 0,
  676. 'selected' => 0, 'echo' => 1,
  677. 'name' => 'page_id', 'id' => '',
  678. 'show_option_none' => '', 'show_option_no_change' => '',
  679. 'option_none_value' => ''
  680. );
  681. $r = wp_parse_args( $args, $defaults );
  682. extract( $r, EXTR_SKIP );
  683. $pages = get_pages($r);
  684. $output = '';
  685. // Back-compat with old system where both id and name were based on $name argument
  686. if ( empty($id) )
  687. $id = $name;
  688. if ( ! empty($pages) ) {
  689. $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
  690. if ( $show_option_no_change )
  691. $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
  692. if ( $show_option_none )
  693. $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
  694. $output .= walk_page_dropdown_tree($pages, $depth, $r);
  695. $output .= "</select>\n";
  696. }
  697. $output = apply_filters('wp_dropdown_pages', $output);
  698. if ( $echo )
  699. echo $output;
  700. return $output;
  701. }
  702. /**
  703. * Retrieve or display list of pages in list (li) format.
  704. *
  705. * @since 1.5.0
  706. *
  707. * @param array|string $args Optional. Override default arguments.
  708. * @return string HTML content, if not displaying.
  709. */
  710. function wp_list_pages($args = '') {
  711. $defaults = array(
  712. 'depth' => 0, 'show_date' => '',
  713. 'date_format' => get_option('date_format'),
  714. 'child_of' => 0, 'exclude' => '',
  715. 'title_li' => __('Pages'), 'echo' => 1,
  716. 'authors' => '', 'sort_column' => 'menu_order, post_title',
  717. 'link_before' => '', 'link_after' => '', 'walker' => '',
  718. );
  719. $r = wp_parse_args( $args, $defaults );
  720. extract( $r, EXTR_SKIP );
  721. $output = '';
  722. $current_page = 0;
  723. // sanitize, mostly to keep spaces out
  724. $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
  725. // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
  726. $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
  727. $r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
  728. // Query pages.
  729. $r['hierarchical'] = 0;
  730. $pages = get_pages($r);
  731. if ( !empty($pages) ) {
  732. if ( $r['title_li'] )
  733. $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
  734. global $wp_query;
  735. if ( is_page() || is_attachment() || $wp_query->is_posts_page )
  736. $current_page = $wp_query->get_queried_object_id();
  737. $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
  738. if ( $r['title_li'] )
  739. $output .= '</ul></li>';
  740. }
  741. $output = apply_filters('wp_list_pages', $output, $r);
  742. if ( $r['echo'] )
  743. echo $output;
  744. else
  745. return $output;
  746. }
  747. /**
  748. * Display or retrieve list of pages with optional home link.
  749. *
  750. * The arguments are listed below and part of the arguments are for {@link
  751. * wp_list_pages()} function. Check that function for more info on those
  752. * arguments.
  753. *
  754. * <ul>
  755. * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
  756. * to page title. Use column for posts table.</li>
  757. * <li><strong>menu_class</strong> - Class to use for the div ID which contains
  758. * the page list. Defaults to 'menu'.</li>
  759. * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
  760. * echo.</li>
  761. * <li><strong>link_before</strong> - Text before show_home argument text.</li>
  762. * <li><strong>link_after</strong> - Text after show_home argument text.</li>
  763. * <li><strong>show_home</strong> - If you set this argument, then it will
  764. * display the link to the home page. The show_home argument really just needs
  765. * to be set to the value of the text of the link.</li>
  766. * </ul>
  767. *
  768. * @since 2.7.0
  769. *
  770. * @param array|string $args
  771. * @return string html menu
  772. */
  773. function wp_page_menu( $args = array() ) {
  774. $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
  775. $args = wp_parse_args( $args, $defaults );
  776. $args = apply_filters( 'wp_page_menu_args', $args );
  777. $menu = '';
  778. $list_args = $args;
  779. // Show Home in the menu
  780. if ( ! empty($args['show_home']) ) {
  781. if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
  782. $text = __('Home');
  783. else
  784. $text = $args['show_home'];
  785. $class = '';
  786. if ( is_front_page() && !is_paged() )
  787. $class = 'class="current_page_item"';
  788. $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
  789. // If the front page is a page, add it to the exclude list
  790. if (get_option('show_on_front') == 'page') {
  791. if ( !empty( $list_args['exclude'] ) ) {
  792. $list_args['exclude'] .= ',';
  793. } else {
  794. $list_args['exclude'] = '';
  795. }
  796. $list_args['exclude'] .= get_option('page_on_front');
  797. }
  798. }
  799. $list_args['echo'] = false;
  800. $list_args['title_li'] = '';
  801. $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
  802. if ( $menu )
  803. $menu = '<ul>' . $menu . '</ul>';
  804. $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
  805. $menu = apply_filters( 'wp_page_menu', $menu, $args );
  806. if ( $args['echo'] )
  807. echo $menu;
  808. else
  809. return $menu;
  810. }
  811. //
  812. // Page helpers
  813. //
  814. /**
  815. * Retrieve HTML list content for page list.
  816. *
  817. * @uses Walker_Page to create HTML list content.
  818. * @since 2.1.0
  819. * @see Walker_Page::walk() for parameters and return description.
  820. */
  821. function walk_page_tree($pages, $depth, $current_page, $r) {
  822. if ( empty($r['walker']) )
  823. $walker = new Walker_Page;
  824. else
  825. $walker = $r['walker'];
  826. $args = array($pages, $depth, $r, $current_page);
  827. return call_user_func_array(array($walker, 'walk'), $args);
  828. }
  829. /**
  830. * Retrieve HTML dropdown (select) content for page list.
  831. *
  832. * @uses Walker_PageDropdown to create HTML dropdown content.
  833. * @since 2.1.0
  834. * @see Walker_PageDropdown::walk() for parameters and return description.
  835. */
  836. function walk_page_dropdown_tree() {
  837. $args = func_get_args();
  838. if ( empty($args[2]['walker']) ) // the user's options are the third parameter
  839. $walker = new Walker_PageDropdown;
  840. else
  841. $walker = $args[2]['walker'];
  842. return call_user_func_array(array($walker, 'walk'), $args);
  843. }
  844. /**
  845. * Create HTML list of pages.
  846. *
  847. * @package WordPress
  848. * @since 2.1.0
  849. * @uses Walker
  850. */
  851. class Walker_Page extends Walker {
  852. /**
  853. * @see Walker::$tree_type
  854. * @since 2.1.0
  855. * @var string
  856. */
  857. var $tree_type = 'page';
  858. /**
  859. * @see Walker::$db_fields
  860. * @since 2.1.0
  861. * @todo Decouple this.
  862. * @var array
  863. */
  864. var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  865. /**
  866. * @see Walker::start_lvl()
  867. * @since 2.1.0
  868. *
  869. * @param string $output Passed by reference. Used to append additional content.
  870. * @param int $depth Depth of page. Used for padding.
  871. * @param array $args
  872. */
  873. function start_lvl( &$output, $depth = 0, $args = array() ) {
  874. $indent = str_repeat("\t", $depth);
  875. $output .= "\n$indent<ul class='children'>\n";
  876. }
  877. /**
  878. * @see Walker::end_lvl()
  879. * @since 2.1.0
  880. *
  881. * @param string $output Passed by reference. Used to append additional content.
  882. * @param int $depth Depth of page. Used for padding.
  883. * @param array $args
  884. */
  885. function end_lvl( &$output, $depth = 0, $args = array() ) {
  886. $indent = str_repeat("\t", $depth);
  887. $output .= "$indent</ul>\n";
  888. }
  889. /**
  890. * @see Walker::start_el()
  891. * @since 2.1.0
  892. *
  893. * @param string $output Passed by reference. Used to append additional content.
  894. * @param object $page Page data object.
  895. * @param int $depth Depth of page. Used for padding.
  896. * @param int $current_page Page ID.
  897. * @param array $args
  898. */
  899. function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  900. if ( $depth )
  901. $indent = str_repeat("\t", $depth);
  902. else
  903. $indent = '';
  904. extract($args, EXTR_SKIP);
  905. $css_class = array('page_item', 'page-item-'.$page->ID);
  906. if ( !empty($current_page) ) {
  907. $_current_page = get_post( $current_page );
  908. if ( in_array( $page->ID, $_current_page->ancestors ) )
  909. $css_class[] = 'current_page_ancestor';
  910. if ( $page->ID == $current_page )
  911. $css_class[] = 'current_page_item';
  912. elseif ( $_current_page && $page->ID == $_current_page->post_parent )
  913. $css_class[] = 'current_page_parent';
  914. } elseif ( $page->ID == get_option('page_for_posts') ) {
  915. $css_class[] = 'current_page_parent';
  916. }
  917. $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  918. if ( '' === $page->post_title )
  919. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
  920. $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
  921. if ( !empty($show_date) ) {
  922. if ( 'modified' == $show_date )
  923. $time = $page->post_modified;
  924. else
  925. $time = $page->post_date;
  926. $output .= " " . mysql2date($date_format, $time);
  927. }
  928. }
  929. /**
  930. * @see Walker::end_el()
  931. * @since 2.1.0
  932. *
  933. * @param string $output Passed by reference. Used to append additional content.
  934. * @param object $page Page data object. Not used.
  935. * @param int $depth Depth of page. Not Used.
  936. * @param array $args
  937. */
  938. function end_el( &$output, $page, $depth = 0, $args = array() ) {
  939. $output .= "</li>\n";
  940. }
  941. }
  942. /**
  943. * Create HTML dropdown list of pages.
  944. *
  945. * @package WordPress
  946. * @since 2.1.0
  947. * @uses Walker
  948. */
  949. class Walker_PageDropdown extends Walker {
  950. /**
  951. * @see Walker::$tree_type
  952. * @since 2.1.0
  953. * @var string
  954. */
  955. var $tree_type = 'page';
  956. /**
  957. * @see Walker::$db_fields
  958. * @since 2.1.0
  959. * @todo Decouple this
  960. * @var array
  961. */
  962. var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  963. /**
  964. * @see Walker::start_el()
  965. * @since 2.1.0
  966. *
  967. * @param string $output Passed by reference. Used to append additional content.
  968. * @param object $page Page data object.
  969. * @param int $depth Depth of page in reference to parent pages. Used for padding.
  970. * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
  971. * @param int $id
  972. */
  973. function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
  974. $pad = str_repeat('&nbsp;', $depth * 3);
  975. $output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
  976. if ( $page->ID == $args['selected'] )
  977. $output .= ' selected="selected"';
  978. $output .= '>';
  979. $title = apply_filters( 'list_pages', $page->post_title, $page );
  980. $output .= $pad . esc_html( $title );
  981. $output .= "</option>\n";
  982. }
  983. }
  984. //
  985. // Attachments
  986. //
  987. /**
  988. * Display an attachment page link using an image or icon.
  989. *
  990. * @since 2.0.0
  991. *
  992. * @param int $id Optional. Post ID.
  993. * @param bool $fullsize Optional, default is false. Whether to use full size.
  994. * @param bool $deprecated Deprecated. Not used.
  995. * @param bool $permalink Optional, default is false. Whether to include permalink.
  996. */
  997. function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
  998. if ( !empty( $deprecated ) )
  999. _deprecated_argument( __FUNCTION__, '2.5' );
  1000. if ( $fullsize )
  1001. echo wp_get_attachment_link($id, 'full', $permalink);
  1002. else
  1003. echo wp_get_attachment_link($id, 'thumbnail', $permalink);
  1004. }
  1005. /**
  1006. * Retrieve an attachment page link using an image or icon, if possible.
  1007. *
  1008. * @since 2.5.0
  1009. * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
  1010. *
  1011. * @param int $id Optional. Post ID.
  1012. * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
  1013. * @param bool $permalink Optional, default is false. Whether to add permalink to image.
  1014. * @param bool $icon Optional, default is false. Whether to include icon.
  1015. * @param string|bool $text Optional, default is false. If string, then will be link text.
  1016. * @return string HTML content.
  1017. */
  1018. function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
  1019. $id = intval( $id );
  1020. $_post = get_post( $id );
  1021. if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
  1022. return __( 'Missing Attachment' );
  1023. if ( $permalink )
  1024. $url = get_attachment_link( $_post->ID );
  1025. $post_title = esc_attr( $_post->post_title );
  1026. if ( $text )
  1027. $link_text = $text;
  1028. elseif ( $size && 'none' != $size )
  1029. $link_text = wp_get_attachment_image( $id, $size, $icon );
  1030. else
  1031. $link_text = '';
  1032. if ( trim( $link_text ) == '' )
  1033. $link_text = $_post->post_title;
  1034. return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
  1035. }
  1036. /**
  1037. * Wrap attachment in <<p>> element before content.
  1038. *
  1039. * @since 2.0.0
  1040. * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
  1041. *
  1042. * @param string $content
  1043. * @return string
  1044. */
  1045. function prepend_attachment($content) {
  1046. $post = get_post();
  1047. if ( empty($post->post_type) || $post->post_type != 'attachment' )
  1048. return $content;
  1049. $p = '<p class="attachment">';
  1050. // show the medium sized image representation of the attachment if available, and link to the raw file
  1051. $p .= wp_get_attachment_link(0, 'medium', false);
  1052. $p .= '</p>';
  1053. $p = apply_filters('prepend_attachment', $p);
  1054. return "$p\n$content";
  1055. }
  1056. //
  1057. // Misc
  1058. //
  1059. /**
  1060. * Retrieve protected post password form content.
  1061. *
  1062. * @since 1.0.0
  1063. * @uses apply_filters() Calls 'the_password_form' filter on output.
  1064. * @param int|WP_Post $post Optional. A post id or post object. Defaults to the current post when in The Loop, undefined otherwise.
  1065. * @return string HTML content for password form for password protected post.
  1066. */
  1067. function get_the_password_form( $post = 0 ) {
  1068. $post = get_post( $post );
  1069. $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
  1070. $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
  1071. <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
  1072. <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>
  1073. </form>
  1074. ';
  1075. return apply_filters('the_password_form', $output);
  1076. }
  1077. /**
  1078. * Whether currently in a page template.
  1079. *
  1080. * This template tag allows you to determine if you are in a page template.
  1081. * You can optionally provide a template name and then the check will be
  1082. * specific to that template.
  1083. *
  1084. * @since 2.5.0
  1085. * @uses $wp_query
  1086. *
  1087. * @param string $template The specific template name if specific matching is required.
  1088. * @return bool True on success, false on failure.
  1089. */
  1090. function is_page_template( $template = '' ) {
  1091. if ( ! is_page() )
  1092. return false;
  1093. $page_template = get_page_template_slug( get_queried_object_id() );
  1094. if ( empty( $template ) )
  1095. return (bool) $page_template;
  1096. if ( $template == $page_template )
  1097. return true;
  1098. if ( 'default' == $template && ! $page_template )
  1099. return true;
  1100. return false;
  1101. }
  1102. /**
  1103. * Get the specific template name for a page.
  1104. *
  1105. * @since 3.4.0
  1106. *
  1107. * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
  1108. * @return string|bool Page template filename. Returns an empty string when the default page template
  1109. * is in use. Returns false if the post is not a page.
  1110. */
  1111. function get_page_template_slug( $post_id = null ) {
  1112. $post = get_post( $post_id );
  1113. if ( ! $post || 'page' != $post->post_type )
  1114. return false;
  1115. $template = get_post_meta( $post->ID, '_wp_page_template', true );
  1116. if ( ! $template || 'default' == $template )
  1117. return '';
  1118. return $template;
  1119. }
  1120. /**
  1121. * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
  1122. *
  1123. * @package WordPress
  1124. * @subpackage Post_Revisions
  1125. * @since 2.6.0
  1126. *
  1127. * @uses date_i18n()
  1128. *
  1129. * @param int|object $revision Revision ID or revision object.
  1130. * @param bool $link Optional, default is true. Link to revisions's page?
  1131. * @return string i18n formatted datetimestamp or localized 'Current Revision'.
  1132. */
  1133. function wp_post_revision_title( $revision, $link = true ) {
  1134. if ( !$revision = get_post( $revision ) )
  1135. return $revision;
  1136. if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
  1137. return false;
  1138. /* translators: revision date format, see http://php.net/date */
  1139. $datef = _x( 'j F, Y @ G:i', 'revision date format');
  1140. /* translators: 1: date */
  1141. $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
  1142. /* translators: 1: date */
  1143. $currentf = _x( '%1$s [Current Revision]', 'post revision title extra' );
  1144. $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
  1145. if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
  1146. $date = "<a href='$link'>$date</a>";
  1147. if ( !wp_is_post_revision( $revision ) )
  1148. $date = sprintf( $currentf, $date );
  1149. elseif ( wp_is_post_autosave( $revision ) )
  1150. $date = sprintf( $autosavef, $date );
  1151. return $date;
  1152. }
  1153. /**
  1154. * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
  1155. *
  1156. * @package WordPress
  1157. * @subpackage Post_Revisions
  1158. * @since 3.6.0
  1159. *
  1160. * @uses date_i18n()
  1161. *
  1162. * @param int|object $revision Revision ID or revision object.
  1163. * @param bool $link Optional, default is true. Link to revisions's page?
  1164. * @return string gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
  1165. */
  1166. function wp_post_revision_title_expanded( $revision, $link = true ) {
  1167. if ( !$revision = get_post( $revision ) )
  1168. return $revision;
  1169. if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
  1170. return false;
  1171. $author = get_the_author_meta( 'display_name', $revision->post_author );
  1172. /* translators: revision date format, see http://php.net/date */
  1173. $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
  1174. $gravatar = get_avatar( $revision->post_author, 24 );
  1175. $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
  1176. if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
  1177. $date = "<a href='$link'>$date</a>";
  1178. $revision_date_author = sprintf(
  1179. /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
  1180. _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
  1181. $gravatar,
  1182. $author,
  1183. human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
  1184. $date
  1185. );
  1186. $autosavef = __( '%1$s [Autosave]' );
  1187. $currentf = __( '%1$s [Current Revision]' );
  1188. if ( !wp_is_post_revision( $revision ) )
  1189. $revision_date_author = sprintf( $currentf, $revision_date_author );
  1190. elseif ( wp_is_post_autosave( $revision ) )
  1191. $revision_date_author = sprintf( $autosavef, $revision_date_author );
  1192. return $revision_date_author;
  1193. }
  1194. /**
  1195. * Display list of a post's revisions.
  1196. *
  1197. * Can output either a UL with edit links or a TABLE with diff interface, and
  1198. * restore action links.
  1199. *
  1200. * @package WordPress
  1201. * @subpackage Post_Revisions
  1202. * @since 2.6.0
  1203. *
  1204. * @uses wp_get_post_revisions()
  1205. * @uses wp_post_revision_title_expanded()
  1206. * @uses get_edit_post_link()
  1207. * @uses get_the_author_meta()
  1208. *
  1209. * @param int|object $post_id Post ID or post object.
  1210. * @param string $type 'all' (default), 'revision' or 'autosave'
  1211. * @return null
  1212. */
  1213. function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
  1214. if ( ! $post = get_post( $post_id ) )
  1215. return;
  1216. // $args array with (parent, format, right, left, type) deprecated since 3.6
  1217. if ( is_array( $type ) ) {
  1218. $type = ! empty( $type['type'] ) ? $type['type'] : $type;
  1219. _deprecated_argument( __FUNCTION__, '3.6' );
  1220. }
  1221. if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
  1222. return;
  1223. $rows = '';
  1224. foreach ( $revisions as $revision ) {
  1225. if ( ! current_user_can( 'read_post', $revision->ID ) )
  1226. continue;
  1227. $is_autosave = wp_is_post_autosave( $revision );
  1228. if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
  1229. continue;
  1230. $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
  1231. }
  1232. echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
  1233. echo "<ul class='post-revisions hide-if-no-js'>\n";
  1234. echo $rows;
  1235. echo "</ul>";
  1236. }