PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post-template.php

https://gitlab.com/geyson/geyson
PHP | 1862 lines | 1303 code | 127 blank | 432 comment | 134 complexity | b8f8aad5ab246c755d50586782c210f9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

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

Large files files are truncated, but you can click here to view the full file