PageRenderTime 28ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://bitbucket.org/nlyn/mr.-peacocks
PHP | 2478 lines | 1289 code | 335 blank | 854 comment | 328 complexity | bd2333678b94a67e8fa2e527e59cfd1e MD5 | raw file
Possible License(s): GPL-2.0

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. else
  734. $struct = $wp_rewrite->root . $struct;
  735. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  736. } else {
  737. $link = home_url( '?post_type=' . $post_type );
  738. }
  739. return apply_filters( 'post_type_archive_link', $link, $post_type );
  740. }
  741. /**
  742. * Retrieve the permalink for a post type archive feed.
  743. *
  744. * @since 3.1.0
  745. *
  746. * @param string $post_type Post type
  747. * @param string $feed Optional. Feed type
  748. * @return string
  749. */
  750. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  751. $default_feed = get_default_feed();
  752. if ( empty( $feed ) )
  753. $feed = $default_feed;
  754. if ( ! $link = get_post_type_archive_link( $post_type ) )
  755. return false;
  756. $post_type_obj = get_post_type_object( $post_type );
  757. if ( $post_type_obj->rewrite['feeds'] && get_option( 'permalink_structure' ) ) {
  758. $link = trailingslashit($link);
  759. $link .= 'feed/';
  760. if ( $feed != $default_feed )
  761. $link .= "$feed/";
  762. } else {
  763. $link = add_query_arg( 'feed', $feed, $link );
  764. }
  765. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  766. }
  767. /**
  768. * Retrieve edit posts link for post.
  769. *
  770. * Can be used within the WordPress loop or outside of it. Can be used with
  771. * pages, posts, attachments, and revisions.
  772. *
  773. * @since 2.3.0
  774. *
  775. * @param int $id Optional. Post ID.
  776. * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
  777. * @return string
  778. */
  779. function get_edit_post_link( $id = 0, $context = 'display' ) {
  780. if ( !$post = &get_post( $id ) )
  781. return;
  782. if ( 'display' == $context )
  783. $action = '&amp;action=edit';
  784. else
  785. $action = '&action=edit';
  786. $post_type_object = get_post_type_object( $post->post_type );
  787. if ( !$post_type_object )
  788. return;
  789. if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) )
  790. return;
  791. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  792. }
  793. /**
  794. * Display edit post link for post.
  795. *
  796. * @since 1.0.0
  797. *
  798. * @param string $link Optional. Anchor text.
  799. * @param string $before Optional. Display before edit link.
  800. * @param string $after Optional. Display after edit link.
  801. * @param int $id Optional. Post ID.
  802. */
  803. function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  804. if ( !$post = &get_post( $id ) )
  805. return;
  806. if ( !$url = get_edit_post_link( $post->ID ) )
  807. return;
  808. if ( null === $link )
  809. $link = __('Edit This');
  810. $post_type_obj = get_post_type_object( $post->post_type );
  811. $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( $post_type_obj->labels->edit_item ) . '">' . $link . '</a>';
  812. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  813. }
  814. /**
  815. * Retrieve delete posts link for post.
  816. *
  817. * Can be used within the WordPress loop or outside of it, with any post type.
  818. *
  819. * @since 2.9.0
  820. *
  821. * @param int $id Optional. Post ID.
  822. * @param string $deprecated Not used.
  823. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  824. * @return string
  825. */
  826. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  827. if ( ! empty( $deprecated ) )
  828. _deprecated_argument( __FUNCTION__, '3.0' );
  829. if ( !$post = &get_post( $id ) )
  830. return;
  831. $post_type_object = get_post_type_object( $post->post_type );
  832. if ( !$post_type_object )
  833. return;
  834. if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
  835. return;
  836. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  837. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  838. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-{$post->post_type}_{$post->ID}" ), $post->ID, $force_delete );
  839. }
  840. /**
  841. * Retrieve edit comment link.
  842. *
  843. * @since 2.3.0
  844. *
  845. * @param int $comment_id Optional. Comment ID.
  846. * @return string
  847. */
  848. function get_edit_comment_link( $comment_id = 0 ) {
  849. $comment = &get_comment( $comment_id );
  850. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  851. return;
  852. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  853. return apply_filters( 'get_edit_comment_link', $location );
  854. }
  855. /**
  856. * Display or retrieve edit comment link with formatting.
  857. *
  858. * @since 1.0.0
  859. *
  860. * @param string $link Optional. Anchor text.
  861. * @param string $before Optional. Display before edit link.
  862. * @param string $after Optional. Display after edit link.
  863. * @return string|null HTML content, if $echo is set to false.
  864. */
  865. function edit_comment_link( $link = null, $before = '', $after = '' ) {
  866. global $comment;
  867. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  868. return;
  869. if ( null === $link )
  870. $link = __('Edit This');
  871. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . esc_attr__( 'Edit comment' ) . '">' . $link . '</a>';
  872. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  873. }
  874. /**
  875. * Display edit bookmark (literally a URL external to blog) link.
  876. *
  877. * @since 2.7.0
  878. *
  879. * @param int $link Optional. Bookmark ID.
  880. * @return string
  881. */
  882. function get_edit_bookmark_link( $link = 0 ) {
  883. $link = get_bookmark( $link );
  884. if ( !current_user_can('manage_links') )
  885. return;
  886. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  887. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  888. }
  889. /**
  890. * Display edit bookmark (literally a URL external to blog) link anchor content.
  891. *
  892. * @since 2.7.0
  893. *
  894. * @param string $link Optional. Anchor text.
  895. * @param string $before Optional. Display before edit link.
  896. * @param string $after Optional. Display after edit link.
  897. * @param int $bookmark Optional. Bookmark ID.
  898. */
  899. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  900. $bookmark = get_bookmark($bookmark);
  901. if ( !current_user_can('manage_links') )
  902. return;
  903. if ( empty($link) )
  904. $link = __('Edit This');
  905. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '" title="' . esc_attr__( 'Edit Link' ) . '">' . $link . '</a>';
  906. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  907. }
  908. // Navigation links
  909. /**
  910. * Retrieve previous post that is adjacent to current post.
  911. *
  912. * @since 1.5.0
  913. *
  914. * @param bool $in_same_cat Optional. Whether post should be in same category.
  915. * @param string $excluded_categories Optional. Excluded categories IDs.
  916. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  917. */
  918. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  919. return get_adjacent_post($in_same_cat, $excluded_categories);
  920. }
  921. /**
  922. * Retrieve next post that is adjacent to current post.
  923. *
  924. * @since 1.5.0
  925. *
  926. * @param bool $in_same_cat Optional. Whether post should be in same category.
  927. * @param string $excluded_categories Optional. Excluded categories IDs.
  928. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  929. */
  930. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  931. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  932. }
  933. /**
  934. * Retrieve adjacent post.
  935. *
  936. * Can either be next or previous post.
  937. *
  938. * @since 2.5.0
  939. *
  940. * @param bool $in_same_cat Optional. Whether post should be in same category.
  941. * @param string $excluded_categories Optional. Excluded categories IDs.
  942. * @param bool $previous Optional. Whether to retrieve previous post.
  943. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  944. */
  945. function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
  946. global $post, $wpdb;
  947. if ( empty( $post ) )
  948. return null;
  949. $current_post_date = $post->post_date;
  950. $join = '';
  951. $posts_in_ex_cats_sql = '';
  952. if ( $in_same_cat || !empty($excluded_categories) ) {
  953. $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";
  954. if ( $in_same_cat ) {
  955. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  956. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  957. }
  958. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  959. if ( !empty($excluded_categories) ) {
  960. $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
  961. if ( !empty($cat_array) ) {
  962. $excluded_categories = array_diff($excluded_categories, $cat_array);
  963. $posts_in_ex_cats_sql = '';
  964. }
  965. if ( !empty($excluded_categories) ) {
  966. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  967. }
  968. }
  969. }
  970. $adjacent = $previous ? 'previous' : 'next';
  971. $op = $previous ? '<' : '>';
  972. $order = $previous ? 'DESC' : 'ASC';
  973. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  974. $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 );
  975. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  976. $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
  977. $query_key = 'adjacent_post_' . md5($query);
  978. $result = wp_cache_get($query_key, 'counts');
  979. if ( false !== $result )
  980. return $result;
  981. $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  982. if ( null === $result )
  983. $result = '';
  984. wp_cache_set($query_key, $result, 'counts');
  985. return $result;
  986. }
  987. /**
  988. * Get adjacent post relational link.
  989. *
  990. * Can either be next or previous post relational link.
  991. *
  992. * @since 2.8.0
  993. *
  994. * @param string $title Optional. Link title format.
  995. * @param bool $in_same_cat Optional. Whether link should be in same category.
  996. * @param string $excluded_categories Optional. Excluded categories IDs.
  997. * @param bool $previous Optional, default is true. Whether display link to previous post.
  998. * @return string
  999. */
  1000. function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1001. if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
  1002. $post = & get_post($GLOBALS['post']->post_parent);
  1003. else
  1004. $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
  1005. if ( empty($post) )
  1006. return;
  1007. if ( empty($post->post_title) )
  1008. $post->post_title = $previous ? __('Previous Post') : __('Next Post');
  1009. $date = mysql2date(get_option('date_format'), $post->post_date);
  1010. $title = str_replace('%title', $post->post_title, $title);
  1011. $title = str_replace('%date', $date, $title);
  1012. $title = apply_filters('the_title', $title, $post->ID);
  1013. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1014. $link .= esc_attr( $title );
  1015. $link .= "' href='" . get_permalink($post) . "' />\n";
  1016. $adjacent = $previous ? 'previous' : 'next';
  1017. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1018. }
  1019. /**
  1020. * Display relational links for the posts adjacent to the current post.
  1021. *
  1022. * @since 2.8.0
  1023. *
  1024. * @param string $title Optional. Link title format.
  1025. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1026. * @param string $excluded_categories Optional. Excluded categories IDs.
  1027. */
  1028. function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1029. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1030. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1031. }
  1032. /**
  1033. * Display relational links for the posts adjacent to the current post for single post pages.
  1034. *
  1035. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1036. * @since 3.0.0
  1037. *
  1038. */
  1039. function adjacent_posts_rel_link_wp_head() {
  1040. if ( !is_singular() || is_attachment() )
  1041. return;
  1042. adjacent_posts_rel_link();
  1043. }
  1044. /**
  1045. * Display relational link for the next post adjacent to the current post.
  1046. *
  1047. * @since 2.8.0
  1048. *
  1049. * @param string $title Optional. Link title format.
  1050. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1051. * @param string $excluded_categories Optional. Excluded categories IDs.
  1052. */
  1053. function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1054. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1055. }
  1056. /**
  1057. * Display relational link for the previous post adjacent to the current post.
  1058. *
  1059. * @since 2.8.0
  1060. *
  1061. * @param string $title Optional. Link title format.
  1062. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1063. * @param string $excluded_categories Optional. Excluded categories IDs.
  1064. */
  1065. function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1066. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1067. }
  1068. /**
  1069. * Retrieve boundary post.
  1070. *
  1071. * Boundary being either the first or last post by publish date within the constraints specified
  1072. * by in same category or excluded categories.
  1073. *
  1074. * @since 2.8.0
  1075. *
  1076. * @param bool $in_same_cat Optional. Whether returned post should be in same category.
  1077. * @param string $excluded_categories Optional. Excluded categories IDs.
  1078. * @param bool $start Optional. Whether to retrieve first or last post.
  1079. * @return object
  1080. */
  1081. function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
  1082. global $post;
  1083. if ( empty($post) || !is_single() || is_attachment() )
  1084. return null;
  1085. $cat_array = array();
  1086. $excluded_categories = array();
  1087. if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
  1088. if ( !empty($in_same_cat) ) {
  1089. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  1090. }
  1091. if ( !empty($excluded_categories) ) {
  1092. $excluded_categories = array_map('intval', explode(',', $excluded_categories));
  1093. if ( !empty($cat_array) )
  1094. $excluded_categories = array_diff($excluded_categories, $cat_array);
  1095. $inverse_cats = array();
  1096. foreach ( $excluded_categories as $excluded_category)
  1097. $inverse_cats[] = $excluded_category * -1;
  1098. $excluded_categories = $inverse_cats;
  1099. }
  1100. }
  1101. $categories = implode(',', array_merge($cat_array, $excluded_categories) );
  1102. $order = $start ? 'ASC' : 'DESC';
  1103. return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
  1104. }
  1105. /**
  1106. * Get boundary post relational link.
  1107. *
  1108. * Can either be start or end post relational link.
  1109. *
  1110. * @since 2.8.0
  1111. *
  1112. * @param string $title Optional. Link title format.
  1113. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1114. * @param string $excluded_categories Optional. Excluded categories IDs.
  1115. * @param bool $start Optional, default is true. Whether display link to first or last post.
  1116. * @return string
  1117. */
  1118. function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
  1119. $posts = get_boundary_post($in_same_cat, $excluded_categories, $start);
  1120. // If there is no post stop.
  1121. if ( empty($posts) )
  1122. return;
  1123. // Even though we limited get_posts to return only 1 item it still returns an array of objects.
  1124. $post = $posts[0];
  1125. if ( empty($post->post_title) )
  1126. $post->post_title = $start ? __('First Post') : __('Last Post');
  1127. $date = mysql2date(get_option('date_format'), $post->post_date);
  1128. $title = str_replace('%title', $post->post_title, $title);
  1129. $title = str_replace('%date', $date, $title);
  1130. $title = apply_filters('the_title', $title, $post->ID);
  1131. $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
  1132. $link .= esc_attr($title);
  1133. $link .= "' href='" . get_permalink($post) . "' />\n";
  1134. $boundary = $start ? 'start' : 'end';
  1135. return apply_filters( "{$boundary}_post_rel_link", $link );
  1136. }
  1137. /**
  1138. * Display relational link for the first post.
  1139. *
  1140. * @since 2.8.0
  1141. *
  1142. * @param string $title Optional. Link title format.
  1143. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1144. * @param string $excluded_categories Optional. Excluded categories IDs.
  1145. */
  1146. function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1147. echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
  1148. }
  1149. /**
  1150. * Get site index relational link.
  1151. *
  1152. * @since 2.8.0
  1153. *
  1154. * @return string
  1155. */
  1156. function get_index_rel_link() {
  1157. $link = "<link rel='index' title='" . esc_attr( get_bloginfo( 'name', 'display' ) ) . "' href='" . esc_url( user_trailingslashit( get_bloginfo( 'url', 'display' ) ) ) . "' />\n";
  1158. return apply_filters( "index_rel_link", $link );
  1159. }
  1160. /**
  1161. * Display relational link for the site index.
  1162. *
  1163. * @since 2.8.0
  1164. */
  1165. function index_rel_link() {
  1166. echo get_index_rel_link();
  1167. }
  1168. /**
  1169. * Get parent post relational link.
  1170. *
  1171. * @since 2.8.0
  1172. *
  1173. * @param string $title Optional. Link title format.
  1174. * @return string
  1175. */
  1176. function get_parent_post_rel_link($title = '%title') {
  1177. if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
  1178. $post = & get_post($GLOBALS['post']->post_parent);
  1179. if ( empty($post) )
  1180. return;
  1181. $date = mysql2date(get_option('date_format'), $post->post_date);
  1182. $title = str_replace('%title', $post->post_title, $title);
  1183. $title = str_replace('%date', $date, $title);
  1184. $title = apply_filters('the_title', $title, $post->ID);
  1185. $link = "<link rel='up' title='";
  1186. $link .= esc_attr( $title );
  1187. $link .= "' href='" . get_permalink($post) . "' />\n";
  1188. return apply_filters( "parent_post_rel_link", $link );
  1189. }
  1190. /**
  1191. * Display relational link for parent item
  1192. *
  1193. * @since 2.8.0
  1194. */
  1195. function parent_post_rel_link($title = '%title') {
  1196. echo get_parent_post_rel_link($title);
  1197. }
  1198. /**
  1199. * Display previous post link that is adjacent to the current post.
  1200. *
  1201. * @since 1.5.0
  1202. *
  1203. * @param string $format Optional. Link anchor format.
  1204. * @param string $link Optional. Link permalink format.
  1205. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1206. * @param string $excluded_categories Optional. Excluded categories IDs.
  1207. */
  1208. function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1209. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
  1210. }
  1211. /**
  1212. * Display next post link that is adjacent to the current post.
  1213. *
  1214. * @since 1.5.0
  1215. *
  1216. * @param string $format Optional. Link anchor format.
  1217. * @param string $link Optional. Link permalink format.
  1218. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1219. * @param string $excluded_categories Optional. Excluded categories IDs.
  1220. */
  1221. function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1222. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
  1223. }
  1224. /**
  1225. * Display adjacent post link.
  1226. *
  1227. * Can be either next post link or previous.
  1228. *
  1229. * @since 2.5.0
  1230. *
  1231. * @param string $format Link anchor format.
  1232. * @param string $link Link permalink format.
  1233. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1234. * @param string $excluded_categories Optional. Excluded categories IDs.
  1235. * @param bool $previous Optional, default is true. Whether display link to previous post.
  1236. */
  1237. function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1238. if ( $previous && is_attachment() )
  1239. $post = & get_post($GLOBALS['post']->post_parent);
  1240. else
  1241. $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
  1242. if ( !$post )
  1243. return;
  1244. $title = $post->post_title;
  1245. if ( empty($post->post_title) )
  1246. $title = $previous ? __('Previous Post') : __('Next Post');
  1247. $title = apply_filters('the_title', $title, $post->ID);
  1248. $date = mysql2date(get_option('date_format'), $post->post_date);
  1249. $rel = $previous ? 'prev' : 'next';
  1250. $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
  1251. $link = str_replace('%title', $title, $link);
  1252. $link = str_replace('%date', $date, $link);
  1253. $link = $string . $link . '</a>';
  1254. $format = str_replace('%link', $link, $format);
  1255. $adjacent = $previous ? 'previous' : 'next';
  1256. echo apply_filters( "{$adjacent}_post_link", $format, $link );
  1257. }
  1258. /**
  1259. * Retrieve get links for page numbers.
  1260. *
  1261. * @since 1.5.0
  1262. *
  1263. * @param int $pagenum Optional. Page ID.
  1264. * @return string
  1265. */
  1266. function get_pagenum_link($pagenum = 1) {
  1267. global $wp_rewrite;
  1268. $pagenum = (int) $pagenum;
  1269. $request = remove_query_arg( 'paged' );
  1270. $home_root = parse_url(home_url());
  1271. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1272. $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  1273. $request = preg_replace('|^'. $home_root . '|', '', $request);
  1274. $request = preg_replace('|^/+|', '', $request);
  1275. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1276. $base = trailingslashit( get_bloginfo( 'url' ) );
  1277. if ( $pagenum > 1 ) {
  1278. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1279. } else {
  1280. $result = $base . $request;
  1281. }
  1282. } else {
  1283. $qs_regex = '|\?.*?$|';
  1284. preg_match( $qs_regex, $request, $qs_match );
  1285. if ( !empty( $qs_match[0] ) ) {
  1286. $query_string = $qs_match[0];
  1287. $request = preg_replace( $qs_regex, '', $request );
  1288. } else {
  1289. $query_string = '';
  1290. }
  1291. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1292. $request = preg_replace( '|^index\.php|', '', $request);
  1293. $request = ltrim($request, '/');
  1294. $base = trailingslashit( get_bloginfo( 'url' ) );
  1295. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1296. $base .= 'index.php/';
  1297. if ( $pagenum > 1 ) {
  1298. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1299. }
  1300. $result = $base . $request . $query_string;
  1301. }
  1302. $result = apply_filters('get_pagenum_link', $result);
  1303. return $result;
  1304. }
  1305. /**
  1306. * Retrieve next posts pages link.
  1307. *
  1308. * Backported from 2.1.3 to 2.0.10.
  1309. *
  1310. * @since 2.0.10
  1311. *
  1312. * @param int $max_page Optional. Max pages.
  1313. * @return string
  1314. */
  1315. function get_next_posts_page_link($max_page = 0) {
  1316. global $paged;
  1317. if ( !is_single() ) {
  1318. if ( !$paged )
  1319. $paged = 1;
  1320. $nextpage = intval($paged) + 1;
  1321. if ( !$max_page || $max_page >= $nextpage )
  1322. return get_pagenum_link($nextpage);
  1323. }
  1324. }
  1325. /**
  1326. * Display or return the next posts pages link.
  1327. *
  1328. * @since 0.71
  1329. *
  1330. * @param int $max_page Optional. Max pages.
  1331. * @param boolean $echo Optional. Echo or return;
  1332. */
  1333. function next_posts( $max_page = 0, $echo = true ) {
  1334. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1335. if ( $echo )
  1336. echo $output;
  1337. else
  1338. return $output;
  1339. }
  1340. /**
  1341. * Return the next posts pages link.
  1342. *
  1343. * @since 2.7.0
  1344. *
  1345. * @param string $label Content for link text.
  1346. * @param int $max_page Optional. Max pages.
  1347. * @return string|null
  1348. */
  1349. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1350. global $paged, $wp_query;
  1351. if ( !$max_page )
  1352. $max_page = $wp_query->max_num_pages;
  1353. if ( !$paged )
  1354. $paged = 1;
  1355. $nextpage = intval($paged) + 1;
  1356. if ( null === $label )
  1357. $label = __( 'Next Page &raquo;' );
  1358. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1359. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1360. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1361. }
  1362. }
  1363. /**
  1364. * Display the next posts pages link.
  1365. *
  1366. * @since 0.71
  1367. * @uses get_next_posts_link()
  1368. *
  1369. * @param string $label Content for link text.
  1370. * @param int $max_page Optional. Max pages.
  1371. */
  1372. function next_posts_link( $label = null, $max_page = 0 ) {
  1373. echo get_next_posts_link( $label, $max_page );
  1374. }
  1375. /**
  1376. * Retrieve previous post pages link.
  1377. *
  1378. * Will only return string, if not on a single page or post.
  1379. *
  1380. * Backported to 2.0.10 from 2.1.3.
  1381. *
  1382. * @since 2.0.10
  1383. *
  1384. * @return string|null
  1385. */
  1386. function get_previous_posts_page_link() {
  1387. global $paged;
  1388. if ( !is_single() ) {
  1389. $nextpage = intval($paged) - 1;
  1390. if ( $nextpage < 1 )
  1391. $nextpage = 1;
  1392. return get_pagenum_link($nextpage);
  1393. }
  1394. }
  1395. /**
  1396. * Display or return the previous posts pages link.
  1397. *
  1398. * @since 0.71
  1399. *
  1400. * @param boolean $echo Optional. Echo or return;
  1401. */
  1402. function previous_posts( $echo = true ) {
  1403. $output = esc_url( get_previous_posts_page_link() );
  1404. if ( $echo )
  1405. echo $output;
  1406. else
  1407. return $output;
  1408. }
  1409. /**
  1410. * Return the previous posts pages link.
  1411. *
  1412. * @since 2.7.0
  1413. *
  1414. * @param string $label Optional. Previous page link text.
  1415. * @return string|null
  1416. */
  1417. function get_previous_posts_link( $label = null ) {
  1418. global $paged;
  1419. if ( null === $label )
  1420. $label = __( '&laquo; Previous Page' );
  1421. if ( !is_single() && $paged > 1 ) {
  1422. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1423. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a>';
  1424. }
  1425. }
  1426. /**
  1427. * Display the previous posts page link.
  1428. *
  1429. * @since 0.71
  1430. * @uses get_previous_posts_link()
  1431. *
  1432. * @param string $label Optional. Previous page link text.
  1433. */
  1434. function previous_posts_link( $label = null ) {
  1435. echo get_previous_posts_link( $label );
  1436. }
  1437. /**
  1438. * Return post pages link navigation for previous and next pages.
  1439. *
  1440. * @since 2.8
  1441. *
  1442. * @param string|array $args Optional args.
  1443. * @return string The posts link navigation.
  1444. */
  1445. function get_posts_nav_link( $args = array() ) {
  1446. global $wp_query;
  1447. $return = '';
  1448. if ( !is_singular() ) {
  1449. $defaults = array(
  1450. 'sep' => ' &#8212; ',
  1451. 'prelabel' => __('&laquo; Previous Page'),
  1452. 'nxtlabel' => __('Next Page &raquo;'),
  1453. );
  1454. $args = wp_parse_args( $args, $defaults );
  1455. $ma

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