PageRenderTime 146ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 3139 lines | 2103 code | 211 blank | 825 comment | 191 complexity | 779d8ccd21f7f98c5f3a242feeb14a05 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.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, $leavename, $sample);
  131. elseif ( $post->post_type == 'attachment' )
  132. return get_attachment_link( $post, $leavename );
  133. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
  134. return get_post_permalink($post, $leavename, $sample);
  135. $permalink = get_option('permalink_structure');
  136. /**
  137. * Filter the permalink structure for a post before token replacement occurs.
  138. *
  139. * Only applies to posts with post_type of 'post'.
  140. *
  141. * @since 3.0.0
  142. *
  143. * @param string $permalink The site's permalink structure.
  144. * @param WP_Post $post The post in question.
  145. * @param bool $leavename Whether to keep the post name.
  146. */
  147. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
  148. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  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|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  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 $text 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( $text = null, $before = '', $after = '', $id = 0 ) {
  1068. if ( ! $post = get_post( $id ) ) {
  1069. return;
  1070. }
  1071. if ( ! $url = get_edit_post_link( $post->ID ) ) {
  1072. return;
  1073. }
  1074. if ( null === $text ) {
  1075. $text = __( 'Edit This' );
  1076. }
  1077. $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
  1078. /**
  1079. * Filter the post edit link anchor tag.
  1080. *
  1081. * @since 2.3.0
  1082. *
  1083. * @param string $link Anchor tag for the edit link.
  1084. * @param int $post_id Post ID.
  1085. * @param string $text Anchor text.
  1086. */
  1087. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
  1088. }
  1089. /**
  1090. * Retrieve delete posts link for post.
  1091. *
  1092. * Can be used within the WordPress loop or outside of it, with any post type.
  1093. *
  1094. * @since 2.9.0
  1095. *
  1096. * @param int $id Optional. Post ID.
  1097. * @param string $deprecated Not used.
  1098. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  1099. * @return string
  1100. */
  1101. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  1102. if ( ! empty( $deprecated ) )
  1103. _deprecated_argument( __FUNCTION__, '3.0' );
  1104. if ( !$post = get_post( $id ) )
  1105. return;
  1106. $post_type_object = get_post_type_object( $post->post_type );
  1107. if ( !$post_type_object )
  1108. return;
  1109. if ( !current_user_can( 'delete_post', $post->ID ) )
  1110. return;
  1111. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  1112. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  1113. /**
  1114. * Filter the post delete link.
  1115. *
  1116. * @since 2.9.0
  1117. *
  1118. * @param string $link The delete link.
  1119. * @param int $post_id Post ID.
  1120. * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
  1121. */
  1122. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  1123. }
  1124. /**
  1125. * Retrieve edit comment link.
  1126. *
  1127. * @since 2.3.0
  1128. *
  1129. * @param int $comment_id Optional. Comment ID.
  1130. * @return string
  1131. */
  1132. function get_edit_comment_link( $comment_id = 0 ) {
  1133. $comment = get_comment( $comment_id );
  1134. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  1135. return;
  1136. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  1137. /**
  1138. * Filter the comment edit link.
  1139. *
  1140. * @since 2.3.0
  1141. *
  1142. * @param string $location The edit link.
  1143. */
  1144. return apply_filters( 'get_edit_comment_link', $location );
  1145. }
  1146. /**
  1147. * Display edit comment link with formatting.
  1148. *
  1149. * @since 1.0.0
  1150. *
  1151. * @param string $text Optional. Anchor text.
  1152. * @param string $before Optional. Display before edit link.
  1153. * @param string $after Optional. Display after edit link.
  1154. */
  1155. function edit_comment_link( $text = null, $before = '', $after = '' ) {
  1156. global $comment;
  1157. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1158. return;
  1159. }
  1160. if ( null === $text ) {
  1161. $text = __( 'Edit This' );
  1162. }
  1163. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
  1164. /**
  1165. * Filter the comment edit link anchor tag.
  1166. *
  1167. * @since 2.3.0
  1168. *
  1169. * @param string $link Anchor tag for the edit link.
  1170. * @param int $comment_id Comment ID.
  1171. * @param string $text Anchor text.
  1172. */
  1173. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
  1174. }
  1175. /**
  1176. * Display edit bookmark (literally a URL external to blog) link.
  1177. *
  1178. * @since 2.7.0
  1179. *
  1180. * @param int $link Optional. Bookmark ID.
  1181. * @return string
  1182. */
  1183. function get_edit_bookmark_link( $link = 0 ) {
  1184. $link = get_bookmark( $link );
  1185. if ( !current_user_can('manage_links') )
  1186. return;
  1187. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  1188. /**
  1189. * Filter the bookmark (link) edit link.
  1190. *
  1191. * @since 2.7.0
  1192. *
  1193. * @param string $location The edit link.
  1194. * @param int $link_id Bookmark ID.
  1195. */
  1196. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  1197. }
  1198. /**
  1199. * Display edit bookmark (literally a URL external to blog) link anchor content.
  1200. *
  1201. * @since 2.7.0
  1202. *
  1203. * @param string $link Optional. Anchor text.
  1204. * @param string $before Optional. Display before edit link.
  1205. * @param string $after Optional. Display after edit link.
  1206. * @param int $bookmark Optional. Bookmark ID.
  1207. */
  1208. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  1209. $bookmark = get_bookmark($bookmark);
  1210. if ( !current_user_can('manage_links') )
  1211. return;
  1212. if ( empty($link) )
  1213. $link = __('Edit This');
  1214. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
  1215. /**
  1216. * Filter the bookmark edit link anchor tag.
  1217. *
  1218. * @since 2.7.0
  1219. *
  1220. * @param string $link Anchor tag for the edit link.
  1221. * @param int $link_id Bookmark ID.
  1222. */
  1223. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  1224. }
  1225. /**
  1226. * Retrieve edit user link
  1227. *
  1228. * @since 3.5.0
  1229. *
  1230. * @param int $user_id Optional. User ID. Defaults to the current user.
  1231. * @return string URL to edit user page or empty string.
  1232. */
  1233. function get_edit_user_link( $user_id = null ) {
  1234. if ( ! $user_id )
  1235. $user_id = get_current_user_id();
  1236. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
  1237. return '';
  1238. $user = get_userdata( $user_id );
  1239. if ( ! $user )
  1240. return '';
  1241. if ( get_current_user_id() == $user->ID )
  1242. $link = get_edit_profile_url( $user->ID );
  1243. else
  1244. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  1245. /**
  1246. * Filter the user edit link.
  1247. *
  1248. * @since 3.5.0
  1249. *
  1250. * @param string $link The edit link.
  1251. * @param int $user_id User ID.
  1252. */
  1253. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  1254. }
  1255. // Navigation links
  1256. /**
  1257. * Retrieve previous post that is adjacent to current post.
  1258. *
  1259. * @since 1.5.0
  1260. *
  1261. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1262. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1263. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1264. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1265. */
  1266. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1267. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
  1268. }
  1269. /**
  1270. * Retrieve next post that is adjacent to current post.
  1271. *
  1272. * @since 1.5.0
  1273. *
  1274. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1275. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1276. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1277. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1278. */
  1279. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1280. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
  1281. }
  1282. /**
  1283. * Retrieve adjacent post.
  1284. *
  1285. * Can either be next or previous post.
  1286. *
  1287. * @since 2.5.0
  1288. *
  1289. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1290. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1291. * @param bool $previous Optional. Whether to retrieve previous post.
  1292. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1293. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1294. */
  1295. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1296. global $wpdb;
  1297. if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
  1298. return null;
  1299. $current_post_date = $post->post_date;
  1300. $join = '';
  1301. $where = '';
  1302. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1303. $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";
  1304. $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
  1305. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
  1306. // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
  1307. if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  1308. _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
  1309. $excluded_terms = explode( ' and ', $excluded_terms );
  1310. } else {
  1311. $excluded_terms = explode( ',', $excluded_terms );
  1312. }
  1313. $excluded_terms = array_map( 'intval', $excluded_terms );
  1314. }
  1315. if ( $in_same_term ) {
  1316. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
  1317. return '';
  1318. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1319. // Remove any exclusions from the term array to include.
  1320. $term_array = array_diff( $term_array, (array) $excluded_terms );
  1321. $term_array = array_map( 'intval', $term_array );
  1322. if ( ! $term_array || is_wp_error( $term_array ) )
  1323. return '';
  1324. $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
  1325. }
  1326. if ( ! empty( $excluded_terms ) ) {
  1327. $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )';
  1328. }
  1329. }
  1330. $adjacent = $previous ? 'previous' : 'next';
  1331. $op = $previous ? '<' : '>';
  1332. $order = $previous ? 'DESC' : 'ASC';
  1333. /**
  1334. * Filter the JOIN clause in the SQL for an adjacent post query.
  1335. *
  1336. * The dynamic portion of the hook name, $adjacent, refers to the type
  1337. * of adjacency, 'next' or 'previous'.
  1338. *
  1339. * @since 2.5.0
  1340. *
  1341. * @param string $join The JOIN clause in the SQL.
  1342. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1343. * @param array $excluded_terms Array of excluded term IDs.
  1344. */
  1345. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
  1346. /**
  1347. * Filter the WHERE clause in the SQL for an adjacent post query.
  1348. *
  1349. * The dynamic portion of the hook name, $adjacent, refers to the type
  1350. * of adjacency, 'next' or 'previous'.
  1351. *
  1352. * @since 2.5.0
  1353. *
  1354. * @param string $where The WHERE clause in the SQL.
  1355. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1356. * @param array $excluded_terms Array of excluded term IDs.
  1357. */
  1358. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
  1359. /**
  1360. * Filter the ORDER BY clause in the SQL for an adjacent post query.
  1361. *
  1362. * The dynamic portion of the hook name, $adjacent, refers to the type
  1363. * of adjacency, 'next' or 'previous'.
  1364. *
  1365. * @since 2.5.0
  1366. *
  1367. * @param string $order_by The ORDER BY clause in the SQL.
  1368. */
  1369. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  1370. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1371. $query_key = 'adjacent_post_' . md5( $query );
  1372. $result = wp_cache_get( $query_key, 'counts' );
  1373. if ( false !== $result ) {
  1374. if ( $result )
  1375. $result = get_post( $result );
  1376. return $result;
  1377. }
  1378. $result = $wpdb->get_var( $query );
  1379. if ( null === $result )
  1380. $result = '';
  1381. wp_cache_set( $query_key, $result, 'counts' );
  1382. if ( $result )
  1383. $result = get_post( $result );
  1384. return $result;
  1385. }
  1386. /**
  1387. * Get adjacent post relational link.
  1388. *
  1389. * Can either be next or previous post relational link.
  1390. *
  1391. * @since 2.8.0
  1392. *
  1393. * @param string $title Optional. Link title format.
  1394. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1395. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1396. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1397. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1398. * @return string
  1399. */
  1400. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1401. if ( $previous && is_attachment() && $post = get_post() )
  1402. $post = get_post( $post->post_parent );
  1403. else
  1404. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1405. if ( empty( $post ) )
  1406. return;
  1407. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
  1408. if ( empty( $post_title ) )
  1409. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1410. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1411. $title = str_replace( '%title', $post_title, $title );
  1412. $title = str_replace( '%date', $date, $title );
  1413. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1414. $link .= esc_attr( $title );
  1415. $link .= "' href='" . get_permalink( $post ) . "' />\n";
  1416. $adjacent = $previous ? 'previous' : 'next';
  1417. /**
  1418. * Filter the adjacent post relational link.
  1419. *
  1420. * The dynamic portion of the hook name, $adjacent, refers to the type
  1421. * of adjacency, 'next' or 'previous'.
  1422. *
  1423. * @since 2.8.0
  1424. *
  1425. * @param string $link The relational link.
  1426. */
  1427. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1428. }
  1429. /**
  1430. * Display relational links for the posts adjacent to the current post.
  1431. *
  1432. * @since 2.8.0
  1433. *
  1434. * @param string $title Optional. Link title format.
  1435. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1436. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1437. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1438. */
  1439. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1440. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1441. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1442. }
  1443. /**
  1444. * Display relational links for the posts adjacent to the current post for single post pages.
  1445. *
  1446. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1447. * @since 3.0.0
  1448. *
  1449. */
  1450. function adjacent_posts_rel_link_wp_head() {
  1451. if ( ! is_single() || is_attachment() ) {
  1452. return;
  1453. }
  1454. adjacent_posts_rel_link();
  1455. }
  1456. /**
  1457. * Display relational link for the next post adjacent to the current post.
  1458. *
  1459. * @since 2.8.0
  1460. *
  1461. * @param string $title Optional. Link title format.
  1462. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1463. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1464. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1465. */
  1466. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1467. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1468. }
  1469. /**
  1470. * Display relational link for the previous post adjacent to the current post.
  1471. *
  1472. * @since 2.8.0
  1473. *
  1474. * @param string $title Optional. Link title format.
  1475. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1476. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
  1477. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1478. */
  1479. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1480. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1481. }
  1482. /**
  1483. * Retrieve boundary post.
  1484. *
  1485. * Boundary being either the first or last post by publish date within the constraints specified
  1486. * by $in_same_term or $excluded_terms.
  1487. *
  1488. * @since 2.8.0
  1489. *
  1490. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
  1491. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1492. * @param bool $start Optional. Whether to retrieve first or last post.
  1493. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1494. * @return mixed Array containing the boundary post object if successful, null otherwise.
  1495. */
  1496. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
  1497. $post = get_post();
  1498. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
  1499. return null;
  1500. $query_args = array(
  1501. 'posts_per_page' => 1,
  1502. 'order' => $start ? 'ASC' : 'DESC',
  1503. 'update_post_term_cache' => false,
  1504. 'update_post_meta_cache' => false
  1505. );
  1506. $term_array = array();
  1507. if ( ! is_array( $excluded_terms ) ) {
  1508. if ( ! empty( $excluded_terms ) )
  1509. $excluded_terms = explode( ',', $excluded_terms );
  1510. else
  1511. $excluded_terms = array();
  1512. }
  1513. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1514. if ( $in_same_term )
  1515. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1516. if ( ! empty( $excluded_terms ) ) {
  1517. $excluded_terms = array_map( 'intval', $excluded_terms );
  1518. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1519. $inverse_terms = array();
  1520. foreach ( $excluded_terms as $excluded_term )
  1521. $inverse_terms[] = $excluded_term * -1;
  1522. $excluded_terms = $inverse_terms;
  1523. }
  1524. $query_args[ 'tax_query' ] = array( array(
  1525. 'taxonomy' => $taxonomy,
  1526. 'terms' => array_merge( $term_array, $excluded_terms )
  1527. ) );
  1528. }
  1529. return get_posts( $query_args );
  1530. }
  1531. /*
  1532. * Get previous post link that is adjacent to the current post.
  1533. *
  1534. * @since 3.7.0
  1535. *
  1536. * @param string $format Optional. Link anchor format.
  1537. * @param string $link Optional. Link permalink format.
  1538. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1539. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1540. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1541. * @return string
  1542. */
  1543. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1544. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
  1545. }
  1546. /**
  1547. * Display previous post link that is adjacent to the current post.
  1548. *
  1549. * @since 1.5.0
  1550. * @see get_previous_post_link()
  1551. *
  1552. * @param string $format Optional. Link anchor format.
  1553. * @param string $link Optional. Link permalink format.
  1554. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1555. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1556. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1557. */
  1558. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1559. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1560. }
  1561. /**
  1562. * Get next post link that is adjacent to the current post.
  1563. *
  1564. * @since 3.7.0
  1565. *
  1566. * @param string $format Optional. Link anchor format.
  1567. * @param string $link Optional. Link permalink format.
  1568. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1569. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1570. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1571. * @return string
  1572. */
  1573. function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1574. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
  1575. }
  1576. /**
  1577. * Display next post link that is adjacent to the current post.
  1578. *
  1579. * @since 1.5.0
  1580. * @see get_next_post_link()
  1581. *
  1582. * @param string $format Optional. Link anchor format.
  1583. * @param string $link Optional. Link permalink format.
  1584. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1585. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1586. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1587. */
  1588. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1589. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1590. }
  1591. /**
  1592. * Get adjacent post link.
  1593. *
  1594. * Can be either next post link or previous.
  1595. *
  1596. * @since 3.7.0
  1597. *
  1598. * @param string $format Link anchor format.
  1599. * @param string $link Link permalink format.
  1600. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1601. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
  1602. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1603. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1604. * @return string
  1605. */
  1606. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1607. if ( $previous && is_attachment() )
  1608. $post = get_post( get_post()->post_parent );
  1609. else
  1610. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1611. if ( ! $post ) {
  1612. $output = '';
  1613. } else {
  1614. $title = $post->post_title;
  1615. if ( empty( $post->post_title ) )
  1616. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1617. /** This filter is documented in wp-includes/post-template.php */
  1618. $title = apply_filters( 'the_title', $title, $post->ID );
  1619. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1620. $rel = $previous ? 'prev' : 'next';
  1621. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
  1622. $inlink = str_replace( '%title', $title, $link );
  1623. $inlink = str_replace( '%date', $date, $inlink );
  1624. $inlink = $string . $inlink . '</a>';
  1625. $output = str_replace( '%link', $inlink, $format );
  1626. }
  1627. $adjacent = $previous ? 'previous' : 'next';
  1628. /**
  1629. * Filter the adjacent post link.
  1630. *
  1631. * The dynamic portion of the hook name, $adjacent, refers to the type
  1632. * of adjacency, 'next' or 'previous'.
  1633. *
  1634. * @since 2.6.0
  1635. *
  1636. * @param string $output The adjacent post link.
  1637. * @param string $format Link anchor format.
  1638. * @param string $link Link permalink format.
  1639. * @param WP_Post $post The adjacent post.
  1640. */
  1641. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
  1642. }
  1643. /**
  1644. * Display adjacent post link.
  1645. *
  1646. * Can be either next post link or previous.
  1647. *
  1648. * @since 2.5.0
  1649. * @uses get_adjacent_post_link()
  1650. *
  1651. * @param string $format Link anchor format.
  1652. * @param string $link Link permalink format.
  1653. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1654. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
  1655. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1656. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1657. * @return string
  1658. */
  1659. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1660. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
  1661. }
  1662. /**
  1663. * Retrieve links for page numbers.
  1664. *
  1665. * @since 1.5.0
  1666. *
  1667. * @param int $pagenum Optional. Page ID.
  1668. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1669. * Otherwise, prepares the URL with esc_url_raw().
  1670. * @return string
  1671. */
  1672. function get_pagenum_link($pagenum = 1, $escape = true ) {
  1673. global $wp_rewrite;
  1674. $pagenum = (int) $pagenum;
  1675. $request = remove_query_arg( 'paged' );
  1676. $home_root = parse_url(home_url());
  1677. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1678. $home_root = preg_quote( $home_root, '|' );
  1679. $request = preg_replace('|^'. $home_root . '|i', '', $request);
  1680. $request = preg_replace('|^/+|', '', $request);
  1681. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1682. $base = trailingslashit( get_bloginfo( 'url' ) );
  1683. if ( $pagenum > 1 ) {
  1684. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1685. } else {
  1686. $result = $base . $request;
  1687. }
  1688. } else {
  1689. $qs_regex = '|\?.*?$|';
  1690. preg_match( $qs_regex, $request, $qs_match );
  1691. if ( !empty( $qs_match[0] ) ) {
  1692. $query_string = $qs_match[0];
  1693. $request = preg_replace( $qs_regex, '', $request );
  1694. } else {
  1695. $query_string = '';
  1696. }
  1697. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1698. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
  1699. $request = ltrim($request, '/');
  1700. $base = trailingslashit( get_bloginfo( 'url' ) );
  1701. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1702. $base .= $wp_rewrite->index . '/';
  1703. if ( $pagenum > 1 ) {
  1704. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1705. }
  1706. $result = $base . $request . $query_string;
  1707. }
  1708. /**
  1709. * Filter the page number link for the current request.
  1710. *
  1711. * @since 2.5.0
  1712. *
  1713. * @param string $result The page number link.
  1714. */
  1715. $result = apply_filters( 'get_pagenum_link', $result );
  1716. if ( $escape )
  1717. return esc_url( $result );
  1718. else
  1719. return esc_url_raw( $result );
  1720. }
  1721. /**
  1722. * Retrieve next posts page link.
  1723. *
  1724. * Backported from 2.1.3 to 2.0.10.
  1725. *
  1726. * @since 2.0.10
  1727. *
  1728. * @param int $max_page Optional. Max pages.
  1729. * @return string
  1730. */
  1731. function get_next_posts_page_link($max_page = 0) {
  1732. global $paged;
  1733. if ( !is_single() ) {
  1734. if ( !$paged )
  1735. $paged = 1;
  1736. $nextpage = intval($paged) + 1;
  1737. if ( !$max_page || $max_page >= $nextpage )
  1738. return get_pagenum_link($nextpage);
  1739. }
  1740. }
  1741. /**
  1742. * Display or return the next posts page link.
  1743. *
  1744. * @since 0.71
  1745. *
  1746. * @param int $max_page Optional. Max pages.
  1747. * @param boolean $echo Optional. Echo or return;
  1748. */
  1749. function next_posts( $max_page = 0, $echo = true ) {
  1750. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1751. if ( $echo )
  1752. echo $output;
  1753. else
  1754. return $output;
  1755. }
  1756. /**
  1757. * Return the next posts page link.
  1758. *
  1759. * @since 2.7.0
  1760. *
  1761. * @param string $label Content for link text.
  1762. * @param int $max_page Optional. Max pages.
  1763. * @return string|null
  1764. */
  1765. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1766. global $paged, $wp_query;
  1767. if ( !$max_page )
  1768. $max_page = $wp_query->max_num_pages;
  1769. if ( !$paged )
  1770. $paged = 1;
  1771. $nextpage = intval($paged) + 1;
  1772. if ( null === $label )
  1773. $label = __( 'Next Page &raquo;' );
  1774. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1775. /**
  1776. * Filter the anchor tag attributes for the next posts page link.
  1777. *
  1778. * @since 2.7.0
  1779. *
  1780. * @param string $attributes Attributes for the anchor tag.
  1781. */
  1782. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1783. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1784. }
  1785. }
  1786. /**
  1787. * Display the next posts page link.
  1788. *
  1789. * @since 0.71
  1790. * @uses get_next_posts_link()
  1791. *
  1792. * @param string $label Content for link text.
  1793. * @param int $max_page Optional. Max pages.
  1794. */
  1795. function next_posts_link( $label = null, $max_page = 0 ) {
  1796. echo get_next_posts_link( $label, $max_page );
  1797. }
  1798. /**
  1799. * Retrieve previous posts page link.
  1800. *
  1801. * Will only return string, if not on a single page or post.
  1802. *
  1803. * Backported to 2.0.10 from 2.1.3.
  1804. *
  1805. * @since 2.0.10
  1806. *
  1807. * @return string|null
  1808. */
  1809. function get_previous_posts_page_link() {
  1810. global $paged;
  1811. if ( !is_single() ) {
  1812. $nextpage = intval($paged) - 1;
  1813. if ( $nextpage < 1 )
  1814. $nextpage = 1;
  1815. return get_pagenum_link($nextpage);
  1816. }
  1817. }
  1818. /**
  1819. * Display or return the previous posts page link.
  1820. *
  1821. * @since 0.71
  1822. *
  1823. * @param boolean $echo Optional. Echo or return;
  1824. */
  1825. function previous_posts( $echo = true ) {
  1826. $output = esc_url( get_previous_posts_page_link() );
  1827. if ( $echo )
  1828. echo $output;
  1829. else
  1830. return $output;
  1831. }
  1832. /**
  1833. * Return the previous posts page link.
  1834. *
  1835. * @since 2.7.0
  1836. *
  1837. * @param string $label Optional. Previous page link text.
  1838. * @return string|null
  1839. */
  1840. function get_previous_posts_link( $label = null ) {
  1841. global $paged;
  1842. if ( null === $label )
  1843. $label = __( '&laquo; Previous Page' );
  1844. if ( !is_single() && $paged > 1 ) {
  1845. /**
  1846. * Filter the anchor tag attributes for the previous posts page link.
  1847. *
  1848. * @since 2.7.0
  1849. *
  1850. * @param string $attributes Attributes for the anchor tag.
  1851. */
  1852. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1853. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
  1854. }
  1855. }
  1856. /**
  1857. * Display the previous posts page link.
  1858. *
  1859. * @since 0.71
  1860. * @uses get_previous_posts_link()
  1861. *
  1862. * @param string $label Optional. Previous page link text.
  1863. */
  1864. function previous_posts_link( $label = null ) {
  1865. echo get_previous_posts_link( $label );
  1866. }
  1867. /**
  1868. * Return post pages link navigation for previous and next pages.
  1869. *
  1870. * @since 2.8.0
  1871. *
  1872. * @param string|array $args Optional args.
  1873. * @return string The posts link navigation.
  1874. */
  1875. function get_posts_nav_link( $args = array() ) {
  1876. global $wp_query;
  1877. $return = '';
  1878. if ( !is_singular() ) {
  1879. $defaults = array(
  1880. 'sep' => ' &#8212; ',
  1881. 'prelabel' => __('&laquo; Previous Page'),
  1882. 'nxtlabel' => __('Next Page &raquo;'),
  1883. );
  1884. $args = wp_parse_args( $args, $defaults );
  1885. $max_num_pages = $wp_query->max_num_pages;
  1886. $paged = get_query_var('paged');
  1887. //only have sep if there's both prev and next results
  1888. if ($paged < 2 || $paged >= $max_num_pages) {
  1889. $args['sep'] = '';
  1890. }
  1891. if ( $max_num_pages > 1 ) {
  1892. $return = get_previous_posts_link($args['prelabel']);
  1893. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
  1894. $return .= get_next_posts_link($args['nxtlabel']);
  1895. }
  1896. }
  1897. return $return;
  1898. }
  1899. /**
  1900. * Display post pages link navigation for previous and next pages.
  1901. *
  1902. * @since 0.71
  1903. *
  1904. * @param string $sep Optional. Separator for posts navigation links.
  1905. * @param string $prelabel Optional. Label for previous pages.
  1906. * @param string $nxtlabel Optional Label for next pages.
  1907. */
  1908. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1909. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1910. echo get_posts_nav_link($args);
  1911. }
  1912. /**
  1913. * Retrieve comments page number link.
  1914. *
  1915. * @since 2.7.0
  1916. *
  1917. * @param int $pagenum Optional. Page number.
  1918. * @return string
  1919. */
  1920. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1921. global $wp_rewrite;
  1922. $pagenum = (int) $pagenum;
  1923. $result = get_permalink();
  1924. if ( 'newest' == get_option('default_comments_page') ) {
  1925. if ( $pagenum != $max_page ) {
  1926. if ( $wp_rewrite->using_permalinks() )
  1927. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1928. else
  1929. $result = add_query_arg( 'cpage', $pagenum, $result );
  1930. }
  1931. } elseif ( $pagenum > 1 ) {
  1932. if ( $wp_rewrite->using_permalinks() )
  1933. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1934. else
  1935. $result = add_query_arg( 'cpage', $pagenum, $result );
  1936. }
  1937. $result .= '#comments';
  1938. /**
  1939. * Filter the comments page number link for the current request.
  1940. *
  1941. * @since 2.7.0
  1942. *
  1943. * @param string $result The comments page number link.
  1944. */
  1945. $result = apply_filters( 'get_comments_pagenum_link', $result );
  1946. return $result;
  1947. }
  1948. /**
  1949. * Return the link to next comments page.
  1950. *
  1951. * @since 2.7.1
  1952. *
  1953. * @param string $label Optional. Label for link text.
  1954. * @param int $max_page Optional. Max page.
  1955. * @return string|null
  1956. */
  1957. function get_next_comments_link( $label = '', $max_page = 0 ) {
  1958. global $wp_query;
  1959. if ( !is_singular() || !get_option('page_comments') )
  1960. return;
  1961. $page = get_query_var('cpage');
  1962. $nextpage = intval($page) + 1;
  1963. if ( empty($max_page) )
  1964. $max_page = $wp_query->max_num_comment_pages;
  1965. if ( empty($max_page) )
  1966. $max_page = get_comment_pages_count();
  1967. if ( $nextpage > $max_page )
  1968. return;
  1969. if ( empty($label) )
  1970. $label = __('Newer Comments &raquo;');
  1971. /**
  1972. * Filter the anchor tag attributes for the next comments page link.
  1973. *
  1974. * @since 2.7.0
  1975. *
  1976. * @param string $attributes Attributes for the anchor tag.
  1977. */
  1978. 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>';
  1979. }
  1980. /**
  1981. * Display the link to next comments page.
  1982. *
  1983. * @since 2.7.0
  1984. *
  1985. * @param string $label Optional. Label for link text.
  1986. * @param int $max_page Optional. Max page.
  1987. */
  1988. function next_comments_link( $label = '', $max_page = 0 ) {
  1989. echo get_next_comments_link( $label, $max_page );
  1990. }
  1991. /**
  1992. * Return the previous comments page link.
  1993. *
  1994. * @since 2.7.1
  1995. *
  1996. * @param string $label Optional. Label for comments link text.
  1997. * @return string|null
  1998. */
  1999. function get_previous_comments_link( $label = '' ) {
  2000. if ( !is_singular() || !get_option('page_comments') )
  2001. return;
  2002. $page = get_query_var('cpage');
  2003. if ( intval($page) <= 1 )
  2004. return;
  2005. $prevpage = intval($page) - 1;
  2006. if ( empty($label) )
  2007. $label = __('&laquo; Older Comments');
  2008. /**
  2009. * Filter the anchor tag attributes for the previous comments page link.
  2010. *
  2011. * @since 2.7.0
  2012. *
  2013. * @param string $attributes Attributes for the anchor tag.
  2014. */
  2015. 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>';
  2016. }
  2017. /**
  2018. * Display the previous comments page link.
  2019. *
  2020. * @since 2.7.0
  2021. *
  2022. * @param string $label Optional. Label for comments link text.
  2023. */
  2024. function previous_comments_link( $label = '' ) {
  2025. echo get_previous_comments_link( $label );
  2026. }
  2027. /**
  2028. * Create pagination links for the comments on the current post.
  2029. *
  2030. * @see paginate_links()
  2031. * @since 2.7.0
  2032. *
  2033. * @param string|array $args Optional args. See paginate_links().
  2034. * @return string Markup for pagination links.
  2035. */
  2036. function paginate_comments_links($args = array()) {
  2037. global $wp_rewrite;
  2038. if ( !is_singular() || !get_option('page_comments') )
  2039. return;
  2040. $page = get_query_var('cpage');
  2041. if ( !$page )
  2042. $page = 1;
  2043. $max_page = get_comment_pages_count();
  2044. $defaults = array(
  2045. 'base' => add_query_arg( 'cpage', '%#%' ),
  2046. 'format' => '',
  2047. 'total' => $max_page,
  2048. 'current' => $page,
  2049. 'echo' => true,
  2050. 'add_fragment' => '#comments'
  2051. );
  2052. if ( $wp_rewrite->using_permalinks() )
  2053. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
  2054. $args = wp_parse_args( $args, $defaults );
  2055. $page_links = paginate_links( $args );
  2056. if ( $args['echo'] )
  2057. echo $page_links;
  2058. else
  2059. return $page_links;
  2060. }
  2061. /**
  2062. * Retrieve the Press This bookmarklet link.
  2063. *
  2064. * Use this in 'a' element 'href' attribute.
  2065. *
  2066. * @since 2.6.0
  2067. *
  2068. * @return string
  2069. */
  2070. function get_shortcut_link() {
  2071. // In case of breaking changes, version this. #WP20071
  2072. $link = "javascript:
  2073. var d=document,
  2074. w=window,
  2075. e=w.getSelection,
  2076. k=d.getSelection,
  2077. x=d.selection,
  2078. s=(e?e():(k)?k():(x?x.createRange().text:0)),
  2079. f='" . admin_url('press-this.php') . "',
  2080. l=d.location,
  2081. e=encodeURIComponent,
  2082. u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
  2083. a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
  2084. if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
  2085. void(0)";
  2086. $link = str_replace(array("\r", "\n", "\t"), '', $link);
  2087. /**
  2088. * Filter the Press This bookmarklet link.
  2089. *
  2090. * @since 2.6.0
  2091. *
  2092. * @param string $link The Press This bookmarklet link.
  2093. */
  2094. return apply_filters( 'shortcut_link', $link );
  2095. }
  2096. /**
  2097. * Retrieve the home url for the current site.
  2098. *
  2099. * Returns the 'home' option with the appropriate protocol, 'https' if
  2100. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2101. * overridden.
  2102. *
  2103. * @since 3.0.0
  2104. *
  2105. * @uses get_home_url()
  2106. *
  2107. * @param string $path (optional) Path relative to the home url.
  2108. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  2109. * @return string Home url link with optional path appended.
  2110. */
  2111. function home_url( $path = '', $scheme = null ) {
  2112. return get_home_url( null, $path, $scheme );
  2113. }
  2114. /**
  2115. * Retrieve the home url for a given site.
  2116. *
  2117. * Returns the 'home' option with the appropriate protocol, 'https' if
  2118. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2119. * overridden.
  2120. *
  2121. * @since 3.0.0
  2122. *
  2123. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  2124. * @param string $path (optional) Path relative to the home url.
  2125. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  2126. * @return string Home url link with optional path appended.
  2127. */
  2128. function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  2129. $orig_scheme = $scheme;
  2130. if ( empty( $blog_id ) || !is_multisite() ) {
  2131. $url = get_option( 'home' );
  2132. } else {
  2133. switch_to_blog( $blog_id );
  2134. $url = get_option( 'home' );
  2135. restore_current_blog();
  2136. }
  2137. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
  2138. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
  2139. $scheme = 'https';
  2140. else
  2141. $scheme = parse_url( $url, PHP_URL_SCHEME );
  2142. }
  2143. $url = set_url_scheme( $url, $scheme );
  2144. if ( $path && is_string( $path ) )
  2145. $url .= '/' . ltrim( $path, '/' );
  2146. /**
  2147. * Filter the home URL.
  2148. *
  2149. * @since 3.0.0
  2150. *
  2151. * @param string $url The complete home URL including scheme and path.
  2152. * @param string $path Path relative to the home URL. Blank string if no path is specified.
  2153. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
  2154. * @param int|null $blog_id Blog ID, or null for the current blog.
  2155. */
  2156. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
  2157. }
  2158. /**
  2159. * Retrieve the site url for the current site.
  2160. *
  2161. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2162. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2163. * overridden.
  2164. *
  2165. * @since 3.0.0
  2166. *
  2167. * @uses get_site_url()
  2168. *
  2169. * @param string $path Optional. Path relative to the site url.
  2170. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  2171. * @return string Site url link with optional path appended.
  2172. */
  2173. function site_url( $path = '', $scheme = null ) {
  2174. return get_site_url( null, $path, $scheme );
  2175. }
  2176. /**
  2177. * Retrieve the site url for a given site.
  2178. *
  2179. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2180. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2181. * overridden.
  2182. *
  2183. * @since 3.0.0
  2184. *
  2185. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  2186. * @param string $path Optional. Path relative to the site url.
  2187. * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
  2188. * @return string Site url link with optional path appended.
  2189. */
  2190. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
  2191. if ( empty( $blog_id ) || !is_multisite() ) {
  2192. $url = get_option( 'siteurl' );
  2193. } else {
  2194. switch_to_blog( $blog_id );
  2195. $url = get_option( 'siteurl' );
  2196. restore_current_blog();
  2197. }
  2198. $url = set_url_scheme( $url, $scheme );
  2199. if ( $path && is_string( $path ) )
  2200. $url .= '/' . ltrim( $path, '/' );
  2201. /**
  2202. * Filter the site URL.
  2203. *
  2204. * @since 2.7.0
  2205. *
  2206. * @param string $url The complete site URL including scheme and path.
  2207. * @param string $path Path relative to the site URL. Blank string if no path is specified.
  2208. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
  2209. * 'login_post', 'admin', 'relative' or null.
  2210. * @param int|null $blog_id Blog ID, or null for the current blog.
  2211. */
  2212. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
  2213. }
  2214. /**
  2215. * Retrieve the url to the admin area for the current site.
  2216. *
  2217. * @since 2.6.0
  2218. *
  2219. * @param string $path Optional path relative to the admin url.
  2220. * @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.
  2221. * @return string Admin url link with optional path appended.
  2222. */
  2223. function admin_url( $path = '', $scheme = 'admin' ) {
  2224. return get_admin_url( null, $path, $scheme );
  2225. }
  2226. /**
  2227. * Retrieve the url to the admin area for a given site.
  2228. *
  2229. * @since 3.0.0
  2230. *
  2231. * @param int $blog_id (optional) Blog ID. Defaults to current blog.
  2232. * @param string $path Optional path relative to the admin url.
  2233. * @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.
  2234. * @return string Admin url link with optional path appended.
  2235. */
  2236. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
  2237. $url = get_site_url($blog_id, 'wp-admin/', $scheme);
  2238. if ( $path && is_string( $path ) )
  2239. $url .= ltrim( $path, '/' );
  2240. /**
  2241. * Filter the admin area URL.
  2242. *
  2243. * @since 2.8.0
  2244. *
  2245. * @param string $url The complete admin area URL including scheme and path.
  2246. * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
  2247. * @param int|null $blog_id Blog ID, or null for the current blog.
  2248. */
  2249. return apply_filters( 'admin_url', $url, $path, $blog_id );
  2250. }
  2251. /**
  2252. * Retrieve the url to the includes directory.
  2253. *
  2254. * @since 2.6.0
  2255. *
  2256. * @param string $path Optional. Path relative to the includes url.
  2257. * @param string $scheme Optional. Scheme to give the includes url context.
  2258. * @return string Includes url link with optional path appended.
  2259. */
  2260. function includes_url( $path = '', $scheme = null ) {
  2261. $url = site_url( '/' . WPINC . '/', $scheme );
  2262. if ( $path && is_string( $path ) )
  2263. $url .= ltrim($path, '/');
  2264. /**
  2265. * Filter the URL to the includes directory.
  2266. *
  2267. * @since 2.8.0
  2268. *
  2269. * @param string $url The complete URL to the includes directory including scheme and path.
  2270. * @param string $path Path relative to the URL to the wp-includes directory. Blank string
  2271. * if no path is specified.
  2272. */
  2273. return apply_filters( 'includes_url', $url, $path );
  2274. }
  2275. /**
  2276. * Retrieve the url to the content directory.
  2277. *
  2278. * @since 2.6.0
  2279. *
  2280. * @param string $path Optional. Path relative to the content url.
  2281. * @return string Content url link with optional path appended.
  2282. */
  2283. function content_url($path = '') {
  2284. $url = set_url_scheme( WP_CONTENT_URL );
  2285. if ( $path && is_string( $path ) )
  2286. $url .= '/' . ltrim($path, '/');
  2287. /**
  2288. * Filter the URL to the content directory.
  2289. *
  2290. * @since 2.8.0
  2291. *
  2292. * @param string $url The complete URL to the content directory including scheme and path.
  2293. * @param string $path Path relative to the URL to the content directory. Blank string
  2294. * if no path is specified.
  2295. */
  2296. return apply_filters( 'content_url', $url, $path);
  2297. }
  2298. /**
  2299. * Retrieve a URL within the plugins or mu-plugins directory.
  2300. *
  2301. * Defaults to the plugins directory URL if no arguments are supplied.
  2302. *
  2303. * @since 2.6.0
  2304. *
  2305. * @param string $path Optional. Extra path appended to the end of the URL, including
  2306. * the relative directory if $plugin is supplied. Default empty.
  2307. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
  2308. * The URL will be relative to its directory. Default empty.
  2309. * Typically this is done by passing `__FILE__` as the argument.
  2310. * @return string Plugins URL link with optional paths appended.
  2311. */
  2312. function plugins_url( $path = '', $plugin = '' ) {
  2313. $path = wp_normalize_path( $path );
  2314. $plugin = wp_normalize_path( $plugin );
  2315. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
  2316. if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
  2317. $url = WPMU_PLUGIN_URL;
  2318. else
  2319. $url = WP_PLUGIN_URL;
  2320. $url = set_url_scheme( $url );
  2321. if ( !empty($plugin) && is_string($plugin) ) {
  2322. $folder = dirname(plugin_basename($plugin));
  2323. if ( '.' != $folder )
  2324. $url .= '/' . ltrim($folder, '/');
  2325. }
  2326. if ( $path && is_string( $path ) )
  2327. $url .= '/' . ltrim($path, '/');
  2328. /**
  2329. * Filter the URL to the plugins directory.
  2330. *
  2331. * @since 2.8.0
  2332. *
  2333. * @param string $url The complete URL to the plugins directory including scheme and path.
  2334. * @param string $path Path relative to the URL to the plugins directory. Blank string
  2335. * if no path is specified.
  2336. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
  2337. * is specified.
  2338. */
  2339. return apply_filters( 'plugins_url', $url, $path, $plugin );
  2340. }
  2341. /**
  2342. * Retrieve the site url for the current network.
  2343. *
  2344. * Returns the site url with the appropriate protocol, 'https' if
  2345. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2346. * overridden.
  2347. *
  2348. * @since 3.0.0
  2349. *
  2350. * @param string $path Optional. Path relative to the site url.
  2351. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  2352. * @return string Site url link with optional path appended.
  2353. */
  2354. function network_site_url( $path = '', $scheme = null ) {
  2355. if ( ! is_multisite() )
  2356. return site_url($path, $scheme);
  2357. $current_site = get_current_site();
  2358. if ( 'relative' == $scheme )
  2359. $url = $current_site->path;
  2360. else
  2361. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  2362. if ( $path && is_string( $path ) )
  2363. $url .= ltrim( $path, '/' );
  2364. /**
  2365. * Filter the network site URL.
  2366. *
  2367. * @since 3.0.0
  2368. *
  2369. * @param string $url The complete network site URL including scheme and path.
  2370. * @param string $path Path relative to the network site URL. Blank string if
  2371. * no path is specified.
  2372. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
  2373. * 'relative' or null.
  2374. */
  2375. return apply_filters( 'network_site_url', $url, $path, $scheme );
  2376. }
  2377. /**
  2378. * Retrieve the home url for the current network.
  2379. *
  2380. * Returns the home url with the appropriate protocol, 'https' if
  2381. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2382. * overridden.
  2383. *
  2384. * @since 3.0.0
  2385. *
  2386. * @param string $path (optional) Path relative to the home url.
  2387. * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
  2388. * @return string Home url link with optional path appended.
  2389. */
  2390. function network_home_url( $path = '', $scheme = null ) {
  2391. if ( ! is_multisite() )
  2392. return home_url($path, $scheme);
  2393. $current_site = get_current_site();
  2394. $orig_scheme = $scheme;
  2395. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
  2396. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
  2397. if ( 'relative' == $scheme )
  2398. $url = $current_site->path;
  2399. else
  2400. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  2401. if ( $path && is_string( $path ) )
  2402. $url .= ltrim( $path, '/' );
  2403. /**
  2404. * Filter the network home URL.
  2405. *
  2406. * @since 3.0.0
  2407. *
  2408. * @param string $url The complete network home URL including scheme and path.
  2409. * @param string $path Path relative to the network home URL. Blank string
  2410. * if no path is specified.
  2411. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
  2412. * 'relative' or null.
  2413. */
  2414. return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
  2415. }
  2416. /**
  2417. * Retrieve the url to the admin area for the network.
  2418. *
  2419. * @since 3.0.0
  2420. *
  2421. * @param string $path Optional path relative to the admin url.
  2422. * @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.
  2423. * @return string Admin url link with optional path appended.
  2424. */
  2425. function network_admin_url( $path = '', $scheme = 'admin' ) {
  2426. if ( ! is_multisite() )
  2427. return admin_url( $path, $scheme );
  2428. $url = network_site_url('wp-admin/network/', $scheme);
  2429. if ( $path && is_string( $path ) )
  2430. $url .= ltrim($path, '/');
  2431. /**
  2432. * Filter the network admin URL.
  2433. *
  2434. * @since 3.0.0
  2435. *
  2436. * @param string $url The complete network admin URL including scheme and path.
  2437. * @param string $path Path relative to the network admin URL. Blank string if
  2438. * no path is specified.
  2439. */
  2440. return apply_filters( 'network_admin_url', $url, $path );
  2441. }
  2442. /**
  2443. * Retrieve the url to the admin area for the current user.
  2444. *
  2445. * @since 3.0.0
  2446. *
  2447. * @param string $path Optional path relative to the admin url.
  2448. * @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.
  2449. * @return string Admin url link with optional path appended.
  2450. */
  2451. function user_admin_url( $path = '', $scheme = 'admin' ) {
  2452. $url = network_site_url('wp-admin/user/', $scheme);
  2453. if ( $path && is_string( $path ) )
  2454. $url .= ltrim($path, '/');
  2455. /**
  2456. * Filter the user admin URL for the current user.
  2457. *
  2458. * @since 3.1.0
  2459. *
  2460. * @param string $url The complete URL including scheme and path.
  2461. * @param string $path Path relative to the URL. Blank string if
  2462. * no path is specified.
  2463. */
  2464. return apply_filters( 'user_admin_url', $url, $path );
  2465. }
  2466. /**
  2467. * Retrieve the url to the admin area for either the current blog or the network depending on context.
  2468. *
  2469. * @since 3.1.0
  2470. *
  2471. * @param string $path Optional path relative to the admin url.
  2472. * @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.
  2473. * @return string Admin url link with optional path appended.
  2474. */
  2475. function self_admin_url($path = '', $scheme = 'admin') {
  2476. if ( is_network_admin() )
  2477. return network_admin_url($path, $scheme);
  2478. elseif ( is_user_admin() )
  2479. return user_admin_url($path, $scheme);
  2480. else
  2481. return admin_url($path, $scheme);
  2482. }
  2483. /**
  2484. * Set the scheme for a URL
  2485. *
  2486. * @since 3.4.0
  2487. *
  2488. * @param string $url Absolute url that includes a scheme
  2489. * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
  2490. * @return string $url URL with chosen scheme.
  2491. */
  2492. function set_url_scheme( $url, $scheme = null ) {
  2493. $orig_scheme = $scheme;
  2494. if ( ! $scheme ) {
  2495. $scheme = is_ssl() ? 'https' : 'http';
  2496. } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
  2497. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
  2498. } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
  2499. $scheme = is_ssl() ? 'https' : 'http';
  2500. }
  2501. $url = trim( $url );
  2502. if ( substr( $url, 0, 2 ) === '//' )
  2503. $url = 'http:' . $url;
  2504. if ( 'relative' == $scheme ) {
  2505. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
  2506. if ( $url !== '' && $url[0] === '/' )
  2507. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
  2508. } else {
  2509. $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
  2510. }
  2511. /**
  2512. * Filter the resulting URL after setting the scheme.
  2513. *
  2514. * @since 3.4.0
  2515. *
  2516. * @param string $url The complete URL including scheme and path.
  2517. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
  2518. * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
  2519. * 'login_post', 'admin', 'rpc', or 'relative'.
  2520. */
  2521. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
  2522. }
  2523. /**
  2524. * Get the URL to the user's dashboard.
  2525. *
  2526. * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
  2527. * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
  2528. * primary blog is returned.
  2529. *
  2530. * @since 3.1.0
  2531. *
  2532. * @param int $user_id Optional. User ID. Defaults to current user.
  2533. * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
  2534. * @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.
  2535. * @return string Dashboard url link with optional path appended.
  2536. */
  2537. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
  2538. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  2539. $blogs = get_blogs_of_user( $user_id );
  2540. if ( ! is_super_admin() && empty($blogs) ) {
  2541. $url = user_admin_url( $path, $scheme );
  2542. } elseif ( ! is_multisite() ) {
  2543. $url = admin_url( $path, $scheme );
  2544. } else {
  2545. $current_blog = get_current_blog_id();
  2546. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
  2547. $url = admin_url( $path, $scheme );
  2548. } else {
  2549. $active = get_active_blog_for_user( $user_id );
  2550. if ( $active )
  2551. $url = get_admin_url( $active->blog_id, $path, $scheme );
  2552. else
  2553. $url = user_admin_url( $path, $scheme );
  2554. }
  2555. }
  2556. /**
  2557. * Filter the dashboard URL for a user.
  2558. *
  2559. * @since 3.1.0
  2560. *
  2561. * @param string $url The complete URL including scheme and path.
  2562. * @param int $user_id The user ID.
  2563. * @param string $path Path relative to the URL. Blank string if no path is specified.
  2564. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  2565. * 'login_post', 'admin', 'relative' or null.
  2566. */
  2567. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
  2568. }
  2569. /**
  2570. * Get the URL to the user's profile editor.
  2571. *
  2572. * @since 3.1.0
  2573. *
  2574. * @param int $user_id Optional. User ID. Defaults to current user.
  2575. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  2576. * 'http' or 'https' can be passed to force those schemes.
  2577. * @return string Dashboard url link with optional path appended.
  2578. */
  2579. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
  2580. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  2581. if ( is_user_admin() )
  2582. $url = user_admin_url( 'profile.php', $scheme );
  2583. elseif ( is_network_admin() )
  2584. $url = network_admin_url( 'profile.php', $scheme );
  2585. else
  2586. $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
  2587. /**
  2588. * Filter the URL for a user's profile editor.
  2589. *
  2590. * @since 3.1.0
  2591. *
  2592. * @param string $url The complete URL including scheme and path.
  2593. * @param int $user_id The user ID.
  2594. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  2595. * 'login_post', 'admin', 'relative' or null.
  2596. */
  2597. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
  2598. }
  2599. /**
  2600. * Output rel=canonical for singular queries.
  2601. *
  2602. * @since 2.9.0
  2603. */
  2604. function rel_canonical() {
  2605. if ( !is_singular() )
  2606. return;
  2607. global $wp_the_query;
  2608. if ( !$id = $wp_the_query->get_queried_object_id() )
  2609. return;
  2610. $link = get_permalink( $id );
  2611. if ( $page = get_query_var('cpage') )
  2612. $link = get_comments_pagenum_link( $page );
  2613. echo "<link rel='canonical' href='$link' />\n";
  2614. }
  2615. /**
  2616. * Return a shortlink for a post, page, attachment, or blog.
  2617. *
  2618. * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
  2619. * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
  2620. * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
  2621. * via the get_shortlink filter.
  2622. *
  2623. * @since 3.0.0.
  2624. *
  2625. * @param int $id A post or blog id. Default is 0, which means the current post or blog.
  2626. * @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'.
  2627. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
  2628. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
  2629. */
  2630. function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
  2631. /**
  2632. * Filter whether to preempt generating a shortlink for the given post.
  2633. *
  2634. * Passing a truthy value to the filter will effectively short-circuit the
  2635. * shortlink-generation process, returning that value instead.
  2636. *
  2637. * @since 3.0.0
  2638. *
  2639. * @param bool|string $return Short-circuit return value. Either false or a URL string.
  2640. * @param int $id Post ID, or 0 for the current post.
  2641. * @param string $context The context for the link. One of 'post' or 'query',
  2642. * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
  2643. */
  2644. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
  2645. if ( false !== $shortlink )
  2646. return $shortlink;
  2647. global $wp_query;
  2648. $post_id = 0;
  2649. if ( 'query' == $context && is_singular() ) {
  2650. $post_id = $wp_query->get_queried_object_id();
  2651. $post = get_post( $post_id );
  2652. } elseif ( 'post' == $context ) {
  2653. $post = get_post( $id );
  2654. if ( ! empty( $post->ID ) )
  2655. $post_id = $post->ID;
  2656. }
  2657. $shortlink = '';
  2658. // Return p= link for all public post types.
  2659. if ( ! empty( $post_id ) ) {
  2660. $post_type = get_post_type_object( $post->post_type );
  2661. if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
  2662. $shortlink = home_url( '/' );
  2663. } elseif ( $post_type->public ) {
  2664. $shortlink = home_url( '?p=' . $post_id );
  2665. }
  2666. }
  2667. /**
  2668. * Filter the shortlink for a post.
  2669. *
  2670. * @since 3.0.0
  2671. *
  2672. * @param string $shortlink Shortlink URL.
  2673. * @param int $id Post ID, or 0 for the current post.
  2674. * @param string $context The context for the link. One of 'post' or 'query',
  2675. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
  2676. */
  2677. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
  2678. }
  2679. /**
  2680. * Inject rel=shortlink into head if a shortlink is defined for the current page.
  2681. *
  2682. * Attached to the wp_head action.
  2683. *
  2684. * @since 3.0.0
  2685. *
  2686. * @uses wp_get_shortlink()
  2687. */
  2688. function wp_shortlink_wp_head() {
  2689. $shortlink = wp_get_shortlink( 0, 'query' );
  2690. if ( empty( $shortlink ) )
  2691. return;
  2692. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
  2693. }
  2694. /**
  2695. * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
  2696. *
  2697. * Attached to the wp action.
  2698. *
  2699. * @since 3.0.0
  2700. *
  2701. * @uses wp_get_shortlink()
  2702. */
  2703. function wp_shortlink_header() {
  2704. if ( headers_sent() )
  2705. return;
  2706. $shortlink = wp_get_shortlink(0, 'query');
  2707. if ( empty($shortlink) )
  2708. return;
  2709. header('Link: <' . $shortlink . '>; rel=shortlink', false);
  2710. }
  2711. /**
  2712. * Display the Short Link for a Post
  2713. *
  2714. * Must be called from inside "The Loop"
  2715. *
  2716. * Call like the_shortlink(__('Shortlinkage FTW'))
  2717. *
  2718. * @since 3.0.0
  2719. *
  2720. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
  2721. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
  2722. * @param string $before Optional HTML to display before the link.
  2723. * @param string $after Optional HTML to display after the link.
  2724. */
  2725. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
  2726. $post = get_post();
  2727. if ( empty( $text ) )
  2728. $text = __('This is the short link.');
  2729. if ( empty( $title ) )
  2730. $title = the_title_attribute( array( 'echo' => false ) );
  2731. $shortlink = wp_get_shortlink( $post->ID );
  2732. if ( !empty( $shortlink ) ) {
  2733. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
  2734. /**
  2735. * Filter the shortlink anchor tag for a post.
  2736. *
  2737. * @since 3.0.0
  2738. *
  2739. * @param string $link Shortlink anchor tag.
  2740. * @param string $shortlink Shortlink URL.
  2741. * @param string $text Shortlink's text.
  2742. * @param string $title Shortlink's title attribute.
  2743. */
  2744. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
  2745. echo $before, $link, $after;
  2746. }
  2747. }