PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

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