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

/wp-includes/link-template.php

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