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

/msw/dev/wp-includes/link-template.php

https://github.com/chrissiebrodigan/USC
PHP | 2263 lines | 1190 code | 314 blank | 759 comment | 310 complexity | d00508cd9e9db0bc5714328d45d6e68d MD5 | raw file

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 = 'draft' == $post->post_status || 'pending' == $post->post_status;
  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);
  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);
  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. $cat_id = (int) $cat_id;
  503. $category = get_category($cat_id);
  504. if ( empty($category) || is_wp_error($category) )
  505. return false;
  506. if ( empty($feed) )
  507. $feed = get_default_feed();
  508. $permalink_structure = get_option('permalink_structure');
  509. if ( '' == $permalink_structure ) {
  510. $link = home_url("?feed=$feed&amp;cat=" . $cat_id);
  511. } else {
  512. $link = get_category_link($cat_id);
  513. if( $feed == get_default_feed() )
  514. $feed_link = 'feed';
  515. else
  516. $feed_link = "feed/$feed";
  517. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  518. }
  519. $link = apply_filters('category_feed_link', $link, $feed);
  520. return $link;
  521. }
  522. /**
  523. * Retrieve permalink for feed of tag.
  524. *
  525. * @since 2.3.0
  526. *
  527. * @param int $tag_id Tag ID.
  528. * @param string $feed Optional. Feed type.
  529. * @return string
  530. */
  531. function get_tag_feed_link($tag_id, $feed = '') {
  532. $tag_id = (int) $tag_id;
  533. $tag = get_tag($tag_id);
  534. if ( empty($tag) || is_wp_error($tag) )
  535. return false;
  536. $permalink_structure = get_option('permalink_structure');
  537. if ( empty($feed) )
  538. $feed = get_default_feed();
  539. if ( '' == $permalink_structure ) {
  540. $link = home_url("?feed=$feed&amp;tag=" . $tag->slug);
  541. } else {
  542. $link = get_tag_link($tag->term_id);
  543. if ( $feed == get_default_feed() )
  544. $feed_link = 'feed';
  545. else
  546. $feed_link = "feed/$feed";
  547. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  548. }
  549. $link = apply_filters('tag_feed_link', $link, $feed);
  550. return $link;
  551. }
  552. /**
  553. * Retrieve edit tag link.
  554. *
  555. * @since 2.7.0
  556. *
  557. * @param int $tag_id Tag ID
  558. * @return string
  559. */
  560. function get_edit_tag_link( $tag_id = 0, $taxonomy = 'post_tag' ) {
  561. global $post_type;
  562. $tax = get_taxonomy($taxonomy);
  563. if ( !current_user_can($tax->edit_cap) )
  564. return;
  565. $tag = get_term($tag_id, $taxonomy);
  566. $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);
  567. return apply_filters( 'get_edit_tag_link', $location );
  568. }
  569. /**
  570. * Display or retrieve edit tag link with formatting.
  571. *
  572. * @since 2.7.0
  573. *
  574. * @param string $link Optional. Anchor text.
  575. * @param string $before Optional. Display before edit link.
  576. * @param string $after Optional. Display after edit link.
  577. * @param int|object $tag Tag object or ID
  578. * @return string|null HTML content, if $echo is set to false.
  579. */
  580. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  581. $tax = get_taxonomy('post_tag');
  582. if ( !current_user_can($tax->edit_cap) )
  583. return;
  584. $tag = get_term($tag, 'post_tag');
  585. if ( empty($link) )
  586. $link = __('Edit This');
  587. $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit Tag' ) . '">' . $link . '</a>';
  588. echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
  589. }
  590. /**
  591. * Retrieve permalink for search.
  592. *
  593. * @since 3.0.0
  594. * @param string $query Optional. The query string to use. If empty the current query is used.
  595. * @return string
  596. */
  597. function get_search_link( $query = '' ) {
  598. global $wp_rewrite;
  599. if ( empty($query) )
  600. $search = get_search_query( false );
  601. else
  602. $search = stripslashes($query);
  603. $permastruct = $wp_rewrite->get_search_permastruct();
  604. if ( empty( $permastruct ) ) {
  605. $link = home_url('?s=' . urlencode($search) );
  606. } else {
  607. $search = urlencode($search);
  608. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
  609. $link = str_replace( '%search%', $search, $permastruct );
  610. $link = home_url( user_trailingslashit( $link, 'search' ) );
  611. }
  612. return apply_filters( 'search_link', $link, $search );
  613. }
  614. /**
  615. * Retrieve the permalink for the feed of the search results.
  616. *
  617. * @since 2.5.0
  618. *
  619. * @param string $search_query Optional. Search query.
  620. * @param string $feed Optional. Feed type.
  621. * @return string
  622. */
  623. function get_search_feed_link($search_query = '', $feed = '') {
  624. global $wp_rewrite;
  625. $link = get_search_link($search_query);
  626. if ( empty($feed) )
  627. $feed = get_default_feed();
  628. $permastruct = $wp_rewrite->get_search_permastruct();
  629. if ( empty($permastruct) ) {
  630. $link = add_query_arg('feed', $feed, $link);
  631. } else {
  632. $link = trailingslashit($link);
  633. $link .= "feed/$feed/";
  634. }
  635. $link = apply_filters('search_feed_link', $link, $feed, 'posts');
  636. return $link;
  637. }
  638. /**
  639. * Retrieve the permalink for the comments feed of the search results.
  640. *
  641. * @since 2.5.0
  642. *
  643. * @param string $search_query Optional. Search query.
  644. * @param string $feed Optional. Feed type.
  645. * @return string
  646. */
  647. function get_search_comments_feed_link($search_query = '', $feed = '') {
  648. global $wp_rewrite;
  649. if ( empty($feed) )
  650. $feed = get_default_feed();
  651. $link = get_search_feed_link($search_query, $feed);
  652. $permastruct = $wp_rewrite->get_search_permastruct();
  653. if ( empty($permastruct) )
  654. $link = add_query_arg('feed', 'comments-' . $feed, $link);
  655. else
  656. $link = add_query_arg('withcomments', 1, $link);
  657. $link = apply_filters('search_feed_link', $link, $feed, 'comments');
  658. return $link;
  659. }
  660. /**
  661. * Retrieve edit posts link for post.
  662. *
  663. * Can be used within the WordPress loop or outside of it. Can be used with
  664. * pages, posts, attachments, and revisions.
  665. *
  666. * @since 2.3.0
  667. *
  668. * @param int $id Optional. Post ID.
  669. * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
  670. * @return string
  671. */
  672. function get_edit_post_link( $id = 0, $context = 'display' ) {
  673. if ( !$post = &get_post( $id ) )
  674. return;
  675. if ( 'display' == $context )
  676. $action = '&amp;action=edit';
  677. else
  678. $action = '&action=edit';
  679. $post_type_object = get_post_type_object( $post->post_type );
  680. if ( !$post_type_object )
  681. return;
  682. if ( !current_user_can( $post_type_object->edit_cap, $post->ID ) )
  683. return;
  684. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  685. }
  686. /**
  687. * Display edit post link for post.
  688. *
  689. * @since 1.0.0
  690. *
  691. * @param string $link Optional. Anchor text.
  692. * @param string $before Optional. Display before edit link.
  693. * @param string $after Optional. Display after edit link.
  694. * @param int $id Optional. Post ID.
  695. */
  696. function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  697. if ( !$post = &get_post( $id ) )
  698. return;
  699. if ( !$url = get_edit_post_link( $post->ID ) )
  700. return;
  701. if ( null === $link )
  702. $link = __('Edit This');
  703. $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( __( 'Edit Post' ) ) . '">' . $link . '</a>';
  704. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  705. }
  706. /**
  707. * Retrieve delete posts link for post.
  708. *
  709. * Can be used within the WordPress loop or outside of it, with any post type.
  710. *
  711. * @since 2.9.0
  712. *
  713. * @param int $id Optional. Post ID.
  714. * @param string $deprecated Not used.
  715. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  716. * @return string
  717. */
  718. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  719. if ( ! empty( $deprecated ) )
  720. _deprecated_argument( __FUNCTION__, '3.0.0' );
  721. if ( !$post = &get_post( $id ) )
  722. return;
  723. $post_type_object = get_post_type_object( $post->post_type );
  724. if ( !$post_type_object )
  725. return;
  726. if ( !current_user_can( $post_type_object->delete_cap, $post->ID ) )
  727. return;
  728. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  729. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  730. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-{$post->post_type}_{$post->ID}" ), $post->ID, $force_delete );
  731. }
  732. /**
  733. * Retrieve edit comment link.
  734. *
  735. * @since 2.3.0
  736. *
  737. * @param int $comment_id Optional. Comment ID.
  738. * @return string
  739. */
  740. function get_edit_comment_link( $comment_id = 0 ) {
  741. $comment = &get_comment( $comment_id );
  742. $post = &get_post( $comment->comment_post_ID );
  743. if ( $post->post_type == 'page' ) {
  744. if ( !current_user_can( 'edit_page', $post->ID ) )
  745. return;
  746. } else {
  747. if ( !current_user_can( 'edit_post', $post->ID ) )
  748. return;
  749. }
  750. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  751. return apply_filters( 'get_edit_comment_link', $location );
  752. }
  753. /**
  754. * Display or retrieve edit comment link with formatting.
  755. *
  756. * @since 1.0.0
  757. *
  758. * @param string $link Optional. Anchor text.
  759. * @param string $before Optional. Display before edit link.
  760. * @param string $after Optional. Display after edit link.
  761. * @return string|null HTML content, if $echo is set to false.
  762. */
  763. function edit_comment_link( $link = null, $before = '', $after = '' ) {
  764. global $comment, $post;
  765. if ( $post->post_type == 'page' ) {
  766. if ( !current_user_can( 'edit_page', $post->ID ) )
  767. return;
  768. } else {
  769. if ( !current_user_can( 'edit_post', $post->ID ) )
  770. return;
  771. }
  772. if ( null === $link )
  773. $link = __('Edit This');
  774. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
  775. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  776. }
  777. /**
  778. * Display edit bookmark (literally a URL external to blog) link.
  779. *
  780. * @since 2.7.0
  781. *
  782. * @param int $link Optional. Bookmark ID.
  783. * @return string
  784. */
  785. function get_edit_bookmark_link( $link = 0 ) {
  786. $link = get_bookmark( $link );
  787. if ( !current_user_can('manage_links') )
  788. return;
  789. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  790. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  791. }
  792. /**
  793. * Display edit bookmark (literally a URL external to blog) link anchor content.
  794. *
  795. * @since 2.7.0
  796. *
  797. * @param string $link Optional. Anchor text.
  798. * @param string $before Optional. Display before edit link.
  799. * @param string $after Optional. Display after edit link.
  800. * @param int $bookmark Optional. Bookmark ID.
  801. */
  802. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  803. $bookmark = get_bookmark($bookmark);
  804. if ( !current_user_can('manage_links') )
  805. return;
  806. if ( empty($link) )
  807. $link = __('Edit This');
  808. $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit Link' ) . '">' . $link . '</a>';
  809. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  810. }
  811. // Navigation links
  812. /**
  813. * Retrieve previous post link that is adjacent to current post.
  814. *
  815. * @since 1.5.0
  816. *
  817. * @param bool $in_same_cat Optional. Whether link should be in same category.
  818. * @param string $excluded_categories Optional. Excluded categories IDs.
  819. * @return string
  820. */
  821. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  822. return get_adjacent_post($in_same_cat, $excluded_categories);
  823. }
  824. /**
  825. * Retrieve next post link that is adjacent to current post.
  826. *
  827. * @since 1.5.0
  828. *
  829. * @param bool $in_same_cat Optional. Whether link should be in same category.
  830. * @param string $excluded_categories Optional. Excluded categories IDs.
  831. * @return string
  832. */
  833. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  834. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  835. }
  836. /**
  837. * Retrieve adjacent post link.
  838. *
  839. * Can either be next or previous post link.
  840. *
  841. * @since 2.5.0
  842. *
  843. * @param bool $in_same_cat Optional. Whether link should be in same category.
  844. * @param string $excluded_categories Optional. Excluded categories IDs.
  845. * @param bool $previous Optional. Whether to retrieve previous post.
  846. * @return string
  847. */
  848. function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
  849. global $post, $wpdb;
  850. if ( empty( $post ) )
  851. return null;
  852. $current_post_date = $post->post_date;
  853. $join = '';
  854. $posts_in_ex_cats_sql = '';
  855. if ( $in_same_cat || !empty($excluded_categories) ) {
  856. $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";
  857. if ( $in_same_cat ) {
  858. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  859. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  860. }
  861. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  862. if ( !empty($excluded_categories) ) {
  863. $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
  864. if ( !empty($cat_array) ) {
  865. $excluded_categories = array_diff($excluded_categories, $cat_array);
  866. $posts_in_ex_cats_sql = '';
  867. }
  868. if ( !empty($excluded_categories) ) {
  869. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  870. }
  871. }
  872. }
  873. $adjacent = $previous ? 'previous' : 'next';
  874. $op = $previous ? '<' : '>';
  875. $order = $previous ? 'DESC' : 'ASC';
  876. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  877. $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 );
  878. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  879. $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
  880. $query_key = 'adjacent_post_' . md5($query);
  881. $result = wp_cache_get($query_key, 'counts');
  882. if ( false !== $result )
  883. return $result;
  884. $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  885. if ( null === $result )
  886. $result = '';
  887. wp_cache_set($query_key, $result, 'counts');
  888. return $result;
  889. }
  890. /**
  891. * Get adjacent post relational link.
  892. *
  893. * Can either be next or previous post relational link.
  894. *
  895. * @since 2.8.0
  896. *
  897. * @param string $title Optional. Link title format.
  898. * @param bool $in_same_cat Optional. Whether link should be in same category.
  899. * @param string $excluded_categories Optional. Excluded categories IDs.
  900. * @param bool $previous Optional, default is true. Whether display link to previous post.
  901. * @return string
  902. */
  903. function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
  904. if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
  905. $post = & get_post($GLOBALS['post']->post_parent);
  906. else
  907. $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
  908. if ( empty($post) )
  909. return;
  910. if ( empty($post->post_title) )
  911. $post->post_title = $previous ? __('Previous Post') : __('Next Post');
  912. $date = mysql2date(get_option('date_format'), $post->post_date);
  913. $title = str_replace('%title', $post->post_title, $title);
  914. $title = str_replace('%date', $date, $title);
  915. $title = apply_filters('the_title', $title, $post);
  916. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  917. $link .= esc_attr( $title );
  918. $link .= "' href='" . get_permalink($post) . "' />\n";
  919. $adjacent = $previous ? 'previous' : 'next';
  920. return apply_filters( "{$adjacent}_post_rel_link", $link );
  921. }
  922. /**
  923. * Display relational links for the posts adjacent to the current post.
  924. *
  925. * @since 2.8.0
  926. *
  927. * @param string $title Optional. Link title format.
  928. * @param bool $in_same_cat Optional. Whether link should be in same category.
  929. * @param string $excluded_categories Optional. Excluded categories IDs.
  930. */
  931. function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  932. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  933. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  934. }
  935. /**
  936. * Display relational links for the posts adjacent to the current post for single post pages.
  937. *
  938. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  939. * @since 3.0.0
  940. *
  941. */
  942. function adjacent_posts_rel_link_wp_head() {
  943. if ( !is_singular() || is_attachment() )
  944. return;
  945. adjacent_posts_rel_link();
  946. }
  947. /**
  948. * Display relational link for the next post adjacent to the current post.
  949. *
  950. * @since 2.8.0
  951. *
  952. * @param string $title Optional. Link title format.
  953. * @param bool $in_same_cat Optional. Whether link should be in same category.
  954. * @param string $excluded_categories Optional. Excluded categories IDs.
  955. */
  956. function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  957. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  958. }
  959. /**
  960. * Display relational link for the previous post adjacent to the current post.
  961. *
  962. * @since 2.8.0
  963. *
  964. * @param string $title Optional. Link title format.
  965. * @param bool $in_same_cat Optional. Whether link should be in same category.
  966. * @param string $excluded_categories Optional. Excluded categories IDs.
  967. */
  968. function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  969. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  970. }
  971. /**
  972. * Retrieve boundary post.
  973. *
  974. * Boundary being either the first or last post by publish date within the contraitns specified
  975. * by in same category or excluded categories.
  976. *
  977. * @since 2.8.0
  978. *
  979. * @param bool $in_same_cat Optional. Whether returned post should be in same category.
  980. * @param string $excluded_categories Optional. Excluded categories IDs.
  981. * @param bool $previous Optional. Whether to retrieve first post.
  982. * @return object
  983. */
  984. function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
  985. global $post;
  986. if ( empty($post) || !is_single() || is_attachment() )
  987. return null;
  988. $cat_array = array();
  989. $excluded_categories = array();
  990. if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
  991. if ( !empty($in_same_cat) ) {
  992. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  993. }
  994. if ( !empty($excluded_categories) ) {
  995. $excluded_categories = array_map('intval', explode(',', $excluded_categories));
  996. if ( !empty($cat_array) )
  997. $excluded_categories = array_diff($excluded_categories, $cat_array);
  998. $inverse_cats = array();
  999. foreach ( $excluded_categories as $excluded_category)
  1000. $inverse_cats[] = $excluded_category * -1;
  1001. $excluded_categories = $inverse_cats;
  1002. }
  1003. }
  1004. $categories = implode(',', array_merge($cat_array, $excluded_categories) );
  1005. $order = $start ? 'ASC' : 'DESC';
  1006. return get_posts( array('numberposts' => 1, 'no_found_rows' => true, 'order' => $order, 'orderby' => 'ID', 'category' => $categories) );
  1007. }
  1008. /**
  1009. * Get boundary post relational link.
  1010. *
  1011. * Can either be start or end post relational link.
  1012. *
  1013. * @since 2.8.0
  1014. *
  1015. * @param string $title Optional. Link title format.
  1016. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1017. * @param string $excluded_categories Optional. Excluded categories IDs.
  1018. * @param bool $start Optional, default is true. Whether display link to first post.
  1019. * @return string
  1020. */
  1021. function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
  1022. $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
  1023. // If there is no post stop.
  1024. if ( empty($posts) )
  1025. return;
  1026. // Even though we limited get_posts to return only 1 item it still returns an array of objects.
  1027. $post = $posts[0];
  1028. if ( empty($post->post_title) )
  1029. $post->post_title = $start ? __('First Post') : __('Last Post');
  1030. $date = mysql2date(get_option('date_format'), $post->post_date);
  1031. $title = str_replace('%title', $post->post_title, $title);
  1032. $title = str_replace('%date', $date, $title);
  1033. $title = apply_filters('the_title', $title, $post);
  1034. $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
  1035. $link .= esc_attr($title);
  1036. $link .= "' href='" . get_permalink($post) . "' />\n";
  1037. $boundary = $start ? 'start' : 'end';
  1038. return apply_filters( "{$boundary}_post_rel_link", $link );
  1039. }
  1040. /**
  1041. * Display relational link for the first post.
  1042. *
  1043. * @since 2.8.0
  1044. *
  1045. * @param string $title Optional. Link title format.
  1046. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1047. * @param string $excluded_categories Optional. Excluded categories IDs.
  1048. */
  1049. function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1050. echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
  1051. }
  1052. /**
  1053. * Get site index relational link.
  1054. *
  1055. * @since 2.8.0
  1056. *
  1057. * @return string
  1058. */
  1059. function get_index_rel_link() {
  1060. $link = "<link rel='index' title='" . esc_attr( get_bloginfo( 'name', 'display' ) ) . "' href='" . esc_url( user_trailingslashit( get_bloginfo( 'url', 'display' ) ) ) . "' />\n";
  1061. return apply_filters( "index_rel_link", $link );
  1062. }
  1063. /**
  1064. * Display relational link for the site index.
  1065. *
  1066. * @since 2.8.0
  1067. */
  1068. function index_rel_link() {
  1069. echo get_index_rel_link();
  1070. }
  1071. /**
  1072. * Get parent post relational link.
  1073. *
  1074. * @since 2.8.0
  1075. *
  1076. * @param string $title Optional. Link title format.
  1077. * @return string
  1078. */
  1079. function get_parent_post_rel_link($title = '%title') {
  1080. if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
  1081. $post = & get_post($GLOBALS['post']->post_parent);
  1082. if ( empty($post) )
  1083. return;
  1084. $date = mysql2date(get_option('date_format'), $post->post_date);
  1085. $title = str_replace('%title', $post->post_title, $title);
  1086. $title = str_replace('%date', $date, $title);
  1087. $title = apply_filters('the_title', $title, $post);
  1088. $link = "<link rel='up' title='";
  1089. $link .= esc_attr( $title );
  1090. $link .= "' href='" . get_permalink($post) . "' />\n";
  1091. return apply_filters( "parent_post_rel_link", $link );
  1092. }
  1093. /**
  1094. * Display relational link for parent item
  1095. *
  1096. * @since 2.8.0
  1097. */
  1098. function parent_post_rel_link($title = '%title') {
  1099. echo get_parent_post_rel_link($title);
  1100. }
  1101. /**
  1102. * Display previous post link that is adjacent to the current post.
  1103. *
  1104. * @since 1.5.0
  1105. *
  1106. * @param string $format Optional. Link anchor format.
  1107. * @param string $link Optional. Link permalink format.
  1108. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1109. * @param string $excluded_categories Optional. Excluded categories IDs.
  1110. */
  1111. function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1112. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
  1113. }
  1114. /**
  1115. * Display next 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 next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1125. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
  1126. }
  1127. /**
  1128. * Display adjacent post link.
  1129. *
  1130. * Can be either next post link or previous.
  1131. *
  1132. * @since 2.5.0
  1133. *
  1134. * @param string $format Link anchor format.
  1135. * @param string $link Link permalink format.
  1136. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1137. * @param string $excluded_categories Optional. Excluded categories IDs.
  1138. * @param bool $previous Optional, default is true. Whether display link to previous post.
  1139. */
  1140. function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1141. if ( $previous && is_attachment() )
  1142. $post = & get_post($GLOBALS['post']->post_parent);
  1143. else
  1144. $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
  1145. if ( !$post )
  1146. return;
  1147. $title = $post->post_title;
  1148. if ( empty($post->post_title) )
  1149. $title = $previous ? __('Previous Post') : __('Next Post');
  1150. $title = apply_filters('the_title', $title, $post);
  1151. $date = mysql2date(get_option('date_format'), $post->post_date);
  1152. $rel = $previous ? 'prev' : 'next';
  1153. $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
  1154. $link = str_replace('%title', $title, $link);
  1155. $link = str_replace('%date', $date, $link);
  1156. $link = $string . $link . '</a>';
  1157. $format = str_replace('%link', $link, $format);
  1158. $adjacent = $previous ? 'previous' : 'next';
  1159. echo apply_filters( "{$adjacent}_post_link", $format, $link );
  1160. }
  1161. /**
  1162. * Retrieve get links for page numbers.
  1163. *
  1164. * @since 1.5.0
  1165. *
  1166. * @param int $pagenum Optional. Page ID.
  1167. * @return string
  1168. */
  1169. function get_pagenum_link($pagenum = 1) {
  1170. global $wp_rewrite;
  1171. $pagenum = (int) $pagenum;
  1172. $request = remove_query_arg( 'paged' );
  1173. $home_root = parse_url(home_url());
  1174. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1175. $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  1176. $request = preg_replace('|^'. $home_root . '|', '', $request);
  1177. $request = preg_replace('|^/+|', '', $request);
  1178. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1179. $base = trailingslashit( get_bloginfo( 'url' ) );
  1180. if ( $pagenum > 1 ) {
  1181. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1182. } else {
  1183. $result = $base . $request;
  1184. }
  1185. } else {
  1186. $qs_regex = '|\?.*?$|';
  1187. preg_match( $qs_regex, $request, $qs_match );
  1188. if ( !empty( $qs_match[0] ) ) {
  1189. $query_string = $qs_match[0];
  1190. $request = preg_replace( $qs_regex, '', $request );
  1191. } else {
  1192. $query_string = '';
  1193. }
  1194. $request = preg_replace( '|page/\d+/?$|', '', $request);
  1195. $request = preg_replace( '|^index\.php|', '', $request);
  1196. $request = ltrim($request, '/');
  1197. $base = trailingslashit( get_bloginfo( 'url' ) );
  1198. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1199. $base .= 'index.php/';
  1200. if ( $pagenum > 1 ) {
  1201. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
  1202. }
  1203. $result = $base . $request . $query_string;
  1204. }
  1205. $result = apply_filters('get_pagenum_link', $result);
  1206. return $result;
  1207. }
  1208. /**
  1209. * Retrieve next posts pages link.
  1210. *
  1211. * Backported from 2.1.3 to 2.0.10.
  1212. *
  1213. * @since 2.0.10
  1214. *
  1215. * @param int $max_page Optional. Max pages.
  1216. * @return string
  1217. */
  1218. function get_next_posts_page_link($max_page = 0) {
  1219. global $paged;
  1220. if ( !is_single() ) {
  1221. if ( !$paged )
  1222. $paged = 1;
  1223. $nextpage = intval($paged) + 1;
  1224. if ( !$max_page || $max_page >= $nextpage )
  1225. return get_pagenum_link($nextpage);
  1226. }
  1227. }
  1228. /**
  1229. * Display or return the next posts pages link.
  1230. *
  1231. * @since 0.71
  1232. *
  1233. * @param int $max_page Optional. Max pages.
  1234. * @param boolean $echo Optional. Echo or return;
  1235. */
  1236. function next_posts( $max_page = 0, $echo = true ) {
  1237. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1238. if ( $echo )
  1239. echo $output;
  1240. else
  1241. return $output;
  1242. }
  1243. /**
  1244. * Return the next posts pages link.
  1245. *
  1246. * @since 2.7.0
  1247. *
  1248. * @param string $label Content for link text.
  1249. * @param int $max_page Optional. Max pages.
  1250. * @return string|null
  1251. */
  1252. function get_next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  1253. global $paged, $wp_query;
  1254. if ( !$max_page )
  1255. $max_page = $wp_query->max_num_pages;
  1256. if ( !$paged )
  1257. $paged = 1;
  1258. $nextpage = intval($paged) + 1;
  1259. if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
  1260. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1261. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1262. }
  1263. }
  1264. /**
  1265. * Display the next posts pages link.
  1266. *
  1267. * @since 0.71
  1268. * @uses get_next_posts_link()
  1269. *
  1270. * @param string $label Content for link text.
  1271. * @param int $max_page Optional. Max pages.
  1272. */
  1273. function next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  1274. echo get_next_posts_link( $label, $max_page );
  1275. }
  1276. /**
  1277. * Retrieve previous post pages link.
  1278. *
  1279. * Will only return string, if not on a single page or post.
  1280. *
  1281. * Backported to 2.0.10 from 2.1.3.
  1282. *
  1283. * @since 2.0.10
  1284. *
  1285. * @return string|null
  1286. */
  1287. function get_previous_posts_page_link() {
  1288. global $paged;
  1289. if ( !is_single() ) {
  1290. $nextpage = intval($paged) - 1;
  1291. if ( $nextpage < 1 )
  1292. $nextpage = 1;
  1293. return get_pagenum_link($nextpage);
  1294. }
  1295. }
  1296. /**
  1297. * Display or return the previous posts pages link.
  1298. *
  1299. * @since 0.71
  1300. *
  1301. * @param boolean $echo Optional. Echo or return;
  1302. */
  1303. function previous_posts( $echo = true ) {
  1304. $output = esc_url( get_previous_posts_page_link() );
  1305. if ( $echo )
  1306. echo $output;
  1307. else
  1308. return $output;
  1309. }
  1310. /**
  1311. * Return the previous posts pages link.
  1312. *
  1313. * @since 2.7.0
  1314. *
  1315. * @param string $label Optional. Previous page link text.
  1316. * @return string|null
  1317. */
  1318. function get_previous_posts_link( $label = '&laquo; Previous Page' ) {
  1319. global $paged;
  1320. if ( !is_single() && $paged > 1 ) {
  1321. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1322. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a>';
  1323. }
  1324. }
  1325. /**
  1326. * Display the previous posts page link.
  1327. *
  1328. * @since 0.71
  1329. * @uses get_previous_posts_link()
  1330. *
  1331. * @param string $label Optional. Previous page link text.
  1332. */
  1333. function previous_posts_link( $label = '&laquo; Previous Page' ) {
  1334. echo get_previous_posts_link( $label );
  1335. }
  1336. /**
  1337. * Return post pages link navigation for previous and next pages.
  1338. *
  1339. * @since 2.8
  1340. *
  1341. * @param string|array $args Optional args.
  1342. * @return string The posts link navigation.
  1343. */
  1344. function get_posts_nav_link( $args = array() ) {
  1345. global $wp_query;
  1346. $return = '';
  1347. if ( !is_singular() ) {
  1348. $defaults = array(
  1349. 'sep' => ' &#8212; ',
  1350. 'prelabel' => __('&laquo; Previous Page'),
  1351. 'nxtlabel' => __('Next Page &raquo;'),
  1352. );
  1353. $args = wp_parse_args( $args, $defaults );
  1354. $max_num_pages = $wp_query->max_num_pages;
  1355. $paged = get_query_var('paged');
  1356. //only have sep if there's both prev and next results
  1357. if ($paged < 2 || $paged >= $max_num_pages) {
  1358. $args['sep'] = '';
  1359. }
  1360. if ( $max_num_pages > 1 ) {
  1361. $return = get_previous_posts_link($args['prelabel']);
  1362. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
  1363. $return .= get_next_posts_link($args['nxtlabel']);
  1364. }
  1365. }
  1366. return $return;
  1367. }
  1368. /**
  1369. * Display post pages link navigation for previous and next pages.
  1370. *
  1371. * @since 0.71
  1372. *
  1373. * @param string $sep Optional. Separator for posts navigation links.
  1374. * @param string $prelabel Optional. Label for previous pages.
  1375. * @param string $nxtlabel Optional Label for next pages.
  1376. */
  1377. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1378. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1379. echo get_posts_nav_link($args);
  1380. }
  1381. /**
  1382. * Retrieve page numbers links.
  1383. *
  1384. * @since 2.7.0
  1385. *
  1386. * @param int $pagenum Optional. Page number.
  1387. * @return string
  1388. */
  1389. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1390. global $post, $wp_rewrite;
  1391. $pagenum = (int) $pagenum;
  1392. $result = get_permalink( $post->ID );
  1393. if ( 'newest' == get_option('default_comments_page') ) {
  1394. if ( $pagenum != $max_page ) {
  1395. if ( $wp_rewrite->using_permalinks() )
  1396. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1397. else
  1398. $result = add_query_arg( 'cpage', $pagenum, $result );
  1399. }
  1400. } elseif ( $pagenum > 1 ) {
  1401. if ( $wp_rewrite->using_permalinks() )
  1402. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1403. else
  1404. $result = add_query_arg( 'cpage', $pagenum, $result );
  1405. }
  1406. $result .= '#comments';
  1407. $result = apply_filters('get_comments_pagenum_link', $result);
  1408. return $result;
  1409. }
  1410. /**
  1411. * Return the link to next comments pages.
  1412. *
  1413. * @since 2.7.1
  1414. *
  1415. * @param string $label Optional. Label for link text.
  1416. * @param int $max_page Optional. Max page.
  1417. * @return string|null
  1418. */
  1419. function get_next_comments_link( $label = '', $max_page = 0 ) {
  1420. global $wp_query;
  1421. if ( !is_singular() || !get_option('page_comments') )
  1422. return;
  1423. $page = get_query_var('cpage');
  1424. $nextpage = intval($page) + 1;
  1425. if ( empty($max_page) )
  1426. $max_page = $wp_query->max_num_comment_pages;
  1427. if ( empty($max_page) )
  1428. $max_page = get_comment_pages_count();
  1429. if ( $nextpage > $max_page )
  1430. return;
  1431. if ( empty($label) )
  1432. $label = __('Newer Comments &raquo;');
  1433. 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>';
  1434. }
  1435. /**
  1436. * Display the link to next comments pages.
  1437. *
  1438. * @since 2.7.0
  1439. *
  1440. * @param string $label Optional. Label for link text.
  1441. * @param int $max_page Optional. Max page.
  1442. */
  1443. function next_comments_link( $label = '', $max_page = 0 ) {
  1444. echo get_next_comments_link( $label, $max_page );
  1445. }
  1446. /**
  1447. * Return the previous comments page link.
  1448. *
  1449. * @since 2.7.1
  1450. *
  1451. * @param string $label Optional. Label for comments link text.
  1452. * @return string|null
  1453. */
  1454. function get_previous_comments_link( $label = '' ) {
  1455. if ( !is_singular() || !get_option('page_comments') )
  1456. return;
  1457. $page = get_query_var('cpage');
  1458. if ( intval($page) <= 1 )
  1459. return;
  1460. $prevpage = intval($page) - 1;
  1461. if ( empty($label) )
  1462. $label = __('&laquo; Older Comments');
  1463. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
  1464. }
  1465. /

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