post-template.php 57 KB

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