PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

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

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