PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/link-template.php

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