PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-includes/post-template.php

https://gitlab.com/darmawan.fatria/df-skp-2014
PHP | 1617 lines | 1077 code | 126 blank | 414 comment | 133 complexity | efa52063eed1a03420ec872f03a18a3a MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Post Template Functions.
  4. *
  5. * Gets content for the current post in the loop.
  6. *
  7. * @package WordPress
  8. * @subpackage Template
  9. */
  10. /**
  11. * Display the ID of the current item in the WordPress Loop.
  12. *
  13. * @since 0.71
  14. */
  15. function the_ID() {
  16. echo get_the_ID();
  17. }
  18. /**
  19. * Retrieve the ID of the current item in the WordPress Loop.
  20. *
  21. * @since 2.1.0
  22. *
  23. * @return int|bool 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 content.
  31. *
  32. * @since 0.71
  33. *
  34. * @param string $before Optional. Content to prepend to the title.
  35. * @param string $after Optional. Content to append to the title.
  36. * @param bool $echo Optional, default to true.Whether to display or return.
  37. * @return null|string Null on no title. String if $echo parameter is false.
  38. */
  39. function the_title($before = '', $after = '', $echo = true) {
  40. $title = get_the_title();
  41. if ( strlen($title) == 0 )
  42. return;
  43. $title = $before . $title . $after;
  44. if ( $echo )
  45. echo $title;
  46. else
  47. return $title;
  48. }
  49. /**
  50. * Sanitize the current title when retrieving or displaying.
  51. *
  52. * Works like {@link the_title()}, except the parameters can be in a string or
  53. * an array. See the function for what can be override in the $args parameter.
  54. *
  55. * The title before it is displayed will have the tags stripped and {@link
  56. * esc_attr()} before it is passed to the user or displayed. The default
  57. * as with {@link the_title()}, is to display the title.
  58. *
  59. * @since 2.3.0
  60. *
  61. * @param string|array $args {
  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|null Null on failure or display. 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. * Filter 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. * Filter 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. * Filter 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 an 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 $id Optional. Post ID or post object.
  155. */
  156. function the_guid( $id = 0 ) {
  157. /**
  158. * Filter the escaped Global Unique Identifier (guid) of the post.
  159. *
  160. * @since 4.2.0
  161. *
  162. * @see get_the_guid()
  163. *
  164. * @param string $post_guid Escaped Global Unique Identifier (guid) of the post.
  165. */
  166. echo apply_filters( 'the_guid', get_the_guid( $id ) );
  167. }
  168. /**
  169. * Retrieve the Post Global Unique Identifier (guid).
  170. *
  171. * The guid will appear to be a link, but should not be used as an link to the
  172. * post. The reason you should not use it as a link, is because of moving the
  173. * blog across domains.
  174. *
  175. * @since 1.5.0
  176. *
  177. * @param int|WP_Post $id Optional. Post ID or post object.
  178. * @return string
  179. */
  180. function get_the_guid( $id = 0 ) {
  181. $post = get_post($id);
  182. /**
  183. * Filter the Global Unique Identifier (guid) of the post.
  184. *
  185. * @since 1.5.0
  186. *
  187. * @param string $post_guid Global Unique Identifier (guid) of the post.
  188. */
  189. return apply_filters( 'get_the_guid', $post->guid );
  190. }
  191. /**
  192. * Display the post content.
  193. *
  194. * @since 0.71
  195. *
  196. * @param string $more_link_text Optional. Content for when there is more text.
  197. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
  198. */
  199. function the_content( $more_link_text = null, $strip_teaser = false) {
  200. $content = get_the_content( $more_link_text, $strip_teaser );
  201. /**
  202. * Filter the post content.
  203. *
  204. * @since 0.71
  205. *
  206. * @param string $content Content of the current post.
  207. */
  208. $content = apply_filters( 'the_content', $content );
  209. $content = str_replace( ']]>', ']]&gt;', $content );
  210. echo $content;
  211. }
  212. /**
  213. * Retrieve the post content.
  214. *
  215. * @since 0.71
  216. *
  217. * @param string $more_link_text Optional. Content for when there is more text.
  218. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
  219. * @return string
  220. */
  221. function get_the_content( $more_link_text = null, $strip_teaser = false ) {
  222. global $page, $more, $preview, $pages, $multipage;
  223. $post = get_post();
  224. if ( null === $more_link_text )
  225. $more_link_text = __( '(more&hellip;)' );
  226. $output = '';
  227. $has_teaser = false;
  228. // If post password required and it doesn't match the cookie.
  229. if ( post_password_required( $post ) )
  230. return get_the_password_form( $post );
  231. if ( $page > count( $pages ) ) // if the requested page doesn't exist
  232. $page = count( $pages ); // give them the highest numbered page that DOES exist
  233. $content = $pages[$page - 1];
  234. if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
  235. $content = explode( $matches[0], $content, 2 );
  236. if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
  237. $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
  238. $has_teaser = true;
  239. } else {
  240. $content = array( $content );
  241. }
  242. if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
  243. $strip_teaser = true;
  244. $teaser = $content[0];
  245. if ( $more && $strip_teaser && $has_teaser )
  246. $teaser = '';
  247. $output .= $teaser;
  248. if ( count( $content ) > 1 ) {
  249. if ( $more ) {
  250. $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
  251. } else {
  252. if ( ! empty( $more_link_text ) )
  253. /**
  254. * Filter the Read More link text.
  255. *
  256. * @since 2.8.0
  257. *
  258. * @param string $more_link_element Read More link element.
  259. * @param string $more_link_text Read More text.
  260. */
  261. $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
  262. $output = force_balance_tags( $output );
  263. }
  264. }
  265. if ( $preview ) // Preview fix for JavaScript bug with foreign languages.
  266. $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
  267. return $output;
  268. }
  269. /**
  270. * Preview fix for JavaScript bug with foreign languages.
  271. *
  272. * @since 3.1.0
  273. * @access private
  274. * @param array $match Match array from preg_replace_callback
  275. * @return string
  276. */
  277. function _convert_urlencoded_to_entities( $match ) {
  278. return '&#' . base_convert( $match[1], 16, 10 ) . ';';
  279. }
  280. /**
  281. * Display the post excerpt.
  282. *
  283. * @since 0.71
  284. */
  285. function the_excerpt() {
  286. /**
  287. * Filter the displayed post excerpt.
  288. *
  289. * @since 0.71
  290. *
  291. * @see get_the_excerpt()
  292. *
  293. * @param string $post_excerpt The post excerpt.
  294. */
  295. echo apply_filters( 'the_excerpt', get_the_excerpt() );
  296. }
  297. /**
  298. * Retrieve the post excerpt.
  299. *
  300. * @since 0.71
  301. *
  302. * @param mixed $deprecated Not used.
  303. * @return string
  304. */
  305. function get_the_excerpt( $deprecated = '' ) {
  306. if ( !empty( $deprecated ) )
  307. _deprecated_argument( __FUNCTION__, '2.3' );
  308. $post = get_post();
  309. if ( empty( $post ) ) {
  310. return '';
  311. }
  312. if ( post_password_required() ) {
  313. return __( 'There is no excerpt because this is a protected post.' );
  314. }
  315. /**
  316. * Filter the retrieved post excerpt.
  317. *
  318. * @since 1.2.0
  319. *
  320. * @param string $post_excerpt The post excerpt.
  321. */
  322. return apply_filters( 'get_the_excerpt', $post->post_excerpt );
  323. }
  324. /**
  325. * Whether post has excerpt.
  326. *
  327. * @since 2.3.0
  328. *
  329. * @param int|WP_Post $id Optional. Post ID or post object.
  330. * @return bool
  331. */
  332. function has_excerpt( $id = 0 ) {
  333. $post = get_post( $id );
  334. return ( !empty( $post->post_excerpt ) );
  335. }
  336. /**
  337. * Display the classes for the post div.
  338. *
  339. * @since 2.7.0
  340. *
  341. * @param string|array $class One or more classes to add to the class list.
  342. * @param int|WP_Post $post_id Optional. Post ID or post object.
  343. */
  344. function post_class( $class = '', $post_id = null ) {
  345. // Separates classes with a single space, collates classes for post DIV
  346. echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
  347. }
  348. /**
  349. * Retrieve the classes for the post div as an array.
  350. *
  351. * The class names are many. If the post is a sticky, then the 'sticky'
  352. * class name. The class 'hentry' is always added to each post. If the post has a
  353. * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
  354. * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
  355. * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
  356. * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
  357. * passed through the filter, 'post_class' with the list of classes, followed by
  358. * $class parameter value, with the post ID as the last parameter.
  359. *
  360. * @since 2.7.0
  361. * @since 4.2.0 Custom taxonomy classes were added.
  362. *
  363. * @param string|array $class One or more classes to add to the class list.
  364. * @param int|WP_Post $post_id Optional. Post ID or post object.
  365. * @return array Array of classes.
  366. */
  367. function get_post_class( $class = '', $post_id = null ) {
  368. $post = get_post( $post_id );
  369. $classes = array();
  370. if ( $class ) {
  371. if ( ! is_array( $class ) ) {
  372. $class = preg_split( '#\s+#', $class );
  373. }
  374. $classes = array_map( 'esc_attr', $class );
  375. }
  376. if ( ! $post ) {
  377. return $classes;
  378. }
  379. $classes[] = 'post-' . $post->ID;
  380. if ( ! is_admin() )
  381. $classes[] = $post->post_type;
  382. $classes[] = 'type-' . $post->post_type;
  383. $classes[] = 'status-' . $post->post_status;
  384. // Post Format
  385. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  386. $post_format = get_post_format( $post->ID );
  387. if ( $post_format && !is_wp_error($post_format) )
  388. $classes[] = 'format-' . sanitize_html_class( $post_format );
  389. else
  390. $classes[] = 'format-standard';
  391. }
  392. // Post requires password
  393. if ( post_password_required( $post->ID ) ) {
  394. $classes[] = 'post-password-required';
  395. // Post thumbnails
  396. } elseif ( ! is_attachment( $post ) && current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) ) {
  397. $classes[] = 'has-post-thumbnail';
  398. }
  399. // sticky for Sticky Posts
  400. if ( is_sticky( $post->ID ) ) {
  401. if ( is_home() && ! is_paged() ) {
  402. $classes[] = 'sticky';
  403. } elseif ( is_admin() ) {
  404. $classes[] = 'status-sticky';
  405. }
  406. }
  407. // hentry for hAtom compliance
  408. $classes[] = 'hentry';
  409. // All public taxonomies
  410. $taxonomies = get_taxonomies( array( 'public' => true ) );
  411. foreach ( (array) $taxonomies as $taxonomy ) {
  412. if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
  413. foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
  414. if ( empty( $term->slug ) ) {
  415. continue;
  416. }
  417. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  418. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  419. $term_class = $term->term_id;
  420. }
  421. // 'post_tag' uses the 'tag' prefix for backward compatibility.
  422. if ( 'post_tag' == $taxonomy ) {
  423. $classes[] = 'tag-' . $term_class;
  424. } else {
  425. $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
  426. }
  427. }
  428. }
  429. }
  430. $classes = array_map( 'esc_attr', $classes );
  431. /**
  432. * Filter the list of CSS classes for the current post.
  433. *
  434. * @since 2.7.0
  435. *
  436. * @param array $classes An array of post classes.
  437. * @param string $class A comma-separated list of additional classes added to the post.
  438. * @param int $post_id The post ID.
  439. */
  440. $classes = apply_filters( 'post_class', $classes, $class, $post->ID );
  441. return array_unique( $classes );
  442. }
  443. /**
  444. * Display the classes for the body element.
  445. *
  446. * @since 2.8.0
  447. *
  448. * @param string|array $class One or more classes to add to the class list.
  449. */
  450. function body_class( $class = '' ) {
  451. // Separates classes with a single space, collates classes for body element
  452. echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
  453. }
  454. /**
  455. * Retrieve the classes for the body element as an array.
  456. *
  457. * @since 2.8.0
  458. *
  459. * @param string|array $class One or more classes to add to the class list.
  460. * @return array Array of classes.
  461. */
  462. function get_body_class( $class = '' ) {
  463. global $wp_query, $wpdb;
  464. $classes = array();
  465. if ( is_rtl() )
  466. $classes[] = 'rtl';
  467. if ( is_front_page() )
  468. $classes[] = 'home';
  469. if ( is_home() )
  470. $classes[] = 'blog';
  471. if ( is_archive() )
  472. $classes[] = 'archive';
  473. if ( is_date() )
  474. $classes[] = 'date';
  475. if ( is_search() ) {
  476. $classes[] = 'search';
  477. $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
  478. }
  479. if ( is_paged() )
  480. $classes[] = 'paged';
  481. if ( is_attachment() )
  482. $classes[] = 'attachment';
  483. if ( is_404() )
  484. $classes[] = 'error404';
  485. if ( is_single() ) {
  486. $post_id = $wp_query->get_queried_object_id();
  487. $post = $wp_query->get_queried_object();
  488. $classes[] = 'single';
  489. if ( isset( $post->post_type ) ) {
  490. $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
  491. $classes[] = 'postid-' . $post_id;
  492. // Post Format
  493. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  494. $post_format = get_post_format( $post->ID );
  495. if ( $post_format && !is_wp_error($post_format) )
  496. $classes[] = 'single-format-' . sanitize_html_class( $post_format );
  497. else
  498. $classes[] = 'single-format-standard';
  499. }
  500. }
  501. if ( is_attachment() ) {
  502. $mime_type = get_post_mime_type($post_id);
  503. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
  504. $classes[] = 'attachmentid-' . $post_id;
  505. $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
  506. }
  507. } elseif ( is_archive() ) {
  508. if ( is_post_type_archive() ) {
  509. $classes[] = 'post-type-archive';
  510. $post_type = get_query_var( 'post_type' );
  511. if ( is_array( $post_type ) )
  512. $post_type = reset( $post_type );
  513. $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
  514. } elseif ( is_author() ) {
  515. $author = $wp_query->get_queried_object();
  516. $classes[] = 'author';
  517. if ( isset( $author->user_nicename ) ) {
  518. $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
  519. $classes[] = 'author-' . $author->ID;
  520. }
  521. } elseif ( is_category() ) {
  522. $cat = $wp_query->get_queried_object();
  523. $classes[] = 'category';
  524. if ( isset( $cat->term_id ) ) {
  525. $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
  526. if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
  527. $cat_class = $cat->term_id;
  528. }
  529. $classes[] = 'category-' . $cat_class;
  530. $classes[] = 'category-' . $cat->term_id;
  531. }
  532. } elseif ( is_tag() ) {
  533. $tag = $wp_query->get_queried_object();
  534. $classes[] = 'tag';
  535. if ( isset( $tag->term_id ) ) {
  536. $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
  537. if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
  538. $tag_class = $tag->term_id;
  539. }
  540. $classes[] = 'tag-' . $tag_class;
  541. $classes[] = 'tag-' . $tag->term_id;
  542. }
  543. } elseif ( is_tax() ) {
  544. $term = $wp_query->get_queried_object();
  545. if ( isset( $term->term_id ) ) {
  546. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  547. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  548. $term_class = $term->term_id;
  549. }
  550. $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
  551. $classes[] = 'term-' . $term_class;
  552. $classes[] = 'term-' . $term->term_id;
  553. }
  554. }
  555. } elseif ( is_page() ) {
  556. $classes[] = 'page';
  557. $page_id = $wp_query->get_queried_object_id();
  558. $post = get_post($page_id);
  559. $classes[] = 'page-id-' . $page_id;
  560. if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
  561. $classes[] = 'page-parent';
  562. }
  563. if ( $post->post_parent ) {
  564. $classes[] = 'page-child';
  565. $classes[] = 'parent-pageid-' . $post->post_parent;
  566. }
  567. if ( is_page_template() ) {
  568. $classes[] = 'page-template';
  569. $template_slug = get_page_template_slug( $page_id );
  570. $template_parts = explode( '/', $template_slug );
  571. foreach ( $template_parts as $part ) {
  572. $classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
  573. }
  574. $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
  575. } else {
  576. $classes[] = 'page-template-default';
  577. }
  578. }
  579. if ( is_user_logged_in() )
  580. $classes[] = 'logged-in';
  581. if ( is_admin_bar_showing() ) {
  582. $classes[] = 'admin-bar';
  583. $classes[] = 'no-customize-support';
  584. }
  585. if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() )
  586. $classes[] = 'custom-background';
  587. $page = $wp_query->get( 'page' );
  588. if ( ! $page || $page < 2 )
  589. $page = $wp_query->get( 'paged' );
  590. if ( $page && $page > 1 && ! is_404() ) {
  591. $classes[] = 'paged-' . $page;
  592. if ( is_single() )
  593. $classes[] = 'single-paged-' . $page;
  594. elseif ( is_page() )
  595. $classes[] = 'page-paged-' . $page;
  596. elseif ( is_category() )
  597. $classes[] = 'category-paged-' . $page;
  598. elseif ( is_tag() )
  599. $classes[] = 'tag-paged-' . $page;
  600. elseif ( is_date() )
  601. $classes[] = 'date-paged-' . $page;
  602. elseif ( is_author() )
  603. $classes[] = 'author-paged-' . $page;
  604. elseif ( is_search() )
  605. $classes[] = 'search-paged-' . $page;
  606. elseif ( is_post_type_archive() )
  607. $classes[] = 'post-type-paged-' . $page;
  608. }
  609. if ( ! empty( $class ) ) {
  610. if ( !is_array( $class ) )
  611. $class = preg_split( '#\s+#', $class );
  612. $classes = array_merge( $classes, $class );
  613. } else {
  614. // Ensure that we always coerce class to being an array.
  615. $class = array();
  616. }
  617. $classes = array_map( 'esc_attr', $classes );
  618. /**
  619. * Filter the list of CSS body classes for the current post or page.
  620. *
  621. * @since 2.8.0
  622. *
  623. * @param array $classes An array of body classes.
  624. * @param string $class A comma-separated list of additional classes added to the body.
  625. */
  626. $classes = apply_filters( 'body_class', $classes, $class );
  627. return array_unique( $classes );
  628. }
  629. /**
  630. * Whether post requires password and correct password has been provided.
  631. *
  632. * @since 2.7.0
  633. *
  634. * @param int|WP_Post $post An optional post. Global $post used if not provided.
  635. * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
  636. */
  637. function post_password_required( $post = null ) {
  638. $post = get_post($post);
  639. if ( empty( $post->post_password ) )
  640. return false;
  641. if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
  642. return true;
  643. require_once ABSPATH . WPINC . '/class-phpass.php';
  644. $hasher = new PasswordHash( 8, true );
  645. $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
  646. if ( 0 !== strpos( $hash, '$P$B' ) )
  647. return true;
  648. return ! $hasher->CheckPassword( $post->post_password, $hash );
  649. }
  650. //
  651. // Page Template Functions for usage in Themes
  652. //
  653. /**
  654. * The formatted output of a list of pages.
  655. *
  656. * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
  657. * Quicktag one or more times). This tag must be within The Loop.
  658. *
  659. * @since 1.2.0
  660. *
  661. * @param string|array $args {
  662. * Optional. Array or string of default arguments.
  663. *
  664. * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`.
  665. * @type string $after HTML or text to append to each link. Default is `</p>`.
  666. * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag.
  667. * Also prepended to the current item, which is not linked. Default empty.
  668. * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag.
  669. * Also appended to the current item, which is not linked. Default empty.
  670. * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number
  671. * and next. Default is 'number'.
  672. * @type string $separator Text between pagination links. Default is ' '.
  673. * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'.
  674. * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
  675. * @type string $pagelink Format string for page numbers. The % in the parameter string will be
  676. * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
  677. * Defaults to '%', just the page number.
  678. * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
  679. * }
  680. * @return string Formatted output in HTML.
  681. */
  682. function wp_link_pages( $args = '' ) {
  683. $defaults = array(
  684. 'before' => '<p>' . __( 'Pages:' ),
  685. 'after' => '</p>',
  686. 'link_before' => '',
  687. 'link_after' => '',
  688. 'next_or_number' => 'number',
  689. 'separator' => ' ',
  690. 'nextpagelink' => __( 'Next page' ),
  691. 'previouspagelink' => __( 'Previous page' ),
  692. 'pagelink' => '%',
  693. 'echo' => 1
  694. );
  695. $params = wp_parse_args( $args, $defaults );
  696. /**
  697. * Filter the arguments used in retrieving page links for paginated posts.
  698. *
  699. * @since 3.0.0
  700. *
  701. * @param array $params An array of arguments for page links for paginated posts.
  702. */
  703. $r = apply_filters( 'wp_link_pages_args', $params );
  704. global $page, $numpages, $multipage, $more;
  705. $output = '';
  706. if ( $multipage ) {
  707. if ( 'number' == $r['next_or_number'] ) {
  708. $output .= $r['before'];
  709. for ( $i = 1; $i <= $numpages; $i++ ) {
  710. $link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after'];
  711. if ( $i != $page || ! $more && 1 == $page ) {
  712. $link = _wp_link_page( $i ) . $link . '</a>';
  713. }
  714. /**
  715. * Filter the HTML output of individual page number links.
  716. *
  717. * @since 3.6.0
  718. *
  719. * @param string $link The page number HTML output.
  720. * @param int $i Page number for paginated posts' page links.
  721. */
  722. $link = apply_filters( 'wp_link_pages_link', $link, $i );
  723. // Use the custom links separator beginning with the second link.
  724. $output .= ( 1 === $i ) ? ' ' : $r['separator'];
  725. $output .= $link;
  726. }
  727. $output .= $r['after'];
  728. } elseif ( $more ) {
  729. $output .= $r['before'];
  730. $prev = $page - 1;
  731. if ( $prev ) {
  732. $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>';
  733. /** This filter is documented in wp-includes/post-template.php */
  734. $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
  735. }
  736. $next = $page + 1;
  737. if ( $next <= $numpages ) {
  738. if ( $prev ) {
  739. $output .= $r['separator'];
  740. }
  741. $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';
  742. /** This filter is documented in wp-includes/post-template.php */
  743. $output .= apply_filters( 'wp_link_pages_link', $link, $next );
  744. }
  745. $output .= $r['after'];
  746. }
  747. }
  748. /**
  749. * Filter the HTML output of page links for paginated posts.
  750. *
  751. * @since 3.6.0
  752. *
  753. * @param string $output HTML output of paginated posts' page links.
  754. * @param array $args An array of arguments.
  755. */
  756. $html = apply_filters( 'wp_link_pages', $output, $args );
  757. if ( $r['echo'] ) {
  758. echo $html;
  759. }
  760. return $html;
  761. }
  762. /**
  763. * Helper function for wp_link_pages().
  764. *
  765. * @since 3.1.0
  766. * @access private
  767. *
  768. * @param int $i Page number.
  769. * @return string Link.
  770. */
  771. function _wp_link_page( $i ) {
  772. global $wp_rewrite;
  773. $post = get_post();
  774. if ( 1 == $i ) {
  775. $url = get_permalink();
  776. } else {
  777. if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
  778. $url = add_query_arg( 'page', $i, get_permalink() );
  779. elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
  780. $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
  781. else
  782. $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
  783. }
  784. if ( is_preview() ) {
  785. $url = add_query_arg( array(
  786. 'preview' => 'true'
  787. ), $url );
  788. if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
  789. $url = add_query_arg( array(
  790. 'preview_id' => wp_unslash( $_GET['preview_id'] ),
  791. 'preview_nonce' => wp_unslash( $_GET['preview_nonce'] )
  792. ), $url );
  793. }
  794. }
  795. return '<a href="' . esc_url( $url ) . '">';
  796. }
  797. //
  798. // Post-meta: Custom per-post fields.
  799. //
  800. /**
  801. * Retrieve post custom meta data field.
  802. *
  803. * @since 1.5.0
  804. *
  805. * @param string $key Meta data key name.
  806. * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
  807. */
  808. function post_custom( $key = '' ) {
  809. $custom = get_post_custom();
  810. if ( !isset( $custom[$key] ) )
  811. return false;
  812. elseif ( 1 == count($custom[$key]) )
  813. return $custom[$key][0];
  814. else
  815. return $custom[$key];
  816. }
  817. /**
  818. * Display list of post custom fields.
  819. *
  820. * @internal This will probably change at some point...
  821. * @since 1.2.0
  822. */
  823. function the_meta() {
  824. if ( $keys = get_post_custom_keys() ) {
  825. echo "<ul class='post-meta'>\n";
  826. foreach ( (array) $keys as $key ) {
  827. $keyt = trim($key);
  828. if ( is_protected_meta( $keyt, 'post' ) )
  829. continue;
  830. $values = array_map('trim', get_post_custom_values($key));
  831. $value = implode($values,', ');
  832. /**
  833. * Filter the HTML output of the li element in the post custom fields list.
  834. *
  835. * @since 2.2.0
  836. *
  837. * @param string $html The HTML output for the li element.
  838. * @param string $key Meta key.
  839. * @param string $value Meta value.
  840. */
  841. echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
  842. }
  843. echo "</ul>\n";
  844. }
  845. }
  846. //
  847. // Pages
  848. //
  849. /**
  850. * Retrieve or display list of pages as a dropdown (select list).
  851. *
  852. * @since 2.1.0
  853. * @since 4.2.0 The `$value_field` argument was added.
  854. *
  855. * @param array|string $args {
  856. * Optional. Array or string of arguments to generate a pages drop-down element.
  857. *
  858. * @type int $depth Maximum depth. Default 0.
  859. * @type int $child_of Page ID to retrieve child pages of. Default 0.
  860. * @type int|string $selected Value of the option that should be selected. Default 0.
  861. * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1,
  862. * or their bool equivalents. Default 1.
  863. * @type string $name Value for the 'name' attribute of the select element.
  864. * Default 'page_id'.
  865. * @type string $id Value for the 'id' attribute of the select element.
  866. * Defaults to the value of `$name`.
  867. * @type string $show_option_none Text to display for showing no pages. Default empty (does not display).
  868. * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display).
  869. * @type string $option_none_value Value to use when no page is selected. Default empty.
  870. * @type string $value_field Post field used to populate the 'value' attribute of the option
  871. * elements. Accepts any valid post field. Default 'ID'.
  872. * }
  873. * @return string HTML content, if not displaying.
  874. */
  875. function wp_dropdown_pages( $args = '' ) {
  876. $defaults = array(
  877. 'depth' => 0, 'child_of' => 0,
  878. 'selected' => 0, 'echo' => 1,
  879. 'name' => 'page_id', 'id' => '',
  880. 'show_option_none' => '', 'show_option_no_change' => '',
  881. 'option_none_value' => '',
  882. 'value_field' => 'ID',
  883. );
  884. $r = wp_parse_args( $args, $defaults );
  885. $pages = get_pages( $r );
  886. $output = '';
  887. // Back-compat with old system where both id and name were based on $name argument
  888. if ( empty( $r['id'] ) ) {
  889. $r['id'] = $r['name'];
  890. }
  891. if ( ! empty( $pages ) ) {
  892. $output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "'>\n";
  893. if ( $r['show_option_no_change'] ) {
  894. $output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n";
  895. }
  896. if ( $r['show_option_none'] ) {
  897. $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '">' . $r['show_option_none'] . "</option>\n";
  898. }
  899. $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r );
  900. $output .= "</select>\n";
  901. }
  902. /**
  903. * Filter the HTML output of a list of pages as a drop down.
  904. *
  905. * @since 2.1.0
  906. *
  907. * @param string $output HTML output for drop down list of pages.
  908. */
  909. $html = apply_filters( 'wp_dropdown_pages', $output );
  910. if ( $r['echo'] ) {
  911. echo $html;
  912. }
  913. return $html;
  914. }
  915. /**
  916. * Retrieve or display list of pages in list (li) format.
  917. *
  918. * @since 1.5.0
  919. *
  920. * @see get_pages()
  921. *
  922. * @param array|string $args {
  923. * Array or string of arguments. Optional.
  924. *
  925. * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
  926. * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
  927. * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
  928. * Default is the value of 'date_format' option.
  929. * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
  930. * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
  931. * the given n depth). Default 0.
  932. * @type bool $echo Whether or not to echo the list of pages. Default true.
  933. * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
  934. * @type array $include Comma-separated list of page IDs to include. Default empty.
  935. * @type string $link_after Text or HTML to follow the page link label. Default null.
  936. * @type string $link_before Text or HTML to precede the page link label. Default null.
  937. * @type string $post_type Post type to query for. Default 'page'.
  938. * @type string $post_status Comma-separated list of post statuses to include. Default 'publish'.
  939. * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
  940. * 'modified' or any other value. An empty value hides the date. Default empty.
  941. * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
  942. * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
  943. * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
  944. * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
  945. * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
  946. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
  947. * }
  948. * @return string HTML list of pages.
  949. */
  950. function wp_list_pages( $args = '' ) {
  951. $defaults = array(
  952. 'depth' => 0, 'show_date' => '',
  953. 'date_format' => get_option( 'date_format' ),
  954. 'child_of' => 0, 'exclude' => '',
  955. 'title_li' => __( 'Pages' ), 'echo' => 1,
  956. 'authors' => '', 'sort_column' => 'menu_order, post_title',
  957. 'link_before' => '', 'link_after' => '', 'walker' => '',
  958. );
  959. $r = wp_parse_args( $args, $defaults );
  960. $output = '';
  961. $current_page = 0;
  962. // sanitize, mostly to keep spaces out
  963. $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );
  964. // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
  965. $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();
  966. /**
  967. * Filter the array of pages to exclude from the pages list.
  968. *
  969. * @since 2.1.0
  970. *
  971. * @param array $exclude_array An array of page IDs to exclude.
  972. */
  973. $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
  974. // Query pages.
  975. $r['hierarchical'] = 0;
  976. $pages = get_pages( $r );
  977. if ( ! empty( $pages ) ) {
  978. if ( $r['title_li'] ) {
  979. $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
  980. }
  981. global $wp_query;
  982. if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
  983. $current_page = get_queried_object_id();
  984. } elseif ( is_singular() ) {
  985. $queried_object = get_queried_object();
  986. if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
  987. $current_page = $queried_object->ID;
  988. }
  989. }
  990. $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r );
  991. if ( $r['title_li'] ) {
  992. $output .= '</ul></li>';
  993. }
  994. }
  995. /**
  996. * Filter the HTML output of the pages to list.
  997. *
  998. * @since 1.5.1
  999. *
  1000. * @see wp_list_pages()
  1001. *
  1002. * @param string $output HTML output of the pages list.
  1003. * @param array $r An array of page-listing arguments.
  1004. */
  1005. $html = apply_filters( 'wp_list_pages', $output, $r );
  1006. if ( $r['echo'] ) {
  1007. echo $html;
  1008. } else {
  1009. return $html;
  1010. }
  1011. }
  1012. /**
  1013. * Display or retrieve list of pages with optional home link.
  1014. *
  1015. * The arguments are listed below and part of the arguments are for {@link
  1016. * wp_list_pages()} function. Check that function for more info on those
  1017. * arguments.
  1018. *
  1019. * @since 2.7.0
  1020. *
  1021. * @param array|string $args {
  1022. * Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
  1023. *
  1024. * @type string $sort_column How to short the list of pages. Accepts post column names.
  1025. * Default 'menu_order, post_title'.
  1026. * @type string $menu_class Class to use for the div ID containing the page list. Default 'menu'.
  1027. * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
  1028. * Default true.
  1029. * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
  1030. * @type string $link_after The HTML or text to append to $show_home text. Default empty.
  1031. * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
  1032. * you'd like shown for the home link. 1|true defaults to 'Home'.
  1033. * }
  1034. * @return string html menu
  1035. */
  1036. function wp_page_menu( $args = array() ) {
  1037. $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
  1038. $args = wp_parse_args( $args, $defaults );
  1039. /**
  1040. * Filter the arguments used to generate a page-based menu.
  1041. *
  1042. * @since 2.7.0
  1043. *
  1044. * @see wp_page_menu()
  1045. *
  1046. * @param array $args An array of page menu arguments.
  1047. */
  1048. $args = apply_filters( 'wp_page_menu_args', $args );
  1049. $menu = '';
  1050. $list_args = $args;
  1051. // Show Home in the menu
  1052. if ( ! empty($args['show_home']) ) {
  1053. if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
  1054. $text = __('Home');
  1055. else
  1056. $text = $args['show_home'];
  1057. $class = '';
  1058. if ( is_front_page() && !is_paged() )
  1059. $class = 'class="current_page_item"';
  1060. $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
  1061. // If the front page is a page, add it to the exclude list
  1062. if (get_option('show_on_front') == 'page') {
  1063. if ( !empty( $list_args['exclude'] ) ) {
  1064. $list_args['exclude'] .= ',';
  1065. } else {
  1066. $list_args['exclude'] = '';
  1067. }
  1068. $list_args['exclude'] .= get_option('page_on_front');
  1069. }
  1070. }
  1071. $list_args['echo'] = false;
  1072. $list_args['title_li'] = '';
  1073. $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
  1074. if ( $menu )
  1075. $menu = '<ul>' . $menu . '</ul>';
  1076. $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
  1077. /**
  1078. * Filter the HTML output of a page-based menu.
  1079. *
  1080. * @since 2.7.0
  1081. *
  1082. * @see wp_page_menu()
  1083. *
  1084. * @param string $menu The HTML output.
  1085. * @param array $args An array of arguments.
  1086. */
  1087. $menu = apply_filters( 'wp_page_menu', $menu, $args );
  1088. if ( $args['echo'] )
  1089. echo $menu;
  1090. else
  1091. return $menu;
  1092. }
  1093. //
  1094. // Page helpers
  1095. //
  1096. /**
  1097. * Retrieve HTML list content for page list.
  1098. *
  1099. * @uses Walker_Page to create HTML list content.
  1100. * @since 2.1.0
  1101. * @see Walker_Page::walk() for parameters and return description.
  1102. */
  1103. function walk_page_tree($pages, $depth, $current_page, $r) {
  1104. if ( empty($r['walker']) )
  1105. $walker = new Walker_Page;
  1106. else
  1107. $walker = $r['walker'];
  1108. foreach ( (array) $pages as $page ) {
  1109. if ( $page->post_parent )
  1110. $r['pages_with_children'][ $page->post_parent ] = true;
  1111. }
  1112. $args = array($pages, $depth, $r, $current_page);
  1113. return call_user_func_array(array($walker, 'walk'), $args);
  1114. }
  1115. /**
  1116. * Retrieve HTML dropdown (select) content for page list.
  1117. *
  1118. * @uses Walker_PageDropdown to create HTML dropdown content.
  1119. * @since 2.1.0
  1120. * @see Walker_PageDropdown::walk() for parameters and return description.
  1121. */
  1122. function walk_page_dropdown_tree() {
  1123. $args = func_get_args();
  1124. if ( empty($args[2]['walker']) ) // the user's options are the third parameter
  1125. $walker = new Walker_PageDropdown;
  1126. else
  1127. $walker = $args[2]['walker'];
  1128. return call_user_func_array(array($walker, 'walk'), $args);
  1129. }
  1130. /**
  1131. * Create HTML list of pages.
  1132. *
  1133. * @since 2.1.0
  1134. * @uses Walker
  1135. */
  1136. class Walker_Page extends Walker {
  1137. /**
  1138. * @see Walker::$tree_type
  1139. * @since 2.1.0
  1140. * @var string
  1141. */
  1142. public $tree_type = 'page';
  1143. /**
  1144. * @see Walker::$db_fields
  1145. * @since 2.1.0
  1146. * @todo Decouple this.
  1147. * @var array
  1148. */
  1149. public $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  1150. /**
  1151. * @see Walker::start_lvl()
  1152. * @since 2.1.0
  1153. *
  1154. * @param string $output Passed by reference. Used to append additional content.
  1155. * @param int $depth Depth of page. Used for padding.
  1156. * @param array $args
  1157. */
  1158. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  1159. $indent = str_repeat("\t", $depth);
  1160. $output .= "\n$indent<ul class='children'>\n";
  1161. }
  1162. /**
  1163. * @see Walker::end_lvl()
  1164. * @since 2.1.0
  1165. *
  1166. * @param string $output Passed by reference. Used to append additional content.
  1167. * @param int $depth Depth of page. Used for padding.
  1168. * @param array $args
  1169. */
  1170. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  1171. $indent = str_repeat("\t", $depth);
  1172. $output .= "$indent</ul>\n";
  1173. }
  1174. /**
  1175. * @see Walker::start_el()
  1176. * @since 2.1.0
  1177. *
  1178. * @param string $output Passed by reference. Used to append additional content.
  1179. * @param object $page Page data object.
  1180. * @param int $depth Depth of page. Used for padding.
  1181. * @param int $current_page Page ID.
  1182. * @param array $args
  1183. */
  1184. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  1185. if ( $depth ) {
  1186. $indent = str_repeat( "\t", $depth );
  1187. } else {
  1188. $indent = '';
  1189. }
  1190. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  1191. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  1192. $css_class[] = 'page_item_has_children';
  1193. }
  1194. if ( ! empty( $current_page ) ) {
  1195. $_current_page = get_post( $current_page );
  1196. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
  1197. $css_class[] = 'current_page_ancestor';
  1198. }
  1199. if ( $page->ID == $current_page ) {
  1200. $css_class[] = 'current_page_item';
  1201. } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
  1202. $css_class[] = 'current_page_parent';
  1203. }
  1204. } elseif ( $page->ID == get_option('page_for_posts') ) {
  1205. $css_class[] = 'current_page_parent';
  1206. }
  1207. /**
  1208. * Filter the list of CSS classes to include with each page item in the list.
  1209. *
  1210. * @since 2.8.0
  1211. *
  1212. * @see wp_list_pages()
  1213. *
  1214. * @param array $css_class An array of CSS classes to be applied
  1215. * to each list item.
  1216. * @param WP_Post $page Page data object.
  1217. * @param int $depth Depth of page, used for padding.
  1218. * @param array $args An array of arguments.
  1219. * @param int $current_page ID of the current page.
  1220. */
  1221. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  1222. if ( '' === $page->post_title ) {
  1223. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
  1224. }
  1225. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  1226. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  1227. /** This filter is documented in wp-includes/post-template.php */
  1228. $output .= $indent . sprintf(
  1229. '<li class="%s"><a href="%s">%s%s%s</a>',
  1230. $css_classes,
  1231. get_permalink( $page->ID ),
  1232. $args['link_before'],
  1233. apply_filters( 'the_title', $page->post_title, $page->ID ),
  1234. $args['link_after']
  1235. );
  1236. if ( ! empty( $args['show_date'] ) ) {
  1237. if ( 'modified' == $args['show_date'] ) {
  1238. $time = $page->post_modified;
  1239. } else {
  1240. $time = $page->post_date;
  1241. }
  1242. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  1243. $output .= " " . mysql2date( $date_format, $time );
  1244. }
  1245. }
  1246. /**
  1247. * @see Walker::end_el()
  1248. * @since 2.1.0
  1249. *
  1250. * @param string $output Passed by reference. Used to append additional content.
  1251. * @param object $page Page data object. Not used.
  1252. * @param int $depth Depth of page. Not Used.
  1253. * @param array $args
  1254. */
  1255. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  1256. $output .= "</li>\n";
  1257. }
  1258. }
  1259. /**
  1260. * Create HTML dropdown list of pages.
  1261. *
  1262. * @since 2.1.0
  1263. * @uses Walker
  1264. */
  1265. class Walker_PageDropdown extends Walker {
  1266. /**
  1267. * @see Walker::$tree_type
  1268. * @since 2.1.0
  1269. * @var string
  1270. */
  1271. public $tree_type = 'page';
  1272. /**
  1273. * @see Walker::$db_fields
  1274. * @since 2.1.0
  1275. * @todo Decouple this
  1276. * @var array
  1277. */
  1278. public $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  1279. /**
  1280. * @see Walker::start_el()
  1281. * @since 2.1.0
  1282. *
  1283. * @param string $output Passed by reference. Used to append additional content.
  1284. * @param object $page Page data object.
  1285. * @param int $depth Depth of page in reference to parent pages. Used for padding.
  1286. * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option
  1287. * element. Uses 'value_field' argument to fill "value" attribute. See {@see wp_dropdown_pages()}.
  1288. * @param int $id
  1289. */
  1290. public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
  1291. $pad = str_repeat('&nbsp;', $depth * 3);
  1292. if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
  1293. $args['value_field'] = 'ID';
  1294. }
  1295. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . "\"";
  1296. if ( $page->ID == $args['selected'] )
  1297. $output .= ' selected="selected"';
  1298. $output .= '>';
  1299. $title = $page->post_title;
  1300. if ( '' === $title ) {
  1301. $title = sprintf( __( '#%d (no title)' ), $page->ID );
  1302. }
  1303. /**
  1304. * Filter the page title when creating an HTML drop-down list of pages.
  1305. *
  1306. * @since 3.1.0
  1307. *
  1308. * @param string $title Page title.
  1309. * @param object $page Page data object.
  1310. */
  1311. $title = apply_filters( 'list_pages', $title, $page );
  1312. $output .= $pad . esc_html( $title );
  1313. $output .= "</option>\n";
  1314. }
  1315. }
  1316. //
  1317. // Attachments
  1318. //
  1319. /**
  1320. * Display an attachment page link using an image or icon.
  1321. *
  1322. * @since 2.0.0
  1323. *
  1324. * @param int|WP_Post $id Optional. Post ID or post object.
  1325. * @param bool $fullsize Optional, default is false. Whether to use full size.
  1326. * @param bool $deprecated Deprecated. Not used.
  1327. * @param bool $permalink Optional, default is false. Whether to include permalink.
  1328. */
  1329. function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
  1330. if ( !empty( $deprecated ) )
  1331. _deprecated_argument( __FUNCTION__, '2.5' );
  1332. if ( $fullsize )
  1333. echo wp_get_attachment_link($id, 'full', $permalink);
  1334. else
  1335. echo wp_get_attachment_link($id, 'thumbnail', $permalink);
  1336. }
  1337. /**
  1338. * Retrieve an attachment page link using an image or icon, if possible.
  1339. *
  1340. * @since 2.5.0
  1341. *
  1342. * @param int|WP_Post $id Optional. Post ID or post object.
  1343. * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
  1344. * @param bool $permalink Optional, default is false. Whether to add permalink to image.
  1345. * @param bool $icon Optional, default is false. Whether to include icon.
  1346. * @param string|bool $text Optional, default is false. If string, then will be link text.
  1347. * @param array|string $attr Optional. Array or string of attributes.
  1348. * @return string HTML content.
  1349. */
  1350. function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
  1351. $id = intval( $id );
  1352. $_post = get_post( $id );
  1353. if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
  1354. return __( 'Missing Attachment' );
  1355. if ( $permalink )
  1356. $url = get_attachment_link( $_post->ID );
  1357. if ( $text ) {
  1358. $link_text = $text;
  1359. } elseif ( $size && 'none' != $size ) {
  1360. $link_text = wp_get_attachment_image( $id, $size, $icon, $attr );
  1361. } else {
  1362. $link_text = '';
  1363. }
  1364. if ( trim( $link_text ) == '' )
  1365. $link_text = $_post->post_title;
  1366. /**
  1367. * Filter a retrieved attachment page link.
  1368. *
  1369. * @since 2.7.0
  1370. *
  1371. * @param string $link_html The page link HTML output.
  1372. * @param int $id Post ID.
  1373. * @param string $size Image size. Default 'thumbnail'.
  1374. * @param bool $permalink Whether to add permalink to image. Default false.
  1375. * @param bool $icon Whether to include an icon. Default false.
  1376. * @param string|bool $text If string, will be link text. Default false.
  1377. */
  1378. return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );
  1379. }
  1380. /**
  1381. * Wrap attachment in paragraph tag before content.
  1382. *
  1383. * @since 2.0.0
  1384. *
  1385. * @param string $content
  1386. * @return string
  1387. */
  1388. function prepend_attachment($content) {
  1389. $post = get_post();
  1390. if ( empty($post->post_type) || $post->post_type != 'attachment' )
  1391. return $content;
  1392. if ( wp_attachment_is( 'video', $post ) ) {
  1393. $meta = wp_get_attachment_metadata( get_the_ID() );
  1394. $atts = array( 'src' => wp_get_attachment_url() );
  1395. if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
  1396. $atts['width'] = (int) $meta['width'];
  1397. $atts['height'] = (int) $meta['height'];
  1398. }
  1399. if ( has_post_thumbnail() ) {
  1400. $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
  1401. }
  1402. $p = wp_video_shortcode( $atts );
  1403. } elseif ( wp_attachment_is( 'audio', $post ) ) {
  1404. $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
  1405. } else {
  1406. $p = '<p class="attachment">';
  1407. // show the medium sized image representation of the attachment if available, and link to the raw file
  1408. $p .= wp_get_attachment_link(0, 'medium', false);
  1409. $p .= '</p>';
  1410. }
  1411. /**
  1412. * Filter the attachment markup to be prepended to the post content.
  1413. *
  1414. * @since 2.0.0
  1415. *
  1416. * @see prepend_attachment()
  1417. *
  1418. * @param string $p The attachment HTML output.
  1419. */
  1420. $p = apply_filters( 'prepend_attachment', $p );
  1421. return "$p\n$content";
  1422. }
  1423. //
  1424. // M