PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

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

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