post-template.php 61 KB

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