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

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

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