PageRenderTime 61ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/link-template.php

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