PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/link-template.php

http://github.com/markjaquith/WordPress
PHP | 4473 lines | 3318 code | 224 blank | 931 comment | 230 complexity | 317a745f2d5a5a23fe368a709d0a4135 MD5 | raw file
Possible License(s): 0BSD
  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. * Retrieves the full permalink for the current post or post ID.
  89. *
  90. * This function is an alias for get_permalink().
  91. *
  92. * @since 3.9.0
  93. *
  94. * @see get_permalink()
  95. *
  96. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  97. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  98. *
  99. * @return string|false The permalink URL or false if post does not exist.
  100. */
  101. function get_the_permalink( $post = 0, $leavename = false ) {
  102. return get_permalink( $post, $leavename );
  103. }
  104. /**
  105. * Retrieves the full permalink for the current post or post ID.
  106. *
  107. * @since 1.0.0
  108. *
  109. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  110. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  111. * @return string|false The permalink URL or false if post does not exist.
  112. */
  113. function get_permalink( $post = 0, $leavename = false ) {
  114. $rewritecode = array(
  115. '%year%',
  116. '%monthnum%',
  117. '%day%',
  118. '%hour%',
  119. '%minute%',
  120. '%second%',
  121. $leavename ? '' : '%postname%',
  122. '%post_id%',
  123. '%category%',
  124. '%author%',
  125. $leavename ? '' : '%pagename%',
  126. );
  127. if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
  128. $sample = true;
  129. } else {
  130. $post = get_post( $post );
  131. $sample = false;
  132. }
  133. if ( empty( $post->ID ) ) {
  134. return false;
  135. }
  136. if ( 'page' === $post->post_type ) {
  137. return get_page_link( $post, $leavename, $sample );
  138. } elseif ( 'attachment' === $post->post_type ) {
  139. return get_attachment_link( $post, $leavename );
  140. } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
  141. return get_post_permalink( $post, $leavename, $sample );
  142. }
  143. $permalink = get_option( 'permalink_structure' );
  144. /**
  145. * Filters the permalink structure for a post before token replacement occurs.
  146. *
  147. * Only applies to posts with post_type of 'post'.
  148. *
  149. * @since 3.0.0
  150. *
  151. * @param string $permalink The site's permalink structure.
  152. * @param WP_Post $post The post in question.
  153. * @param bool $leavename Whether to keep the post name.
  154. */
  155. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
  156. if ( '' != $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ), true ) ) {
  157. $category = '';
  158. if ( strpos( $permalink, '%category%' ) !== false ) {
  159. $cats = get_the_category( $post->ID );
  160. if ( $cats ) {
  161. $cats = wp_list_sort(
  162. $cats,
  163. array(
  164. 'term_id' => 'ASC',
  165. )
  166. );
  167. /**
  168. * Filters the category that gets used in the %category% permalink token.
  169. *
  170. * @since 3.5.0
  171. *
  172. * @param WP_Term $cat The category to use in the permalink.
  173. * @param array $cats Array of all categories (WP_Term objects) associated with the post.
  174. * @param WP_Post $post The post in question.
  175. */
  176. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  177. $category_object = get_term( $category_object, 'category' );
  178. $category = $category_object->slug;
  179. if ( $category_object->parent ) {
  180. $category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
  181. }
  182. }
  183. // Show default category in permalinks,
  184. // without having to assign it explicitly.
  185. if ( empty( $category ) ) {
  186. $default_category = get_term( get_option( 'default_category' ), 'category' );
  187. if ( $default_category && ! is_wp_error( $default_category ) ) {
  188. $category = $default_category->slug;
  189. }
  190. }
  191. }
  192. $author = '';
  193. if ( strpos( $permalink, '%author%' ) !== false ) {
  194. $authordata = get_userdata( $post->post_author );
  195. $author = $authordata->user_nicename;
  196. }
  197. // This is not an API call because the permalink is based on the stored post_date value,
  198. // which should be parsed as local time regardless of the default PHP timezone.
  199. $date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );
  200. $rewritereplace = array(
  201. $date[0],
  202. $date[1],
  203. $date[2],
  204. $date[3],
  205. $date[4],
  206. $date[5],
  207. $post->post_name,
  208. $post->ID,
  209. $category,
  210. $author,
  211. $post->post_name,
  212. );
  213. $permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
  214. $permalink = user_trailingslashit( $permalink, 'single' );
  215. } else { // If they're not using the fancy permalink option.
  216. $permalink = home_url( '?p=' . $post->ID );
  217. }
  218. /**
  219. * Filters the permalink for a post.
  220. *
  221. * Only applies to posts with post_type of 'post'.
  222. *
  223. * @since 1.5.0
  224. *
  225. * @param string $permalink The post's permalink.
  226. * @param WP_Post $post The post in question.
  227. * @param bool $leavename Whether to keep the post name.
  228. */
  229. return apply_filters( 'post_link', $permalink, $post, $leavename );
  230. }
  231. /**
  232. * Retrieves the permalink for a post of a custom post type.
  233. *
  234. * @since 3.0.0
  235. *
  236. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  237. *
  238. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  239. * @param bool $leavename Optional, defaults to false. Whether to keep post name. Default false.
  240. * @param bool $sample Optional, defaults to false. Is it a sample permalink. Default false.
  241. * @return string|WP_Error The post permalink.
  242. */
  243. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  244. global $wp_rewrite;
  245. $post = get_post( $id );
  246. if ( is_wp_error( $post ) ) {
  247. return $post;
  248. }
  249. $post_link = $wp_rewrite->get_extra_permastruct( $post->post_type );
  250. $slug = $post->post_name;
  251. $draft_or_pending = get_post_status( $post ) && in_array( get_post_status( $post ), array( 'draft', 'pending', 'auto-draft', 'future' ), true );
  252. $post_type = get_post_type_object( $post->post_type );
  253. if ( $post_type->hierarchical ) {
  254. $slug = get_page_uri( $post );
  255. }
  256. if ( ! empty( $post_link ) && ( ! $draft_or_pending || $sample ) ) {
  257. if ( ! $leavename ) {
  258. $post_link = str_replace( "%$post->post_type%", $slug, $post_link );
  259. }
  260. $post_link = home_url( user_trailingslashit( $post_link ) );
  261. } else {
  262. if ( $post_type->query_var && ( isset( $post->post_status ) && ! $draft_or_pending ) ) {
  263. $post_link = add_query_arg( $post_type->query_var, $slug, '' );
  264. } else {
  265. $post_link = add_query_arg(
  266. array(
  267. 'post_type' => $post->post_type,
  268. 'p' => $post->ID,
  269. ),
  270. ''
  271. );
  272. }
  273. $post_link = home_url( $post_link );
  274. }
  275. /**
  276. * Filters the permalink for a post of a custom post type.
  277. *
  278. * @since 3.0.0
  279. *
  280. * @param string $post_link The post's permalink.
  281. * @param WP_Post $post The post in question.
  282. * @param bool $leavename Whether to keep the post name.
  283. * @param bool $sample Is it a sample permalink.
  284. */
  285. return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
  286. }
  287. /**
  288. * Retrieves the permalink for the current page or page ID.
  289. *
  290. * Respects page_on_front. Use this one.
  291. *
  292. * @since 1.5.0
  293. *
  294. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
  295. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  296. * @param bool $sample Optional. Whether it should be treated as a sample permalink.
  297. * Default false.
  298. * @return string The page permalink.
  299. */
  300. function get_page_link( $post = false, $leavename = false, $sample = false ) {
  301. $post = get_post( $post );
  302. if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) {
  303. $link = home_url( '/' );
  304. } else {
  305. $link = _get_page_link( $post, $leavename, $sample );
  306. }
  307. /**
  308. * Filters the permalink for a page.
  309. *
  310. * @since 1.5.0
  311. *
  312. * @param string $link The page's permalink.
  313. * @param int $post_id The ID of the page.
  314. * @param bool $sample Is it a sample permalink.
  315. */
  316. return apply_filters( 'page_link', $link, $post->ID, $sample );
  317. }
  318. /**
  319. * Retrieves the page permalink.
  320. *
  321. * Ignores page_on_front. Internal use only.
  322. *
  323. * @since 2.1.0
  324. * @access private
  325. *
  326. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  327. *
  328. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
  329. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  330. * @param bool $sample Optional. Whether it should be treated as a sample permalink.
  331. * Default false.
  332. * @return string The page permalink.
  333. */
  334. function _get_page_link( $post = false, $leavename = false, $sample = false ) {
  335. global $wp_rewrite;
  336. $post = get_post( $post );
  337. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ), true );
  338. $link = $wp_rewrite->get_page_permastruct();
  339. if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $draft_or_pending ) || $sample ) ) {
  340. if ( ! $leavename ) {
  341. $link = str_replace( '%pagename%', get_page_uri( $post ), $link );
  342. }
  343. $link = home_url( $link );
  344. $link = user_trailingslashit( $link, 'page' );
  345. } else {
  346. $link = home_url( '?page_id=' . $post->ID );
  347. }
  348. /**
  349. * Filters the permalink for a non-page_on_front page.
  350. *
  351. * @since 2.1.0
  352. *
  353. * @param string $link The page's permalink.
  354. * @param int $post_id The ID of the page.
  355. */
  356. return apply_filters( '_get_page_link', $link, $post->ID );
  357. }
  358. /**
  359. * Retrieves the permalink for an attachment.
  360. *
  361. * This can be used in the WordPress Loop or outside of it.
  362. *
  363. * @since 2.0.0
  364. *
  365. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  366. *
  367. * @param int|object $post Optional. Post ID or object. Default uses the global `$post`.
  368. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  369. * @return string The attachment permalink.
  370. */
  371. function get_attachment_link( $post = null, $leavename = false ) {
  372. global $wp_rewrite;
  373. $link = false;
  374. $post = get_post( $post );
  375. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
  376. if ( $parent && ! in_array( $parent->post_type, get_post_types(), true ) ) {
  377. $parent = false;
  378. }
  379. if ( $wp_rewrite->using_permalinks() && $parent ) {
  380. if ( 'page' === $parent->post_type ) {
  381. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
  382. } else {
  383. $parentlink = get_permalink( $post->post_parent );
  384. }
  385. if ( is_numeric( $post->post_name ) || false !== strpos( get_option( 'permalink_structure' ), '%category%' ) ) {
  386. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker.
  387. } else {
  388. $name = $post->post_name;
  389. }
  390. if ( strpos( $parentlink, '?' ) === false ) {
  391. $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' );
  392. }
  393. if ( ! $leavename ) {
  394. $link = str_replace( '%postname%', $name, $link );
  395. }
  396. } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
  397. $link = home_url( user_trailingslashit( $post->post_name ) );
  398. }
  399. if ( ! $link ) {
  400. $link = home_url( '/?attachment_id=' . $post->ID );
  401. }
  402. /**
  403. * Filters the permalink for an attachment.
  404. *
  405. * @since 2.0.0
  406. *
  407. * @param string $link The attachment's permalink.
  408. * @param int $post_id Attachment ID.
  409. */
  410. return apply_filters( 'attachment_link', $link, $post->ID );
  411. }
  412. /**
  413. * Retrieves the permalink for the year archives.
  414. *
  415. * @since 1.5.0
  416. *
  417. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  418. *
  419. * @param int|false $year Integer of year. False for current year.
  420. * @return string The permalink for the specified year archive.
  421. */
  422. function get_year_link( $year ) {
  423. global $wp_rewrite;
  424. if ( ! $year ) {
  425. $year = current_time( 'Y' );
  426. }
  427. $yearlink = $wp_rewrite->get_year_permastruct();
  428. if ( ! empty( $yearlink ) ) {
  429. $yearlink = str_replace( '%year%', $year, $yearlink );
  430. $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
  431. } else {
  432. $yearlink = home_url( '?m=' . $year );
  433. }
  434. /**
  435. * Filters the year archive permalink.
  436. *
  437. * @since 1.5.0
  438. *
  439. * @param string $yearlink Permalink for the year archive.
  440. * @param int $year Year for the archive.
  441. */
  442. return apply_filters( 'year_link', $yearlink, $year );
  443. }
  444. /**
  445. * Retrieves the permalink for the month archives with year.
  446. *
  447. * @since 1.0.0
  448. *
  449. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  450. *
  451. * @param int|false $year Integer of year. False for current year.
  452. * @param int|false $month Integer of month. False for current month.
  453. * @return string The permalink for the specified month and year archive.
  454. */
  455. function get_month_link( $year, $month ) {
  456. global $wp_rewrite;
  457. if ( ! $year ) {
  458. $year = current_time( 'Y' );
  459. }
  460. if ( ! $month ) {
  461. $month = current_time( 'm' );
  462. }
  463. $monthlink = $wp_rewrite->get_month_permastruct();
  464. if ( ! empty( $monthlink ) ) {
  465. $monthlink = str_replace( '%year%', $year, $monthlink );
  466. $monthlink = str_replace( '%monthnum%', zeroise( intval( $month ), 2 ), $monthlink );
  467. $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
  468. } else {
  469. $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
  470. }
  471. /**
  472. * Filters the month archive permalink.
  473. *
  474. * @since 1.5.0
  475. *
  476. * @param string $monthlink Permalink for the month archive.
  477. * @param int $year Year for the archive.
  478. * @param int $month The month for the archive.
  479. */
  480. return apply_filters( 'month_link', $monthlink, $year, $month );
  481. }
  482. /**
  483. * Retrieves the permalink for the day archives with year and month.
  484. *
  485. * @since 1.0.0
  486. *
  487. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  488. *
  489. * @param int|false $year Integer of year. False for current year.
  490. * @param int|false $month Integer of month. False for current month.
  491. * @param int|false $day Integer of day. False for current day.
  492. * @return string The permalink for the specified day, month, and year archive.
  493. */
  494. function get_day_link( $year, $month, $day ) {
  495. global $wp_rewrite;
  496. if ( ! $year ) {
  497. $year = current_time( 'Y' );
  498. }
  499. if ( ! $month ) {
  500. $month = current_time( 'm' );
  501. }
  502. if ( ! $day ) {
  503. $day = current_time( 'j' );
  504. }
  505. $daylink = $wp_rewrite->get_day_permastruct();
  506. if ( ! empty( $daylink ) ) {
  507. $daylink = str_replace( '%year%', $year, $daylink );
  508. $daylink = str_replace( '%monthnum%', zeroise( intval( $month ), 2 ), $daylink );
  509. $daylink = str_replace( '%day%', zeroise( intval( $day ), 2 ), $daylink );
  510. $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
  511. } else {
  512. $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
  513. }
  514. /**
  515. * Filters the day archive permalink.
  516. *
  517. * @since 1.5.0
  518. *
  519. * @param string $daylink Permalink for the day archive.
  520. * @param int $year Year for the archive.
  521. * @param int $month Month for the archive.
  522. * @param int $day The day for the archive.
  523. */
  524. return apply_filters( 'day_link', $daylink, $year, $month, $day );
  525. }
  526. /**
  527. * Displays the permalink for the feed type.
  528. *
  529. * @since 3.0.0
  530. *
  531. * @param string $anchor The link's anchor text.
  532. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  533. * Default is the value of get_default_feed().
  534. */
  535. function the_feed_link( $anchor, $feed = '' ) {
  536. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  537. /**
  538. * Filters the feed link anchor tag.
  539. *
  540. * @since 3.0.0
  541. *
  542. * @param string $link The complete anchor tag for a feed link.
  543. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  544. * or an empty string for the default feed type.
  545. */
  546. echo apply_filters( 'the_feed_link', $link, $feed );
  547. }
  548. /**
  549. * Retrieves the permalink for the feed type.
  550. *
  551. * @since 1.5.0
  552. *
  553. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  554. *
  555. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  556. * Default is the value of get_default_feed().
  557. * @return string The feed permalink.
  558. */
  559. function get_feed_link( $feed = '' ) {
  560. global $wp_rewrite;
  561. $permalink = $wp_rewrite->get_feed_permastruct();
  562. if ( '' != $permalink ) {
  563. if ( false !== strpos( $feed, 'comments_' ) ) {
  564. $feed = str_replace( 'comments_', '', $feed );
  565. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  566. }
  567. if ( get_default_feed() == $feed ) {
  568. $feed = '';
  569. }
  570. $permalink = str_replace( '%feed%', $feed, $permalink );
  571. $permalink = preg_replace( '#/+#', '/', "/$permalink" );
  572. $output = home_url( user_trailingslashit( $permalink, 'feed' ) );
  573. } else {
  574. if ( empty( $feed ) ) {
  575. $feed = get_default_feed();
  576. }
  577. if ( false !== strpos( $feed, 'comments_' ) ) {
  578. $feed = str_replace( 'comments_', 'comments-', $feed );
  579. }
  580. $output = home_url( "?feed={$feed}" );
  581. }
  582. /**
  583. * Filters the feed type permalink.
  584. *
  585. * @since 1.5.0
  586. *
  587. * @param string $output The feed permalink.
  588. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  589. * or an empty string for the default feed type.
  590. */
  591. return apply_filters( 'feed_link', $output, $feed );
  592. }
  593. /**
  594. * Retrieves the permalink for the post comments feed.
  595. *
  596. * @since 2.2.0
  597. *
  598. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  599. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  600. * Default is the value of get_default_feed().
  601. * @return string The permalink for the comments feed for the given post.
  602. */
  603. function get_post_comments_feed_link( $post_id = 0, $feed = '' ) {
  604. $post_id = absint( $post_id );
  605. if ( ! $post_id ) {
  606. $post_id = get_the_ID();
  607. }
  608. if ( empty( $feed ) ) {
  609. $feed = get_default_feed();
  610. }
  611. $post = get_post( $post_id );
  612. $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent;
  613. if ( '' != get_option( 'permalink_structure' ) ) {
  614. if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post_id ) {
  615. $url = _get_page_link( $post_id );
  616. } else {
  617. $url = get_permalink( $post_id );
  618. }
  619. if ( $unattached ) {
  620. $url = home_url( '/feed/' );
  621. if ( get_default_feed() !== $feed ) {
  622. $url .= "$feed/";
  623. }
  624. $url = add_query_arg( 'attachment_id', $post_id, $url );
  625. } else {
  626. $url = trailingslashit( $url ) . 'feed';
  627. if ( get_default_feed() != $feed ) {
  628. $url .= "/$feed";
  629. }
  630. $url = user_trailingslashit( $url, 'single_feed' );
  631. }
  632. } else {
  633. if ( $unattached ) {
  634. $url = add_query_arg(
  635. array(
  636. 'feed' => $feed,
  637. 'attachment_id' => $post_id,
  638. ),
  639. home_url( '/' )
  640. );
  641. } elseif ( 'page' === $post->post_type ) {
  642. $url = add_query_arg(
  643. array(
  644. 'feed' => $feed,
  645. 'page_id' => $post_id,
  646. ),
  647. home_url( '/' )
  648. );
  649. } else {
  650. $url = add_query_arg(
  651. array(
  652. 'feed' => $feed,
  653. 'p' => $post_id,
  654. ),
  655. home_url( '/' )
  656. );
  657. }
  658. }
  659. /**
  660. * Filters the post comments feed permalink.
  661. *
  662. * @since 1.5.1
  663. *
  664. * @param string $url Post comments feed permalink.
  665. */
  666. return apply_filters( 'post_comments_feed_link', $url );
  667. }
  668. /**
  669. * Displays the comment feed link for a post.
  670. *
  671. * Prints out the comment feed link for a post. Link text is placed in the
  672. * anchor. If no link text is specified, default text is used. If no post ID is
  673. * specified, the current post is used.
  674. *
  675. * @since 2.5.0
  676. *
  677. * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'.
  678. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  679. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  680. * Default is the value of get_default_feed().
  681. */
  682. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  683. $url = get_post_comments_feed_link( $post_id, $feed );
  684. if ( empty( $link_text ) ) {
  685. $link_text = __( 'Comments Feed' );
  686. }
  687. $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>';
  688. /**
  689. * Filters the post comment feed link anchor tag.
  690. *
  691. * @since 2.8.0
  692. *
  693. * @param string $link The complete anchor tag for the comment feed link.
  694. * @param int $post_id Post ID.
  695. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  696. * or an empty string for the default feed type.
  697. */
  698. echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed );
  699. }
  700. /**
  701. * Retrieves the feed link for a given author.
  702. *
  703. * Returns a link to the feed for all posts by a given author. A specific feed
  704. * can be requested or left blank to get the default feed.
  705. *
  706. * @since 2.5.0
  707. *
  708. * @param int $author_id Author ID.
  709. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  710. * Default is the value of get_default_feed().
  711. * @return string Link to the feed for the author specified by $author_id.
  712. */
  713. function get_author_feed_link( $author_id, $feed = '' ) {
  714. $author_id = (int) $author_id;
  715. $permalink_structure = get_option( 'permalink_structure' );
  716. if ( empty( $feed ) ) {
  717. $feed = get_default_feed();
  718. }
  719. if ( '' == $permalink_structure ) {
  720. $link = home_url( "?feed=$feed&amp;author=" . $author_id );
  721. } else {
  722. $link = get_author_posts_url( $author_id );
  723. if ( get_default_feed() == $feed ) {
  724. $feed_link = 'feed';
  725. } else {
  726. $feed_link = "feed/$feed";
  727. }
  728. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  729. }
  730. /**
  731. * Filters the feed link for a given author.
  732. *
  733. * @since 1.5.1
  734. *
  735. * @param string $link The author feed link.
  736. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  737. */
  738. $link = apply_filters( 'author_feed_link', $link, $feed );
  739. return $link;
  740. }
  741. /**
  742. * Retrieves the feed link for a category.
  743. *
  744. * Returns a link to the feed for all posts in a given category. A specific feed
  745. * can be requested or left blank to get the default feed.
  746. *
  747. * @since 2.5.0
  748. *
  749. * @param int $cat_id Category ID.
  750. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  751. * Default is the value of get_default_feed().
  752. * @return string Link to the feed for the category specified by $cat_id.
  753. */
  754. function get_category_feed_link( $cat_id, $feed = '' ) {
  755. return get_term_feed_link( $cat_id, 'category', $feed );
  756. }
  757. /**
  758. * Retrieves the feed link for a term.
  759. *
  760. * Returns a link to the feed for all posts in a given term. A specific feed
  761. * can be requested or left blank to get the default feed.
  762. *
  763. * @since 3.0.0
  764. *
  765. * @param int $term_id Term ID.
  766. * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'.
  767. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  768. * Default is the value of get_default_feed().
  769. * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
  770. */
  771. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  772. $term_id = (int) $term_id;
  773. $term = get_term( $term_id, $taxonomy );
  774. if ( empty( $term ) || is_wp_error( $term ) ) {
  775. return false;
  776. }
  777. if ( empty( $feed ) ) {
  778. $feed = get_default_feed();
  779. }
  780. $permalink_structure = get_option( 'permalink_structure' );
  781. if ( '' == $permalink_structure ) {
  782. if ( 'category' == $taxonomy ) {
  783. $link = home_url( "?feed=$feed&amp;cat=$term_id" );
  784. } elseif ( 'post_tag' == $taxonomy ) {
  785. $link = home_url( "?feed=$feed&amp;tag=$term->slug" );
  786. } else {
  787. $t = get_taxonomy( $taxonomy );
  788. $link = home_url( "?feed=$feed&amp;$t->query_var=$term->slug" );
  789. }
  790. } else {
  791. $link = get_term_link( $term_id, $term->taxonomy );
  792. if ( get_default_feed() == $feed ) {
  793. $feed_link = 'feed';
  794. } else {
  795. $feed_link = "feed/$feed";
  796. }
  797. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  798. }
  799. if ( 'category' == $taxonomy ) {
  800. /**
  801. * Filters the category feed link.
  802. *
  803. * @since 1.5.1
  804. *
  805. * @param string $link The category feed link.
  806. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  807. */
  808. $link = apply_filters( 'category_feed_link', $link, $feed );
  809. } elseif ( 'post_tag' == $taxonomy ) {
  810. /**
  811. * Filters the post tag feed link.
  812. *
  813. * @since 2.3.0
  814. *
  815. * @param string $link The tag feed link.
  816. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  817. */
  818. $link = apply_filters( 'tag_feed_link', $link, $feed );
  819. } else {
  820. /**
  821. * Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
  822. *
  823. * @since 3.0.0
  824. *
  825. * @param string $link The taxonomy feed link.
  826. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  827. * @param string $taxonomy The taxonomy name.
  828. */
  829. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  830. }
  831. return $link;
  832. }
  833. /**
  834. * Retrieves the permalink for a tag feed.
  835. *
  836. * @since 2.3.0
  837. *
  838. * @param int $tag_id Tag ID.
  839. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  840. * Default is the value of get_default_feed().
  841. * @return string The feed permalink for the given tag.
  842. */
  843. function get_tag_feed_link( $tag_id, $feed = '' ) {
  844. return get_term_feed_link( $tag_id, 'post_tag', $feed );
  845. }
  846. /**
  847. * Retrieves the edit link for a tag.
  848. *
  849. * @since 2.7.0
  850. *
  851. * @param int $tag_id Tag ID.
  852. * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
  853. * @return string The edit tag link URL for the given tag.
  854. */
  855. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  856. /**
  857. * Filters the edit link for a tag (or term in another taxonomy).
  858. *
  859. * @since 2.7.0
  860. *
  861. * @param string $link The term edit link.
  862. */
  863. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  864. }
  865. /**
  866. * Displays or retrieves the edit link for a tag with formatting.
  867. *
  868. * @since 2.7.0
  869. *
  870. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  871. * @param string $before Optional. Display before edit link. Default empty.
  872. * @param string $after Optional. Display after edit link. Default empty.
  873. * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected.
  874. * Default null.
  875. */
  876. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  877. $link = edit_term_link( $link, '', '', $tag, false );
  878. /**
  879. * Filters the anchor tag for the edit link for a tag (or term in another taxonomy).
  880. *
  881. * @since 2.7.0
  882. *
  883. * @param string $link The anchor tag for the edit link.
  884. */
  885. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  886. }
  887. /**
  888. * Retrieves the URL for editing a given term.
  889. *
  890. * @since 3.1.0
  891. * @since 4.5.0 The `$taxonomy` parameter was made optional.
  892. *
  893. * @param int $term_id Term ID.
  894. * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified
  895. * by `$term_id`.
  896. * @param string $object_type Optional. The object type. Used to highlight the proper post type
  897. * menu on the linked page. Defaults to the first object_type associated
  898. * with the taxonomy.
  899. * @return string|null The edit term link URL for the given term, or null on failure.
  900. */
  901. function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
  902. $term = get_term( $term_id, $taxonomy );
  903. if ( ! $term || is_wp_error( $term ) ) {
  904. return;
  905. }
  906. $tax = get_taxonomy( $term->taxonomy );
  907. if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) {
  908. return;
  909. }
  910. $args = array(
  911. 'taxonomy' => $taxonomy,
  912. 'tag_ID' => $term->term_id,
  913. );
  914. if ( $object_type ) {
  915. $args['post_type'] = $object_type;
  916. } elseif ( ! empty( $tax->object_type ) ) {
  917. $args['post_type'] = reset( $tax->object_type );
  918. }
  919. if ( $tax->show_ui ) {
  920. $location = add_query_arg( $args, admin_url( 'term.php' ) );
  921. } else {
  922. $location = '';
  923. }
  924. /**
  925. * Filters the edit link for a term.
  926. *
  927. * @since 3.1.0
  928. *
  929. * @param string $location The edit link.
  930. * @param int $term_id Term ID.
  931. * @param string $taxonomy Taxonomy name.
  932. * @param string $object_type The object type (eg. the post type).
  933. */
  934. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  935. }
  936. /**
  937. * Displays or retrieves the edit term link with formatting.
  938. *
  939. * @since 3.1.0
  940. *
  941. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  942. * @param string $before Optional. Display before edit link. Default empty.
  943. * @param string $after Optional. Display after edit link. Default empty.
  944. * @param WP_Term $term Optional. Term object. If null, the queried object will be inspected. Default null.
  945. * @param bool $echo Optional. Whether or not to echo the return. Default true.
  946. * @return string|void HTML content.
  947. */
  948. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  949. if ( is_null( $term ) ) {
  950. $term = get_queried_object();
  951. }
  952. if ( ! $term ) {
  953. return;
  954. }
  955. $tax = get_taxonomy( $term->taxonomy );
  956. if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
  957. return;
  958. }
  959. if ( empty( $link ) ) {
  960. $link = __( 'Edit This' );
  961. }
  962. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
  963. /**
  964. * Filters the anchor tag for the edit link of a term.
  965. *
  966. * @since 3.1.0
  967. *
  968. * @param string $link The anchor tag for the edit link.
  969. * @param int $term_id Term ID.
  970. */
  971. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  972. if ( $echo ) {
  973. echo $link;
  974. } else {
  975. return $link;
  976. }
  977. }
  978. /**
  979. * Retrieves the permalink for a search.
  980. *
  981. * @since 3.0.0
  982. *
  983. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  984. *
  985. * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
  986. * @return string The search permalink.
  987. */
  988. function get_search_link( $query = '' ) {
  989. global $wp_rewrite;
  990. if ( empty( $query ) ) {
  991. $search = get_search_query( false );
  992. } else {
  993. $search = stripslashes( $query );
  994. }
  995. $permastruct = $wp_rewrite->get_search_permastruct();
  996. if ( empty( $permastruct ) ) {
  997. $link = home_url( '?s=' . urlencode( $search ) );
  998. } else {
  999. $search = urlencode( $search );
  1000. $search = str_replace( '%2F', '/', $search ); // %2F(/) is not valid within a URL, send it un-encoded.
  1001. $link = str_replace( '%search%', $search, $permastruct );
  1002. $link = home_url( user_trailingslashit( $link, 'search' ) );
  1003. }
  1004. /**
  1005. * Filters the search permalink.
  1006. *
  1007. * @since 3.0.0
  1008. *
  1009. * @param string $link Search permalink.
  1010. * @param string $search The URL-encoded search term.
  1011. */
  1012. return apply_filters( 'search_link', $link, $search );
  1013. }
  1014. /**
  1015. * Retrieves the permalink for the search results feed.
  1016. *
  1017. * @since 2.5.0
  1018. *
  1019. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1020. *
  1021. * @param string $search_query Optional. Search query. Default empty.
  1022. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1023. * Default is the value of get_default_feed().
  1024. * @return string The search results feed permalink.
  1025. */
  1026. function get_search_feed_link( $search_query = '', $feed = '' ) {
  1027. global $wp_rewrite;
  1028. $link = get_search_link( $search_query );
  1029. if ( empty( $feed ) ) {
  1030. $feed = get_default_feed();
  1031. }
  1032. $permastruct = $wp_rewrite->get_search_permastruct();
  1033. if ( empty( $permastruct ) ) {
  1034. $link = add_query_arg( 'feed', $feed, $link );
  1035. } else {
  1036. $link = trailingslashit( $link );
  1037. $link .= "feed/$feed/";
  1038. }
  1039. /**
  1040. * Filters the search feed link.
  1041. *
  1042. * @since 2.5.0
  1043. *
  1044. * @param string $link Search feed link.
  1045. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  1046. * @param string $type The search type. One of 'posts' or 'comments'.
  1047. */
  1048. return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
  1049. }
  1050. /**
  1051. * Retrieves the permalink for the search results comments feed.
  1052. *
  1053. * @since 2.5.0
  1054. *
  1055. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1056. *
  1057. * @param string $search_query Optional. Search query. Default empty.
  1058. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1059. * Default is the value of get_default_feed().
  1060. * @return string The comments feed search results permalink.
  1061. */
  1062. function get_search_comments_feed_link( $search_query = '', $feed = '' ) {
  1063. global $wp_rewrite;
  1064. if ( empty( $feed ) ) {
  1065. $feed = get_default_feed();
  1066. }
  1067. $link = get_search_feed_link( $search_query, $feed );
  1068. $permastruct = $wp_rewrite->get_search_permastruct();
  1069. if ( empty( $permastruct ) ) {
  1070. $link = add_query_arg( 'feed', 'comments-' . $feed, $link );
  1071. } else {
  1072. $link = add_query_arg( 'withcomments', 1, $link );
  1073. }
  1074. /** This filter is documented in wp-includes/link-template.php */
  1075. return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
  1076. }
  1077. /**
  1078. * Retrieves the permalink for a post type archive.
  1079. *
  1080. * @since 3.1.0
  1081. * @since 4.5.0 Support for posts was added.
  1082. *
  1083. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1084. *
  1085. * @param string $post_type Post type.
  1086. * @return string|false The post type archive permalink.
  1087. */
  1088. function get_post_type_archive_link( $post_type ) {
  1089. global $wp_rewrite;
  1090. $post_type_obj = get_post_type_object( $post_type );
  1091. if ( ! $post_type_obj ) {
  1092. return false;
  1093. }
  1094. if ( 'post' === $post_type ) {
  1095. $show_on_front = get_option( 'show_on_front' );
  1096. $page_for_posts = get_option( 'page_for_posts' );
  1097. if ( 'page' === $show_on_front && $page_for_posts ) {
  1098. $link = get_permalink( $page_for_posts );
  1099. } else {
  1100. $link = get_home_url();
  1101. }
  1102. /** This filter is documented in wp-includes/link-template.php */
  1103. return apply_filters( 'post_type_archive_link', $link, $post_type );
  1104. }
  1105. if ( ! $post_type_obj->has_archive ) {
  1106. return false;
  1107. }
  1108. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  1109. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  1110. if ( $post_type_obj->rewrite['with_front'] ) {
  1111. $struct = $wp_rewrite->front . $struct;
  1112. } else {
  1113. $struct = $wp_rewrite->root . $struct;
  1114. }
  1115. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  1116. } else {
  1117. $link = home_url( '?post_type=' . $post_type );
  1118. }
  1119. /**
  1120. * Filters the post type archive permalink.
  1121. *
  1122. * @since 3.1.0
  1123. *
  1124. * @param string $link The post type archive permalink.
  1125. * @param string $post_type Post type name.
  1126. */
  1127. return apply_filters( 'post_type_archive_link', $link, $post_type );
  1128. }
  1129. /**
  1130. * Retrieves the permalink for a post type archive feed.
  1131. *
  1132. * @since 3.1.0
  1133. *
  1134. * @param string $post_type Post type
  1135. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1136. * Default is the value of get_default_feed().
  1137. * @return string|false The post type feed permalink.
  1138. */
  1139. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  1140. $default_feed = get_default_feed();
  1141. if ( empty( $feed ) ) {
  1142. $feed = $default_feed;
  1143. }
  1144. $link = get_post_type_archive_link( $post_type );
  1145. if ( ! $link ) {
  1146. return false;
  1147. }
  1148. $post_type_obj = get_post_type_object( $post_type );
  1149. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
  1150. $link = trailingslashit( $link );
  1151. $link .= 'feed/';
  1152. if ( $feed != $default_feed ) {
  1153. $link .= "$feed/";
  1154. }
  1155. } else {
  1156. $link = add_query_arg( 'feed', $feed, $link );
  1157. }
  1158. /**
  1159. * Filters the post type archive feed link.
  1160. *
  1161. * @since 3.1.0
  1162. *
  1163. * @param string $link The post type archive feed link.
  1164. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  1165. */
  1166. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  1167. }
  1168. /**
  1169. * Retrieves the URL used for the post preview.
  1170. *
  1171. * Allows additional query args to be appended.
  1172. *
  1173. * @since 4.4.0
  1174. *
  1175. * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`.
  1176. * @param array $query_args Optional. Array of additional query args to be appended to the link.
  1177. * Default empty array.
  1178. * @param string $preview_link Optional. Base preview link to be used if it should differ from the
  1179. * post permalink. Default empty.
  1180. * @return string|null URL used for the post preview, or null if the post does not exist.
  1181. */
  1182. function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
  1183. $post = get_post( $post );
  1184. if ( ! $post ) {
  1185. return;
  1186. }
  1187. $post_type_object = get_post_type_object( $post->post_type );
  1188. if ( is_post_type_viewable( $post_type_object ) ) {
  1189. if ( ! $preview_link ) {
  1190. $preview_link = set_url_scheme( get_permalink( $post ) );
  1191. }
  1192. $query_args['preview'] = 'true';
  1193. $preview_link = add_query_arg( $query_args, $preview_link );
  1194. }
  1195. /**
  1196. * Filters the URL used for a post preview.
  1197. *
  1198. * @since 2.0.5
  1199. * @since 4.0.0 Added the `$post` parameter.
  1200. *
  1201. * @param string $preview_link URL used for the post preview.
  1202. * @param WP_Post $post Post object.
  1203. */
  1204. return apply_filters( 'preview_post_link', $preview_link, $post );
  1205. }
  1206. /**
  1207. * Retrieves the edit post link for post.
  1208. *
  1209. * Can be used within the WordPress loop or outside of it. Can be used with
  1210. * pages, posts, attachments, and revisions.
  1211. *
  1212. * @since 2.3.0
  1213. *
  1214. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1215. * @param string $context Optional. How to output the '&' character. Default '&amp;'.
  1216. * @return string|null The edit post link for the given post. null if the post type is invalid or does
  1217. * not allow an editing UI.
  1218. */
  1219. function get_edit_post_link( $id = 0, $context = 'display' ) {
  1220. $post = get_post( $id );
  1221. if ( ! $post ) {
  1222. return;
  1223. }
  1224. if ( 'revision' === $post->post_type ) {
  1225. $action = '';
  1226. } elseif ( 'display' == $context ) {
  1227. $action = '&amp;action=edit';
  1228. } else {
  1229. $action = '&action=edit';
  1230. }
  1231. $post_type_object = get_post_type_object( $post->post_type );
  1232. if ( ! $post_type_object ) {
  1233. return;
  1234. }
  1235. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1236. return;
  1237. }
  1238. if ( $post_type_object->_edit_link ) {
  1239. $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
  1240. } else {
  1241. $link = '';
  1242. }
  1243. /**
  1244. * Filters the post edit link.
  1245. *
  1246. * @since 2.3.0
  1247. *
  1248. * @param string $link The edit link.
  1249. * @param int $post_id Post ID.
  1250. * @param string $context The link context. If set to 'display' then ampersands
  1251. * are encoded.
  1252. */
  1253. return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
  1254. }
  1255. /**
  1256. * Displays the edit post link for post.
  1257. *
  1258. * @since 1.0.0
  1259. * @since 4.4.0 The `$class` argument was added.
  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. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1265. * @param string $class Optional. Add custom class to link. Default 'post-edit-link'.
  1266. */
  1267. function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
  1268. $post = get_post( $id );
  1269. if ( ! $post ) {
  1270. return;
  1271. }
  1272. $url = get_edit_post_link( $post->ID );
  1273. if ( ! $url ) {
  1274. return;
  1275. }
  1276. if ( null === $text ) {
  1277. $text = __( 'Edit This' );
  1278. }
  1279. $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
  1280. /**
  1281. * Filters the post edit link anchor tag.
  1282. *
  1283. * @since 2.3.0
  1284. *
  1285. * @param string $link Anchor tag for the edit link.
  1286. * @param int $post_id Post ID.
  1287. * @param string $text Anchor text.
  1288. */
  1289. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
  1290. }
  1291. /**
  1292. * Retrieves the delete posts link for post.
  1293. *
  1294. * Can be used within the WordPress loop or outside of it, with any post type.
  1295. *
  1296. * @since 2.9.0
  1297. *
  1298. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1299. * @param string $deprecated Not used.
  1300. * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. Default false.
  1301. * @return string|void The delete post link URL for the given post.
  1302. */
  1303. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  1304. if ( ! empty( $deprecated ) ) {
  1305. _deprecated_argument( __FUNCTION__, '3.0.0' );
  1306. }
  1307. $post = get_post( $id );
  1308. if ( ! $post ) {
  1309. return;
  1310. }
  1311. $post_type_object = get_post_type_object( $post->post_type );
  1312. if ( ! $post_type_object ) {
  1313. return;
  1314. }
  1315. if ( ! current_user_can( 'delete_post', $post->ID ) ) {
  1316. return;
  1317. }
  1318. $action = ( $force_delete || ! EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  1319. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  1320. /**
  1321. * Filters the post delete link.
  1322. *
  1323. * @since 2.9.0
  1324. *
  1325. * @param string $link The delete link.
  1326. * @param int $post_id Post ID.
  1327. * @param bool $force_delete Whether to bypass the Trash and force deletion. Default false.
  1328. */
  1329. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  1330. }
  1331. /**
  1332. * Retrieves the edit comment link.
  1333. *
  1334. * @since 2.3.0
  1335. *
  1336. * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
  1337. * @return string|void The edit comment link URL for the given comment.
  1338. */
  1339. function get_edit_comment_link( $comment_id = 0 ) {
  1340. $comment = get_comment( $comment_id );
  1341. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1342. return;
  1343. }
  1344. $location = admin_url( 'comment.php?action=editcomment&amp;c=' ) . $comment->comment_ID;
  1345. /**
  1346. * Filters the comment edit link.
  1347. *
  1348. * @since 2.3.0
  1349. *
  1350. * @param string $location The edit link.
  1351. */
  1352. return apply_filters( 'get_edit_comment_link', $location );
  1353. }
  1354. /**
  1355. * Displays the edit comment link with formatting.
  1356. *
  1357. * @since 1.0.0
  1358. *
  1359. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
  1360. * @param string $before Optional. Display before edit link. Default empty.
  1361. * @param string $after Optional. Display after edit link. Default empty.
  1362. */
  1363. function edit_comment_link( $text = null, $before = '', $after = '' ) {
  1364. $comment = get_comment();
  1365. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1366. return;
  1367. }
  1368. if ( null === $text ) {
  1369. $text = __( 'Edit This' );
  1370. }
  1371. $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
  1372. /**
  1373. * Filters the comment edit link anchor tag.
  1374. *
  1375. * @since 2.3.0
  1376. *
  1377. * @param string $link Anchor tag for the edit link.
  1378. * @param int $comment_id Comment ID.
  1379. * @param string $text Anchor text.
  1380. */
  1381. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
  1382. }
  1383. /**
  1384. * Displays the edit bookmark link.
  1385. *
  1386. * @since 2.7.0
  1387. *
  1388. * @param int|stdClass $link Optional. Bookmark ID. Default is the id of the current bookmark.
  1389. * @return string|void The edit bookmark link URL.
  1390. */
  1391. function get_edit_bookmark_link( $link = 0 ) {
  1392. $link = get_bookmark( $link );
  1393. if ( ! current_user_can( 'manage_links' ) ) {
  1394. return;
  1395. }
  1396. $location = admin_url( 'link.php?action=edit&amp;link_id=' ) . $link->link_id;
  1397. /**
  1398. * Filters the bookmark edit link.
  1399. *
  1400. * @since 2.7.0
  1401. *
  1402. * @param string $location The edit link.
  1403. * @param int $link_id Bookmark ID.
  1404. */
  1405. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  1406. }
  1407. /**
  1408. * Displays the edit bookmark link anchor content.
  1409. *
  1410. * @since 2.7.0
  1411. *
  1412. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  1413. * @param string $before Optional. Display before edit link. Default empty.
  1414. * @param string $after Optional. Display after edit link. Default empty.
  1415. * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark.
  1416. */
  1417. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  1418. $bookmark = get_bookmark( $bookmark );
  1419. if ( ! current_user_can( 'manage_links' ) ) {
  1420. return;
  1421. }
  1422. if ( empty( $link ) ) {
  1423. $link = __( 'Edit This' );
  1424. }
  1425. $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
  1426. /**
  1427. * Filters the bookmark edit link anchor tag.
  1428. *
  1429. * @since 2.7.0
  1430. *
  1431. * @param string $link Anchor tag for the edit link.
  1432. * @param int $link_id Bookmark ID.
  1433. */
  1434. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  1435. }
  1436. /**
  1437. * Retrieves the edit user link.
  1438. *
  1439. * @since 3.5.0
  1440. *
  1441. * @param int $user_id Optional. User ID. Defaults to the current user.
  1442. * @return string URL to edit user page or empty string.
  1443. */
  1444. function get_edit_user_link( $user_id = null ) {
  1445. if ( ! $user_id ) {
  1446. $user_id = get_current_user_id();
  1447. }
  1448. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) {
  1449. return '';
  1450. }
  1451. $user = get_userdata( $user_id );
  1452. if ( ! $user ) {
  1453. return '';
  1454. }
  1455. if ( get_current_user_id() == $user->ID ) {
  1456. $link = get_edit_profile_url( $user->ID );
  1457. } else {
  1458. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  1459. }
  1460. /**
  1461. * Filters the user edit link.
  1462. *
  1463. * @since 3.5.0
  1464. *
  1465. * @param string $link The edit link.
  1466. * @param int $user_id User ID.
  1467. */
  1468. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  1469. }
  1470. //
  1471. // Navigation links.
  1472. //
  1473. /**
  1474. * Retrieves the previous post that is adjacent to the current post.
  1475. *
  1476. * @since 1.5.0
  1477. *
  1478. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1479. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1480. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1481. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1482. * corresponding post exists.
  1483. */
  1484. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1485. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
  1486. }
  1487. /**
  1488. * Retrieves the next post that is adjacent to the current post.
  1489. *
  1490. * @since 1.5.0
  1491. *
  1492. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1493. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1494. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1495. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1496. * corresponding post exists.
  1497. */
  1498. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1499. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
  1500. }
  1501. /**
  1502. * Retrieves the adjacent post.
  1503. *
  1504. * Can either be next or previous post.
  1505. *
  1506. * @since 2.5.0
  1507. *
  1508. * @global wpdb $wpdb WordPress database abstraction object.
  1509. *
  1510. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1511. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1512. * @param bool $previous Optional. Whether to retrieve previous post. Default true
  1513. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1514. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1515. * corresponding post exists.
  1516. */
  1517. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1518. global $wpdb;
  1519. $post = get_post();
  1520. if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {
  1521. return null;
  1522. }
  1523. $current_post_date = $post->post_date;
  1524. $join = '';
  1525. $where = '';
  1526. $adjacent = $previous ? 'previous' : 'next';
  1527. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
  1528. // Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".
  1529. if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  1530. _deprecated_argument(
  1531. __FUNCTION__,
  1532. '3.3.0',
  1533. sprintf(
  1534. /* translators: %s: The word 'and'. */
  1535. __( 'Use commas instead of %s to separate excluded terms.' ),
  1536. "'and'"
  1537. )
  1538. );
  1539. $excluded_terms = explode( ' and ', $excluded_terms );
  1540. } else {
  1541. $excluded_terms = explode( ',', $excluded_terms );
  1542. }
  1543. $excluded_terms = array_map( 'intval', $excluded_terms );
  1544. }
  1545. /**
  1546. * Filters the IDs of terms excluded from adjacent post queries.
  1547. *
  1548. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1549. * of adjacency, 'next' or 'previous'.
  1550. *
  1551. * @since 4.4.0
  1552. *
  1553. * @param array $excluded_terms Array of excluded term IDs.
  1554. */
  1555. $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
  1556. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1557. if ( $in_same_term ) {
  1558. $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";
  1559. $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );
  1560. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
  1561. return '';
  1562. }
  1563. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1564. // Remove any exclusions from the term array to include.
  1565. $term_array = array_diff( $term_array, (array) $excluded_terms );
  1566. $term_array = array_map( 'intval', $term_array );
  1567. if ( ! $term_array || is_wp_error( $term_array ) ) {
  1568. return '';
  1569. }
  1570. $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
  1571. }
  1572. if ( ! empty( $excluded_terms ) ) {
  1573. $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
  1574. }
  1575. }
  1576. // 'post_status' clause depends on the current user.
  1577. if ( is_user_logged_in() ) {
  1578. $user_id = get_current_user_id();
  1579. $post_type_object = get_post_type_object( $post->post_type );
  1580. if ( empty( $post_type_object ) ) {
  1581. $post_type_cap = $post->post_type;
  1582. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  1583. } else {
  1584. $read_private_cap = $post_type_object->cap->read_private_posts;
  1585. }
  1586. /*
  1587. * Results should include private posts belonging to the current user, or private posts where the
  1588. * current user has the 'read_private_posts' cap.
  1589. */
  1590. $private_states = get_post_stati( array( 'private' => true ) );
  1591. $where .= " AND ( p.post_status = 'publish'";
  1592. foreach ( (array) $private_states as $state ) {
  1593. if ( current_user_can( $read_private_cap ) ) {
  1594. $where .= $wpdb->prepare( ' OR p.post_status = %s', $state );
  1595. } else {
  1596. $where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state );
  1597. }
  1598. }
  1599. $where .= ' )';
  1600. } else {
  1601. $where .= " AND p.post_status = 'publish'";
  1602. }
  1603. $op = $previous ? '<' : '>';
  1604. $order = $previous ? 'DESC' : 'ASC';
  1605. /**
  1606. * Filters the JOIN clause in the SQL for an adjacent post query.
  1607. *
  1608. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1609. * of adjacency, 'next' or 'previous'.
  1610. *
  1611. * @since 2.5.0
  1612. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
  1613. *
  1614. * @param string $join The JOIN clause in the SQL.
  1615. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1616. * @param array $excluded_terms Array of excluded term IDs.
  1617. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
  1618. * @param WP_Post $post WP_Post object.
  1619. */
  1620. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
  1621. /**
  1622. * Filters the WHERE clause in the SQL for an adjacent post query.
  1623. *
  1624. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1625. * of adjacency, 'next' or 'previous'.
  1626. *
  1627. * @since 2.5.0
  1628. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
  1629. *
  1630. * @param string $where The `WHERE` clause in the SQL.
  1631. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1632. * @param array $excluded_terms Array of excluded term IDs.
  1633. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
  1634. * @param WP_Post $post WP_Post object.
  1635. */
  1636. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
  1637. /**
  1638. * Filters the ORDER BY clause in the SQL for an adjacent post query.
  1639. *
  1640. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1641. * of adjacency, 'next' or 'previous'.
  1642. *
  1643. * @since 2.5.0
  1644. * @since 4.4.0 Added the `$post` parameter.
  1645. * @since 4.9.0 Added the `$order` parameter.
  1646. *
  1647. * @param string $order_by The `ORDER BY` clause in the SQL.
  1648. * @param WP_Post $post WP_Post object.
  1649. * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
  1650. */
  1651. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
  1652. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1653. $query_key = 'adjacent_post_' . md5( $query );
  1654. $result = wp_cache_get( $query_key, 'counts' );
  1655. if ( false !== $result ) {
  1656. if ( $result ) {
  1657. $result = get_post( $result );
  1658. }
  1659. return $result;
  1660. }
  1661. $result = $wpdb->get_var( $query );
  1662. if ( null === $result ) {
  1663. $result = '';
  1664. }
  1665. wp_cache_set( $query_key, $result, 'counts' );
  1666. if ( $result ) {
  1667. $result = get_post( $result );
  1668. }
  1669. return $result;
  1670. }
  1671. /**
  1672. * Retrieves the adjacent post relational link.
  1673. *
  1674. * Can either be next or previous post relational link.
  1675. *
  1676. * @since 2.8.0
  1677. *
  1678. * @param string $title Optional. Link title format. Default '%title'.
  1679. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1680. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1681. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1682. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1683. * @return string|void The adjacent post relational link URL.
  1684. */
  1685. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1686. $post = get_post();
  1687. if ( $previous && is_attachment() && $post ) {
  1688. $post = get_post( $post->post_parent );
  1689. } else {
  1690. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1691. }
  1692. if ( empty( $post ) ) {
  1693. return;
  1694. }
  1695. $post_title = the_title_attribute(
  1696. array(
  1697. 'echo' => false,
  1698. 'post' => $post,
  1699. )
  1700. );
  1701. if ( empty( $post_title ) ) {
  1702. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1703. }
  1704. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1705. $title = str_replace( '%title', $post_title, $title );
  1706. $title = str_replace( '%date', $date, $title );
  1707. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1708. $link .= esc_attr( $title );
  1709. $link .= "' href='" . get_permalink( $post ) . "' />\n";
  1710. $adjacent = $previous ? 'previous' : 'next';
  1711. /**
  1712. * Filters the adjacent post relational link.
  1713. *
  1714. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1715. * of adjacency, 'next' or 'previous'.
  1716. *
  1717. * @since 2.8.0
  1718. *
  1719. * @param string $link The relational link.
  1720. */
  1721. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1722. }
  1723. /**
  1724. * Displays the relational links for the posts adjacent to the current post.
  1725. *
  1726. * @since 2.8.0
  1727. *
  1728. * @param string $title Optional. Link title format. Default '%title'.
  1729. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1730. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1731. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1732. */
  1733. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1734. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1735. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1736. }
  1737. /**
  1738. * Displays relational links for the posts adjacent to the current post for single post pages.
  1739. *
  1740. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins
  1741. * or theme templates.
  1742. *
  1743. * @since 3.0.0
  1744. *
  1745. * @see adjacent_posts_rel_link()
  1746. */
  1747. function adjacent_posts_rel_link_wp_head() {
  1748. if ( ! is_single() || is_attachment() ) {
  1749. return;
  1750. }
  1751. adjacent_posts_rel_link();
  1752. }
  1753. /**
  1754. * Displays the relational link for the next post adjacent to the current post.
  1755. *
  1756. * @since 2.8.0
  1757. *
  1758. * @see get_adjacent_post_rel_link()
  1759. *
  1760. * @param string $title Optional. Link title format. Default '%title'.
  1761. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1762. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1763. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1764. */
  1765. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1766. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1767. }
  1768. /**
  1769. * Displays the relational link for the previous post adjacent to the current post.
  1770. *
  1771. * @since 2.8.0
  1772. *
  1773. * @see get_adjacent_post_rel_link()
  1774. *
  1775. * @param string $title Optional. Link title format. Default '%title'.
  1776. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1777. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
  1778. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1779. */
  1780. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1781. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1782. }
  1783. /**
  1784. * Retrieves the boundary post.
  1785. *
  1786. * Boundary being either the first or last post by publish date within the constraints specified
  1787. * by $in_same_term or $excluded_terms.
  1788. *
  1789. * @since 2.8.0
  1790. *
  1791. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
  1792. * Default false.
  1793. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1794. * Default empty.
  1795. * @param bool $start Optional. Whether to retrieve first or last post. Default true
  1796. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1797. * @return null|array Array containing the boundary post object if successful, null otherwise.
  1798. */
  1799. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
  1800. $post = get_post();
  1801. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) {
  1802. return null;
  1803. }
  1804. $query_args = array(
  1805. 'posts_per_page' => 1,
  1806. 'order' => $start ? 'ASC' : 'DESC',
  1807. 'update_post_term_cache' => false,
  1808. 'update_post_meta_cache' => false,
  1809. );
  1810. $term_array = array();
  1811. if ( ! is_array( $excluded_terms ) ) {
  1812. if ( ! empty( $excluded_terms ) ) {
  1813. $excluded_terms = explode( ',', $excluded_terms );
  1814. } else {
  1815. $excluded_terms = array();
  1816. }
  1817. }
  1818. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1819. if ( $in_same_term ) {
  1820. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1821. }
  1822. if ( ! empty( $excluded_terms ) ) {
  1823. $excluded_terms = array_map( 'intval', $excluded_terms );
  1824. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1825. $inverse_terms = array();
  1826. foreach ( $excluded_terms as $excluded_term ) {
  1827. $inverse_terms[] = $excluded_term * -1;
  1828. }
  1829. $excluded_terms = $inverse_terms;
  1830. }
  1831. $query_args['tax_query'] = array(
  1832. array(
  1833. 'taxonomy' => $taxonomy,
  1834. 'terms' => array_merge( $term_array, $excluded_terms ),
  1835. ),
  1836. );
  1837. }
  1838. return get_posts( $query_args );
  1839. }
  1840. /**
  1841. * Retrieves the previous post link that is adjacent to the current post.
  1842. *
  1843. * @since 3.7.0
  1844. *
  1845. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1846. * @param string $link Optional. Link permalink format. Default '%title'.
  1847. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1848. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1849. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1850. * @return string The link URL of the previous post in relation to the current post.
  1851. */
  1852. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1853. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
  1854. }
  1855. /**
  1856. * Displays the previous post link that is adjacent to the current post.
  1857. *
  1858. * @since 1.5.0
  1859. *
  1860. * @see get_previous_post_link()
  1861. *
  1862. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1863. * @param string $link Optional. Link permalink format. Default '%title'.
  1864. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1865. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1866. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1867. */
  1868. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1869. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1870. }
  1871. /**
  1872. * Retrieves the next post link that is adjacent to the current post.
  1873. *
  1874. * @since 3.7.0
  1875. *
  1876. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1877. * @param string $link Optional. Link permalink format. Default '%title'.
  1878. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1879. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1880. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1881. * @return string The link URL of the next post in relation to the current post.
  1882. */
  1883. function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1884. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
  1885. }
  1886. /**
  1887. * Displays the next post link that is adjacent to the current post.
  1888. *
  1889. * @since 1.5.0
  1890. * @see get_next_post_link()
  1891. *
  1892. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1893. * @param string $link Optional. Link permalink format. Default '%title'
  1894. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1895. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1896. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1897. */
  1898. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1899. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1900. }
  1901. /**
  1902. * Retrieves the adjacent post link.
  1903. *
  1904. * Can be either next post link or previous.
  1905. *
  1906. * @since 3.7.0
  1907. *
  1908. * @param string $format Link anchor format.
  1909. * @param string $link Link permalink format.
  1910. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1911. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty.
  1912. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1913. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1914. * @return string The link URL of the previous or next post in relation to the current post.
  1915. */
  1916. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1917. if ( $previous && is_attachment() ) {
  1918. $post = get_post( get_post()->post_parent );
  1919. } else {
  1920. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1921. }
  1922. if ( ! $post ) {
  1923. $output = '';
  1924. } else {
  1925. $title = $post->post_title;
  1926. if ( empty( $post->post_title ) ) {
  1927. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1928. }
  1929. /** This filter is documented in wp-includes/post-template.php */
  1930. $title = apply_filters( 'the_title', $title, $post->ID );
  1931. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1932. $rel = $previous ? 'prev' : 'next';
  1933. $string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">';
  1934. $inlink = str_replace( '%title', $title, $link );
  1935. $inlink = str_replace( '%date', $date, $inlink );
  1936. $inlink = $string . $inlink . '</a>';
  1937. $output = str_replace( '%link', $inlink, $format );
  1938. }
  1939. $adjacent = $previous ? 'previous' : 'next';
  1940. /**
  1941. * Filters the adjacent post link.
  1942. *
  1943. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1944. * of adjacency, 'next' or 'previous'.
  1945. *
  1946. * @since 2.6.0
  1947. * @since 4.2.0 Added the `$adjacent` parameter.
  1948. *
  1949. * @param string $output The adjacent post link.
  1950. * @param string $format Link anchor format.
  1951. * @param string $link Link permalink format.
  1952. * @param WP_Post $post The adjacent post.
  1953. * @param string $adjacent Whether the post is previous or next.
  1954. */
  1955. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
  1956. }
  1957. /**
  1958. * Displays the adjacent post link.
  1959. *
  1960. * Can be either next post link or previous.
  1961. *
  1962. * @since 2.5.0
  1963. *
  1964. * @param string $format Link anchor format.
  1965. * @param string $link Link permalink format.
  1966. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1967. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty.
  1968. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1969. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1970. */
  1971. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1972. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
  1973. }
  1974. /**
  1975. * Retrieves the link for a page number.
  1976. *
  1977. * @since 1.5.0
  1978. *
  1979. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1980. *
  1981. * @param int $pagenum Optional. Page number. Default 1.
  1982. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1983. * Otherwise, prepares the URL with esc_url_raw().
  1984. * @return string The link URL for the given page number.
  1985. */
  1986. function get_pagenum_link( $pagenum = 1, $escape = true ) {
  1987. global $wp_rewrite;
  1988. $pagenum = (int) $pagenum;
  1989. $request = remove_query_arg( 'paged' );
  1990. $home_root = parse_url( home_url() );
  1991. $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
  1992. $home_root = preg_quote( $home_root, '|' );
  1993. $request = preg_replace( '|^' . $home_root . '|i', '', $request );
  1994. $request = preg_replace( '|^/+|', '', $request );
  1995. if ( ! $wp_rewrite->using_permalinks() || is_admin() ) {
  1996. $base = trailingslashit( get_bloginfo( 'url' ) );
  1997. if ( $pagenum > 1 ) {
  1998. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1999. } else {
  2000. $result = $base . $request;
  2001. }
  2002. } else {
  2003. $qs_regex = '|\?.*?$|';
  2004. preg_match( $qs_regex, $request, $qs_match );
  2005. if ( ! empty( $qs_match[0] ) ) {
  2006. $query_string = $qs_match[0];
  2007. $request = preg_replace( $qs_regex, '', $request );
  2008. } else {
  2009. $query_string = '';
  2010. }
  2011. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request );
  2012. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request );
  2013. $request = ltrim( $request, '/' );
  2014. $base = trailingslashit( get_bloginfo( 'url' ) );
  2015. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) ) {
  2016. $base .= $wp_rewrite->index . '/';
  2017. }
  2018. if ( $pagenum > 1 ) {
  2019. $request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' );
  2020. }
  2021. $result = $base . $request . $query_string;
  2022. }
  2023. /**
  2024. * Filters the page number link for the current request.
  2025. *
  2026. * @since 2.5.0
  2027. * @since 5.2.0 Added the `$pagenum` argument.
  2028. *
  2029. * @param string $result The page number link.
  2030. * @param int $pagenum The page number.
  2031. */
  2032. $result = apply_filters( 'get_pagenum_link', $result, $pagenum );
  2033. if ( $escape ) {
  2034. return esc_url( $result );
  2035. } else {
  2036. return esc_url_raw( $result );
  2037. }
  2038. }
  2039. /**
  2040. * Retrieves the next posts page link.
  2041. *
  2042. * Backported from 2.1.3 to 2.0.10.
  2043. *
  2044. * @since 2.0.10
  2045. *
  2046. * @global int $paged
  2047. *
  2048. * @param int $max_page Optional. Max pages. Default 0.
  2049. * @return string|void The link URL for next posts page.
  2050. */
  2051. function get_next_posts_page_link( $max_page = 0 ) {
  2052. global $paged;
  2053. if ( ! is_single() ) {
  2054. if ( ! $paged ) {
  2055. $paged = 1;
  2056. }
  2057. $nextpage = intval( $paged ) + 1;
  2058. if ( ! $max_page || $max_page >= $nextpage ) {
  2059. return get_pagenum_link( $nextpage );
  2060. }
  2061. }
  2062. }
  2063. /**
  2064. * Displays or retrieves the next posts page link.
  2065. *
  2066. * @since 0.71
  2067. *
  2068. * @param int $max_page Optional. Max pages. Default 0.
  2069. * @param bool $echo Optional. Whether to echo the link. Default true.
  2070. * @return string|void The link URL for next posts page if `$echo = false`.
  2071. */
  2072. function next_posts( $max_page = 0, $echo = true ) {
  2073. $output = esc_url( get_next_posts_page_link( $max_page ) );
  2074. if ( $echo ) {
  2075. echo $output;
  2076. } else {
  2077. return $output;
  2078. }
  2079. }
  2080. /**
  2081. * Retrieves the next posts page link.
  2082. *
  2083. * @since 2.7.0
  2084. *
  2085. * @global int $paged
  2086. * @global WP_Query $wp_query WordPress Query object.
  2087. *
  2088. * @param string $label Content for link text.
  2089. * @param int $max_page Optional. Max pages. Default 0.
  2090. * @return string|void HTML-formatted next posts page link.
  2091. */
  2092. function get_next_posts_link( $label = null, $max_page = 0 ) {
  2093. global $paged, $wp_query;
  2094. if ( ! $max_page ) {
  2095. $max_page = $wp_query->max_num_pages;
  2096. }
  2097. if ( ! $paged ) {
  2098. $paged = 1;
  2099. }
  2100. $nextpage = intval( $paged ) + 1;
  2101. if ( null === $label ) {
  2102. $label = __( 'Next Page &raquo;' );
  2103. }
  2104. if ( ! is_single() && ( $nextpage <= $max_page ) ) {
  2105. /**
  2106. * Filters the anchor tag attributes for the next posts page link.
  2107. *
  2108. * @since 2.7.0
  2109. *
  2110. * @param string $attributes Attributes for the anchor tag.
  2111. */
  2112. $attr = apply_filters( 'next_posts_link_attributes', '' );
  2113. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2114. }
  2115. }
  2116. /**
  2117. * Displays the next posts page link.
  2118. *
  2119. * @since 0.71
  2120. *
  2121. * @param string $label Content for link text.
  2122. * @param int $max_page Optional. Max pages. Default 0.
  2123. */
  2124. function next_posts_link( $label = null, $max_page = 0 ) {
  2125. echo get_next_posts_link( $label, $max_page );
  2126. }
  2127. /**
  2128. * Retrieves the previous posts page link.
  2129. *
  2130. * Will only return string, if not on a single page or post.
  2131. *
  2132. * Backported to 2.0.10 from 2.1.3.
  2133. *
  2134. * @since 2.0.10
  2135. *
  2136. * @global int $paged
  2137. *
  2138. * @return string|void The link for the previous posts page.
  2139. */
  2140. function get_previous_posts_page_link() {
  2141. global $paged;
  2142. if ( ! is_single() ) {
  2143. $nextpage = intval( $paged ) - 1;
  2144. if ( $nextpage < 1 ) {
  2145. $nextpage = 1;
  2146. }
  2147. return get_pagenum_link( $nextpage );
  2148. }
  2149. }
  2150. /**
  2151. * Displays or retrieves the previous posts page link.
  2152. *
  2153. * @since 0.71
  2154. *
  2155. * @param bool $echo Optional. Whether to echo the link. Default true.
  2156. * @return string|void The previous posts page link if `$echo = false`.
  2157. */
  2158. function previous_posts( $echo = true ) {
  2159. $output = esc_url( get_previous_posts_page_link() );
  2160. if ( $echo ) {
  2161. echo $output;
  2162. } else {
  2163. return $output;
  2164. }
  2165. }
  2166. /**
  2167. * Retrieves the previous posts page link.
  2168. *
  2169. * @since 2.7.0
  2170. *
  2171. * @global int $paged
  2172. *
  2173. * @param string $label Optional. Previous page link text.
  2174. * @return string|void HTML-formatted previous page link.
  2175. */
  2176. function get_previous_posts_link( $label = null ) {
  2177. global $paged;
  2178. if ( null === $label ) {
  2179. $label = __( '&laquo; Previous Page' );
  2180. }
  2181. if ( ! is_single() && $paged > 1 ) {
  2182. /**
  2183. * Filters the anchor tag attributes for the previous posts page link.
  2184. *
  2185. * @since 2.7.0
  2186. *
  2187. * @param string $attributes Attributes for the anchor tag.
  2188. */
  2189. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  2190. return '<a href="' . previous_posts( false ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2191. }
  2192. }
  2193. /**
  2194. * Displays the previous posts page link.
  2195. *
  2196. * @since 0.71
  2197. *
  2198. * @param string $label Optional. Previous page link text.
  2199. */
  2200. function previous_posts_link( $label = null ) {
  2201. echo get_previous_posts_link( $label );
  2202. }
  2203. /**
  2204. * Retrieves the post pages link navigation for previous and next pages.
  2205. *
  2206. * @since 2.8.0
  2207. *
  2208. * @global WP_Query $wp_query WordPress Query object.
  2209. *
  2210. * @param string|array $args {
  2211. * Optional. Arguments to build the post pages link navigation.
  2212. *
  2213. * @type string $sep Separator character. Default '&#8212;'.
  2214. * @type string $prelabel Link text to display for the previous page link.
  2215. * Default '&laquo; Previous Page'.
  2216. * @type string $nxtlabel Link text to display for the next page link.
  2217. * Default 'Next Page &raquo;'.
  2218. * }
  2219. * @return string The posts link navigation.
  2220. */
  2221. function get_posts_nav_link( $args = array() ) {
  2222. global $wp_query;
  2223. $return = '';
  2224. if ( ! is_singular() ) {
  2225. $defaults = array(
  2226. 'sep' => ' &#8212; ',
  2227. 'prelabel' => __( '&laquo; Previous Page' ),
  2228. 'nxtlabel' => __( 'Next Page &raquo;' ),
  2229. );
  2230. $args = wp_parse_args( $args, $defaults );
  2231. $max_num_pages = $wp_query->max_num_pages;
  2232. $paged = get_query_var( 'paged' );
  2233. // Only have sep if there's both prev and next results.
  2234. if ( $paged < 2 || $paged >= $max_num_pages ) {
  2235. $args['sep'] = '';
  2236. }
  2237. if ( $max_num_pages > 1 ) {
  2238. $return = get_previous_posts_link( $args['prelabel'] );
  2239. $return .= preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep'] );
  2240. $return .= get_next_posts_link( $args['nxtlabel'] );
  2241. }
  2242. }
  2243. return $return;
  2244. }
  2245. /**
  2246. * Displays the post pages link navigation for previous and next pages.
  2247. *
  2248. * @since 0.71
  2249. *
  2250. * @param string $sep Optional. Separator for posts navigation links. Default empty.
  2251. * @param string $prelabel Optional. Label for previous pages. Default empty.
  2252. * @param string $nxtlabel Optional Label for next pages. Default empty.
  2253. */
  2254. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  2255. $args = array_filter( compact( 'sep', 'prelabel', 'nxtlabel' ) );
  2256. echo get_posts_nav_link( $args );
  2257. }
  2258. /**
  2259. * Retrieves the navigation to next/previous post, when applicable.
  2260. *
  2261. * @since 4.1.0
  2262. * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
  2263. * @since 5.3.0 Added the `aria_label` parameter.
  2264. *
  2265. * @param array $args {
  2266. * Optional. Default post navigation arguments. Default empty array.
  2267. *
  2268. * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
  2269. * @type string $next_text Anchor text to display in the next post link. Default '%title'.
  2270. * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
  2271. * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
  2272. * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
  2273. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Post navigation'.
  2274. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2275. * }
  2276. * @return string Markup for post links.
  2277. */
  2278. function get_the_post_navigation( $args = array() ) {
  2279. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2280. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2281. $args['aria_label'] = $args['screen_reader_text'];
  2282. }
  2283. $args = wp_parse_args(
  2284. $args,
  2285. array(
  2286. 'prev_text' => '%title',
  2287. 'next_text' => '%title',
  2288. 'in_same_term' => false,
  2289. 'excluded_terms' => '',
  2290. 'taxonomy' => 'category',
  2291. 'screen_reader_text' => __( 'Post navigation' ),
  2292. 'aria_label' => __( 'Posts' ),
  2293. )
  2294. );
  2295. $navigation = '';
  2296. $previous = get_previous_post_link(
  2297. '<div class="nav-previous">%link</div>',
  2298. $args['prev_text'],
  2299. $args['in_same_term'],
  2300. $args['excluded_terms'],
  2301. $args['taxonomy']
  2302. );
  2303. $next = get_next_post_link(
  2304. '<div class="nav-next">%link</div>',
  2305. $args['next_text'],
  2306. $args['in_same_term'],
  2307. $args['excluded_terms'],
  2308. $args['taxonomy']
  2309. );
  2310. // Only add markup if there's somewhere to navigate to.
  2311. if ( $previous || $next ) {
  2312. $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'], $args['aria_label'] );
  2313. }
  2314. return $navigation;
  2315. }
  2316. /**
  2317. * Displays the navigation to next/previous post, when applicable.
  2318. *
  2319. * @since 4.1.0
  2320. *
  2321. * @param array $args Optional. See get_the_post_navigation() for available arguments.
  2322. * Default empty array.
  2323. */
  2324. function the_post_navigation( $args = array() ) {
  2325. echo get_the_post_navigation( $args );
  2326. }
  2327. /**
  2328. * Returns the navigation to next/previous set of posts, when applicable.
  2329. *
  2330. * @since 4.1.0
  2331. * @since 5.3.0 Added the `aria_label` parameter.
  2332. *
  2333. * @global WP_Query $wp_query WordPress Query object.
  2334. *
  2335. * @param array $args {
  2336. * Optional. Default posts navigation arguments. Default empty array.
  2337. *
  2338. * @type string $prev_text Anchor text to display in the previous posts link.
  2339. * Default 'Older posts'.
  2340. * @type string $next_text Anchor text to display in the next posts link.
  2341. * Default 'Newer posts'.
  2342. * @type string $screen_reader_text Screen reader text for the nav element.
  2343. * Default 'Posts navigation'.
  2344. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2345. * }
  2346. * @return string Markup for posts links.
  2347. */
  2348. function get_the_posts_navigation( $args = array() ) {
  2349. $navigation = '';
  2350. // Don't print empty markup if there's only one page.
  2351. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  2352. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2353. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2354. $args['aria_label'] = $args['screen_reader_text'];
  2355. }
  2356. $args = wp_parse_args(
  2357. $args,
  2358. array(
  2359. 'prev_text' => __( 'Older posts' ),
  2360. 'next_text' => __( 'Newer posts' ),
  2361. 'screen_reader_text' => __( 'Posts navigation' ),
  2362. 'aria_label' => __( 'Posts' ),
  2363. )
  2364. );
  2365. $next_link = get_previous_posts_link( $args['next_text'] );
  2366. $prev_link = get_next_posts_link( $args['prev_text'] );
  2367. if ( $prev_link ) {
  2368. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
  2369. }
  2370. if ( $next_link ) {
  2371. $navigation .= '<div class="nav-next">' . $next_link . '</div>';
  2372. }
  2373. $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'], $args['aria_label'] );
  2374. }
  2375. return $navigation;
  2376. }
  2377. /**
  2378. * Displays the navigation to next/previous set of posts, when applicable.
  2379. *
  2380. * @since 4.1.0
  2381. *
  2382. * @param array $args Optional. See get_the_posts_navigation() for available arguments.
  2383. * Default empty array.
  2384. */
  2385. function the_posts_navigation( $args = array() ) {
  2386. echo get_the_posts_navigation( $args );
  2387. }
  2388. /**
  2389. * Retrieves a paginated navigation to next/previous set of posts, when applicable.
  2390. *
  2391. * @since 4.1.0
  2392. * @since 5.3.0 Added the `aria_label` parameter.
  2393. *
  2394. * @param array $args {
  2395. * Optional. Default pagination arguments, see paginate_links().
  2396. *
  2397. * @type string $screen_reader_text Screen reader text for navigation element.
  2398. * Default 'Posts navigation'.
  2399. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2400. * }
  2401. * @return string Markup for pagination links.
  2402. */
  2403. function get_the_posts_pagination( $args = array() ) {
  2404. $navigation = '';
  2405. // Don't print empty markup if there's only one page.
  2406. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  2407. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2408. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2409. $args['aria_label'] = $args['screen_reader_text'];
  2410. }
  2411. $args = wp_parse_args(
  2412. $args,
  2413. array(
  2414. 'mid_size' => 1,
  2415. 'prev_text' => _x( 'Previous', 'previous set of posts' ),
  2416. 'next_text' => _x( 'Next', 'next set of posts' ),
  2417. 'screen_reader_text' => __( 'Posts navigation' ),
  2418. 'aria_label' => __( 'Posts' ),
  2419. )
  2420. );
  2421. // Make sure we get a string back. Plain is the next best thing.
  2422. if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
  2423. $args['type'] = 'plain';
  2424. }
  2425. // Set up paginated links.
  2426. $links = paginate_links( $args );
  2427. if ( $links ) {
  2428. $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'], $args['aria_label'] );
  2429. }
  2430. }
  2431. return $navigation;
  2432. }
  2433. /**
  2434. * Displays a paginated navigation to next/previous set of posts, when applicable.
  2435. *
  2436. * @since 4.1.0
  2437. *
  2438. * @param array $args Optional. See get_the_posts_pagination() for available arguments.
  2439. * Default empty array.
  2440. */
  2441. function the_posts_pagination( $args = array() ) {
  2442. echo get_the_posts_pagination( $args );
  2443. }
  2444. /**
  2445. * Wraps passed links in navigational markup.
  2446. *
  2447. * @since 4.1.0
  2448. * @since 5.3.0 Added the `aria_label` parameter.
  2449. * @access private
  2450. *
  2451. * @param string $links Navigational links.
  2452. * @param string $class Optional. Custom class for the nav element. Default: 'posts-navigation'.
  2453. * @param string $screen_reader_text Optional. Screen reader text for the nav element. Default: 'Posts navigation'.
  2454. * @param string $aria_label Optional. ARIA label for the nav element. Default: same value as $screen_reader_text.
  2455. * @return string Navigation template tag.
  2456. */
  2457. function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '', $aria_label = '' ) {
  2458. if ( empty( $screen_reader_text ) ) {
  2459. $screen_reader_text = __( 'Posts navigation' );
  2460. }
  2461. if ( empty( $aria_label ) ) {
  2462. $aria_label = $screen_reader_text;
  2463. }
  2464. $template = '
  2465. <nav class="navigation %1$s" role="navigation" aria-label="%4$s">
  2466. <h2 class="screen-reader-text">%2$s</h2>
  2467. <div class="nav-links">%3$s</div>
  2468. </nav>';
  2469. /**
  2470. * Filters the navigation markup template.
  2471. *
  2472. * Note: The filtered template HTML must contain specifiers for the navigation
  2473. * class (%1$s), the screen-reader-text value (%2$s), placement of the navigation
  2474. * links (%3$s), and ARIA label text if screen-reader-text does not fit that (%4$s):
  2475. *
  2476. * <nav class="navigation %1$s" role="navigation" aria-label="%4$s">
  2477. * <h2 class="screen-reader-text">%2$s</h2>
  2478. * <div class="nav-links">%3$s</div>
  2479. * </nav>
  2480. *
  2481. * @since 4.4.0
  2482. *
  2483. * @param string $template The default template.
  2484. * @param string $class The class passed by the calling function.
  2485. * @return string Navigation template.
  2486. */
  2487. $template = apply_filters( 'navigation_markup_template', $template, $class );
  2488. return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links, esc_html( $aria_label ) );
  2489. }
  2490. /**
  2491. * Retrieves the comments page number link.
  2492. *
  2493. * @since 2.7.0
  2494. *
  2495. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  2496. *
  2497. * @param int $pagenum Optional. Page number. Default 1.
  2498. * @param int $max_page Optional. The maximum number of comment pages. Default 0.
  2499. * @return string The comments page number link URL.
  2500. */
  2501. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  2502. global $wp_rewrite;
  2503. $pagenum = (int) $pagenum;
  2504. $result = get_permalink();
  2505. if ( 'newest' == get_option( 'default_comments_page' ) ) {
  2506. if ( $pagenum != $max_page ) {
  2507. if ( $wp_rewrite->using_permalinks() ) {
  2508. $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
  2509. } else {
  2510. $result = add_query_arg( 'cpage', $pagenum, $result );
  2511. }
  2512. }
  2513. } elseif ( $pagenum > 1 ) {
  2514. if ( $wp_rewrite->using_permalinks() ) {
  2515. $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
  2516. } else {
  2517. $result = add_query_arg( 'cpage', $pagenum, $result );
  2518. }
  2519. }
  2520. $result .= '#comments';
  2521. /**
  2522. * Filters the comments page number link for the current request.
  2523. *
  2524. * @since 2.7.0
  2525. *
  2526. * @param string $result The comments page number link.
  2527. */
  2528. return apply_filters( 'get_comments_pagenum_link', $result );
  2529. }
  2530. /**
  2531. * Retrieves the link to the next comments page.
  2532. *
  2533. * @since 2.7.1
  2534. *
  2535. * @global WP_Query $wp_query WordPress Query object.
  2536. *
  2537. * @param string $label Optional. Label for link text. Default empty.
  2538. * @param int $max_page Optional. Max page. Default 0.
  2539. * @return string|void HTML-formatted link for the next page of comments.
  2540. */
  2541. function get_next_comments_link( $label = '', $max_page = 0 ) {
  2542. global $wp_query;
  2543. if ( ! is_singular() ) {
  2544. return;
  2545. }
  2546. $page = get_query_var( 'cpage' );
  2547. if ( ! $page ) {
  2548. $page = 1;
  2549. }
  2550. $nextpage = intval( $page ) + 1;
  2551. if ( empty( $max_page ) ) {
  2552. $max_page = $wp_query->max_num_comment_pages;
  2553. }
  2554. if ( empty( $max_page ) ) {
  2555. $max_page = get_comment_pages_count();
  2556. }
  2557. if ( $nextpage > $max_page ) {
  2558. return;
  2559. }
  2560. if ( empty( $label ) ) {
  2561. $label = __( 'Newer Comments &raquo;' );
  2562. }
  2563. /**
  2564. * Filters the anchor tag attributes for the next comments page link.
  2565. *
  2566. * @since 2.7.0
  2567. *
  2568. * @param string $attributes Attributes for the anchor tag.
  2569. */
  2570. return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>' . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2571. }
  2572. /**
  2573. * Displays the link to the next comments page.
  2574. *
  2575. * @since 2.7.0
  2576. *
  2577. * @param string $label Optional. Label for link text. Default empty.
  2578. * @param int $max_page Optional. Max page. Default 0.
  2579. */
  2580. function next_comments_link( $label = '', $max_page = 0 ) {
  2581. echo get_next_comments_link( $label, $max_page );
  2582. }
  2583. /**
  2584. * Retrieves the link to the previous comments page.
  2585. *
  2586. * @since 2.7.1
  2587. *
  2588. * @param string $label Optional. Label for comments link text. Default empty.
  2589. * @return string|void HTML-formatted link for the previous page of comments.
  2590. */
  2591. function get_previous_comments_link( $label = '' ) {
  2592. if ( ! is_singular() ) {
  2593. return;
  2594. }
  2595. $page = get_query_var( 'cpage' );
  2596. if ( intval( $page ) <= 1 ) {
  2597. return;
  2598. }
  2599. $prevpage = intval( $page ) - 1;
  2600. if ( empty( $label ) ) {
  2601. $label = __( '&laquo; Older Comments' );
  2602. }
  2603. /**
  2604. * Filters the anchor tag attributes for the previous comments page link.
  2605. *
  2606. * @since 2.7.0
  2607. *
  2608. * @param string $attributes Attributes for the anchor tag.
  2609. */
  2610. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2611. }
  2612. /**
  2613. * Displays the link to the previous comments page.
  2614. *
  2615. * @since 2.7.0
  2616. *
  2617. * @param string $label Optional. Label for comments link text. Default empty.
  2618. */
  2619. function previous_comments_link( $label = '' ) {
  2620. echo get_previous_comments_link( $label );
  2621. }
  2622. /**
  2623. * Displays or retrieves pagination links for the comments on the current post.
  2624. *
  2625. * @see paginate_links()
  2626. * @since 2.7.0
  2627. *
  2628. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  2629. *
  2630. * @param string|array $args Optional args. See paginate_links(). Default empty array.
  2631. * @return void|string|array Void if 'echo' argument is true and 'type' is not an array,
  2632. * or if the query is not for an existing single post of any post type.
  2633. * Otherwise, markup for comment page links or array of comment page links,
  2634. * depending on 'type' argument.
  2635. */
  2636. function paginate_comments_links( $args = array() ) {
  2637. global $wp_rewrite;
  2638. if ( ! is_singular() ) {
  2639. return;
  2640. }
  2641. $page = get_query_var( 'cpage' );
  2642. if ( ! $page ) {
  2643. $page = 1;
  2644. }
  2645. $max_page = get_comment_pages_count();
  2646. $defaults = array(
  2647. 'base' => add_query_arg( 'cpage', '%#%' ),
  2648. 'format' => '',
  2649. 'total' => $max_page,
  2650. 'current' => $page,
  2651. 'echo' => true,
  2652. 'type' => 'plain',
  2653. 'add_fragment' => '#comments',
  2654. );
  2655. if ( $wp_rewrite->using_permalinks() ) {
  2656. $defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' );
  2657. }
  2658. $args = wp_parse_args( $args, $defaults );
  2659. $page_links = paginate_links( $args );
  2660. if ( $args['echo'] && 'array' !== $args['type'] ) {
  2661. echo $page_links;
  2662. } else {
  2663. return $page_links;
  2664. }
  2665. }
  2666. /**
  2667. * Retrieves navigation to next/previous set of comments, when applicable.
  2668. *
  2669. * @since 4.4.0
  2670. * @since 5.3.0 Added the `aria_label` parameter.
  2671. *
  2672. * @param array $args {
  2673. * Optional. Default comments navigation arguments.
  2674. *
  2675. * @type string $prev_text Anchor text to display in the previous comments link.
  2676. * Default 'Older comments'.
  2677. * @type string $next_text Anchor text to display in the next comments link.
  2678. * Default 'Newer comments'.
  2679. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'.
  2680. * @type string $aria_label ARIA label text for the nav element. Default 'Comments'.
  2681. * }
  2682. * @return string Markup for comments links.
  2683. */
  2684. function get_the_comments_navigation( $args = array() ) {
  2685. $navigation = '';
  2686. // Are there comments to navigate through?
  2687. if ( get_comment_pages_count() > 1 ) {
  2688. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2689. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2690. $args['aria_label'] = $args['screen_reader_text'];
  2691. }
  2692. $args = wp_parse_args(
  2693. $args,
  2694. array(
  2695. 'prev_text' => __( 'Older comments' ),
  2696. 'next_text' => __( 'Newer comments' ),
  2697. 'screen_reader_text' => __( 'Comments navigation' ),
  2698. 'aria_label' => __( 'Comments' ),
  2699. )
  2700. );
  2701. $prev_link = get_previous_comments_link( $args['prev_text'] );
  2702. $next_link = get_next_comments_link( $args['next_text'] );
  2703. if ( $prev_link ) {
  2704. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
  2705. }
  2706. if ( $next_link ) {
  2707. $navigation .= '<div class="nav-next">' . $next_link . '</div>';
  2708. }
  2709. $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'], $args['aria_label'] );
  2710. }
  2711. return $navigation;
  2712. }
  2713. /**
  2714. * Displays navigation to next/previous set of comments, when applicable.
  2715. *
  2716. * @since 4.4.0
  2717. *
  2718. * @param array $args See get_the_comments_navigation() for available arguments. Default empty array.
  2719. */
  2720. function the_comments_navigation( $args = array() ) {
  2721. echo get_the_comments_navigation( $args );
  2722. }
  2723. /**
  2724. * Retrieves a paginated navigation to next/previous set of comments, when applicable.
  2725. *
  2726. * @since 4.4.0
  2727. * @since 5.3.0 Added the `aria_label` parameter.
  2728. *
  2729. * @see paginate_comments_links()
  2730. *
  2731. * @param array $args {
  2732. * Optional. Default pagination arguments.
  2733. *
  2734. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'.
  2735. * @type string $aria_label ARIA label text for the nav element. Default 'Comments'.
  2736. * }
  2737. * @return string Markup for pagination links.
  2738. */
  2739. function get_the_comments_pagination( $args = array() ) {
  2740. $navigation = '';
  2741. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2742. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2743. $args['aria_label'] = $args['screen_reader_text'];
  2744. }
  2745. $args = wp_parse_args(
  2746. $args,
  2747. array(
  2748. 'screen_reader_text' => __( 'Comments navigation' ),
  2749. 'aria_label' => __( 'Comments' ),
  2750. )
  2751. );
  2752. $args['echo'] = false;
  2753. // Make sure we get a string back. Plain is the next best thing.
  2754. if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
  2755. $args['type'] = 'plain';
  2756. }
  2757. $links = paginate_comments_links( $args );
  2758. if ( $links ) {
  2759. $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'], $args['aria_label'] );
  2760. }
  2761. return $navigation;
  2762. }
  2763. /**
  2764. * Displays a paginated navigation to next/previous set of comments, when applicable.
  2765. *
  2766. * @since 4.4.0
  2767. *
  2768. * @param array $args See get_the_comments_pagination() for available arguments. Default empty array.
  2769. */
  2770. function the_comments_pagination( $args = array() ) {
  2771. echo get_the_comments_pagination( $args );
  2772. }
  2773. /**
  2774. * Retrieves the URL for the current site where the front end is accessible.
  2775. *
  2776. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
  2777. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
  2778. * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
  2779. *
  2780. * @since 3.0.0
  2781. *
  2782. * @param string $path Optional. Path relative to the home URL. Default empty.
  2783. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
  2784. * 'http', 'https', 'relative', 'rest', or null. Default null.
  2785. * @return string Home URL link with optional path appended.
  2786. */
  2787. function home_url( $path = '', $scheme = null ) {
  2788. return get_home_url( null, $path, $scheme );
  2789. }
  2790. /**
  2791. * Retrieves the URL for a given site where the front end is accessible.
  2792. *
  2793. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
  2794. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
  2795. * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
  2796. *
  2797. * @since 3.0.0
  2798. *
  2799. * @global string $pagenow
  2800. *
  2801. * @param int $blog_id Optional. Site ID. Default null (current site).
  2802. * @param string $path Optional. Path relative to the home URL. Default empty.
  2803. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
  2804. * 'http', 'https', 'relative', 'rest', or null. Default null.
  2805. * @return string Home URL link with optional path appended.
  2806. */
  2807. function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  2808. global $pagenow;
  2809. $orig_scheme = $scheme;
  2810. if ( empty( $blog_id ) || ! is_multisite() ) {
  2811. $url = get_option( 'home' );
  2812. } else {
  2813. switch_to_blog( $blog_id );
  2814. $url = get_option( 'home' );
  2815. restore_current_blog();
  2816. }
  2817. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
  2818. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
  2819. $scheme = 'https';
  2820. } else {
  2821. $scheme = parse_url( $url, PHP_URL_SCHEME );
  2822. }
  2823. }
  2824. $url = set_url_scheme( $url, $scheme );
  2825. if ( $path && is_string( $path ) ) {
  2826. $url .= '/' . ltrim( $path, '/' );
  2827. }
  2828. /**
  2829. * Filters the home URL.
  2830. *
  2831. * @since 3.0.0
  2832. *
  2833. * @param string $url The complete home URL including scheme and path.
  2834. * @param string $path Path relative to the home URL. Blank string if no path is specified.
  2835. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
  2836. * 'relative', 'rest', or null.
  2837. * @param int|null $blog_id Site ID, or null for the current site.
  2838. */
  2839. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
  2840. }
  2841. /**
  2842. * Retrieves the URL for the current site where WordPress application files
  2843. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  2844. *
  2845. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2846. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2847. * overridden.
  2848. *
  2849. * @since 3.0.0
  2850. *
  2851. * @param string $path Optional. Path relative to the site URL. Default empty.
  2852. * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme().
  2853. * @return string Site URL link with optional path appended.
  2854. */
  2855. function site_url( $path = '', $scheme = null ) {
  2856. return get_site_url( null, $path, $scheme );
  2857. }
  2858. /**
  2859. * Retrieves the URL for a given site where WordPress application files
  2860. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  2861. *
  2862. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2863. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
  2864. * `is_ssl()` is overridden.
  2865. *
  2866. * @since 3.0.0
  2867. *
  2868. * @param int $blog_id Optional. Site ID. Default null (current site).
  2869. * @param string $path Optional. Path relative to the site URL. Default empty.
  2870. * @param string $scheme Optional. Scheme to give the site URL context. Accepts
  2871. * 'http', 'https', 'login', 'login_post', 'admin', or
  2872. * 'relative'. Default null.
  2873. * @return string Site URL link with optional path appended.
  2874. */
  2875. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
  2876. if ( empty( $blog_id ) || ! is_multisite() ) {
  2877. $url = get_option( 'siteurl' );
  2878. } else {
  2879. switch_to_blog( $blog_id );
  2880. $url = get_option( 'siteurl' );
  2881. restore_current_blog();
  2882. }
  2883. $url = set_url_scheme( $url, $scheme );
  2884. if ( $path && is_string( $path ) ) {
  2885. $url .= '/' . ltrim( $path, '/' );
  2886. }
  2887. /**
  2888. * Filters the site URL.
  2889. *
  2890. * @since 2.7.0
  2891. *
  2892. * @param string $url The complete site URL including scheme and path.
  2893. * @param string $path Path relative to the site URL. Blank string if no path is specified.
  2894. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
  2895. * 'login_post', 'admin', 'relative' or null.
  2896. * @param int|null $blog_id Site ID, or null for the current site.
  2897. */
  2898. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
  2899. }
  2900. /**
  2901. * Retrieves the URL to the admin area for the current site.
  2902. *
  2903. * @since 2.6.0
  2904. *
  2905. * @param string $path Optional path relative to the admin URL.
  2906. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  2907. * 'http' or 'https' can be passed to force those schemes.
  2908. * @return string Admin URL link with optional path appended.
  2909. */
  2910. function admin_url( $path = '', $scheme = 'admin' ) {
  2911. return get_admin_url( null, $path, $scheme );
  2912. }
  2913. /**
  2914. * Retrieves the URL to the admin area for a given site.
  2915. *
  2916. * @since 3.0.0
  2917. *
  2918. * @param int $blog_id Optional. Site ID. Default null (current site).
  2919. * @param string $path Optional. Path relative to the admin URL. Default empty.
  2920. * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
  2921. * to force those schemes. Default 'admin', which obeys
  2922. * force_ssl_admin() and is_ssl().
  2923. * @return string Admin URL link with optional path appended.
  2924. */
  2925. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
  2926. $url = get_site_url( $blog_id, 'wp-admin/', $scheme );
  2927. if ( $path && is_string( $path ) ) {
  2928. $url .= ltrim( $path, '/' );
  2929. }
  2930. /**
  2931. * Filters the admin area URL.
  2932. *
  2933. * @since 2.8.0
  2934. *
  2935. * @param string $url The complete admin area URL including scheme and path.
  2936. * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
  2937. * @param int|null $blog_id Site ID, or null for the current site.
  2938. */
  2939. return apply_filters( 'admin_url', $url, $path, $blog_id );
  2940. }
  2941. /**
  2942. * Retrieves the URL to the includes directory.
  2943. *
  2944. * @since 2.6.0
  2945. *
  2946. * @param string $path Optional. Path relative to the includes URL. Default empty.
  2947. * @param string $scheme Optional. Scheme to give the includes URL context. Accepts
  2948. * 'http', 'https', or 'relative'. Default null.
  2949. * @return string Includes URL link with optional path appended.
  2950. */
  2951. function includes_url( $path = '', $scheme = null ) {
  2952. $url = site_url( '/' . WPINC . '/', $scheme );
  2953. if ( $path && is_string( $path ) ) {
  2954. $url .= ltrim( $path, '/' );
  2955. }
  2956. /**
  2957. * Filters the URL to the includes directory.
  2958. *
  2959. * @since 2.8.0
  2960. *
  2961. * @param string $url The complete URL to the includes directory including scheme and path.
  2962. * @param string $path Path relative to the URL to the wp-includes directory. Blank string
  2963. * if no path is specified.
  2964. */
  2965. return apply_filters( 'includes_url', $url, $path );
  2966. }
  2967. /**
  2968. * Retrieves the URL to the content directory.
  2969. *
  2970. * @since 2.6.0
  2971. *
  2972. * @param string $path Optional. Path relative to the content URL. Default empty.
  2973. * @return string Content URL link with optional path appended.
  2974. */
  2975. function content_url( $path = '' ) {
  2976. $url = set_url_scheme( WP_CONTENT_URL );
  2977. if ( $path && is_string( $path ) ) {
  2978. $url .= '/' . ltrim( $path, '/' );
  2979. }
  2980. /**
  2981. * Filters the URL to the content directory.
  2982. *
  2983. * @since 2.8.0
  2984. *
  2985. * @param string $url The complete URL to the content directory including scheme and path.
  2986. * @param string $path Path relative to the URL to the content directory. Blank string
  2987. * if no path is specified.
  2988. */
  2989. return apply_filters( 'content_url', $url, $path );
  2990. }
  2991. /**
  2992. * Retrieves a URL within the plugins or mu-plugins directory.
  2993. *
  2994. * Defaults to the plugins directory URL if no arguments are supplied.
  2995. *
  2996. * @since 2.6.0
  2997. *
  2998. * @param string $path Optional. Extra path appended to the end of the URL, including
  2999. * the relative directory if $plugin is supplied. Default empty.
  3000. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
  3001. * The URL will be relative to its directory. Default empty.
  3002. * Typically this is done by passing `__FILE__` as the argument.
  3003. * @return string Plugins URL link with optional paths appended.
  3004. */
  3005. function plugins_url( $path = '', $plugin = '' ) {
  3006. $path = wp_normalize_path( $path );
  3007. $plugin = wp_normalize_path( $plugin );
  3008. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
  3009. if ( ! empty( $plugin ) && 0 === strpos( $plugin, $mu_plugin_dir ) ) {
  3010. $url = WPMU_PLUGIN_URL;
  3011. } else {
  3012. $url = WP_PLUGIN_URL;
  3013. }
  3014. $url = set_url_scheme( $url );
  3015. if ( ! empty( $plugin ) && is_string( $plugin ) ) {
  3016. $folder = dirname( plugin_basename( $plugin ) );
  3017. if ( '.' != $folder ) {
  3018. $url .= '/' . ltrim( $folder, '/' );
  3019. }
  3020. }
  3021. if ( $path && is_string( $path ) ) {
  3022. $url .= '/' . ltrim( $path, '/' );
  3023. }
  3024. /**
  3025. * Filters the URL to the plugins directory.
  3026. *
  3027. * @since 2.8.0
  3028. *
  3029. * @param string $url The complete URL to the plugins directory including scheme and path.
  3030. * @param string $path Path relative to the URL to the plugins directory. Blank string
  3031. * if no path is specified.
  3032. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
  3033. * is specified.
  3034. */
  3035. return apply_filters( 'plugins_url', $url, $path, $plugin );
  3036. }
  3037. /**
  3038. * Retrieves the site URL for the current network.
  3039. *
  3040. * Returns the site URL with the appropriate protocol, 'https' if
  3041. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  3042. * overridden.
  3043. *
  3044. * @since 3.0.0
  3045. *
  3046. * @see set_url_scheme()
  3047. *
  3048. * @param string $path Optional. Path relative to the site URL. Default empty.
  3049. * @param string $scheme Optional. Scheme to give the site URL context. Accepts
  3050. * 'http', 'https', or 'relative'. Default null.
  3051. * @return string Site URL link with optional path appended.
  3052. */
  3053. function network_site_url( $path = '', $scheme = null ) {
  3054. if ( ! is_multisite() ) {
  3055. return site_url( $path, $scheme );
  3056. }
  3057. $current_network = get_network();
  3058. if ( 'relative' == $scheme ) {
  3059. $url = $current_network->path;
  3060. } else {
  3061. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
  3062. }
  3063. if ( $path && is_string( $path ) ) {
  3064. $url .= ltrim( $path, '/' );
  3065. }
  3066. /**
  3067. * Filters the network site URL.
  3068. *
  3069. * @since 3.0.0
  3070. *
  3071. * @param string $url The complete network site URL including scheme and path.
  3072. * @param string $path Path relative to the network site URL. Blank string if
  3073. * no path is specified.
  3074. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
  3075. * 'relative' or null.
  3076. */
  3077. return apply_filters( 'network_site_url', $url, $path, $scheme );
  3078. }
  3079. /**
  3080. * Retrieves the home URL for the current network.
  3081. *
  3082. * Returns the home URL with the appropriate protocol, 'https' is_ssl()
  3083. * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
  3084. * overridden.
  3085. *
  3086. * @since 3.0.0
  3087. *
  3088. * @param string $path Optional. Path relative to the home URL. Default empty.
  3089. * @param string $scheme Optional. Scheme to give the home URL context. Accepts
  3090. * 'http', 'https', or 'relative'. Default null.
  3091. * @return string Home URL link with optional path appended.
  3092. */
  3093. function network_home_url( $path = '', $scheme = null ) {
  3094. if ( ! is_multisite() ) {
  3095. return home_url( $path, $scheme );
  3096. }
  3097. $current_network = get_network();
  3098. $orig_scheme = $scheme;
  3099. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
  3100. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
  3101. }
  3102. if ( 'relative' == $scheme ) {
  3103. $url = $current_network->path;
  3104. } else {
  3105. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
  3106. }
  3107. if ( $path && is_string( $path ) ) {
  3108. $url .= ltrim( $path, '/' );
  3109. }
  3110. /**
  3111. * Filters the network home URL.
  3112. *
  3113. * @since 3.0.0
  3114. *
  3115. * @param string $url The complete network home URL including scheme and path.
  3116. * @param string $path Path relative to the network home URL. Blank string
  3117. * if no path is specified.
  3118. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
  3119. * 'relative' or null.
  3120. */
  3121. return apply_filters( 'network_home_url', $url, $path, $orig_scheme );
  3122. }
  3123. /**
  3124. * Retrieves the URL to the admin area for the network.
  3125. *
  3126. * @since 3.0.0
  3127. *
  3128. * @param string $path Optional path relative to the admin URL. Default empty.
  3129. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3130. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3131. * @return string Admin URL link with optional path appended.
  3132. */
  3133. function network_admin_url( $path = '', $scheme = 'admin' ) {
  3134. if ( ! is_multisite() ) {
  3135. return admin_url( $path, $scheme );
  3136. }
  3137. $url = network_site_url( 'wp-admin/network/', $scheme );
  3138. if ( $path && is_string( $path ) ) {
  3139. $url .= ltrim( $path, '/' );
  3140. }
  3141. /**
  3142. * Filters the network admin URL.
  3143. *
  3144. * @since 3.0.0
  3145. *
  3146. * @param string $url The complete network admin URL including scheme and path.
  3147. * @param string $path Path relative to the network admin URL. Blank string if
  3148. * no path is specified.
  3149. */
  3150. return apply_filters( 'network_admin_url', $url, $path );
  3151. }
  3152. /**
  3153. * Retrieves the URL to the admin area for the current user.
  3154. *
  3155. * @since 3.0.0
  3156. *
  3157. * @param string $path Optional. Path relative to the admin URL. Default empty.
  3158. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3159. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3160. * @return string Admin URL link with optional path appended.
  3161. */
  3162. function user_admin_url( $path = '', $scheme = 'admin' ) {
  3163. $url = network_site_url( 'wp-admin/user/', $scheme );
  3164. if ( $path && is_string( $path ) ) {
  3165. $url .= ltrim( $path, '/' );
  3166. }
  3167. /**
  3168. * Filters the user admin URL for the current user.
  3169. *
  3170. * @since 3.1.0
  3171. *
  3172. * @param string $url The complete URL including scheme and path.
  3173. * @param string $path Path relative to the URL. Blank string if
  3174. * no path is specified.
  3175. */
  3176. return apply_filters( 'user_admin_url', $url, $path );
  3177. }
  3178. /**
  3179. * Retrieves the URL to the admin area for either the current site or the network depending on context.
  3180. *
  3181. * @since 3.1.0
  3182. *
  3183. * @param string $path Optional. Path relative to the admin URL. Default empty.
  3184. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3185. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3186. * @return string Admin URL link with optional path appended.
  3187. */
  3188. function self_admin_url( $path = '', $scheme = 'admin' ) {
  3189. if ( is_network_admin() ) {
  3190. $url = network_admin_url( $path, $scheme );
  3191. } elseif ( is_user_admin() ) {
  3192. $url = user_admin_url( $path, $scheme );
  3193. } else {
  3194. $url = admin_url( $path, $scheme );
  3195. }
  3196. /**
  3197. * Filters the admin URL for the current site or network depending on context.
  3198. *
  3199. * @since 4.9.0
  3200. *
  3201. * @param string $url The complete URL including scheme and path.
  3202. * @param string $path Path relative to the URL. Blank string if no path is specified.
  3203. * @param string $scheme The scheme to use.
  3204. */
  3205. return apply_filters( 'self_admin_url', $url, $path, $scheme );
  3206. }
  3207. /**
  3208. * Sets the scheme for a URL.
  3209. *
  3210. * @since 3.4.0
  3211. * @since 4.4.0 The 'rest' scheme was added.
  3212. *
  3213. * @param string $url Absolute URL that includes a scheme
  3214. * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
  3215. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
  3216. * @return string $url URL with chosen scheme.
  3217. */
  3218. function set_url_scheme( $url, $scheme = null ) {
  3219. $orig_scheme = $scheme;
  3220. if ( ! $scheme ) {
  3221. $scheme = is_ssl() ? 'https' : 'http';
  3222. } elseif ( 'admin' === $scheme || 'login' === $scheme || 'login_post' === $scheme || 'rpc' === $scheme ) {
  3223. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
  3224. } elseif ( 'http' !== $scheme && 'https' !== $scheme && 'relative' !== $scheme ) {
  3225. $scheme = is_ssl() ? 'https' : 'http';
  3226. }
  3227. $url = trim( $url );
  3228. if ( substr( $url, 0, 2 ) === '//' ) {
  3229. $url = 'http:' . $url;
  3230. }
  3231. if ( 'relative' == $scheme ) {
  3232. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
  3233. if ( '' !== $url && '/' === $url[0] ) {
  3234. $url = '/' . ltrim( $url, "/ \t\n\r\0\x0B" );
  3235. }
  3236. } else {
  3237. $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
  3238. }
  3239. /**
  3240. * Filters the resulting URL after setting the scheme.
  3241. *
  3242. * @since 3.4.0
  3243. *
  3244. * @param string $url The complete URL including scheme and path.
  3245. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
  3246. * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
  3247. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
  3248. */
  3249. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
  3250. }
  3251. /**
  3252. * Retrieves the URL to the user's dashboard.
  3253. *
  3254. * If a user does not belong to any site, the global user dashboard is used. If the user
  3255. * belongs to the current site, the dashboard for the current site is returned. If the user
  3256. * cannot edit the current site, the dashboard to the user's primary site is returned.
  3257. *
  3258. * @since 3.1.0
  3259. *
  3260. * @param int $user_id Optional. User ID. Defaults to current user.
  3261. * @param string $path Optional path relative to the dashboard. Use only paths known to
  3262. * both site and user admins. Default empty.
  3263. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3264. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3265. * @return string Dashboard URL link with optional path appended.
  3266. */
  3267. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
  3268. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  3269. $blogs = get_blogs_of_user( $user_id );
  3270. if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
  3271. $url = user_admin_url( $path, $scheme );
  3272. } elseif ( ! is_multisite() ) {
  3273. $url = admin_url( $path, $scheme );
  3274. } else {
  3275. $current_blog = get_current_blog_id();
  3276. if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) {
  3277. $url = admin_url( $path, $scheme );
  3278. } else {
  3279. $active = get_active_blog_for_user( $user_id );
  3280. if ( $active ) {
  3281. $url = get_admin_url( $active->blog_id, $path, $scheme );
  3282. } else {
  3283. $url = user_admin_url( $path, $scheme );
  3284. }
  3285. }
  3286. }
  3287. /**
  3288. * Filters the dashboard URL for a user.
  3289. *
  3290. * @since 3.1.0
  3291. *
  3292. * @param string $url The complete URL including scheme and path.
  3293. * @param int $user_id The user ID.
  3294. * @param string $path Path relative to the URL. Blank string if no path is specified.
  3295. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  3296. * 'login_post', 'admin', 'relative' or null.
  3297. */
  3298. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme );
  3299. }
  3300. /**
  3301. * Retrieves the URL to the user's profile editor.
  3302. *
  3303. * @since 3.1.0
  3304. *
  3305. * @param int $user_id Optional. User ID. Defaults to current user.
  3306. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3307. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3308. * @return string Dashboard URL link with optional path appended.
  3309. */
  3310. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
  3311. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  3312. if ( is_user_admin() ) {
  3313. $url = user_admin_url( 'profile.php', $scheme );
  3314. } elseif ( is_network_admin() ) {
  3315. $url = network_admin_url( 'profile.php', $scheme );
  3316. } else {
  3317. $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
  3318. }
  3319. /**
  3320. * Filters the URL for a user's profile editor.
  3321. *
  3322. * @since 3.1.0
  3323. *
  3324. * @param string $url The complete URL including scheme and path.
  3325. * @param int $user_id The user ID.
  3326. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  3327. * 'login_post', 'admin', 'relative' or null.
  3328. */
  3329. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme );
  3330. }
  3331. /**
  3332. * Returns the canonical URL for a post.
  3333. *
  3334. * When the post is the same as the current requested page the function will handle the
  3335. * pagination arguments too.
  3336. *
  3337. * @since 4.6.0
  3338. *
  3339. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
  3340. * @return string|false The canonical URL, or false if the post does not exist or has not
  3341. * been published yet.
  3342. */
  3343. function wp_get_canonical_url( $post = null ) {
  3344. $post = get_post( $post );
  3345. if ( ! $post ) {
  3346. return false;
  3347. }
  3348. if ( 'publish' !== $post->post_status ) {
  3349. return false;
  3350. }
  3351. $canonical_url = get_permalink( $post );
  3352. // If a canonical is being generated for the current page, make sure it has pagination if needed.
  3353. if ( get_queried_object_id() === $post->ID ) {
  3354. $page = get_query_var( 'page', 0 );
  3355. if ( $page >= 2 ) {
  3356. if ( '' == get_option( 'permalink_structure' ) ) {
  3357. $canonical_url = add_query_arg( 'page', $page, $canonical_url );
  3358. } else {
  3359. $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
  3360. }
  3361. }
  3362. $cpage = get_query_var( 'cpage', 0 );
  3363. if ( $cpage ) {
  3364. $canonical_url = get_comments_pagenum_link( $cpage );
  3365. }
  3366. }
  3367. /**
  3368. * Filters the canonical URL for a post.
  3369. *
  3370. * @since 4.6.0
  3371. *
  3372. * @param string $canonical_url The post's canonical URL.
  3373. * @param WP_Post $post Post object.
  3374. */
  3375. return apply_filters( 'get_canonical_url', $canonical_url, $post );
  3376. }
  3377. /**
  3378. * Outputs rel=canonical for singular queries.
  3379. *
  3380. * @since 2.9.0
  3381. * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
  3382. */
  3383. function rel_canonical() {
  3384. if ( ! is_singular() ) {
  3385. return;
  3386. }
  3387. $id = get_queried_object_id();
  3388. if ( 0 === $id ) {
  3389. return;
  3390. }
  3391. $url = wp_get_canonical_url( $id );
  3392. if ( ! empty( $url ) ) {
  3393. echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
  3394. }
  3395. }
  3396. /**
  3397. * Returns a shortlink for a post, page, attachment, or site.
  3398. *
  3399. * This function exists to provide a shortlink tag that all themes and plugins can target.
  3400. * A plugin must hook in to provide the actual shortlinks. Default shortlink support is
  3401. * limited to providing ?p= style links for posts. Plugins can short-circuit this function
  3402. * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
  3403. * filter.
  3404. *
  3405. * @since 3.0.0
  3406. *
  3407. * @param int $id Optional. A post or site id. Default is 0, which means the current post or site.
  3408. * @param string $context Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',
  3409. * the post_type of the post is consulted. If 'query', the current query is consulted
  3410. * to determine the id and context. Default 'post'.
  3411. * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
  3412. * and whether to honor this. Default true.
  3413. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks
  3414. * are not enabled.
  3415. */
  3416. function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
  3417. /**
  3418. * Filters whether to preempt generating a shortlink for the given post.
  3419. *
  3420. * Passing a truthy value to the filter will effectively short-circuit the
  3421. * shortlink-generation process, returning that value instead.
  3422. *
  3423. * @since 3.0.0
  3424. *
  3425. * @param bool|string $return Short-circuit return value. Either false or a URL string.
  3426. * @param int $id Post ID, or 0 for the current post.
  3427. * @param string $context The context for the link. One of 'post' or 'query',
  3428. * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
  3429. */
  3430. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
  3431. if ( false !== $shortlink ) {
  3432. return $shortlink;
  3433. }
  3434. $post_id = 0;
  3435. if ( 'query' == $context && is_singular() ) {
  3436. $post_id = get_queried_object_id();
  3437. $post = get_post( $post_id );
  3438. } elseif ( 'post' == $context ) {
  3439. $post = get_post( $id );
  3440. if ( ! empty( $post->ID ) ) {
  3441. $post_id = $post->ID;
  3442. }
  3443. }
  3444. $shortlink = '';
  3445. // Return `?p=` link for all public post types.
  3446. if ( ! empty( $post_id ) ) {
  3447. $post_type = get_post_type_object( $post->post_type );
  3448. if ( 'page' === $post->post_type && get_option( 'page_on_front' ) == $post->ID && 'page' === get_option( 'show_on_front' ) ) {
  3449. $shortlink = home_url( '/' );
  3450. } elseif ( $post_type->public ) {
  3451. $shortlink = home_url( '?p=' . $post_id );
  3452. }
  3453. }
  3454. /**
  3455. * Filters the shortlink for a post.
  3456. *
  3457. * @since 3.0.0
  3458. *
  3459. * @param string $shortlink Shortlink URL.
  3460. * @param int $id Post ID, or 0 for the current post.
  3461. * @param string $context The context for the link. One of 'post' or 'query',
  3462. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
  3463. */
  3464. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
  3465. }
  3466. /**
  3467. * Injects rel=shortlink into the head if a shortlink is defined for the current page.
  3468. *
  3469. * Attached to the {@see 'wp_head'} action.
  3470. *
  3471. * @since 3.0.0
  3472. */
  3473. function wp_shortlink_wp_head() {
  3474. $shortlink = wp_get_shortlink( 0, 'query' );
  3475. if ( empty( $shortlink ) ) {
  3476. return;
  3477. }
  3478. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
  3479. }
  3480. /**
  3481. * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
  3482. *
  3483. * Attached to the {@see 'wp'} action.
  3484. *
  3485. * @since 3.0.0
  3486. */
  3487. function wp_shortlink_header() {
  3488. if ( headers_sent() ) {
  3489. return;
  3490. }
  3491. $shortlink = wp_get_shortlink( 0, 'query' );
  3492. if ( empty( $shortlink ) ) {
  3493. return;
  3494. }
  3495. header( 'Link: <' . $shortlink . '>; rel=shortlink', false );
  3496. }
  3497. /**
  3498. * Displays the shortlink for a post.
  3499. *
  3500. * Must be called from inside "The Loop"
  3501. *
  3502. * Call like the_shortlink( __( 'Shortlinkage FTW' ) )
  3503. *
  3504. * @since 3.0.0
  3505. *
  3506. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
  3507. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
  3508. * @param string $before Optional HTML to display before the link. Default empty.
  3509. * @param string $after Optional HTML to display after the link. Default empty.
  3510. */
  3511. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
  3512. $post = get_post();
  3513. if ( empty( $text ) ) {
  3514. $text = __( 'This is the short link.' );
  3515. }
  3516. if ( empty( $title ) ) {
  3517. $title = the_title_attribute( array( 'echo' => false ) );
  3518. }
  3519. $shortlink = wp_get_shortlink( $post->ID );
  3520. if ( ! empty( $shortlink ) ) {
  3521. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
  3522. /**
  3523. * Filters the short link anchor tag for a post.
  3524. *
  3525. * @since 3.0.0
  3526. *
  3527. * @param string $link Shortlink anchor tag.
  3528. * @param string $shortlink Shortlink URL.
  3529. * @param string $text Shortlink's text.
  3530. * @param string $title Shortlink's title attribute.
  3531. */
  3532. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
  3533. echo $before, $link, $after;
  3534. }
  3535. }
  3536. /**
  3537. * Retrieves the avatar URL.
  3538. *
  3539. * @since 4.2.0
  3540. *
  3541. * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
  3542. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3543. * @param array $args {
  3544. * Optional. Arguments to return instead of the default arguments.
  3545. *
  3546. * @type int $size Height and width of the avatar in pixels. Default 96.
  3547. * @type string $default URL for the default image or a default type. Accepts '404' (return
  3548. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
  3549. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
  3550. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
  3551. * 'gravatar_default' (the Gravatar logo). Default is the value of the
  3552. * 'avatar_default' option, with a fallback of 'mystery'.
  3553. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
  3554. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
  3555. * judged in that order. Default is the value of the 'avatar_rating' option.
  3556. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
  3557. * Default null.
  3558. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
  3559. * plus a "found_avatar" guess. Pass as a reference. Default null.
  3560. * }
  3561. * @return string|false The URL of the avatar on success, false on failure.
  3562. */
  3563. function get_avatar_url( $id_or_email, $args = null ) {
  3564. $args = get_avatar_data( $id_or_email, $args );
  3565. return $args['url'];
  3566. }
  3567. /**
  3568. * Check if this comment type allows avatars to be retrieved.
  3569. *
  3570. * @since 5.1.0
  3571. *
  3572. * @param string $comment_type Comment type to check.
  3573. * @return bool Whether the comment type is allowed for retrieving avatars.
  3574. */
  3575. function is_avatar_comment_type( $comment_type ) {
  3576. /**
  3577. * Filters the list of allowed comment types for retrieving avatars.
  3578. *
  3579. * @since 3.0.0
  3580. *
  3581. * @param array $types An array of content types. Default only contains 'comment'.
  3582. */
  3583. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  3584. return in_array( $comment_type, (array) $allowed_comment_types, true );
  3585. }
  3586. /**
  3587. * Retrieves default data about the avatar.
  3588. *
  3589. * @since 4.2.0
  3590. *
  3591. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3592. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3593. * @param array $args {
  3594. * Optional. Arguments to return instead of the default arguments.
  3595. *
  3596. * @type int $size Height and width of the avatar image file in pixels. Default 96.
  3597. * @type int $height Display height of the avatar in pixels. Defaults to $size.
  3598. * @type int $width Display width of the avatar in pixels. Defaults to $size.
  3599. * @type string $default URL for the default image or a default type. Accepts '404' (return
  3600. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
  3601. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
  3602. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
  3603. * 'gravatar_default' (the Gravatar logo). Default is the value of the
  3604. * 'avatar_default' option, with a fallback of 'mystery'.
  3605. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
  3606. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
  3607. * judged in that order. Default is the value of the 'avatar_rating' option.
  3608. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
  3609. * Default null.
  3610. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
  3611. * plus a "found_avatar" guess. Pass as a reference. Default null.
  3612. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
  3613. * }
  3614. * @return array {
  3615. * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
  3616. *
  3617. * @type bool $found_avatar True if we were able to find an avatar for this user,
  3618. * false or not set if we couldn't.
  3619. * @type string $url The URL of the avatar we found.
  3620. * }
  3621. */
  3622. function get_avatar_data( $id_or_email, $args = null ) {
  3623. $args = wp_parse_args(
  3624. $args,
  3625. array(
  3626. 'size' => 96,
  3627. 'height' => null,
  3628. 'width' => null,
  3629. 'default' => get_option( 'avatar_default', 'mystery' ),
  3630. 'force_default' => false,
  3631. 'rating' => get_option( 'avatar_rating' ),
  3632. 'scheme' => null,
  3633. 'processed_args' => null, // If used, should be a reference.
  3634. 'extra_attr' => '',
  3635. )
  3636. );
  3637. if ( is_numeric( $args['size'] ) ) {
  3638. $args['size'] = absint( $args['size'] );
  3639. if ( ! $args['size'] ) {
  3640. $args['size'] = 96;
  3641. }
  3642. } else {
  3643. $args['size'] = 96;
  3644. }
  3645. if ( is_numeric( $args['height'] ) ) {
  3646. $args['height'] = absint( $args['height'] );
  3647. if ( ! $args['height'] ) {
  3648. $args['height'] = $args['size'];
  3649. }
  3650. } else {
  3651. $args['height'] = $args['size'];
  3652. }
  3653. if ( is_numeric( $args['width'] ) ) {
  3654. $args['width'] = absint( $args['width'] );
  3655. if ( ! $args['width'] ) {
  3656. $args['width'] = $args['size'];
  3657. }
  3658. } else {
  3659. $args['width'] = $args['size'];
  3660. }
  3661. if ( empty( $args['default'] ) ) {
  3662. $args['default'] = get_option( 'avatar_default', 'mystery' );
  3663. }
  3664. switch ( $args['default'] ) {
  3665. case 'mm':
  3666. case 'mystery':
  3667. case 'mysteryman':
  3668. $args['default'] = 'mm';
  3669. break;
  3670. case 'gravatar_default':
  3671. $args['default'] = false;
  3672. break;
  3673. }
  3674. $args['force_default'] = (bool) $args['force_default'];
  3675. $args['rating'] = strtolower( $args['rating'] );
  3676. $args['found_avatar'] = false;
  3677. /**
  3678. * Filters whether to retrieve the avatar URL early.
  3679. *
  3680. * Passing a non-null value in the 'url' member of the return array will
  3681. * effectively short circuit get_avatar_data(), passing the value through
  3682. * the {@see 'get_avatar_data'} filter and returning early.
  3683. *
  3684. * @since 4.2.0
  3685. *
  3686. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3687. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3688. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3689. */
  3690. $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
  3691. if ( isset( $args['url'] ) ) {
  3692. /** This filter is documented in wp-includes/link-template.php */
  3693. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3694. }
  3695. $email_hash = '';
  3696. $user = false;
  3697. $email = false;
  3698. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
  3699. $id_or_email = get_comment( $id_or_email );
  3700. }
  3701. // Process the user identifier.
  3702. if ( is_numeric( $id_or_email ) ) {
  3703. $user = get_user_by( 'id', absint( $id_or_email ) );
  3704. } elseif ( is_string( $id_or_email ) ) {
  3705. if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
  3706. // MD5 hash.
  3707. list( $email_hash ) = explode( '@', $id_or_email );
  3708. } else {
  3709. // Email address.
  3710. $email = $id_or_email;
  3711. }
  3712. } elseif ( $id_or_email instanceof WP_User ) {
  3713. // User object.
  3714. $user = $id_or_email;
  3715. } elseif ( $id_or_email instanceof WP_Post ) {
  3716. // Post object.
  3717. $user = get_user_by( 'id', (int) $id_or_email->post_author );
  3718. } elseif ( $id_or_email instanceof WP_Comment ) {
  3719. if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
  3720. $args['url'] = false;
  3721. /** This filter is documented in wp-includes/link-template.php */
  3722. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3723. }
  3724. if ( ! empty( $id_or_email->user_id ) ) {
  3725. $user = get_user_by( 'id', (int) $id_or_email->user_id );
  3726. }
  3727. if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
  3728. $email = $id_or_email->comment_author_email;
  3729. }
  3730. }
  3731. if ( ! $email_hash ) {
  3732. if ( $user ) {
  3733. $email = $user->user_email;
  3734. }
  3735. if ( $email ) {
  3736. $email_hash = md5( strtolower( trim( $email ) ) );
  3737. }
  3738. }
  3739. if ( $email_hash ) {
  3740. $args['found_avatar'] = true;
  3741. $gravatar_server = hexdec( $email_hash[0] ) % 3;
  3742. } else {
  3743. $gravatar_server = rand( 0, 2 );
  3744. }
  3745. $url_args = array(
  3746. 's' => $args['size'],
  3747. 'd' => $args['default'],
  3748. 'f' => $args['force_default'] ? 'y' : false,
  3749. 'r' => $args['rating'],
  3750. );
  3751. if ( is_ssl() ) {
  3752. $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
  3753. } else {
  3754. $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
  3755. }
  3756. $url = add_query_arg(
  3757. rawurlencode_deep( array_filter( $url_args ) ),
  3758. set_url_scheme( $url, $args['scheme'] )
  3759. );
  3760. /**
  3761. * Filters the avatar URL.
  3762. *
  3763. * @since 4.2.0
  3764. *
  3765. * @param string $url The URL of the avatar.
  3766. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3767. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3768. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3769. */
  3770. $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
  3771. /**
  3772. * Filters the avatar data.
  3773. *
  3774. * @since 4.2.0
  3775. *
  3776. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3777. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3778. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3779. */
  3780. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3781. }
  3782. /**
  3783. * Retrieves the URL of a file in the theme.
  3784. *
  3785. * Searches in the stylesheet directory before the template directory so themes
  3786. * which inherit from a parent theme can just override one file.
  3787. *
  3788. * @since 4.7.0
  3789. *
  3790. * @param string $file Optional. File to search for in the stylesheet directory.
  3791. * @return string The URL of the file.
  3792. */
  3793. function get_theme_file_uri( $file = '' ) {
  3794. $file = ltrim( $file, '/' );
  3795. if ( empty( $file ) ) {
  3796. $url = get_stylesheet_directory_uri();
  3797. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  3798. $url = get_stylesheet_directory_uri() . '/' . $file;
  3799. } else {
  3800. $url = get_template_directory_uri() . '/' . $file;
  3801. }
  3802. /**
  3803. * Filters the URL to a file in the theme.
  3804. *
  3805. * @since 4.7.0
  3806. *
  3807. * @param string $url The file URL.
  3808. * @param string $file The requested file to search for.
  3809. */
  3810. return apply_filters( 'theme_file_uri', $url, $file );
  3811. }
  3812. /**
  3813. * Retrieves the URL of a file in the parent theme.
  3814. *
  3815. * @since 4.7.0
  3816. *
  3817. * @param string $file Optional. File to return the URL for in the template directory.
  3818. * @return string The URL of the file.
  3819. */
  3820. function get_parent_theme_file_uri( $file = '' ) {
  3821. $file = ltrim( $file, '/' );
  3822. if ( empty( $file ) ) {
  3823. $url = get_template_directory_uri();
  3824. } else {
  3825. $url = get_template_directory_uri() . '/' . $file;
  3826. }
  3827. /**
  3828. * Filters the URL to a file in the parent theme.
  3829. *
  3830. * @since 4.7.0
  3831. *
  3832. * @param string $url The file URL.
  3833. * @param string $file The requested file to search for.
  3834. */
  3835. return apply_filters( 'parent_theme_file_uri', $url, $file );
  3836. }
  3837. /**
  3838. * Retrieves the path of a file in the theme.
  3839. *
  3840. * Searches in the stylesheet directory before the template directory so themes
  3841. * which inherit from a parent theme can just override one file.
  3842. *
  3843. * @since 4.7.0
  3844. *
  3845. * @param string $file Optional. File to search for in the stylesheet directory.
  3846. * @return string The path of the file.
  3847. */
  3848. function get_theme_file_path( $file = '' ) {
  3849. $file = ltrim( $file, '/' );
  3850. if ( empty( $file ) ) {
  3851. $path = get_stylesheet_directory();
  3852. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  3853. $path = get_stylesheet_directory() . '/' . $file;
  3854. } else {
  3855. $path = get_template_directory() . '/' . $file;
  3856. }
  3857. /**
  3858. * Filters the path to a file in the theme.
  3859. *
  3860. * @since 4.7.0
  3861. *
  3862. * @param string $path The file path.
  3863. * @param string $file The requested file to search for.
  3864. */
  3865. return apply_filters( 'theme_file_path', $path, $file );
  3866. }
  3867. /**
  3868. * Retrieves the path of a file in the parent theme.
  3869. *
  3870. * @since 4.7.0
  3871. *
  3872. * @param string $file Optional. File to return the path for in the template directory.
  3873. * @return string The path of the file.
  3874. */
  3875. function get_parent_theme_file_path( $file = '' ) {
  3876. $file = ltrim( $file, '/' );
  3877. if ( empty( $file ) ) {
  3878. $path = get_template_directory();
  3879. } else {
  3880. $path = get_template_directory() . '/' . $file;
  3881. }
  3882. /**
  3883. * Filters the path to a file in the parent theme.
  3884. *
  3885. * @since 4.7.0
  3886. *
  3887. * @param string $path The file path.
  3888. * @param string $file The requested file to search for.
  3889. */
  3890. return apply_filters( 'parent_theme_file_path', $path, $file );
  3891. }
  3892. /**
  3893. * Retrieves the URL to the privacy policy page.
  3894. *
  3895. * @since 4.9.6
  3896. *
  3897. * @return string The URL to the privacy policy page. Empty string if it doesn't exist.
  3898. */
  3899. function get_privacy_policy_url() {
  3900. $url = '';
  3901. $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
  3902. if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
  3903. $url = (string) get_permalink( $policy_page_id );
  3904. }
  3905. /**
  3906. * Filters the URL of the privacy policy page.
  3907. *
  3908. * @since 4.9.6
  3909. *
  3910. * @param string $url The URL to the privacy policy page. Empty string
  3911. * if it doesn't exist.
  3912. * @param int $policy_page_id The ID of privacy policy page.
  3913. */
  3914. return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
  3915. }
  3916. /**
  3917. * Displays the privacy policy link with formatting, when applicable.
  3918. *
  3919. * @since 4.9.6
  3920. *
  3921. * @param string $before Optional. Display before privacy policy link. Default empty.
  3922. * @param string $after Optional. Display after privacy policy link. Default empty.
  3923. */
  3924. function the_privacy_policy_link( $before = '', $after = '' ) {
  3925. echo get_the_privacy_policy_link( $before, $after );
  3926. }
  3927. /**
  3928. * Returns the privacy policy link with formatting, when applicable.
  3929. *
  3930. * @since 4.9.6
  3931. *
  3932. * @param string $before Optional. Display before privacy policy link. Default empty.
  3933. * @param string $after Optional. Display after privacy policy link. Default empty.
  3934. *
  3935. * @return string Markup for the link and surrounding elements. Empty string if it
  3936. * doesn't exist.
  3937. */
  3938. function get_the_privacy_policy_link( $before = '', $after = '' ) {
  3939. $link = '';
  3940. $privacy_policy_url = get_privacy_policy_url();
  3941. $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
  3942. $page_title = ( $policy_page_id ) ? get_the_title( $policy_page_id ) : '';
  3943. if ( $privacy_policy_url && $page_title ) {
  3944. $link = sprintf(
  3945. '<a class="privacy-policy-link" href="%s">%s</a>',
  3946. esc_url( $privacy_policy_url ),
  3947. esc_html( $page_title )
  3948. );
  3949. }
  3950. /**
  3951. * Filters the privacy policy link.
  3952. *
  3953. * @since 4.9.6
  3954. *
  3955. * @param string $link The privacy policy link. Empty string if it
  3956. * doesn't exist.
  3957. * @param string $privacy_policy_url The URL of the privacy policy. Empty string
  3958. * if it doesn't exist.
  3959. */
  3960. $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
  3961. if ( $link ) {
  3962. return $before . $link . $after;
  3963. }
  3964. return '';
  3965. }