post-template.php 64 KB

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