PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://bitbucket.org/crafttheweb/wordpress-fold
PHP | 2456 lines | 1807 code | 186 blank | 463 comment | 184 complexity | 6a5f77ced3cadd45f2b540909ac58f66 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

  1. <?php
  2. /**
  3. * WordPress Link Template Functions
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Display the permalink for the current post.
  10. *
  11. * @since 1.2.0
  12. * @uses apply_filters() Calls 'the_permalink' filter on the permalink string.
  13. */
  14. function the_permalink() {
  15. echo apply_filters('the_permalink', get_permalink());
  16. }
  17. /**
  18. * Retrieve trailing slash string, if blog set for adding trailing slashes.
  19. *
  20. * Conditionally adds a trailing slash if the permalink structure has a trailing
  21. * slash, strips the trailing slash if not. The string is passed through the
  22. * 'user_trailingslashit' filter. Will remove trailing slash from string, if
  23. * blog is not set to have them.
  24. *
  25. * @since 2.2.0
  26. * @uses $wp_rewrite
  27. *
  28. * @param string $string URL with or without a trailing slash.
  29. * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
  30. * @return string
  31. */
  32. function user_trailingslashit($string, $type_of_url = '') {
  33. global $wp_rewrite;
  34. if ( $wp_rewrite->use_trailing_slashes )
  35. $string = trailingslashit($string);
  36. else
  37. $string = untrailingslashit($string);
  38. // Note that $type_of_url can be one of following:
  39. // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged, post_type_archive
  40. $string = apply_filters('user_trailingslashit', $string, $type_of_url);
  41. return $string;
  42. }
  43. /**
  44. * Display permalink anchor for current post.
  45. *
  46. * The permalink mode title will use the post title for the 'a' element 'id'
  47. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
  48. *
  49. * @since 0.71
  50. *
  51. * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
  52. */
  53. function permalink_anchor($mode = 'id') {
  54. global $post;
  55. switch ( strtolower($mode) ) {
  56. case 'title':
  57. $title = sanitize_title($post->post_title) . '-' . $post->ID;
  58. echo '<a id="'.$title.'"></a>';
  59. break;
  60. case 'id':
  61. default:
  62. echo '<a id="post-' . $post->ID . '"></a>';
  63. break;
  64. }
  65. }
  66. /**
  67. * Retrieve full permalink for current post or post ID.
  68. *
  69. * @since 1.0.0
  70. *
  71. * @param int $id Optional. Post ID.
  72. * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
  73. * @return string
  74. */
  75. function get_permalink($id = 0, $leavename = false) {
  76. $rewritecode = array(
  77. '%year%',
  78. '%monthnum%',
  79. '%day%',
  80. '%hour%',
  81. '%minute%',
  82. '%second%',
  83. $leavename? '' : '%postname%',
  84. '%post_id%',
  85. '%category%',
  86. '%author%',
  87. $leavename? '' : '%pagename%',
  88. );
  89. if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
  90. $post = $id;
  91. $sample = true;
  92. } else {
  93. $post = get_post($id);
  94. $sample = false;
  95. }
  96. if ( empty($post->ID) )
  97. return false;
  98. if ( $post->post_type == 'page' )
  99. return get_page_link($post->ID, $leavename, $sample);
  100. elseif ( $post->post_type == 'attachment' )
  101. return get_attachment_link($post->ID);
  102. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
  103. return get_post_permalink($post->ID, $leavename, $sample);
  104. $permalink = get_option('permalink_structure');
  105. $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);
  106. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  107. $unixtime = strtotime($post->post_date);
  108. $category = '';
  109. if ( strpos($permalink, '%category%') !== false ) {
  110. $cats = get_the_category($post->ID);
  111. if ( $cats ) {
  112. usort($cats, '_usort_terms_by_ID'); // order by ID
  113. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  114. $category_object = get_term( $category_object, 'category' );
  115. $category = $category_object->slug;
  116. if ( $parent = $category_object->parent )
  117. $category = get_category_parents($parent, false, '/', true) . $category;
  118. }
  119. // show default category in permalinks, without
  120. // having to assign it explicitly
  121. if ( empty($category) ) {
  122. $default_category = get_category( get_option( 'default_category' ) );
  123. $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  124. }
  125. }
  126. $author = '';
  127. if ( strpos($permalink, '%author%') !== false ) {
  128. $authordata = get_userdata($post->post_author);
  129. $author = $authordata->user_nicename;
  130. }
  131. $date = explode(" ",date('Y m d H i s', $unixtime));
  132. $rewritereplace =
  133. array(
  134. $date[0],
  135. $date[1],
  136. $date[2],
  137. $date[3],
  138. $date[4],
  139. $date[5],
  140. $post->post_name,
  141. $post->ID,
  142. $category,
  143. $author,
  144. $post->post_name,
  145. );
  146. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
  147. $permalink = user_trailingslashit($permalink, 'single');
  148. } else { // if they're not using the fancy permalink option
  149. $permalink = home_url('?p=' . $post->ID);
  150. }
  151. return apply_filters('post_link', $permalink, $post, $leavename);
  152. }
  153. /**
  154. * Retrieve the permalink for a post with a custom post type.
  155. *
  156. * @since 3.0.0
  157. *
  158. * @param int $id Optional. Post ID.
  159. * @param bool $leavename Optional, defaults to false. Whether to keep post name.
  160. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  161. * @return string
  162. */
  163. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  164. global $wp_rewrite;
  165. $post = get_post($id);
  166. if ( is_wp_error( $post ) )
  167. return $post;
  168. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
  169. $slug = $post->post_name;
  170. $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  171. $post_type = get_post_type_object($post->post_type);
  172. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
  173. if ( ! $leavename ) {
  174. if ( $post_type->hierarchical )
  175. $slug = get_page_uri($id);
  176. $post_link = str_replace("%$post->post_type%", $slug, $post_link);
  177. }
  178. $post_link = home_url( user_trailingslashit($post_link) );
  179. } else {
  180. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
  181. $post_link = add_query_arg($post_type->query_var, $slug, '');
  182. else
  183. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
  184. $post_link = home_url($post_link);
  185. }
  186. return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);
  187. }
  188. /**
  189. * Retrieve permalink from post ID.
  190. *
  191. * @since 1.0.0
  192. *
  193. * @param int $post_id Optional. Post ID.
  194. * @param mixed $deprecated Not used.
  195. * @return string
  196. */
  197. function post_permalink( $post_id = 0, $deprecated = '' ) {
  198. if ( !empty( $deprecated ) )
  199. _deprecated_argument( __FUNCTION__, '1.3' );
  200. return get_permalink($post_id);
  201. }
  202. /**
  203. * Retrieve the permalink for current page or page ID.
  204. *
  205. * Respects page_on_front. Use this one.
  206. *
  207. * @since 1.5.0
  208. *
  209. * @param int $id Optional. Post ID.
  210. * @param bool $leavename Optional, defaults to false. Whether to keep page name.
  211. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  212. * @return string
  213. */
  214. function get_page_link( $id = false, $leavename = false, $sample = false ) {
  215. global $post;
  216. $id = (int) $id;
  217. if ( !$id )
  218. $id = (int) $post->ID;
  219. if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
  220. $link = home_url('/');
  221. else
  222. $link = _get_page_link( $id , $leavename, $sample );
  223. return apply_filters('page_link', $link, $id, $sample);
  224. }
  225. /**
  226. * Retrieve the page permalink.
  227. *
  228. * Ignores page_on_front. Internal use only.
  229. *
  230. * @since 2.1.0
  231. * @access private
  232. *
  233. * @param int $id Optional. Post ID.
  234. * @param bool $leavename Optional. Leave name.
  235. * @param bool $sample Optional. Sample permalink.
  236. * @return string
  237. */
  238. function _get_page_link( $id = false, $leavename = false, $sample = false ) {
  239. global $wp_rewrite;
  240. $post = get_post( $id );
  241. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  242. $link = $wp_rewrite->get_page_permastruct();
  243. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
  244. if ( ! $leavename ) {
  245. $link = str_replace('%pagename%', get_page_uri($id), $link);
  246. }
  247. $link = home_url($link);
  248. $link = user_trailingslashit($link, 'page');
  249. } else {
  250. $link = home_url("?page_id=$id");
  251. }
  252. return apply_filters( '_get_page_link', $link, $id );
  253. }
  254. /**
  255. * Retrieve permalink for attachment.
  256. *
  257. * This can be used in the WordPress Loop or outside of it.
  258. *
  259. * @since 2.0.0
  260. *
  261. * @param int $id Optional. Post ID.
  262. * @return string
  263. */
  264. function get_attachment_link($id = false) {
  265. global $post, $wp_rewrite;
  266. $link = false;
  267. if ( ! $id)
  268. $id = (int) $post->ID;
  269. $object = get_post($id);
  270. if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
  271. $parent = get_post($object->post_parent);
  272. if ( 'page' == $parent->post_type )
  273. $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
  274. else
  275. $parentlink = get_permalink( $object->post_parent );
  276. if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
  277. $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
  278. else
  279. $name = $object->post_name;
  280. if ( strpos($parentlink, '?') === false )
  281. $link = user_trailingslashit( trailingslashit($parentlink) . $name );
  282. }
  283. if ( ! $link )
  284. $link = home_url( "/?attachment_id=$id" );
  285. return apply_filters('attachment_link', $link, $id);
  286. }
  287. /**
  288. * Retrieve the permalink for the year archives.
  289. *
  290. * @since 1.5.0
  291. *
  292. * @param int|bool $year False for current year or year for permalink.
  293. * @return string
  294. */
  295. function get_year_link($year) {
  296. global $wp_rewrite;
  297. if ( !$year )
  298. $year = gmdate('Y', current_time('timestamp'));
  299. $yearlink = $wp_rewrite->get_year_permastruct();
  300. if ( !empty($yearlink) ) {
  301. $yearlink = str_replace('%year%', $year, $yearlink);
  302. return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
  303. } else {
  304. return apply_filters('year_link', home_url('?m=' . $year), $year);
  305. }
  306. }
  307. /**
  308. * Retrieve the permalink for the month archives with year.
  309. *
  310. * @since 1.0.0
  311. *
  312. * @param bool|int $year False for current year. Integer of year.
  313. * @param bool|int $month False for current month. Integer of month.
  314. * @return string
  315. */
  316. function get_month_link($year, $month) {
  317. global $wp_rewrite;
  318. if ( !$year )
  319. $year = gmdate('Y', current_time('timestamp'));
  320. if ( !$month )
  321. $month = gmdate('m', current_time('timestamp'));
  322. $monthlink = $wp_rewrite->get_month_permastruct();
  323. if ( !empty($monthlink) ) {
  324. $monthlink = str_replace('%year%', $year, $monthlink);
  325. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
  326. return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
  327. } else {
  328. return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
  329. }
  330. }
  331. /**
  332. * Retrieve the permalink for the day archives with year and month.
  333. *
  334. * @since 1.0.0
  335. *
  336. * @param bool|int $year False for current year. Integer of year.
  337. * @param bool|int $month False for current month. Integer of month.
  338. * @param bool|int $day False for current day. Integer of day.
  339. * @return string
  340. */
  341. function get_day_link($year, $month, $day) {
  342. global $wp_rewrite;
  343. if ( !$year )
  344. $year = gmdate('Y', current_time('timestamp'));
  345. if ( !$month )
  346. $month = gmdate('m', current_time('timestamp'));
  347. if ( !$day )
  348. $day = gmdate('j', current_time('timestamp'));
  349. $daylink = $wp_rewrite->get_day_permastruct();
  350. if ( !empty($daylink) ) {
  351. $daylink = str_replace('%year%', $year, $daylink);
  352. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
  353. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
  354. return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
  355. } else {
  356. return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
  357. }
  358. }
  359. /**
  360. * Display the permalink for the feed type.
  361. *
  362. * @since 3.0.0
  363. *
  364. * @param string $anchor The link's anchor text.
  365. * @param string $feed Optional, defaults to default feed. Feed type.
  366. */
  367. function the_feed_link( $anchor, $feed = '' ) {
  368. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  369. echo apply_filters( 'the_feed_link', $link, $feed );
  370. }
  371. /**
  372. * Retrieve the permalink for the feed type.
  373. *
  374. * @since 1.5.0
  375. *
  376. * @param string $feed Optional, defaults to default feed. Feed type.
  377. * @return string
  378. */
  379. function get_feed_link($feed = '') {
  380. global $wp_rewrite;
  381. $permalink = $wp_rewrite->get_feed_permastruct();
  382. if ( '' != $permalink ) {
  383. if ( false !== strpos($feed, 'comments_') ) {
  384. $feed = str_replace('comments_', '', $feed);
  385. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  386. }
  387. if ( get_default_feed() == $feed )
  388. $feed = '';
  389. $permalink = str_replace('%feed%', $feed, $permalink);
  390. $permalink = preg_replace('#/+#', '/', "/$permalink");
  391. $output = home_url( user_trailingslashit($permalink, 'feed') );
  392. } else {
  393. if ( empty($feed) )
  394. $feed = get_default_feed();
  395. if ( false !== strpos($feed, 'comments_') )
  396. $feed = str_replace('comments_', 'comments-', $feed);
  397. $output = home_url("?feed={$feed}");
  398. }
  399. return apply_filters('feed_link', $output, $feed);
  400. }
  401. /**
  402. * Retrieve the permalink for the post comments feed.
  403. *
  404. * @since 2.2.0
  405. *
  406. * @param int $post_id Optional. Post ID.
  407. * @param string $feed Optional. Feed type.
  408. * @return string
  409. */
  410. function get_post_comments_feed_link($post_id = 0, $feed = '') {
  411. $post_id = absint( $post_id );
  412. if ( ! $post_id )
  413. $post_id = get_the_ID();
  414. if ( empty( $feed ) )
  415. $feed = get_default_feed();
  416. if ( '' != get_option('permalink_structure') ) {
  417. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
  418. $url = _get_page_link( $post_id );
  419. else
  420. $url = get_permalink($post_id);
  421. $url = trailingslashit($url) . 'feed';
  422. if ( $feed != get_default_feed() )
  423. $url .= "/$feed";
  424. $url = user_trailingslashit($url, 'single_feed');
  425. } else {
  426. $type = get_post_field('post_type', $post_id);
  427. if ( 'page' == $type )
  428. $url = home_url("?feed=$feed&amp;page_id=$post_id");
  429. else
  430. $url = home_url("?feed=$feed&amp;p=$post_id");
  431. }
  432. return apply_filters('post_comments_feed_link', $url);
  433. }
  434. /**
  435. * Display the comment feed link for a post.
  436. *
  437. * Prints out the comment feed link for a post. Link text is placed in the
  438. * anchor. If no link text is specified, default text is used. If no post ID is
  439. * specified, the current post is used.
  440. *
  441. * @package WordPress
  442. * @subpackage Feed
  443. * @since 2.5.0
  444. *
  445. * @param string $link_text Descriptive text.
  446. * @param int $post_id Optional post ID. Default to current post.
  447. * @param string $feed Optional. Feed format.
  448. * @return string Link to the comment feed for the current post.
  449. */
  450. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  451. $url = get_post_comments_feed_link($post_id, $feed);
  452. if ( empty($link_text) )
  453. $link_text = __('Comments Feed');
  454. echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
  455. }
  456. /**
  457. * Retrieve the feed link for a given author.
  458. *
  459. * Returns a link to the feed for all posts by a given author. A specific feed
  460. * can be requested or left blank to get the default feed.
  461. *
  462. * @package WordPress
  463. * @subpackage Feed
  464. * @since 2.5.0
  465. *
  466. * @param int $author_id ID of an author.
  467. * @param string $feed Optional. Feed type.
  468. * @return string Link to the feed for the author specified by $author_id.
  469. */
  470. function get_author_feed_link( $author_id, $feed = '' ) {
  471. $author_id = (int) $author_id;
  472. $permalink_structure = get_option('permalink_structure');
  473. if ( empty($feed) )
  474. $feed = get_default_feed();
  475. if ( '' == $permalink_structure ) {
  476. $link = home_url("?feed=$feed&amp;author=" . $author_id);
  477. } else {
  478. $link = get_author_posts_url($author_id);
  479. if ( $feed == get_default_feed() )
  480. $feed_link = 'feed';
  481. else
  482. $feed_link = "feed/$feed";
  483. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  484. }
  485. $link = apply_filters('author_feed_link', $link, $feed);
  486. return $link;
  487. }
  488. /**
  489. * Retrieve the feed link for a category.
  490. *
  491. * Returns a link to the feed for all posts in a given category. A specific feed
  492. * can be requested or left blank to get the default feed.
  493. *
  494. * @package WordPress
  495. * @subpackage Feed
  496. * @since 2.5.0
  497. *
  498. * @param int $cat_id ID of a category.
  499. * @param string $feed Optional. Feed type.
  500. * @return string Link to the feed for the category specified by $cat_id.
  501. */
  502. function get_category_feed_link($cat_id, $feed = '') {
  503. return get_term_feed_link($cat_id, 'category', $feed);
  504. }
  505. /**
  506. * Retrieve the feed link for a term.
  507. *
  508. * Returns a link to the feed for all posts in a given term. A specific feed
  509. * can be requested or left blank to get the default feed.
  510. *
  511. * @since 3.0
  512. *
  513. * @param int $term_id ID of a category.
  514. * @param string $taxonomy Optional. Taxonomy of $term_id
  515. * @param string $feed Optional. Feed type.
  516. * @return string Link to the feed for the term specified by $term_id and $taxonomy.
  517. */
  518. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  519. $term_id = ( int ) $term_id;
  520. $term = get_term( $term_id, $taxonomy );
  521. if ( empty( $term ) || is_wp_error( $term ) )
  522. return false;
  523. if ( empty( $feed ) )
  524. $feed = get_default_feed();
  525. $permalink_structure = get_option( 'permalink_structure' );
  526. if ( '' == $permalink_structure ) {
  527. if ( 'category' == $taxonomy ) {
  528. $link = home_url("?feed=$feed&amp;cat=$term_id");
  529. }
  530. elseif ( 'post_tag' == $taxonomy ) {
  531. $link = home_url("?feed=$feed&amp;tag=$term->slug");
  532. } else {
  533. $t = get_taxonomy( $taxonomy );
  534. $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
  535. }
  536. } else {
  537. $link = get_term_link( $term_id, $term->taxonomy );
  538. if ( $feed == get_default_feed() )
  539. $feed_link = 'feed';
  540. else
  541. $feed_link = "feed/$feed";
  542. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  543. }
  544. if ( 'category' == $taxonomy )
  545. $link = apply_filters( 'category_feed_link', $link, $feed );
  546. elseif ( 'post_tag' == $taxonomy )
  547. $link = apply_filters( 'category_feed_link', $link, $feed );
  548. else
  549. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  550. return $link;
  551. }
  552. /**
  553. * Retrieve permalink for feed of tag.
  554. *
  555. * @since 2.3.0
  556. *
  557. * @param int $tag_id Tag ID.
  558. * @param string $feed Optional. Feed type.
  559. * @return string
  560. */
  561. function get_tag_feed_link($tag_id, $feed = '') {
  562. return get_term_feed_link($tag_id, 'post_tag', $feed);
  563. }
  564. /**
  565. * Retrieve edit tag link.
  566. *
  567. * @since 2.7.0
  568. *
  569. * @param int $tag_id Tag ID
  570. * @param string $taxonomy Taxonomy
  571. * @return string
  572. */
  573. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  574. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  575. }
  576. /**
  577. * Display or retrieve edit tag link with formatting.
  578. *
  579. * @since 2.7.0
  580. *
  581. * @param string $link Optional. Anchor text.
  582. * @param string $before Optional. Display before edit link.
  583. * @param string $after Optional. Display after edit link.
  584. * @param int|object $tag Tag object or ID
  585. * @return string HTML content.
  586. */
  587. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  588. $link = edit_term_link( $link, '', '', false, $tag );
  589. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  590. }
  591. /**
  592. * Retrieve edit term url.
  593. *
  594. * @since 3.1.0
  595. *
  596. * @param int $term_id Term ID
  597. * @param string $taxonomy Taxonomy
  598. * @param string $object_type The object type
  599. * @return string
  600. */
  601. function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
  602. $tax = get_taxonomy( $taxonomy );
  603. if ( !current_user_can( $tax->cap->edit_terms ) )
  604. return;
  605. $term = get_term( $term_id, $taxonomy );
  606. $args = array(
  607. 'action' => 'edit',
  608. 'taxonomy' => $taxonomy,
  609. 'tag_ID' => $term->term_id,
  610. );
  611. if ( $object_type )
  612. $args['post_type'] = $object_type;
  613. $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
  614. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  615. }
  616. /**
  617. * Display or retrieve edit term link with formatting.
  618. *
  619. * @since 3.1.0
  620. *
  621. * @param string $link Optional. Anchor text.
  622. * @param string $before Optional. Display before edit link.
  623. * @param string $after Optional. Display after edit link.
  624. * @param object $term Term object
  625. * @return string HTML content.
  626. */
  627. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  628. if ( is_null( $term ) ) {
  629. $term = get_queried_object();
  630. }
  631. $tax = get_taxonomy( $term->taxonomy );
  632. if ( !current_user_can($tax->cap->edit_terms) )
  633. return;
  634. if ( empty( $link ) )
  635. $link = __('Edit This');
  636. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '" title="' . $link . '">' . $link . '</a>';
  637. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  638. if ( $echo )
  639. echo $link;
  640. else
  641. return $link;
  642. }
  643. /**
  644. * Retrieve permalink for search.
  645. *
  646. * @since 3.0.0
  647. * @param string $query Optional. The query string to use. If empty the current query is used.
  648. * @return string
  649. */
  650. function get_search_link( $query = '' ) {
  651. global $wp_rewrite;
  652. if ( empty($query) )
  653. $search = get_search_query( false );
  654. else
  655. $search = stripslashes($query);
  656. $permastruct = $wp_rewrite->get_search_permastruct();
  657. if ( empty( $permastruct ) ) {
  658. $link = home_url('?s=' . urlencode($search) );
  659. } else {
  660. $search = urlencode($search);
  661. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
  662. $link = str_replace( '%search%', $search, $permastruct );
  663. $link = home_url( user_trailingslashit( $link, 'search' ) );
  664. }
  665. return apply_filters( 'search_link', $link, $search );
  666. }
  667. /**
  668. * Retrieve the permalink for the feed of the search results.
  669. *
  670. * @since 2.5.0
  671. *
  672. * @param string $search_query Optional. Search query.
  673. * @param string $feed Optional. Feed type.
  674. * @return string
  675. */
  676. function get_search_feed_link($search_query = '', $feed = '') {
  677. global $wp_rewrite;
  678. $link = get_search_link($search_query);
  679. if ( empty($feed) )
  680. $feed = get_default_feed();
  681. $permastruct = $wp_rewrite->get_search_permastruct();
  682. if ( empty($permastruct) ) {
  683. $link = add_query_arg('feed', $feed, $link);
  684. } else {
  685. $link = trailingslashit($link);
  686. $link .= "feed/$feed/";
  687. }
  688. $link = apply_filters('search_feed_link', $link, $feed, 'posts');
  689. return $link;
  690. }
  691. /**
  692. * Retrieve the permalink for the comments feed of the search results.
  693. *
  694. * @since 2.5.0
  695. *
  696. * @param string $search_query Optional. Search query.
  697. * @param string $feed Optional. Feed type.
  698. * @return string
  699. */
  700. function get_search_comments_feed_link($search_query = '', $feed = '') {
  701. global $wp_rewrite;
  702. if ( empty($feed) )
  703. $feed = get_default_feed();
  704. $link = get_search_feed_link($search_query, $feed);
  705. $permastruct = $wp_rewrite->get_search_permastruct();
  706. if ( empty($permastruct) )
  707. $link = add_query_arg('feed', 'comments-' . $feed, $link);
  708. else
  709. $link = add_query_arg('withcomments', 1, $link);
  710. $link = apply_filters('search_feed_link', $link, $feed, 'comments');
  711. return $link;
  712. }
  713. /**
  714. * Retrieve the permalink for a post type archive.
  715. *
  716. * @since 3.1.0
  717. *
  718. * @param string $post_type Post type
  719. * @return string
  720. */
  721. function get_post_type_archive_link( $post_type ) {
  722. global $wp_rewrite;
  723. if ( ! $post_type_obj = get_post_type_object( $post_type ) )
  724. return false;
  725. if ( ! $post_type_obj->has_archive )
  726. return false;
  727. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  728. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  729. if ( $post_type_obj->rewrite['with_front'] )
  730. $struct = $wp_rewrite->front . $struct;
  731. else
  732. $struct = $wp_rewrite->root . $struct;
  733. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  734. } else {
  735. $link = home_url( '?post_type=' . $post_type );
  736. }
  737. return apply_filters( 'post_type_archive_link', $link, $post_type );
  738. }
  739. /**
  740. * Retrieve the permalink for a post type archive feed.
  741. *
  742. * @since 3.1.0
  743. *
  744. * @param string $post_type Post type
  745. * @param string $feed Optional. Feed type
  746. * @return string
  747. */
  748. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  749. $default_feed = get_default_feed();
  750. if ( empty( $feed ) )
  751. $feed = $default_feed;
  752. if ( ! $link = get_post_type_archive_link( $post_type ) )
  753. return false;
  754. $post_type_obj = get_post_type_object( $post_type );
  755. if ( $post_type_obj->rewrite['feeds'] && get_option( 'permalink_structure' ) ) {
  756. $link = trailingslashit($link);
  757. $link .= 'feed/';
  758. if ( $feed != $default_feed )
  759. $link .= "$feed/";
  760. } else {
  761. $link = add_query_arg( 'feed', $feed, $link );
  762. }
  763. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  764. }
  765. /**
  766. * Retrieve edit posts link for post.
  767. *
  768. * Can be used within the WordPress loop or outside of it. Can be used with
  769. * pages, posts, attachments, and revisions.
  770. *
  771. * @since 2.3.0
  772. *
  773. * @param int $id Optional. Post ID.
  774. * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
  775. * @return string
  776. */
  777. function get_edit_post_link( $id = 0, $context = 'display' ) {
  778. if ( !$post = get_post( $id ) )
  779. return;
  780. if ( 'display' == $context )
  781. $action = '&amp;action=edit';
  782. else
  783. $action = '&action=edit';
  784. $post_type_object = get_post_type_object( $post->post_type );
  785. if ( !$post_type_object )
  786. return;
  787. if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) )
  788. return;
  789. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  790. }
  791. /**
  792. * Display edit post link for post.
  793. *
  794. * @since 1.0.0
  795. *
  796. * @param string $link Optional. Anchor text.
  797. * @param string $before Optional. Display before edit link.
  798. * @param string $after Optional. Display after edit link.
  799. * @param int $id Optional. Post ID.
  800. */
  801. function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  802. if ( !$post = get_post( $id ) )
  803. return;
  804. if ( !$url = get_edit_post_link( $post->ID ) )
  805. return;
  806. if ( null === $link )
  807. $link = __('Edit This');
  808. $post_type_obj = get_post_type_object( $post->post_type );
  809. $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( $post_type_obj->labels->edit_item ) . '">' . $link . '</a>';
  810. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  811. }
  812. /**
  813. * Retrieve delete posts link for post.
  814. *
  815. * Can be used within the WordPress loop or outside of it, with any post type.
  816. *
  817. * @since 2.9.0
  818. *
  819. * @param int $id Optional. Post ID.
  820. * @param string $deprecated Not used.
  821. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  822. * @return string
  823. */
  824. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  825. if ( ! empty( $deprecated ) )
  826. _deprecated_argument( __FUNCTION__, '3.0' );
  827. if ( !$post = get_post( $id ) )
  828. return;
  829. $post_type_object = get_post_type_object( $post->post_type );
  830. if ( !$post_type_object )
  831. return;
  832. if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
  833. return;
  834. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  835. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  836. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  837. }
  838. /**
  839. * Retrieve edit comment link.
  840. *
  841. * @since 2.3.0
  842. *
  843. * @param int $comment_id Optional. Comment ID.
  844. * @return string
  845. */
  846. function get_edit_comment_link( $comment_id = 0 ) {
  847. $comment = &get_comment( $comment_id );
  848. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  849. return;
  850. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  851. return apply_filters( 'get_edit_comment_link', $location );
  852. }
  853. /**
  854. * Display or retrieve edit comment link with formatting.
  855. *
  856. * @since 1.0.0
  857. *
  858. * @param string $link Optional. Anchor text.
  859. * @param string $before Optional. Display before edit link.
  860. * @param string $after Optional. Display after edit link.
  861. * @return string|null HTML content, if $echo is set to false.
  862. */
  863. function edit_comment_link( $link = null, $before = '', $after = '' ) {
  864. global $comment;
  865. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  866. return;
  867. if ( null === $link )
  868. $link = __('Edit This');
  869. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . esc_attr__( 'Edit comment' ) . '">' . $link . '</a>';
  870. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  871. }
  872. /**
  873. * Display edit bookmark (literally a URL external to blog) link.
  874. *
  875. * @since 2.7.0
  876. *
  877. * @param int $link Optional. Bookmark ID.
  878. * @return string
  879. */
  880. function get_edit_bookmark_link( $link = 0 ) {
  881. $link = get_bookmark( $link );
  882. if ( !current_user_can('manage_links') )
  883. return;
  884. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  885. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  886. }
  887. /**
  888. * Display edit bookmark (literally a URL external to blog) link anchor content.
  889. *
  890. * @since 2.7.0
  891. *
  892. * @param string $link Optional. Anchor text.
  893. * @param string $before Optional. Display before edit link.
  894. * @param string $after Optional. Display after edit link.
  895. * @param int $bookmark Optional. Bookmark ID.
  896. */
  897. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  898. $bookmark = get_bookmark($bookmark);
  899. if ( !current_user_can('manage_links') )
  900. return;
  901. if ( empty($link) )
  902. $link = __('Edit This');
  903. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '" title="' . esc_attr__( 'Edit Link' ) . '">' . $link . '</a>';
  904. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  905. }
  906. /**
  907. * Retrieve edit user link
  908. *
  909. * @since 3.5.0
  910. *
  911. * @param int $user_id Optional. User ID. Defaults to the current user.
  912. * @return string URL to edit user page or empty string.
  913. */
  914. function get_edit_user_link( $user_id = null ) {
  915. if ( ! $user_id )
  916. $user_id = get_current_user_id();
  917. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
  918. return '';
  919. $user = get_userdata( $user_id );
  920. if ( ! $user )
  921. return '';
  922. if ( get_current_user_id() == $user->ID )
  923. $link = get_edit_profile_url( $user->ID );
  924. else
  925. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  926. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  927. }
  928. // Navigation links
  929. /**
  930. * Retrieve previous post that is adjacent to current post.
  931. *
  932. * @since 1.5.0
  933. *
  934. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  935. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  936. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  937. */
  938. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  939. return get_adjacent_post($in_same_cat, $excluded_categories);
  940. }
  941. /**
  942. * Retrieve next post that is adjacent to current post.
  943. *
  944. * @since 1.5.0
  945. *
  946. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  947. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  948. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  949. */
  950. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  951. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  952. }
  953. /**
  954. * Retrieve adjacent post.
  955. *
  956. * Can either be next or previous post.
  957. *
  958. * @since 2.5.0
  959. *
  960. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  961. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  962. * @param bool $previous Optional. Whether to retrieve previous post.
  963. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  964. */
  965. function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
  966. global $post, $wpdb;
  967. if ( empty( $post ) )
  968. return null;
  969. $current_post_date = $post->post_date;
  970. $join = '';
  971. $posts_in_ex_cats_sql = '';
  972. if ( $in_same_cat || ! empty( $excluded_categories ) ) {
  973. $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
  974. if ( $in_same_cat ) {
  975. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  976. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  977. }
  978. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  979. if ( ! empty( $excluded_categories ) ) {
  980. if ( ! is_array( $excluded_categories ) ) {
  981. // back-compat, $excluded_categories used to be IDs separated by " and "
  982. if ( strpos( $excluded_categories, ' and ' ) !== false ) {
  983. _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
  984. $excluded_categories = explode( ' and ', $excluded_categories );
  985. } else {
  986. $excluded_categories = explode( ',', $excluded_categories );
  987. }
  988. }
  989. $excluded_categories = array_map( 'intval', $excluded_categories );
  990. if ( ! empty( $cat_array ) ) {
  991. $excluded_categories = array_diff($excluded_categories, $cat_array);
  992. $posts_in_ex_cats_sql = '';
  993. }
  994. if ( !empty($excluded_categories) ) {
  995. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  996. }
  997. }
  998. }
  999. $adjacent = $previous ? 'previous' : 'next';
  1000. $op = $previous ? '<' : '>';
  1001. $order = $previous ? 'DESC' : 'ASC';
  1002. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  1003. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
  1004. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  1005. $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
  1006. $query_key = 'adjacent_post_' . md5($query);
  1007. $result = wp_cache_get($query_key, 'counts');
  1008. if ( false !== $result )
  1009. return $result;
  1010. $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  1011. if ( null === $result )
  1012. $result = '';
  1013. wp_cache_set($query_key, $result, 'counts');
  1014. return $result;
  1015. }
  1016. /**
  1017. * Get adjacent post relational link.
  1018. *
  1019. * Can either be next or previous post relational link.
  1020. *
  1021. * @since 2.8.0
  1022. *
  1023. * @param string $title Optional. Link title format.
  1024. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1025. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1026. * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
  1027. * @return string
  1028. */
  1029. function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1030. if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
  1031. $post = get_post($GLOBALS['post']->post_parent);
  1032. else
  1033. $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
  1034. if ( empty($post) )
  1035. return;
  1036. if ( empty($post->post_title) )
  1037. $post->post_title = $previous ? __('Previous Post') : __('Next Post');
  1038. $date = mysql2date(get_option('date_format'), $post->post_date);
  1039. $title = str_replace('%title', $post->post_title, $title);
  1040. $title = str_replace('%date', $date, $title);
  1041. $title = apply_filters('the_title', $title, $post->ID);
  1042. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1043. $link .= esc_attr( $title );
  1044. $link .= "' href='" . get_permalink($post) . "' />\n";
  1045. $adjacent = $previous ? 'previous' : 'next';
  1046. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1047. }
  1048. /**
  1049. * Display relational links for the posts adjacent to the current post.
  1050. *
  1051. * @since 2.8.0
  1052. *
  1053. * @param string $title Optional. Link title format.
  1054. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1055. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1056. */
  1057. function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1058. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1059. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1060. }
  1061. /**
  1062. * Display relational links for the posts adjacent to the current post for single post pages.
  1063. *
  1064. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1065. * @since 3.0.0
  1066. *
  1067. */
  1068. function adjacent_posts_rel_link_wp_head() {
  1069. if ( !is_singular() || is_attachment() )
  1070. return;
  1071. adjacent_posts_rel_link();
  1072. }
  1073. /**
  1074. * Display relational link for the next post adjacent to the current post.
  1075. *
  1076. * @since 2.8.0
  1077. *
  1078. * @param string $title Optional. Link title format.
  1079. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1080. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1081. */
  1082. function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1083. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1084. }
  1085. /**
  1086. * Display relational link for the previous post adjacent to the current post.
  1087. *
  1088. * @since 2.8.0
  1089. *
  1090. * @param string $title Optional. Link title format.
  1091. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1092. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1093. */
  1094. function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1095. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1096. }
  1097. /**
  1098. * Retrieve boundary post.
  1099. *
  1100. * Boundary being either the first or last post by publish date within the constraints specified
  1101. * by $in_same_cat or $excluded_categories.
  1102. *
  1103. * @since 2.8.0
  1104. *
  1105. * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
  1106. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1107. * @param bool $start Optional. Whether to retrieve first or last post.
  1108. * @return object
  1109. */
  1110. function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
  1111. global $post;
  1112. if ( empty($post) || ! is_single() || is_attachment() )
  1113. return null;
  1114. $cat_array = array();
  1115. if( ! is_array( $excluded_categories ) )
  1116. $excluded_categories = explode( ',', $excluded_categories );
  1117. if ( $in_same_cat || ! empty( $excluded_categories ) ) {
  1118. if ( $in_same_cat )
  1119. $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
  1120. if ( ! empty( $excluded_categories ) ) {
  1121. $excluded_categories = array_map( 'intval', $excluded_categories );
  1122. $excluded_categories = array_diff( $excluded_categories, $cat_array );
  1123. $inverse_cats = array();
  1124. foreach ( $excluded_categories as $excluded_category )
  1125. $inverse_cats[] = $excluded_category * -1;
  1126. $excluded_categories = $inverse_cats;
  1127. }
  1128. }
  1129. $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
  1130. $order = $start ? 'ASC' : 'DESC';
  1131. return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
  1132. }
  1133. /**
  1134. * Display previous post link that is adjacent to the current post.
  1135. *
  1136. * @since 1.5.0
  1137. *
  1138. * @param string $format Optional. Link anchor format.
  1139. * @param string $link Optional. Link permalink format.
  1140. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1141. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1142. */
  1143. function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1144. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
  1145. }
  1146. /**
  1147. * Display next post link that is adjacent to the current post.
  1148. *
  1149. * @since 1.5.0
  1150. *
  1151. * @param string $format Optional. Link anchor format.
  1152. * @param string $link Optional. Link permalink format.
  1153. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1154. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1155. */
  1156. function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1157. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
  1158. }
  1159. /**
  1160. * Display adjacent post link.
  1161. *
  1162. * Can be either next post link or previous.
  1163. *
  1164. * @since 2.5.0
  1165. *
  1166. * @param string $format Link anchor format.
  1167. * @param string $link Link permalink format.
  1168. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1169. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1170. * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
  1171. */
  1172. function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1173. if ( $previous && is_attachment() )
  1174. $post = get_post($GLOBALS['post']->post_parent);
  1175. else
  1176. $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
  1177. if ( !$post )
  1178. return;
  1179. $title = $post->post_title;
  1180. if ( empty($post->post_title) )
  1181. $title = $previous ? __('Previous Post') : __('Next Post');
  1182. $title = apply_filters('the_title', $title, $post->ID);
  1183. $date = mysql2date(get_option('date_format'), $post->post_date);
  1184. $rel = $previous ? 'prev' : 'next';
  1185. $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
  1186. $link = str_replace('%title', $title, $link);
  1187. $link = str_replace('%date', $date, $link);
  1188. $link = $string . $link . '</a>';
  1189. $format = str_replace('%link', $link, $format);
  1190. $adjacent = $previous ? 'previous' : 'next';
  1191. echo apply_filters( "{$adjacent}_post_link", $format, $link );
  1192. }
  1193. /**
  1194. * Retrieve links for page numbers.
  1195. *
  1196. * @since 1.5.0
  1197. *
  1198. * @param int $pagenum Optional. Page ID.
  1199. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1200. * Otherwise, prepares the URL with esc_url_raw().
  1201. * @return string
  1202. */
  1203. function get_pagenum_link($pagenum = 1, $escape = true ) {
  1204. global $wp_rewrite;
  1205. $pagenum = (int) $pagenum;
  1206. $request = remove_query_arg( 'paged' );
  1207. $home_root = parse_url(home_url());
  1208. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1209. $home_root = preg_quote( $home_root, '|' );
  1210. $request = preg_replace('|^'. $home_root . '|', '', $request);
  1211. $request = preg_replace('|^/+|', '', $request);
  1212. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1213. $base = trailingslashit( get_bloginfo( 'url' ) );
  1214. if ( $pagenum > 1 ) {
  1215. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1216. } else {
  1217. $result = $base . $request;
  1218. }
  1219. } else {
  1220. $qs_regex = '|\?.*?$|';
  1221. preg_match( $qs_regex, $request, $qs_match );
  1222. if ( !empty( $qs_match[0] ) ) {
  1223. $query_string = $qs_match[0];
  1224. $request = preg_replace( $qs_regex, '', $request );
  1225. } else {
  1226. $query_string = '';
  1227. }
  1228. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1229. $request = preg_replace( '|^index\.php|', '', $request);
  1230. $request = ltrim($request, '/');
  1231. $base = trailingslashit( get_bloginfo( 'url' ) );
  1232. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1233. $base .= 'index.php/';
  1234. if ( $pagenum > 1 ) {
  1235. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1236. }
  1237. $result = $base . $request . $query_string;
  1238. }
  1239. $result = apply_filters('get_pagenum_link', $result);
  1240. if ( $escape )
  1241. return esc_url( $result );
  1242. else
  1243. return esc_url_raw( $result );
  1244. }
  1245. /**
  1246. * Retrieve next posts page link.
  1247. *
  1248. * Backported from 2.1.3 to 2.0.10.
  1249. *
  1250. * @since 2.0.10
  1251. *
  1252. * @param int $max_page Optional. Max pages.
  1253. * @return string
  1254. */
  1255. function get_next_posts_page_link($max_page = 0) {
  1256. global $paged;
  1257. if ( !is_single() ) {
  1258. if ( !$paged )
  1259. $paged = 1;
  1260. $nextpage = intval($paged) + 1;
  1261. if ( !$max_page || $max_page >= $nextpage )
  1262. return get_pagenum_link($nextpage);
  1263. }
  1264. }
  1265. /**
  1266. * Display or return the next posts page link.
  1267. *
  1268. * @since 0.71
  1269. *
  1270. * @param int $max_page Optional. Max pages.
  1271. * @param boolean $echo Optional. Echo or return;
  1272. */
  1273. function next_posts( $max_page = 0, $echo = true ) {
  1274. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1275. if ( $echo )
  1276. echo $output;
  1277. else
  1278. return $output;
  1279. }
  1280. /**
  1281. * Return the next posts page link.
  1282. *
  1283. * @since 2.7.0
  1284. *
  1285. * @param string $label Content for link text.
  1286. * @param int $max_page Optional. Max pages.
  1287. * @return string|null
  1288. */
  1289. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1290. global $paged, $wp_query;
  1291. if ( !$max_page )
  1292. $max_page = $wp_query->max_num_pages;
  1293. if ( !$paged )
  1294. $paged = 1;
  1295. $nextpage = intval($paged) + 1;
  1296. if ( null === $label )
  1297. $label = __( 'Next Page &raquo;' );
  1298. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1299. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1300. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1301. }
  1302. }
  1303. /**
  1304. * Display the next posts page link.
  1305. *
  1306. * @since 0.71
  1307. * @uses get_next_posts_link()
  1308. *
  1309. * @param string $label Content for link text.
  1310. * @param int $max_page Optional. Max pages.
  1311. */
  1312. function next_posts_link( $label = null, $max_page = 0 ) {
  1313. echo get_next_posts_link( $label, $max_page );
  1314. }
  1315. /**
  1316. * Retrieve previous posts page link.
  1317. *
  1318. * Will only return string, if not on a single page or post.
  1319. *
  1320. * Backported to 2.0.10 from 2.1.3.
  1321. *
  1322. * @since 2.0.10
  1323. *
  1324. * @return string|null
  1325. */
  1326. function get_previous_posts_page_link() {
  1327. global $paged;
  1328. if ( !is_single() ) {
  1329. $nextpage = intval($paged) - 1;
  1330. if ( $nextpage < 1 )
  1331. $nextpage = 1;
  1332. return get_pagenum_link($nextpage);
  1333. }
  1334. }
  1335. /**
  1336. * Display or return the previous posts page link.
  1337. *
  1338. * @since 0.71
  1339. *
  1340. * @param boolean $echo Optional. Echo or return;
  1341. */
  1342. function previous_posts( $echo = true ) {
  1343. $output = esc_url( get_previous_posts_page_link() );
  1344. if ( $echo )
  1345. echo $output;
  1346. else
  1347. return $output;
  1348. }
  1349. /**
  1350. * Return the previous posts page link.
  1351. *
  1352. * @since 2.7.0
  1353. *
  1354. * @param string $label Optional. Previous page link text.
  1355. * @return string|null
  1356. */
  1357. function get_previous_posts_link( $label = null ) {
  1358. global $paged;
  1359. if ( null === $label )
  1360. $label = __( '&laquo; Previous Page' );
  1361. if ( !is_single() && $paged > 1 ) {
  1362. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1363. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
  1364. }
  1365. }
  1366. /**
  1367. * Display the previous posts page link.
  1368. *
  1369. * @since 0.71
  1370. * @uses get_previous_posts_link()
  1371. *
  1372. * @param string $label Optional. Previous page link text.
  1373. */
  1374. function previous_posts_link( $label = null ) {
  1375. echo get_previous_posts_link( $label );
  1376. }
  1377. /**
  1378. * Return post pages link navigation for previous and next pages.
  1379. *
  1380. * @since 2.8
  1381. *
  1382. * @param string|array $args Optional args.
  1383. * @return string The posts link navigation.
  1384. */
  1385. function get_posts_nav_link( $args = array() ) {
  1386. global $wp_query;
  1387. $return = '';
  1388. if ( !is_singular() ) {
  1389. $defaults = array(
  1390. 'sep' => ' &#8212; ',
  1391. 'prelabel' => __('&laquo; Previous Page'),
  1392. 'nxtlabel' => __('Next Page &raquo;'),
  1393. );
  1394. $args = wp_parse_args( $args, $defaults );
  1395. $max_num_pages = $wp_query->max_num_pages;
  1396. $paged = get_query_var('paged');
  1397. //only have sep if there's both prev and next results
  1398. if ($paged < 2 || $paged >= $max_num_pages) {
  1399. $args['sep'] = '';
  1400. }
  1401. if ( $max_num_pages > 1 ) {
  1402. $return = get_previous_posts_link($args['prelabel']);
  1403. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
  1404. $return .= get_next_posts_link($args['nxtlabel']);
  1405. }
  1406. }
  1407. return $return;
  1408. }
  1409. /**
  1410. * Display post pages link navigation for previous and next pages.
  1411. *
  1412. * @since 0.71
  1413. *
  1414. * @param string $sep Optional. Separator for posts navigation links.
  1415. * @param string $prelabel Optional. Label for previous pages.
  1416. * @param string $nxtlabel Optional Label for next pages.
  1417. */
  1418. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1419. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1420. echo get_posts_nav_link($args);
  1421. }
  1422. /**
  1423. * Retrieve comments page number link.
  1424. *
  1425. * @since 2.7.0
  1426. *
  1427. * @param int $pagenum Optional. Page number.
  1428. * @return string
  1429. */
  1430. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1431. global $post, $wp_rewrite;
  1432. $pagenum = (int) $pagenum;
  1433. $result = get_permalink( $post->ID );
  1434. if ( 'newest' == get_option('default_comments_page') ) {
  1435. if ( $pagenum != $max_page ) {
  1436. if ( $wp_rewrite->using_permalinks() )
  1437. $result = user_tr

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