PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://github.com/markjaquith/WordPress
PHP | 4615 lines | 3411 code | 234 blank | 970 comment | 225 complexity | 1f218a5c5c7a1e800abafe4eb437c1ba MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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