PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

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

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