post-template.php 57 KB

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