PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post-template.php

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

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