PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

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