PageRenderTime 37ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

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