PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post-template.php

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

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