PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/link-template.php

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

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