PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/php/wp-includes/link-template.php

https://bitbucket.org/cubeee/wp-fin
PHP | 2533 lines | 1873 code | 191 blank | 469 comment | 191 complexity | f95ec4ce5db726b24cd3869f9b852de5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. * @uses apply_filters() Calls 'the_permalink' filter on the permalink string.
  13. */
  14. function the_permalink() {
  15. echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
  16. }
  17. /**
  18. * Retrieve trailing slash string, if blog set for adding trailing slashes.
  19. *
  20. * Conditionally adds a trailing slash if the permalink structure has a trailing
  21. * slash, strips the trailing slash if not. The string is passed through the
  22. * 'user_trailingslashit' filter. Will remove trailing slash from string, if
  23. * blog is not set to have them.
  24. *
  25. * @since 2.2.0
  26. * @uses $wp_rewrite
  27. *
  28. * @param string $string URL with or without a trailing slash.
  29. * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
  30. * @return string
  31. */
  32. function user_trailingslashit($string, $type_of_url = '') {
  33. global $wp_rewrite;
  34. if ( $wp_rewrite->use_trailing_slashes )
  35. $string = trailingslashit($string);
  36. else
  37. $string = untrailingslashit($string);
  38. // Note that $type_of_url can be one of following:
  39. // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged, post_type_archive
  40. $string = apply_filters('user_trailingslashit', $string, $type_of_url);
  41. return $string;
  42. }
  43. /**
  44. * Display permalink anchor for current post.
  45. *
  46. * The permalink mode title will use the post title for the 'a' element 'id'
  47. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
  48. *
  49. * @since 0.71
  50. *
  51. * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
  52. */
  53. function permalink_anchor( $mode = 'id' ) {
  54. $post = get_post();
  55. switch ( strtolower( $mode ) ) {
  56. case 'title':
  57. $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
  58. echo '<a id="'.$title.'"></a>';
  59. break;
  60. case 'id':
  61. default:
  62. echo '<a id="post-' . $post->ID . '"></a>';
  63. break;
  64. }
  65. }
  66. /**
  67. * Retrieve full permalink for current post or post ID.
  68. *
  69. * @since 1.0.0
  70. *
  71. * @param int|WP_Post $id Optional. Post ID or post object, defaults to the current post.
  72. * @param bool $leavename Optional. Whether to keep post name or page name, defaults to false.
  73. * @return string|bool The permalink URL or false if post does not exist.
  74. */
  75. function get_permalink( $id = 0, $leavename = false ) {
  76. $rewritecode = array(
  77. '%year%',
  78. '%monthnum%',
  79. '%day%',
  80. '%hour%',
  81. '%minute%',
  82. '%second%',
  83. $leavename? '' : '%postname%',
  84. '%post_id%',
  85. '%category%',
  86. '%author%',
  87. $leavename? '' : '%pagename%',
  88. );
  89. if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
  90. $post = $id;
  91. $sample = true;
  92. } else {
  93. $post = get_post($id);
  94. $sample = false;
  95. }
  96. if ( empty($post->ID) )
  97. return false;
  98. if ( $post->post_type == 'page' )
  99. return get_page_link($post->ID, $leavename, $sample);
  100. elseif ( $post->post_type == 'attachment' )
  101. return get_attachment_link( $post->ID, $leavename );
  102. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
  103. return get_post_permalink($post->ID, $leavename, $sample);
  104. $permalink = get_option('permalink_structure');
  105. $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);
  106. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  107. $unixtime = strtotime($post->post_date);
  108. $category = '';
  109. if ( strpos($permalink, '%category%') !== false ) {
  110. $cats = get_the_category($post->ID);
  111. if ( $cats ) {
  112. usort($cats, '_usort_terms_by_ID'); // order by ID
  113. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  114. $category_object = get_term( $category_object, 'category' );
  115. $category = $category_object->slug;
  116. if ( $parent = $category_object->parent )
  117. $category = get_category_parents($parent, false, '/', true) . $category;
  118. }
  119. // show default category in permalinks, without
  120. // having to assign it explicitly
  121. if ( empty($category) ) {
  122. $default_category = get_term( get_option( 'default_category' ), 'category' );
  123. $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  124. }
  125. }
  126. $author = '';
  127. if ( strpos($permalink, '%author%') !== false ) {
  128. $authordata = get_userdata($post->post_author);
  129. $author = $authordata->user_nicename;
  130. }
  131. $date = explode(" ",date('Y m d H i s', $unixtime));
  132. $rewritereplace =
  133. array(
  134. $date[0],
  135. $date[1],
  136. $date[2],
  137. $date[3],
  138. $date[4],
  139. $date[5],
  140. $post->post_name,
  141. $post->ID,
  142. $category,
  143. $author,
  144. $post->post_name,
  145. );
  146. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
  147. $permalink = user_trailingslashit($permalink, 'single');
  148. } else { // if they're not using the fancy permalink option
  149. $permalink = home_url('?p=' . $post->ID);
  150. }
  151. return apply_filters('post_link', $permalink, $post, $leavename);
  152. }
  153. /**
  154. * Retrieve the permalink for a post with a custom post type.
  155. *
  156. * @since 3.0.0
  157. *
  158. * @param int $id Optional. Post ID.
  159. * @param bool $leavename Optional, defaults to false. Whether to keep post name.
  160. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  161. * @return string
  162. */
  163. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  164. global $wp_rewrite;
  165. $post = get_post($id);
  166. if ( is_wp_error( $post ) )
  167. return $post;
  168. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
  169. $slug = $post->post_name;
  170. $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  171. $post_type = get_post_type_object($post->post_type);
  172. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
  173. if ( ! $leavename ) {
  174. if ( $post_type->hierarchical )
  175. $slug = get_page_uri($id);
  176. $post_link = str_replace("%$post->post_type%", $slug, $post_link);
  177. }
  178. $post_link = home_url( user_trailingslashit($post_link) );
  179. } else {
  180. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
  181. $post_link = add_query_arg($post_type->query_var, $slug, '');
  182. else
  183. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
  184. $post_link = home_url($post_link);
  185. }
  186. return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);
  187. }
  188. /**
  189. * Retrieve permalink from post ID.
  190. *
  191. * @since 1.0.0
  192. *
  193. * @param int $post_id Optional. Post ID.
  194. * @param mixed $deprecated Not used.
  195. * @return string
  196. */
  197. function post_permalink( $post_id = 0, $deprecated = '' ) {
  198. if ( !empty( $deprecated ) )
  199. _deprecated_argument( __FUNCTION__, '1.3' );
  200. return get_permalink($post_id);
  201. }
  202. /**
  203. * Retrieve the permalink for current page or page ID.
  204. *
  205. * Respects page_on_front. Use this one.
  206. *
  207. * @since 1.5.0
  208. *
  209. * @param int|object $post Optional. Post ID or object.
  210. * @param bool $leavename Optional, defaults to false. Whether to keep page name.
  211. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  212. * @return string
  213. */
  214. function get_page_link( $post = false, $leavename = false, $sample = false ) {
  215. $post = get_post( $post );
  216. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
  217. $link = home_url('/');
  218. else
  219. $link = _get_page_link( $post, $leavename, $sample );
  220. return apply_filters( 'page_link', $link, $post->ID, $sample );
  221. }
  222. /**
  223. * Retrieve the page permalink.
  224. *
  225. * Ignores page_on_front. Internal use only.
  226. *
  227. * @since 2.1.0
  228. * @access private
  229. *
  230. * @param int|object $post Optional. Post ID or object.
  231. * @param bool $leavename Optional. Leave name.
  232. * @param bool $sample Optional. Sample permalink.
  233. * @return string
  234. */
  235. function _get_page_link( $post = false, $leavename = false, $sample = false ) {
  236. global $wp_rewrite;
  237. $post = get_post( $post );
  238. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  239. $link = $wp_rewrite->get_page_permastruct();
  240. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
  241. if ( ! $leavename ) {
  242. $link = str_replace('%pagename%', get_page_uri( $post ), $link);
  243. }
  244. $link = home_url($link);
  245. $link = user_trailingslashit($link, 'page');
  246. } else {
  247. $link = home_url( '?page_id=' . $post->ID );
  248. }
  249. return apply_filters( '_get_page_link', $link, $post->ID );
  250. }
  251. /**
  252. * Retrieve permalink for attachment.
  253. *
  254. * This can be used in the WordPress Loop or outside of it.
  255. *
  256. * @since 2.0.0
  257. *
  258. * @param int|object $post Optional. Post ID or object.
  259. * @param bool $leavename Optional. Leave name.
  260. * @return string
  261. */
  262. function get_attachment_link( $post = null, $leavename = false ) {
  263. global $wp_rewrite;
  264. $link = false;
  265. $post = get_post( $post );
  266. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
  267. if ( $wp_rewrite->using_permalinks() && $parent ) {
  268. if ( 'page' == $parent->post_type )
  269. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
  270. else
  271. $parentlink = get_permalink( $post->post_parent );
  272. if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
  273. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
  274. else
  275. $name = $post->post_name;
  276. if ( strpos($parentlink, '?') === false )
  277. $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
  278. if ( ! $leavename )
  279. $link = str_replace( '%postname%', $name, $link );
  280. }
  281. if ( ! $link )
  282. $link = home_url( '/?attachment_id=' . $post->ID );
  283. return apply_filters( 'attachment_link', $link, $post->ID );
  284. }
  285. /**
  286. * Retrieve the permalink for the year archives.
  287. *
  288. * @since 1.5.0
  289. *
  290. * @param int|bool $year False for current year or year for permalink.
  291. * @return string
  292. */
  293. function get_year_link($year) {
  294. global $wp_rewrite;
  295. if ( !$year )
  296. $year = gmdate('Y', current_time('timestamp'));
  297. $yearlink = $wp_rewrite->get_year_permastruct();
  298. if ( !empty($yearlink) ) {
  299. $yearlink = str_replace('%year%', $year, $yearlink);
  300. return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
  301. } else {
  302. return apply_filters('year_link', home_url('?m=' . $year), $year);
  303. }
  304. }
  305. /**
  306. * Retrieve the permalink for the month archives with year.
  307. *
  308. * @since 1.0.0
  309. *
  310. * @param bool|int $year False for current year. Integer of year.
  311. * @param bool|int $month False for current month. Integer of month.
  312. * @return string
  313. */
  314. function get_month_link($year, $month) {
  315. global $wp_rewrite;
  316. if ( !$year )
  317. $year = gmdate('Y', current_time('timestamp'));
  318. if ( !$month )
  319. $month = gmdate('m', current_time('timestamp'));
  320. $monthlink = $wp_rewrite->get_month_permastruct();
  321. if ( !empty($monthlink) ) {
  322. $monthlink = str_replace('%year%', $year, $monthlink);
  323. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
  324. return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
  325. } else {
  326. return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
  327. }
  328. }
  329. /**
  330. * Retrieve the permalink for the day archives with year and month.
  331. *
  332. * @since 1.0.0
  333. *
  334. * @param bool|int $year False for current year. Integer of year.
  335. * @param bool|int $month False for current month. Integer of month.
  336. * @param bool|int $day False for current day. Integer of day.
  337. * @return string
  338. */
  339. function get_day_link($year, $month, $day) {
  340. global $wp_rewrite;
  341. if ( !$year )
  342. $year = gmdate('Y', current_time('timestamp'));
  343. if ( !$month )
  344. $month = gmdate('m', current_time('timestamp'));
  345. if ( !$day )
  346. $day = gmdate('j', current_time('timestamp'));
  347. $daylink = $wp_rewrite->get_day_permastruct();
  348. if ( !empty($daylink) ) {
  349. $daylink = str_replace('%year%', $year, $daylink);
  350. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
  351. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
  352. return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
  353. } else {
  354. return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
  355. }
  356. }
  357. /**
  358. * Display the permalink for the feed type.
  359. *
  360. * @since 3.0.0
  361. *
  362. * @param string $anchor The link's anchor text.
  363. * @param string $feed Optional, defaults to default feed. Feed type.
  364. */
  365. function the_feed_link( $anchor, $feed = '' ) {
  366. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  367. echo apply_filters( 'the_feed_link', $link, $feed );
  368. }
  369. /**
  370. * Retrieve the permalink for the feed type.
  371. *
  372. * @since 1.5.0
  373. *
  374. * @param string $feed Optional, defaults to default feed. Feed type.
  375. * @return string
  376. */
  377. function get_feed_link($feed = '') {
  378. global $wp_rewrite;
  379. $permalink = $wp_rewrite->get_feed_permastruct();
  380. if ( '' != $permalink ) {
  381. if ( false !== strpos($feed, 'comments_') ) {
  382. $feed = str_replace('comments_', '', $feed);
  383. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  384. }
  385. if ( get_default_feed() == $feed )
  386. $feed = '';
  387. $permalink = str_replace('%feed%', $feed, $permalink);
  388. $permalink = preg_replace('#/+#', '/', "/$permalink");
  389. $output = home_url( user_trailingslashit($permalink, 'feed') );
  390. } else {
  391. if ( empty($feed) )
  392. $feed = get_default_feed();
  393. if ( false !== strpos($feed, 'comments_') )
  394. $feed = str_replace('comments_', 'comments-', $feed);
  395. $output = home_url("?feed={$feed}");
  396. }
  397. return apply_filters('feed_link', $output, $feed);
  398. }
  399. /**
  400. * Retrieve the permalink for the post comments feed.
  401. *
  402. * @since 2.2.0
  403. *
  404. * @param int $post_id Optional. Post ID.
  405. * @param string $feed Optional. Feed type.
  406. * @return string
  407. */
  408. function get_post_comments_feed_link($post_id = 0, $feed = '') {
  409. $post_id = absint( $post_id );
  410. if ( ! $post_id )
  411. $post_id = get_the_ID();
  412. if ( empty( $feed ) )
  413. $feed = get_default_feed();
  414. if ( '' != get_option('permalink_structure') ) {
  415. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
  416. $url = _get_page_link( $post_id );
  417. else
  418. $url = get_permalink($post_id);
  419. $url = trailingslashit($url) . 'feed';
  420. if ( $feed != get_default_feed() )
  421. $url .= "/$feed";
  422. $url = user_trailingslashit($url, 'single_feed');
  423. } else {
  424. $type = get_post_field('post_type', $post_id);
  425. if ( 'page' == $type )
  426. $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
  427. else
  428. $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
  429. }
  430. return apply_filters('post_comments_feed_link', $url);
  431. }
  432. /**
  433. * Display the comment feed link for a post.
  434. *
  435. * Prints out the comment feed link for a post. Link text is placed in the
  436. * anchor. If no link text is specified, default text is used. If no post ID is
  437. * specified, the current post is used.
  438. *
  439. * @package WordPress
  440. * @subpackage Feed
  441. * @since 2.5.0
  442. *
  443. * @param string $link_text Descriptive text.
  444. * @param int $post_id Optional post ID. Default to current post.
  445. * @param string $feed Optional. Feed format.
  446. * @return string Link to the comment feed for the current post.
  447. */
  448. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  449. $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
  450. if ( empty($link_text) )
  451. $link_text = __('Comments Feed');
  452. echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
  453. }
  454. /**
  455. * Retrieve the feed link for a given author.
  456. *
  457. * Returns a link to the feed for all posts by a given author. A specific feed
  458. * can be requested or left blank to get the default feed.
  459. *
  460. * @package WordPress
  461. * @subpackage Feed
  462. * @since 2.5.0
  463. *
  464. * @param int $author_id ID of an author.
  465. * @param string $feed Optional. Feed type.
  466. * @return string Link to the feed for the author specified by $author_id.
  467. */
  468. function get_author_feed_link( $author_id, $feed = '' ) {
  469. $author_id = (int) $author_id;
  470. $permalink_structure = get_option('permalink_structure');
  471. if ( empty($feed) )
  472. $feed = get_default_feed();
  473. if ( '' == $permalink_structure ) {
  474. $link = home_url("?feed=$feed&amp;author=" . $author_id);
  475. } else {
  476. $link = get_author_posts_url($author_id);
  477. if ( $feed == get_default_feed() )
  478. $feed_link = 'feed';
  479. else
  480. $feed_link = "feed/$feed";
  481. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  482. }
  483. $link = apply_filters('author_feed_link', $link, $feed);
  484. return $link;
  485. }
  486. /**
  487. * Retrieve the feed link for a category.
  488. *
  489. * Returns a link to the feed for all posts in a given category. A specific feed
  490. * can be requested or left blank to get the default feed.
  491. *
  492. * @package WordPress
  493. * @subpackage Feed
  494. * @since 2.5.0
  495. *
  496. * @param int $cat_id ID of a category.
  497. * @param string $feed Optional. Feed type.
  498. * @return string Link to the feed for the category specified by $cat_id.
  499. */
  500. function get_category_feed_link($cat_id, $feed = '') {
  501. return get_term_feed_link($cat_id, 'category', $feed);
  502. }
  503. /**
  504. * Retrieve the feed link for a term.
  505. *
  506. * Returns a link to the feed for all posts in a given term. A specific feed
  507. * can be requested or left blank to get the default feed.
  508. *
  509. * @since 3.0
  510. *
  511. * @param int $term_id ID of a category.
  512. * @param string $taxonomy Optional. Taxonomy of $term_id
  513. * @param string $feed Optional. Feed type.
  514. * @return string Link to the feed for the term specified by $term_id and $taxonomy.
  515. */
  516. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  517. $term_id = ( int ) $term_id;
  518. $term = get_term( $term_id, $taxonomy );
  519. if ( empty( $term ) || is_wp_error( $term ) )
  520. return false;
  521. if ( empty( $feed ) )
  522. $feed = get_default_feed();
  523. $permalink_structure = get_option( 'permalink_structure' );
  524. if ( '' == $permalink_structure ) {
  525. if ( 'category' == $taxonomy ) {
  526. $link = home_url("?feed=$feed&amp;cat=$term_id");
  527. }
  528. elseif ( 'post_tag' == $taxonomy ) {
  529. $link = home_url("?feed=$feed&amp;tag=$term->slug");
  530. } else {
  531. $t = get_taxonomy( $taxonomy );
  532. $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
  533. }
  534. } else {
  535. $link = get_term_link( $term_id, $term->taxonomy );
  536. if ( $feed == get_default_feed() )
  537. $feed_link = 'feed';
  538. else
  539. $feed_link = "feed/$feed";
  540. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  541. }
  542. if ( 'category' == $taxonomy )
  543. $link = apply_filters( 'category_feed_link', $link, $feed );
  544. elseif ( 'post_tag' == $taxonomy )
  545. $link = apply_filters( 'tag_feed_link', $link, $feed );
  546. else
  547. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  548. return $link;
  549. }
  550. /**
  551. * Retrieve permalink for feed of tag.
  552. *
  553. * @since 2.3.0
  554. *
  555. * @param int $tag_id Tag ID.
  556. * @param string $feed Optional. Feed type.
  557. * @return string
  558. */
  559. function get_tag_feed_link($tag_id, $feed = '') {
  560. return get_term_feed_link($tag_id, 'post_tag', $feed);
  561. }
  562. /**
  563. * Retrieve edit tag link.
  564. *
  565. * @since 2.7.0
  566. *
  567. * @param int $tag_id Tag ID
  568. * @param string $taxonomy Taxonomy
  569. * @return string
  570. */
  571. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  572. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  573. }
  574. /**
  575. * Display or retrieve edit tag link with formatting.
  576. *
  577. * @since 2.7.0
  578. *
  579. * @param string $link Optional. Anchor text.
  580. * @param string $before Optional. Display before edit link.
  581. * @param string $after Optional. Display after edit link.
  582. * @param object $tag Tag object.
  583. * @return string HTML content.
  584. */
  585. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  586. $link = edit_term_link( $link, '', '', $tag, false );
  587. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  588. }
  589. /**
  590. * Retrieve edit term url.
  591. *
  592. * @since 3.1.0
  593. *
  594. * @param int $term_id Term ID
  595. * @param string $taxonomy Taxonomy
  596. * @param string $object_type The object type
  597. * @return string
  598. */
  599. function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
  600. $tax = get_taxonomy( $taxonomy );
  601. if ( !current_user_can( $tax->cap->edit_terms ) )
  602. return;
  603. $term = get_term( $term_id, $taxonomy );
  604. $args = array(
  605. 'action' => 'edit',
  606. 'taxonomy' => $taxonomy,
  607. 'tag_ID' => $term->term_id,
  608. );
  609. if ( $object_type )
  610. $args['post_type'] = $object_type;
  611. $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
  612. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  613. }
  614. /**
  615. * Display or retrieve edit term link with formatting.
  616. *
  617. * @since 3.1.0
  618. *
  619. * @param string $link Optional. Anchor text.
  620. * @param string $before Optional. Display before edit link.
  621. * @param string $after Optional. Display after edit link.
  622. * @param object $term Term object.
  623. * @return string HTML content.
  624. */
  625. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  626. if ( is_null( $term ) )
  627. $term = get_queried_object();
  628. if ( ! $term )
  629. return;
  630. $tax = get_taxonomy( $term->taxonomy );
  631. if ( ! current_user_can( $tax->cap->edit_terms ) )
  632. return;
  633. if ( empty( $link ) )
  634. $link = __('Edit This');
  635. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
  636. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  637. if ( $echo )
  638. echo $link;
  639. else
  640. return $link;
  641. }
  642. /**
  643. * Retrieve permalink for search.
  644. *
  645. * @since 3.0.0
  646. *
  647. * @param string $query Optional. The query string to use. If empty the current query is used.
  648. * @return string
  649. */
  650. function get_search_link( $query = '' ) {
  651. global $wp_rewrite;
  652. if ( empty($query) )
  653. $search = get_search_query( false );
  654. else
  655. $search = stripslashes($query);
  656. $permastruct = $wp_rewrite->get_search_permastruct();
  657. if ( empty( $permastruct ) ) {
  658. $link = home_url('?s=' . urlencode($search) );
  659. } else {
  660. $search = urlencode($search);
  661. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
  662. $link = str_replace( '%search%', $search, $permastruct );
  663. $link = home_url( user_trailingslashit( $link, 'search' ) );
  664. }
  665. return apply_filters( 'search_link', $link, $search );
  666. }
  667. /**
  668. * Retrieve the permalink for the feed of the search results.
  669. *
  670. * @since 2.5.0
  671. *
  672. * @param string $search_query Optional. Search query.
  673. * @param string $feed Optional. Feed type.
  674. * @return string
  675. */
  676. function get_search_feed_link($search_query = '', $feed = '') {
  677. global $wp_rewrite;
  678. $link = get_search_link($search_query);
  679. if ( empty($feed) )
  680. $feed = get_default_feed();
  681. $permastruct = $wp_rewrite->get_search_permastruct();
  682. if ( empty($permastruct) ) {
  683. $link = add_query_arg('feed', $feed, $link);
  684. } else {
  685. $link = trailingslashit($link);
  686. $link .= "feed/$feed/";
  687. }
  688. $link = apply_filters('search_feed_link', $link, $feed, 'posts');
  689. return $link;
  690. }
  691. /**
  692. * Retrieve the permalink for the comments feed of the search results.
  693. *
  694. * @since 2.5.0
  695. *
  696. * @param string $search_query Optional. Search query.
  697. * @param string $feed Optional. Feed type.
  698. * @return string
  699. */
  700. function get_search_comments_feed_link($search_query = '', $feed = '') {
  701. global $wp_rewrite;
  702. if ( empty($feed) )
  703. $feed = get_default_feed();
  704. $link = get_search_feed_link($search_query, $feed);
  705. $permastruct = $wp_rewrite->get_search_permastruct();
  706. if ( empty($permastruct) )
  707. $link = add_query_arg('feed', 'comments-' . $feed, $link);
  708. else
  709. $link = add_query_arg('withcomments', 1, $link);
  710. $link = apply_filters('search_feed_link', $link, $feed, 'comments');
  711. return $link;
  712. }
  713. /**
  714. * Retrieve the permalink for a post type archive.
  715. *
  716. * @since 3.1.0
  717. *
  718. * @param string $post_type Post type
  719. * @return string
  720. */
  721. function get_post_type_archive_link( $post_type ) {
  722. global $wp_rewrite;
  723. if ( ! $post_type_obj = get_post_type_object( $post_type ) )
  724. return false;
  725. if ( ! $post_type_obj->has_archive )
  726. return false;
  727. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  728. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  729. if ( $post_type_obj->rewrite['with_front'] )
  730. $struct = $wp_rewrite->front . $struct;
  731. else
  732. $struct = $wp_rewrite->root . $struct;
  733. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  734. } else {
  735. $link = home_url( '?post_type=' . $post_type );
  736. }
  737. return apply_filters( 'post_type_archive_link', $link, $post_type );
  738. }
  739. /**
  740. * Retrieve the permalink for a post type archive feed.
  741. *
  742. * @since 3.1.0
  743. *
  744. * @param string $post_type Post type
  745. * @param string $feed Optional. Feed type
  746. * @return string
  747. */
  748. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  749. $default_feed = get_default_feed();
  750. if ( empty( $feed ) )
  751. $feed = $default_feed;
  752. if ( ! $link = get_post_type_archive_link( $post_type ) )
  753. return false;
  754. $post_type_obj = get_post_type_object( $post_type );
  755. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
  756. $link = trailingslashit( $link );
  757. $link .= 'feed/';
  758. if ( $feed != $default_feed )
  759. $link .= "$feed/";
  760. } else {
  761. $link = add_query_arg( 'feed', $feed, $link );
  762. }
  763. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  764. }
  765. /**
  766. * Retrieve edit posts link for post.
  767. *
  768. * Can be used within the WordPress loop or outside of it. Can be used with
  769. * pages, posts, attachments, and revisions.
  770. *
  771. * @since 2.3.0
  772. *
  773. * @param int $id Optional. Post ID.
  774. * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
  775. * @return string
  776. */
  777. function get_edit_post_link( $id = 0, $context = 'display' ) {
  778. if ( ! $post = get_post( $id ) )
  779. return;
  780. if ( 'revision' === $post->post_type )
  781. $action = '';
  782. elseif ( 'display' == $context )
  783. $action = '&amp;action=edit';
  784. else
  785. $action = '&action=edit';
  786. $post_type_object = get_post_type_object( $post->post_type );
  787. if ( !$post_type_object )
  788. return;
  789. if ( !current_user_can( 'edit_post', $post->ID ) )
  790. return;
  791. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  792. }
  793. /**
  794. * Display edit post link for post.
  795. *
  796. * @since 1.0.0
  797. *
  798. * @param string $link Optional. Anchor text.
  799. * @param string $before Optional. Display before edit link.
  800. * @param string $after Optional. Display after edit link.
  801. * @param int $id Optional. Post ID.
  802. */
  803. function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  804. if ( !$post = get_post( $id ) )
  805. return;
  806. if ( !$url = get_edit_post_link( $post->ID ) )
  807. return;
  808. if ( null === $link )
  809. $link = __('Edit This');
  810. $post_type_obj = get_post_type_object( $post->post_type );
  811. $link = '<a class="post-edit-link" href="' . $url . '">' . $link . '</a>';
  812. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  813. }
  814. /**
  815. * Retrieve delete posts link for post.
  816. *
  817. * Can be used within the WordPress loop or outside of it, with any post type.
  818. *
  819. * @since 2.9.0
  820. *
  821. * @param int $id Optional. Post ID.
  822. * @param string $deprecated Not used.
  823. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  824. * @return string
  825. */
  826. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  827. if ( ! empty( $deprecated ) )
  828. _deprecated_argument( __FUNCTION__, '3.0' );
  829. if ( !$post = get_post( $id ) )
  830. return;
  831. $post_type_object = get_post_type_object( $post->post_type );
  832. if ( !$post_type_object )
  833. return;
  834. if ( !current_user_can( 'delete_post', $post->ID ) )
  835. return;
  836. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  837. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  838. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  839. }
  840. /**
  841. * Retrieve edit comment link.
  842. *
  843. * @since 2.3.0
  844. *
  845. * @param int $comment_id Optional. Comment ID.
  846. * @return string
  847. */
  848. function get_edit_comment_link( $comment_id = 0 ) {
  849. $comment = get_comment( $comment_id );
  850. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  851. return;
  852. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  853. return apply_filters( 'get_edit_comment_link', $location );
  854. }
  855. /**
  856. * Display or retrieve edit comment link with formatting.
  857. *
  858. * @since 1.0.0
  859. *
  860. * @param string $link Optional. Anchor text.
  861. * @param string $before Optional. Display before edit link.
  862. * @param string $after Optional. Display after edit link.
  863. * @return string|null HTML content, if $echo is set to false.
  864. */
  865. function edit_comment_link( $link = null, $before = '', $after = '' ) {
  866. global $comment;
  867. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  868. return;
  869. if ( null === $link )
  870. $link = __('Edit This');
  871. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $link . '</a>';
  872. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  873. }
  874. /**
  875. * Display edit bookmark (literally a URL external to blog) link.
  876. *
  877. * @since 2.7.0
  878. *
  879. * @param int $link Optional. Bookmark ID.
  880. * @return string
  881. */
  882. function get_edit_bookmark_link( $link = 0 ) {
  883. $link = get_bookmark( $link );
  884. if ( !current_user_can('manage_links') )
  885. return;
  886. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  887. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  888. }
  889. /**
  890. * Display edit bookmark (literally a URL external to blog) link anchor content.
  891. *
  892. * @since 2.7.0
  893. *
  894. * @param string $link Optional. Anchor text.
  895. * @param string $before Optional. Display before edit link.
  896. * @param string $after Optional. Display after edit link.
  897. * @param int $bookmark Optional. Bookmark ID.
  898. */
  899. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  900. $bookmark = get_bookmark($bookmark);
  901. if ( !current_user_can('manage_links') )
  902. return;
  903. if ( empty($link) )
  904. $link = __('Edit This');
  905. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
  906. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  907. }
  908. /**
  909. * Retrieve edit user link
  910. *
  911. * @since 3.5.0
  912. *
  913. * @param int $user_id Optional. User ID. Defaults to the current user.
  914. * @return string URL to edit user page or empty string.
  915. */
  916. function get_edit_user_link( $user_id = null ) {
  917. if ( ! $user_id )
  918. $user_id = get_current_user_id();
  919. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
  920. return '';
  921. $user = get_userdata( $user_id );
  922. if ( ! $user )
  923. return '';
  924. if ( get_current_user_id() == $user->ID )
  925. $link = get_edit_profile_url( $user->ID );
  926. else
  927. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  928. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  929. }
  930. // Navigation links
  931. /**
  932. * Retrieve previous post that is adjacent to current post.
  933. *
  934. * @since 1.5.0
  935. *
  936. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  937. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  938. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  939. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  940. */
  941. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  942. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
  943. }
  944. /**
  945. * Retrieve next post that is adjacent to current post.
  946. *
  947. * @since 1.5.0
  948. *
  949. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  950. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  951. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  952. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  953. */
  954. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  955. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
  956. }
  957. /**
  958. * Retrieve adjacent post.
  959. *
  960. * Can either be next or previous post.
  961. *
  962. * @since 2.5.0
  963. *
  964. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  965. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  966. * @param bool $previous Optional. Whether to retrieve previous post.
  967. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  968. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  969. */
  970. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  971. global $wpdb;
  972. if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
  973. return null;
  974. $current_post_date = $post->post_date;
  975. $join = '';
  976. $posts_in_ex_terms_sql = '';
  977. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  978. $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";
  979. if ( $in_same_term ) {
  980. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
  981. return '';
  982. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  983. if ( ! $term_array || is_wp_error( $term_array ) )
  984. return '';
  985. $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
  986. }
  987. $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
  988. if ( ! empty( $excluded_terms ) ) {
  989. if ( ! is_array( $excluded_terms ) ) {
  990. // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
  991. if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  992. _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
  993. $excluded_terms = explode( ' and ', $excluded_terms );
  994. } else {
  995. $excluded_terms = explode( ',', $excluded_terms );
  996. }
  997. }
  998. $excluded_terms = array_map( 'intval', $excluded_terms );
  999. if ( ! empty( $term_array ) ) {
  1000. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1001. $posts_in_ex_terms_sql = '';
  1002. }
  1003. if ( ! empty( $excluded_terms ) ) {
  1004. $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
  1005. }
  1006. }
  1007. }
  1008. $adjacent = $previous ? 'previous' : 'next';
  1009. $op = $previous ? '<' : '>';
  1010. $order = $previous ? 'DESC' : 'ASC';
  1011. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
  1012. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
  1013. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  1014. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1015. $query_key = 'adjacent_post_' . md5( $query );
  1016. $result = wp_cache_get( $query_key, 'counts' );
  1017. if ( false !== $result ) {
  1018. if ( $result )
  1019. $result = get_post( $result );
  1020. return $result;
  1021. }
  1022. $result = $wpdb->get_var( $query );
  1023. if ( null === $result )
  1024. $result = '';
  1025. wp_cache_set( $query_key, $result, 'counts' );
  1026. if ( $result )
  1027. $result = get_post( $result );
  1028. return $result;
  1029. }
  1030. /**
  1031. * Get adjacent post relational link.
  1032. *
  1033. * Can either be next or previous post relational link.
  1034. *
  1035. * @since 2.8.0
  1036. *
  1037. * @param string $title Optional. Link title format.
  1038. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1039. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1040. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1041. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1042. * @return string
  1043. */
  1044. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1045. if ( $previous && is_attachment() && $post = get_post() )
  1046. $post = get_post( $post->post_parent );
  1047. else
  1048. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1049. if ( empty( $post ) )
  1050. return;
  1051. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
  1052. if ( empty( $post_title ) )
  1053. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1054. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1055. $title = str_replace( '%title', $post_title, $title );
  1056. $title = str_replace( '%date', $date, $title );
  1057. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1058. $link .= esc_attr( $title );
  1059. $link .= "' href='" . get_permalink( $post ) . "' />\n";
  1060. $adjacent = $previous ? 'previous' : 'next';
  1061. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1062. }
  1063. /**
  1064. * Display relational links for the posts adjacent to the current post.
  1065. *
  1066. * @since 2.8.0
  1067. *
  1068. * @param string $title Optional. Link title format.
  1069. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1070. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1071. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1072. */
  1073. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1074. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
  1075. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
  1076. }
  1077. /**
  1078. * Display relational links for the posts adjacent to the current post for single post pages.
  1079. *
  1080. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1081. * @since 3.0.0
  1082. *
  1083. */
  1084. function adjacent_posts_rel_link_wp_head() {
  1085. if ( !is_singular() || is_attachment() )
  1086. return;
  1087. adjacent_posts_rel_link();
  1088. }
  1089. /**
  1090. * Display relational link for the next post adjacent to the current post.
  1091. *
  1092. * @since 2.8.0
  1093. *
  1094. * @param string $title Optional. Link title format.
  1095. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1096. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1097. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1098. */
  1099. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1100. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
  1101. }
  1102. /**
  1103. * Display relational link for the previous post adjacent to the current post.
  1104. *
  1105. * @since 2.8.0
  1106. *
  1107. * @param string $title Optional. Link title format.
  1108. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1109. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
  1110. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1111. */
  1112. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1113. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
  1114. }
  1115. /**
  1116. * Retrieve boundary post.
  1117. *
  1118. * Boundary being either the first or last post by publish date within the constraints specified
  1119. * by $in_same_term or $excluded_terms.
  1120. *
  1121. * @since 2.8.0
  1122. *
  1123. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
  1124. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1125. * @param bool $start Optional. Whether to retrieve first or last post.
  1126. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1127. * @return object
  1128. */
  1129. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
  1130. $post = get_post();
  1131. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
  1132. return null;
  1133. $query_args = array(
  1134. 'posts_per_page' => 1,
  1135. 'order' => $start ? 'ASC' : 'DESC',
  1136. 'update_post_term_cache' => false,
  1137. 'update_post_meta_cache' => false
  1138. );
  1139. $term_array = array();
  1140. if ( ! is_array( $excluded_terms ) ) {
  1141. if ( ! empty( $excluded_terms ) )
  1142. $excluded_terms = explode( ',', $excluded_terms );
  1143. else
  1144. $excluded_terms = array();
  1145. }
  1146. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1147. if ( $in_same_term )
  1148. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1149. if ( ! empty( $excluded_terms ) ) {
  1150. $excluded_terms = array_map( 'intval', $excluded_terms );
  1151. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1152. $inverse_terms = array();
  1153. foreach ( $excluded_terms as $excluded_term )
  1154. $inverse_terms[] = $excluded_term * -1;
  1155. $excluded_terms = $inverse_terms;
  1156. }
  1157. $query_args[ 'tax_query' ] = array( array(
  1158. 'taxonomy' => $taxonomy,
  1159. 'terms' => array_merge( $term_array, $excluded_terms )
  1160. ) );
  1161. }
  1162. return get_posts( $query_args );
  1163. }
  1164. /*
  1165. * Get previous post link that is adjacent to the current post.
  1166. *
  1167. * @since 3.7.0
  1168. *
  1169. * @param string $format Optional. Link anchor format.
  1170. * @param string $link Optional. Link permalink format.
  1171. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1172. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1173. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1174. * @return string
  1175. */
  1176. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1177. return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
  1178. }
  1179. /**
  1180. * Display previous post link that is adjacent to the current post.
  1181. *
  1182. * @since 1.5.0
  1183. * @see get_previous_post_link()
  1184. *
  1185. * @param string $format Optional. Link anchor format.
  1186. * @param string $link Optional. Link permalink format.
  1187. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1188. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1189. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1190. */
  1191. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1192. echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
  1193. }
  1194. /**
  1195. * Get next post link that is adjacent to the current post.
  1196. *
  1197. * @since 3.7.0
  1198. *
  1199. * @param string $format Optional. Link anchor format.
  1200. * @param string $link Optional. Link permalink format.
  1201. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1202. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1203. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1204. * @return string
  1205. */
  1206. function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1207. return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, false, $taxonomy );
  1208. }
  1209. /**
  1210. * Display next post link that is adjacent to the current post.
  1211. *
  1212. * @since 1.5.0
  1213. * @see get_next_post_link()
  1214. *
  1215. * @param string $format Optional. Link anchor format.
  1216. * @param string $link Optional. Link permalink format.
  1217. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1218. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1219. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1220. */
  1221. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1222. echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
  1223. }
  1224. /**
  1225. * Get adjacent post link.
  1226. *
  1227. * Can be either next post link or previous.
  1228. *
  1229. * @since 3.7.0
  1230. *
  1231. * @param string $format Link anchor format.
  1232. * @param string $link Link permalink format.
  1233. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1234. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
  1235. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1236. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1237. * @return string
  1238. */
  1239. function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1240. if ( $previous && is_attachment() )
  1241. $post = get_post( get_post()->post_parent );
  1242. else
  1243. $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
  1244. if ( ! $post ) {
  1245. $output = '';
  1246. } else {
  1247. $title = $post->post_title;
  1248. if ( empty( $post->post_title ) )
  1249. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1250. /** This filter is documented in wp-includes/post-template.php */
  1251. $title = apply_filters( 'the_title', $title, $post->ID );
  1252. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1253. $rel = $previous ? 'prev' : 'next';
  1254. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
  1255. $inlink = str_replace( '%title', $title, $link );
  1256. $inlink = str_replace( '%date', $date, $inlink );
  1257. $inlink = $string . $inlink . '</a>';
  1258. $output = str_replace( '%link', $inlink, $format );
  1259. }
  1260. $adjacent = $previous ? 'previous' : 'next';
  1261. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
  1262. }
  1263. /**
  1264. * Display adjacent post link.
  1265. *
  1266. * Can be either next post link or previous.
  1267. *
  1268. * @since 2.5.0
  1269. * @uses get_adjacent_post_link()
  1270. *
  1271. * @param string $format Link anchor format.
  1272. * @param string $link Link permalink format.
  1273. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1274. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
  1275. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1276. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1277. * @return string
  1278. */
  1279. function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1280. echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
  1281. }
  1282. /**
  1283. * Retrieve links for page numbers.
  1284. *
  1285. * @since 1.5.0
  1286. *
  1287. * @param int $pagenum Optional. Page ID.
  1288. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1289. * Otherwise, prepares the URL with esc_url_raw().
  1290. * @return string
  1291. */
  1292. function get_pagenum_link($pagenum = 1, $escape = true ) {
  1293. global $wp_rewrite;
  1294. $pagenum = (int) $pagenum;
  1295. $request = remove_query_arg( 'paged' );
  1296. $home_root = parse_url(home_url());
  1297. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1298. $home_root = preg_quote( $home_root, '|' );
  1299. $request = preg_replace('|^'. $home_root . '|i', '', $request);
  1300. $request = preg_replace('|^/+|', '', $request);
  1301. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1302. $base = trailingslashit( get_bloginfo( 'url' ) );
  1303. if ( $pagenum > 1 ) {
  1304. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1305. } else {
  1306. $result = $base . $request;
  1307. }
  1308. } else {
  1309. $qs_regex = '|\?.*?$|';
  1310. preg_match( $qs_regex, $request, $qs_match );
  1311. if ( !empty( $qs_match[0] ) ) {
  1312. $query_string = $qs_match[0];
  1313. $request = preg_replace( $qs_regex, '', $request );
  1314. } else {
  1315. $query_string = '';
  1316. }
  1317. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1318. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
  1319. $request = ltrim($request, '/');
  1320. $base = trailingslashit( get_bloginfo( 'url' ) );
  1321. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1322. $base .= $wp_rewrite->index . '/';
  1323. if ( $pagenum > 1 ) {
  1324. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1325. }
  1326. $result = $base . $request . $query_string;
  1327. }
  1328. $result = apply_filters('get_pagenum_link', $result);
  1329. if ( $escape )
  1330. return esc_url( $result );
  1331. else
  1332. return esc_url_raw( $result );
  1333. }
  1334. /**
  1335. * Retrieve next posts page link.
  1336. *
  1337. * Backported from 2.1.3 to 2.0.10.
  1338. *
  1339. * @since 2.0.10
  1340. *
  1341. * @param int $max_page Optional. Max pages.
  1342. * @return string
  1343. */
  1344. function get_next_posts_page_link($max_page = 0) {
  1345. global $paged;
  1346. if ( !is_single() ) {
  1347. if ( !$paged )
  1348. $paged = 1;
  1349. $nextpage = intval($paged) + 1;
  1350. if ( !$max_page || $max_page >= $nextpage )
  1351. return get_pagenum_link($nextpage);
  1352. }
  1353. }
  1354. /**
  1355. * Display or return the next posts page link.
  1356. *
  1357. * @since 0.71
  1358. *
  1359. * @param int $max_page Optional. Max pages.
  1360. * @param boolean $echo Optional. Echo or return;
  1361. */
  1362. function next_posts( $max_page = 0, $echo = true ) {
  1363. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1364. if ( $echo )
  1365. echo $output;
  1366. else
  1367. return $output;
  1368. }
  1369. /**
  1370. * Return the next posts page link.
  1371. *
  1372. * @since 2.7.0
  1373. *
  1374. * @param string $label Content for link text.
  1375. * @param int $max_page Optional. Max pages.
  1376. * @return string|null
  1377. */
  1378. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1379. global $paged, $wp_query;
  1380. if ( !$max_page )
  1381. $max_page = $wp_query->max_num_pages;
  1382. if ( !$paged )
  1383. $paged = 1;
  1384. $nextpage = intval($paged) + 1;
  1385. if ( null === $label )
  1386. $label = __( 'Next Page &raquo;' );
  1387. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1388. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1389. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1390. }
  1391. }
  1392. /**
  1393. * Display the next posts page link.
  1394. *
  1395. * @since 0.71
  1396. * @uses get_next_posts_link()
  1397. *
  1398. * @param string $label Content for link text.
  1399. * @param int $max_page Optional. Max pages.
  1400. */
  1401. function next_posts_link( $label = null, $max_page = 0 ) {
  1402. echo get_next_posts_link( $label, $max_page );
  1403. }
  1404. /**
  1405. * Retrieve previous posts page link.
  1406. *
  1407. * Will only return string, if not on a single page or post.
  1408. *
  1409. * Backported to 2.0.10 from 2.1.3.
  1410. *
  1411. * @since 2.0.10
  1412. *
  1413. * @return string|null
  1414. */
  1415. function get_previous_posts_page_link() {
  1416. global $paged;
  1417. if ( !is_single() ) {
  1418. $nextpage = intval($paged) - 1;
  1419. if ( $nextpage < 1 )
  1420. $nextpage = 1;
  1421. return get_pagenum_link($nextpage);
  1422. }
  1423. }
  1424. /**
  1425. * Display or return the previous posts page link.
  1426. *
  1427. * @since 0.71
  1428. *
  1429. * @param boolean $echo Optional. Echo or return;
  1430. */
  1431. function previous_posts( $echo = true ) {
  1432. $output = esc_url( get_previous_posts_page_link() );
  1433. if ( $echo )
  1434. echo $output;
  1435. else
  1436. return $output;
  1437. }
  1438. /**
  1439. * Return the previous posts page link.
  1440. *
  1441. * @since 2.7.0
  1442. *
  1443. * @param string $label Optional. Previous page link text.
  1444. * @return string|null
  1445. */
  1446. function get_previous_posts_link( $label = null ) {
  1447. global $paged;
  1448. if ( null === $label )
  1449. $label = __( '&laquo; Previous Page' );
  1450. if ( !is_single() && $paged > 1 ) {
  1451. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1452. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
  1453. }
  1454. }
  1455. /**
  1456. * Display the previous posts page link.
  1457. *
  1458. * @since 0.71
  1459. * @uses get_previous_posts_link()
  1460. *
  1461. * @param string $label Optional. Previous page link text.
  1462. */
  1463. function previous_posts_link( $label = null ) {
  1464. echo get_previous_posts_link( $label );
  1465. }
  1466. /**
  1467. * Return post pages link navigation for previous and next pages.
  1468. *
  1469. * @since 2.8
  1470. *
  1471. * @param string|array $args Optional args.
  1472. * @return string The posts link navigation.
  1473. */
  1474. function get_posts_nav_link( $args = array() ) {
  1475. global $wp_query;
  1476. $return = '';
  1477. if ( !is_singular() ) {
  1478. $defaults = array(
  1479. 'sep' => ' &#8212; ',
  1480. 'prelabel' => __('&laquo; Previous Page'),
  1481. 'nxtlabel' => __('Next Page &raquo;'),
  1482. );
  1483. $args = wp_parse_args( $args, $defaults );
  1484. $max_num_pages = $wp_query->max_num_pages;
  1485. $paged = get_query_var('paged');
  1486. //only have sep if there's both prev and next results
  1487. if ($paged < 2 || $paged >= $max_num_pages) {
  1488. $args['sep'] = '';
  1489. }
  1490. if ( $max_num_pages > 1 ) {
  1491. $return = get_previous_posts_link($args['prelabel']);
  1492. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
  1493. $return .= get_next_posts_link($args['nxtlabel']);
  1494. }
  1495. }
  1496. return $return;
  1497. }
  1498. /**
  1499. * Display post pages link navigation for previous and next pages.
  1500. *
  1501. * @since 0.71
  1502. *
  1503. * @param string $sep Optional. Separator for posts navigation links.
  1504. * @param string $prelabel Optional. Label for previous pages.
  1505. * @param string $nxtlabel Optional Label for next pages.
  1506. */
  1507. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1508. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1509. echo get_posts_nav_link($args);
  1510. }
  1511. /**
  1512. * Retrieve comments page number link.
  1513. *
  1514. * @since 2.7.0
  1515. *
  1516. * @param int $pagenum Optional. Page number.
  1517. * @return string
  1518. */
  1519. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1520. global $wp_rewrite;
  1521. $pagenum = (int) $pagenum;
  1522. $result = get_permalink();
  1523. if ( 'newest' == get_option('default_comments_page') ) {
  1524. if ( $pagenum != $max_page ) {
  1525. if ( $wp_rewrite->using_permalinks() )
  1526. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1527. else
  1528. $result = add_query_arg( 'cpage', $pagenum, $result );
  1529. }
  1530. } elseif ( $pagenum > 1 ) {
  1531. if ( $wp_rewrite->using_permalinks() )
  1532. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1533. else
  1534. $result = add_query_arg( 'cpage', $pagenum, $result );
  1535. }
  1536. $result .= '#comments';
  1537. $result = apply_filters('get_comments_pagenum_link', $result);
  1538. return $result;
  1539. }
  1540. /**
  1541. * Return the link to next comments page.
  1542. *
  1543. * @since 2.7.1
  1544. *
  1545. * @param string $label Optional. Label for link text.
  1546. * @param int $max_page Optional. Max page.
  1547. * @return string|null
  1548. */
  1549. function get_next_comments_link( $label = '', $max_page = 0 ) {
  1550. global $wp_query;
  1551. if ( !is_singular() || !get_option('page_comments') )
  1552. return;
  1553. $page = get_query_var('cpage');
  1554. $nextpage = intval($page) + 1;
  1555. if ( empty($max_page) )
  1556. $max_page = $wp_query->max_num_comment_pages;
  1557. if ( empty($max_page) )
  1558. $max_page = get_comment_pages_count();
  1559. if ( $nextpage > $max_page )
  1560. return;
  1561. if ( empty($label) )
  1562. $label = __('Newer Comments &raquo;');
  1563. 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>';
  1564. }
  1565. /**
  1566. * Display the link to next comments page.
  1567. *
  1568. * @since 2.7.0
  1569. *
  1570. * @param string $label Optional. Label for link text.
  1571. * @param int $max_page Optional. Max page.
  1572. */
  1573. function next_comments_link( $label = '', $max_page = 0 ) {
  1574. echo get_next_comments_link( $label, $max_page );
  1575. }
  1576. /**
  1577. * Return the previous comments page link.
  1578. *
  1579. * @since 2.7.1
  1580. *
  1581. * @param string $label Optional. Label for comments link text.
  1582. * @return string|null
  1583. */
  1584. function get_previous_comments_link( $label = '' ) {
  1585. if ( !is_singular() || !get_option('page_comments') )
  1586. return;
  1587. $page = get_query_var('cpage');
  1588. if ( intval($page) <= 1 )
  1589. return;
  1590. $prevpage = intval($page) - 1;
  1591. if ( empty($label) )
  1592. $label = __('&laquo; Older Comments');
  1593. 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>';
  1594. }
  1595. /**
  1596. * Display the previous comments page link.
  1597. *
  1598. * @since 2.7.0
  1599. *
  1600. * @param string $label Optional. Label for comments link text.
  1601. */
  1602. function previous_comments_link( $label = '' ) {
  1603. echo get_previous_comments_link( $label );
  1604. }
  1605. /**
  1606. * Create pagination links for the comments on the current post.
  1607. *
  1608. * @see paginate_links()
  1609. * @since 2.7.0
  1610. *
  1611. * @param string|array $args Optional args. See paginate_links().
  1612. * @return string Markup for pagination links.
  1613. */
  1614. function paginate_comments_links($args = array()) {
  1615. global $wp_rewrite;
  1616. if ( !is_singular() || !get_option('page_comments') )
  1617. return;
  1618. $page = get_query_var('cpage');
  1619. if ( !$page )
  1620. $page = 1;
  1621. $max_page = get_comment_pages_count();
  1622. $defaults = array(
  1623. 'base' => add_query_arg( 'cpage', '%#%' ),
  1624. 'format' => '',
  1625. 'total' => $max_page,
  1626. 'current' => $page,
  1627. 'echo' => true,
  1628. 'add_fragment' => '#comments'
  1629. );
  1630. if ( $wp_rewrite->using_permalinks() )
  1631. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
  1632. $args = wp_parse_args( $args, $defaults );
  1633. $page_links = paginate_links( $args );
  1634. if ( $args['echo'] )
  1635. echo $page_links;
  1636. else
  1637. return $page_links;
  1638. }
  1639. /**
  1640. * Retrieve the Press This bookmarklet link.
  1641. *
  1642. * Use this in 'a' element 'href' attribute.
  1643. *
  1644. * @since 2.6.0
  1645. *
  1646. * @return string
  1647. */
  1648. function get_shortcut_link() {
  1649. // In case of breaking changes, version this. #WP20071
  1650. $link = "javascript:
  1651. var d=document,
  1652. w=window,
  1653. e=w.getSelection,
  1654. k=d.getSelection,
  1655. x=d.selection,
  1656. s=(e?e():(k)?k():(x?x.createRange().text:0)),
  1657. f='" . admin_url('press-this.php') . "',
  1658. l=d.location,
  1659. e=encodeURIComponent,
  1660. u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
  1661. a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
  1662. if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
  1663. void(0)";
  1664. $link = str_replace(array("\r", "\n", "\t"), '', $link);
  1665. return apply_filters('shortcut_link', $link);
  1666. }
  1667. /**
  1668. * Retrieve the home url for the current site.
  1669. *
  1670. * Returns the 'home' option with the appropriate protocol, 'https' if
  1671. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1672. * overridden.
  1673. *
  1674. * @package WordPress
  1675. * @since 3.0.0
  1676. *
  1677. * @uses get_home_url()
  1678. *
  1679. * @param string $path (optional) Path relative to the home url.
  1680. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  1681. * @return string Home url link with optional path appended.
  1682. */
  1683. function home_url( $path = '', $scheme = null ) {
  1684. return get_home_url( null, $path, $scheme );
  1685. }
  1686. /**
  1687. * Retrieve the home url for a given site.
  1688. *
  1689. * Returns the 'home' option with the appropriate protocol, 'https' if
  1690. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1691. * overridden.
  1692. *
  1693. * @package WordPress
  1694. * @since 3.0.0
  1695. *
  1696. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  1697. * @param string $path (optional) Path relative to the home url.
  1698. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  1699. * @return string Home url link with optional path appended.
  1700. */
  1701. function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  1702. $orig_scheme = $scheme;
  1703. if ( empty( $blog_id ) || !is_multisite() ) {
  1704. $url = get_option( 'home' );
  1705. } else {
  1706. switch_to_blog( $blog_id );
  1707. $url = get_option( 'home' );
  1708. restore_current_blog();
  1709. }
  1710. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
  1711. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
  1712. $scheme = 'https';
  1713. else
  1714. $scheme = parse_url( $url, PHP_URL_SCHEME );
  1715. }
  1716. $url = set_url_scheme( $url, $scheme );
  1717. if ( $path && is_string( $path ) )
  1718. $url .= '/' . ltrim( $path, '/' );
  1719. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
  1720. }
  1721. /**
  1722. * Retrieve the site url for the current site.
  1723. *
  1724. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  1725. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1726. * overridden.
  1727. *
  1728. * @package WordPress
  1729. * @since 2.6.0
  1730. *
  1731. * @uses get_site_url()
  1732. *
  1733. * @param string $path Optional. Path relative to the site url.
  1734. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  1735. * @return string Site url link with optional path appended.
  1736. */
  1737. function site_url( $path = '', $scheme = null ) {
  1738. return get_site_url( null, $path, $scheme );
  1739. }
  1740. /**
  1741. * Retrieve the site url for a given site.
  1742. *
  1743. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  1744. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1745. * overridden.
  1746. *
  1747. * @package WordPress
  1748. * @since 3.0.0
  1749. *
  1750. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  1751. * @param string $path Optional. Path relative to the site url.
  1752. * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
  1753. * @return string Site url link with optional path appended.
  1754. */
  1755. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
  1756. if ( empty( $blog_id ) || !is_multisite() ) {
  1757. $url = get_option( 'siteurl' );
  1758. } else {
  1759. switch_to_blog( $blog_id );
  1760. $url = get_option( 'siteurl' );
  1761. restore_current_blog();
  1762. }
  1763. $url = set_url_scheme( $url, $scheme );
  1764. if ( $path && is_string( $path ) )
  1765. $url .= '/' . ltrim( $path, '/' );
  1766. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
  1767. }
  1768. /**
  1769. * Retrieve the url to the admin area for the current site.
  1770. *
  1771. * @package WordPress
  1772. * @since 2.6.0
  1773. *
  1774. * @param string $path Optional path relative to the admin url.
  1775. * @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.
  1776. * @return string Admin url link with optional path appended.
  1777. */
  1778. function admin_url( $path = '', $scheme = 'admin' ) {
  1779. return get_admin_url( null, $path, $scheme );
  1780. }
  1781. /**
  1782. * Retrieve the url to the admin area for a given site.
  1783. *
  1784. * @package WordPress
  1785. * @since 3.0.0
  1786. *
  1787. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  1788. * @param string $path Optional path relative to the admin url.
  1789. * @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.
  1790. * @return string Admin url link with optional path appended.
  1791. */
  1792. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
  1793. $url = get_site_url($blog_id, 'wp-admin/', $scheme);
  1794. if ( $path && is_string( $path ) )
  1795. $url .= ltrim( $path, '/' );
  1796. return apply_filters( 'admin_url', $url, $path, $blog_id );
  1797. }
  1798. /**
  1799. * Retrieve the url to the includes directory.
  1800. *
  1801. * @package WordPress
  1802. * @since 2.6.0
  1803. *
  1804. * @param string $path Optional. Path relative to the includes url.
  1805. * @param string $scheme Optional. Scheme to give the includes url context.
  1806. * @return string Includes url link with optional path appended.
  1807. */
  1808. function includes_url( $path = '', $scheme = null ) {
  1809. $url = site_url( '/' . WPINC . '/', $scheme );
  1810. if ( $path && is_string( $path ) )
  1811. $url .= ltrim($path, '/');
  1812. return apply_filters('includes_url', $url, $path);
  1813. }
  1814. /**
  1815. * Retrieve the url to the content directory.
  1816. *
  1817. * @package WordPress
  1818. * @since 2.6.0
  1819. *
  1820. * @param string $path Optional. Path relative to the content url.
  1821. * @return string Content url link with optional path appended.
  1822. */
  1823. function content_url($path = '') {
  1824. $url = set_url_scheme( WP_CONTENT_URL );
  1825. if ( $path && is_string( $path ) )
  1826. $url .= '/' . ltrim($path, '/');
  1827. return apply_filters('content_url', $url, $path);
  1828. }
  1829. /**
  1830. * Retrieve the url to the plugins directory or to a specific file within that directory.
  1831. * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
  1832. *
  1833. * @package WordPress
  1834. * @since 2.6.0
  1835. *
  1836. * @param string $path Optional. Path relative to the plugins url.
  1837. * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
  1838. * @return string Plugins url link with optional path appended.
  1839. */
  1840. function plugins_url($path = '', $plugin = '') {
  1841. $mu_plugin_dir = WPMU_PLUGIN_DIR;
  1842. foreach ( array('path', 'plugin', 'mu_plugin_dir') as $var ) {
  1843. $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
  1844. $$var = preg_replace('|/+|', '/', $$var);
  1845. }
  1846. if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
  1847. $url = WPMU_PLUGIN_URL;
  1848. else
  1849. $url = WP_PLUGIN_URL;
  1850. $url = set_url_scheme( $url );
  1851. if ( !empty($plugin) && is_string($plugin) ) {
  1852. $folder = dirname(plugin_basename($plugin));
  1853. if ( '.' != $folder )
  1854. $url .= '/' . ltrim($folder, '/');
  1855. }
  1856. if ( $path && is_string( $path ) )
  1857. $url .= '/' . ltrim($path, '/');
  1858. return apply_filters('plugins_url', $url, $path, $plugin);
  1859. }
  1860. /**
  1861. * Retrieve the site url for the current network.
  1862. *
  1863. * Returns the site url with the appropriate protocol, 'https' if
  1864. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1865. * overridden.
  1866. *
  1867. * @package WordPress
  1868. * @since 3.0.0
  1869. *
  1870. * @param string $path Optional. Path relative to the site url.
  1871. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  1872. * @return string Site url link with optional path appended.
  1873. */
  1874. function network_site_url( $path = '', $scheme = null ) {
  1875. if ( ! is_multisite() )
  1876. return site_url($path, $scheme);
  1877. $current_site = get_current_site();
  1878. if ( 'relative' == $scheme )
  1879. $url = $current_site->path;
  1880. else
  1881. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  1882. if ( $path && is_string( $path ) )
  1883. $url .= ltrim( $path, '/' );
  1884. return apply_filters( 'network_site_url', $url, $path, $scheme );
  1885. }
  1886. /**
  1887. * Retrieve the home url for the current network.
  1888. *
  1889. * Returns the home url with the appropriate protocol, 'https' if
  1890. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1891. * overridden.
  1892. *
  1893. * @package WordPress
  1894. * @since 3.0.0
  1895. *
  1896. * @param string $path (optional) Path relative to the home url.
  1897. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  1898. * @return string Home url link with optional path appended.
  1899. */
  1900. function network_home_url( $path = '', $scheme = null ) {
  1901. if ( ! is_multisite() )
  1902. return home_url($path, $scheme);
  1903. $current_site = get_current_site();
  1904. $orig_scheme = $scheme;
  1905. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
  1906. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
  1907. if ( 'relative' == $scheme )
  1908. $url = $current_site->path;
  1909. else
  1910. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  1911. if ( $path && is_string( $path ) )
  1912. $url .= ltrim( $path, '/' );
  1913. return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
  1914. }
  1915. /**
  1916. * Retrieve the url to the admin area for the network.
  1917. *
  1918. * @package WordPress
  1919. * @since 3.0.0
  1920. *
  1921. * @param string $path Optional path relative to the admin url.
  1922. * @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.
  1923. * @return string Admin url link with optional path appended.
  1924. */
  1925. function network_admin_url( $path = '', $scheme = 'admin' ) {
  1926. if ( ! is_multisite() )
  1927. return admin_url( $path, $scheme );
  1928. $url = network_site_url('wp-admin/network/', $scheme);
  1929. if ( $path && is_string( $path ) )
  1930. $url .= ltrim($path, '/');
  1931. return apply_filters('network_admin_url', $url, $path);
  1932. }
  1933. /**
  1934. * Retrieve the url to the admin area for the current user.
  1935. *
  1936. * @package WordPress
  1937. * @since 3.0.0
  1938. *
  1939. * @param string $path Optional path relative to the admin url.
  1940. * @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.
  1941. * @return string Admin url link with optional path appended.
  1942. */
  1943. function user_admin_url( $path = '', $scheme = 'admin' ) {
  1944. $url = network_site_url('wp-admin/user/', $scheme);
  1945. if ( $path && is_string( $path ) )
  1946. $url .= ltrim($path, '/');
  1947. return apply_filters('user_admin_url', $url, $path);
  1948. }
  1949. /**
  1950. * Retrieve the url to the admin area for either the current blog or the network depending on context.
  1951. *
  1952. * @package WordPress
  1953. * @since 3.1.0
  1954. *
  1955. * @param string $path Optional path relative to the admin url.
  1956. * @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.
  1957. * @return string Admin url link with optional path appended.
  1958. */
  1959. function self_admin_url($path = '', $scheme = 'admin') {
  1960. if ( is_network_admin() )
  1961. return network_admin_url($path, $scheme);
  1962. elseif ( is_user_admin() )
  1963. return user_admin_url($path, $scheme);
  1964. else
  1965. return admin_url($path, $scheme);
  1966. }
  1967. /**
  1968. * Set the scheme for a URL
  1969. *
  1970. * @since 3.4.0
  1971. *
  1972. * @param string $url Absolute url that includes a scheme
  1973. * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
  1974. * @return string $url URL with chosen scheme.
  1975. */
  1976. function set_url_scheme( $url, $scheme = null ) {
  1977. $orig_scheme = $scheme;
  1978. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
  1979. if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
  1980. $scheme = 'https';
  1981. elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
  1982. $scheme = 'https';
  1983. elseif ( ( 'admin' == $scheme ) && force_ssl_admin() )
  1984. $scheme = 'https';
  1985. else
  1986. $scheme = ( is_ssl() ? 'https' : 'http' );
  1987. }
  1988. $url = trim( $url );
  1989. if ( substr( $url, 0, 2 ) === '//' )
  1990. $url = 'http:' . $url;
  1991. if ( 'relative' == $scheme ) {
  1992. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
  1993. if ( $url !== '' && $url[0] === '/' )
  1994. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
  1995. } else {
  1996. $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
  1997. }
  1998. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
  1999. }
  2000. /**
  2001. * Get the URL to the user's dashboard.
  2002. *
  2003. * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
  2004. * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
  2005. * primary blog is returned.
  2006. *
  2007. * @since 3.1.0
  2008. *
  2009. * @param int $user_id User ID
  2010. * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
  2011. * @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.
  2012. * @return string Dashboard url link with optional path appended.
  2013. */
  2014. function get_dashboard_url( $user_id, $path = '', $scheme = 'admin' ) {
  2015. $user_id = (int) $user_id;
  2016. $blogs = get_blogs_of_user( $user_id );
  2017. if ( ! is_super_admin() && empty($blogs) ) {
  2018. $url = user_admin_url( $path, $scheme );
  2019. } elseif ( ! is_multisite() ) {
  2020. $url = admin_url( $path, $scheme );
  2021. } else {
  2022. $current_blog = get_current_blog_id();
  2023. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
  2024. $url = admin_url( $path, $scheme );
  2025. } else {
  2026. $active = get_active_blog_for_user( $user_id );
  2027. if ( $active )
  2028. $url = get_admin_url( $active->blog_id, $path, $scheme );
  2029. else
  2030. $url = user_admin_url( $path, $scheme );
  2031. }
  2032. }
  2033. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
  2034. }
  2035. /**
  2036. * Get the URL to the user's profile editor.
  2037. *
  2038. * @since 3.1.0
  2039. *
  2040. * @param int $user User ID
  2041. * @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.
  2042. * @return string Dashboard url link with optional path appended.
  2043. */
  2044. function get_edit_profile_url( $user, $scheme = 'admin' ) {
  2045. $user = (int) $user;
  2046. if ( is_user_admin() )
  2047. $url = user_admin_url( 'profile.php', $scheme );
  2048. elseif ( is_network_admin() )
  2049. $url = network_admin_url( 'profile.php', $scheme );
  2050. else
  2051. $url = get_dashboard_url( $user, 'profile.php', $scheme );
  2052. return apply_filters( 'edit_profile_url', $url, $user, $scheme);
  2053. }
  2054. /**
  2055. * Output rel=canonical for singular queries.
  2056. *
  2057. * @package WordPress
  2058. * @since 2.9.0
  2059. */
  2060. function rel_canonical() {
  2061. if ( !is_singular() )
  2062. return;
  2063. global $wp_the_query;
  2064. if ( !$id = $wp_the_query->get_queried_object_id() )
  2065. return;
  2066. $link = get_permalink( $id );
  2067. if ( $page = get_query_var('cpage') )
  2068. $link = get_comments_pagenum_link( $page );
  2069. echo "<link rel='canonical' href='$link' />\n";
  2070. }
  2071. /**
  2072. * Return a shortlink for a post, page, attachment, or blog.
  2073. *
  2074. * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
  2075. * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
  2076. * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
  2077. * via the get_shortlink filter.
  2078. *
  2079. * @since 3.0.0.
  2080. *
  2081. * @param int $id A post or blog id. Default is 0, which means the current post or blog.
  2082. * @param string $context Whether the id is a 'blog' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the id and context. Default is 'post'.
  2083. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
  2084. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
  2085. */
  2086. function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
  2087. // Allow plugins to short-circuit this function.
  2088. $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
  2089. if ( false !== $shortlink )
  2090. return $shortlink;
  2091. global $wp_query;
  2092. $post_id = 0;
  2093. if ( 'query' == $context && is_singular() ) {
  2094. $post_id = $wp_query->get_queried_object_id();
  2095. $post = get_post( $post_id );
  2096. } elseif ( 'post' == $context ) {
  2097. $post = get_post( $id );
  2098. if ( ! empty( $post->ID ) )
  2099. $post_id = $post->ID;
  2100. }
  2101. $shortlink = '';
  2102. // Return p= link for all public post types.
  2103. if ( ! empty( $post_id ) ) {
  2104. $post_type = get_post_type_object( $post->post_type );
  2105. if ( $post_type->public )
  2106. $shortlink = home_url('?p=' . $post_id);
  2107. }
  2108. return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
  2109. }
  2110. /**
  2111. * Inject rel=shortlink into head if a shortlink is defined for the current page.
  2112. *
  2113. * Attached to the wp_head action.
  2114. *
  2115. * @since 3.0.0
  2116. *
  2117. * @uses wp_get_shortlink()
  2118. */
  2119. function wp_shortlink_wp_head() {
  2120. $shortlink = wp_get_shortlink( 0, 'query' );
  2121. if ( empty( $shortlink ) )
  2122. return;
  2123. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
  2124. }
  2125. /**
  2126. * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
  2127. *
  2128. * Attached to the wp action.
  2129. *
  2130. * @since 3.0.0
  2131. *
  2132. * @uses wp_get_shortlink()
  2133. */
  2134. function wp_shortlink_header() {
  2135. if ( headers_sent() )
  2136. return;
  2137. $shortlink = wp_get_shortlink(0, 'query');
  2138. if ( empty($shortlink) )
  2139. return;
  2140. header('Link: <' . $shortlink . '>; rel=shortlink', false);
  2141. }
  2142. /**
  2143. * Display the Short Link for a Post
  2144. *
  2145. * Must be called from inside "The Loop"
  2146. *
  2147. * Call like the_shortlink(__('Shortlinkage FTW'))
  2148. *
  2149. * @since 3.0.0
  2150. *
  2151. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
  2152. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
  2153. * @param string $before Optional HTML to display before the link.
  2154. * @param string $after Optional HTML to display after the link.
  2155. */
  2156. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
  2157. $post = get_post();
  2158. if ( empty( $text ) )
  2159. $text = __('This is the short link.');
  2160. if ( empty( $title ) )
  2161. $title = the_title_attribute( array( 'echo' => false ) );
  2162. $shortlink = wp_get_shortlink( $post->ID );
  2163. if ( !empty( $shortlink ) ) {
  2164. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
  2165. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
  2166. echo $before, $link, $after;
  2167. }
  2168. }