PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://bitbucket.org/stephenharris/stephenharris
PHP | 4146 lines | 3015 code | 220 blank | 911 comment | 221 complexity | 592aba670ddc2508b4e99c6693dbc349 MD5 | raw file

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

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

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