PageRenderTime 91ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

https://github.com/schr/wordpress
PHP | 1538 lines | 816 code | 209 blank | 513 comment | 224 complexity | d067a546567ec30731501fe81ee9496d 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 a URL with or without a trailing slash.
  29. * @param $type_of_url String 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
  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) ) return false;
  97. if ( $post->post_type == 'page' )
  98. return get_page_link($post->ID, $leavename, $sample);
  99. elseif ($post->post_type == 'attachment')
  100. return get_attachment_link($post->ID);
  101. $permalink = get_option('permalink_structure');
  102. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
  103. $unixtime = strtotime($post->post_date);
  104. $category = '';
  105. if ( strpos($permalink, '%category%') !== false ) {
  106. $cats = get_the_category($post->ID);
  107. if ( $cats ) {
  108. usort($cats, '_usort_terms_by_ID'); // order by ID
  109. $category = $cats[0]->slug;
  110. if ( $parent = $cats[0]->parent )
  111. $category = get_category_parents($parent, false, '/', true) . $category;
  112. }
  113. // show default category in permalinks, without
  114. // having to assign it explicitly
  115. if ( empty($category) ) {
  116. $default_category = get_category( get_option( 'default_category' ) );
  117. $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  118. }
  119. }
  120. $author = '';
  121. if ( strpos($permalink, '%author%') !== false ) {
  122. $authordata = get_userdata($post->post_author);
  123. $author = $authordata->user_nicename;
  124. }
  125. $date = explode(" ",date('Y m d H i s', $unixtime));
  126. $rewritereplace =
  127. array(
  128. $date[0],
  129. $date[1],
  130. $date[2],
  131. $date[3],
  132. $date[4],
  133. $date[5],
  134. $post->post_name,
  135. $post->ID,
  136. $category,
  137. $author,
  138. $post->post_name,
  139. );
  140. $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
  141. $permalink = user_trailingslashit($permalink, 'single');
  142. return apply_filters('post_link', $permalink, $post, $leavename);
  143. } else { // if they're not using the fancy permalink option
  144. $permalink = get_option('home') . '/?p=' . $post->ID;
  145. return apply_filters('post_link', $permalink, $post, $leavename);
  146. }
  147. }
  148. /**
  149. * Retrieve permalink from post ID.
  150. *
  151. * @since 1.0.0
  152. *
  153. * @param int $post_id Optional. Post ID.
  154. * @param mixed $deprecated Not used.
  155. * @return string
  156. */
  157. function post_permalink($post_id = 0, $deprecated = '') {
  158. return get_permalink($post_id);
  159. }
  160. /**
  161. * Retrieve the permalink for current page or page ID.
  162. *
  163. * Respects page_on_front. Use this one.
  164. *
  165. * @since 1.5.0
  166. *
  167. * @param int $id Optional. Post ID.
  168. * @param bool $leavename Optional, defaults to false. Whether to keep page name.
  169. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  170. * @return string
  171. */
  172. function get_page_link( $id = false, $leavename = false, $sample = false ) {
  173. global $post;
  174. $id = (int) $id;
  175. if ( !$id )
  176. $id = (int) $post->ID;
  177. if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
  178. $link = get_option('home');
  179. else
  180. $link = _get_page_link( $id , $leavename, $sample );
  181. return apply_filters('page_link', $link, $id);
  182. }
  183. /**
  184. * Retrieve the page permalink.
  185. *
  186. * Ignores page_on_front. Internal use only.
  187. *
  188. * @since 2.1.0
  189. * @access private
  190. *
  191. * @param int $id Optional. Post ID.
  192. * @param bool $leavename Optional. Leave name.
  193. * @param bool $sample Optional. Sample permalink.
  194. * @return string
  195. */
  196. function _get_page_link( $id = false, $leavename = false, $sample = false ) {
  197. global $post, $wp_rewrite;
  198. if ( !$id )
  199. $id = (int) $post->ID;
  200. else
  201. $post = &get_post($id);
  202. $pagestruct = $wp_rewrite->get_page_permastruct();
  203. if ( '' != $pagestruct && ( ( isset($post->post_status) && 'draft' != $post->post_status ) || $sample ) ) {
  204. $link = get_page_uri($id);
  205. $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
  206. $link = get_option('home') . "/$link";
  207. $link = user_trailingslashit($link, 'page');
  208. } else {
  209. $link = get_option('home') . "/?page_id=$id";
  210. }
  211. return apply_filters( '_get_page_link', $link, $id );
  212. }
  213. /**
  214. * Retrieve permalink for attachment.
  215. *
  216. * This can be used in the WordPress Loop or outside of it.
  217. *
  218. * @since 2.0.0
  219. *
  220. * @param int $id Optional. Post ID.
  221. * @return string
  222. */
  223. function get_attachment_link($id = false) {
  224. global $post, $wp_rewrite;
  225. $link = false;
  226. if (! $id) {
  227. $id = (int) $post->ID;
  228. }
  229. $object = get_post($id);
  230. if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
  231. $parent = get_post($object->post_parent);
  232. if ( 'page' == $parent->post_type )
  233. $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
  234. else
  235. $parentlink = get_permalink( $object->post_parent );
  236. if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
  237. $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
  238. else
  239. $name = $object->post_name;
  240. if (strpos($parentlink, '?') === false)
  241. $link = user_trailingslashit( trailingslashit($parentlink) . $name );
  242. }
  243. if (! $link ) {
  244. $link = get_bloginfo('url') . "/?attachment_id=$id";
  245. }
  246. return apply_filters('attachment_link', $link, $id);
  247. }
  248. /**
  249. * Retrieve the permalink for the year archives.
  250. *
  251. * @since 1.5.0
  252. *
  253. * @param int|bool $year False for current year or year for permalink.
  254. * @return string
  255. */
  256. function get_year_link($year) {
  257. global $wp_rewrite;
  258. if ( !$year )
  259. $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
  260. $yearlink = $wp_rewrite->get_year_permastruct();
  261. if ( !empty($yearlink) ) {
  262. $yearlink = str_replace('%year%', $year, $yearlink);
  263. return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
  264. } else {
  265. return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
  266. }
  267. }
  268. /**
  269. * Retrieve the permalink for the month archives with year.
  270. *
  271. * @since 1.0.0
  272. *
  273. * @param bool|int $year False for current year. Integer of year.
  274. * @param bool|int $month False for current month. Integer of month.
  275. * @return string
  276. */
  277. function get_month_link($year, $month) {
  278. global $wp_rewrite;
  279. if ( !$year )
  280. $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
  281. if ( !$month )
  282. $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
  283. $monthlink = $wp_rewrite->get_month_permastruct();
  284. if ( !empty($monthlink) ) {
  285. $monthlink = str_replace('%year%', $year, $monthlink);
  286. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
  287. return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
  288. } else {
  289. return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
  290. }
  291. }
  292. /**
  293. * Retrieve the permalink for the day archives with year and month.
  294. *
  295. * @since 1.0.0
  296. *
  297. * @param bool|int $year False for current year. Integer of year.
  298. * @param bool|int $month False for current month. Integer of month.
  299. * @param bool|int $day False for current day. Integer of day.
  300. * @return string
  301. */
  302. function get_day_link($year, $month, $day) {
  303. global $wp_rewrite;
  304. if ( !$year )
  305. $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
  306. if ( !$month )
  307. $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
  308. if ( !$day )
  309. $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
  310. $daylink = $wp_rewrite->get_day_permastruct();
  311. if ( !empty($daylink) ) {
  312. $daylink = str_replace('%year%', $year, $daylink);
  313. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
  314. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
  315. return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
  316. } else {
  317. return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
  318. }
  319. }
  320. /**
  321. * Retrieve the permalink for the feed type.
  322. *
  323. * @since 1.5.0
  324. *
  325. * @param string $feed Optional, defaults to default feed. Feed type.
  326. * @return string
  327. */
  328. function get_feed_link($feed = '') {
  329. global $wp_rewrite;
  330. $permalink = $wp_rewrite->get_feed_permastruct();
  331. if ( '' != $permalink ) {
  332. if ( false !== strpos($feed, 'comments_') ) {
  333. $feed = str_replace('comments_', '', $feed);
  334. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  335. }
  336. if ( get_default_feed() == $feed )
  337. $feed = '';
  338. $permalink = str_replace('%feed%', $feed, $permalink);
  339. $permalink = preg_replace('#/+#', '/', "/$permalink");
  340. $output = get_option('home') . user_trailingslashit($permalink, 'feed');
  341. } else {
  342. if ( empty($feed) )
  343. $feed = get_default_feed();
  344. if ( false !== strpos($feed, 'comments_') )
  345. $feed = str_replace('comments_', 'comments-', $feed);
  346. $output = get_option('home') . "/?feed={$feed}";
  347. }
  348. return apply_filters('feed_link', $output, $feed);
  349. }
  350. /**
  351. * Retrieve the permalink for the post comments feed.
  352. *
  353. * @since 2.2.0
  354. *
  355. * @param int $post_id Optional. Post ID.
  356. * @param string $feed Optional. Feed type.
  357. * @return string
  358. */
  359. function get_post_comments_feed_link($post_id = '', $feed = '') {
  360. global $id;
  361. if ( empty($post_id) )
  362. $post_id = (int) $id;
  363. if ( empty($feed) )
  364. $feed = get_default_feed();
  365. if ( '' != get_option('permalink_structure') ) {
  366. $url = trailingslashit( get_permalink($post_id) ) . 'feed';
  367. if ( $feed != get_default_feed() )
  368. $url .= "/$feed";
  369. $url = user_trailingslashit($url, 'single_feed');
  370. } else {
  371. $type = get_post_field('post_type', $post_id);
  372. if ( 'page' == $type )
  373. $url = get_option('home') . "/?feed=$feed&amp;page_id=$post_id";
  374. else
  375. $url = get_option('home') . "/?feed=$feed&amp;p=$post_id";
  376. }
  377. return apply_filters('post_comments_feed_link', $url);
  378. }
  379. /**
  380. * Display the comment feed link for a post.
  381. *
  382. * Prints out the comment feed link for a post. Link text is placed in the
  383. * anchor. If no link text is specified, default text is used. If no post ID is
  384. * specified, the current post is used.
  385. *
  386. * @package WordPress
  387. * @subpackage Feed
  388. * @since 2.5.0
  389. *
  390. * @param string $link_text Descriptive text.
  391. * @param int $post_id Optional post ID. Default to current post.
  392. * @param string $feed Optional. Feed format.
  393. * @return string Link to the comment feed for the current post.
  394. */
  395. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  396. $url = get_post_comments_feed_link($post_id, $feed);
  397. if ( empty($link_text) )
  398. $link_text = __('Comments Feed');
  399. echo "<a href='$url'>$link_text</a>";
  400. }
  401. /**
  402. * Retrieve the feed link for a given author.
  403. *
  404. * Returns a link to the feed for all posts by a given author. A specific feed
  405. * can be requested or left blank to get the default feed.
  406. *
  407. * @package WordPress
  408. * @subpackage Feed
  409. * @since 2.5.0
  410. *
  411. * @param int $author_id ID of an author.
  412. * @param string $feed Optional. Feed type.
  413. * @return string Link to the feed for the author specified by $author_id.
  414. */
  415. function get_author_feed_link( $author_id, $feed = '' ) {
  416. $author_id = (int) $author_id;
  417. $permalink_structure = get_option('permalink_structure');
  418. if ( empty($feed) )
  419. $feed = get_default_feed();
  420. if ( '' == $permalink_structure ) {
  421. $link = get_option('home') . "?feed=$feed&amp;author=" . $author_id;
  422. } else {
  423. $link = get_author_posts_url($author_id);
  424. if ( $feed == get_default_feed() )
  425. $feed_link = 'feed';
  426. else
  427. $feed_link = "feed/$feed";
  428. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  429. }
  430. $link = apply_filters('author_feed_link', $link, $feed);
  431. return $link;
  432. }
  433. /**
  434. * Retrieve the feed link for a category.
  435. *
  436. * Returns a link to the feed for all post in a given category. A specific feed
  437. * can be requested or left blank to get the default feed.
  438. *
  439. * @package WordPress
  440. * @subpackage Feed
  441. * @since 2.5.0
  442. *
  443. * @param int $cat_id ID of a category.
  444. * @param string $feed Optional. Feed type.
  445. * @return string Link to the feed for the category specified by $cat_id.
  446. */
  447. function get_category_feed_link($cat_id, $feed = '') {
  448. $cat_id = (int) $cat_id;
  449. $category = get_category($cat_id);
  450. if ( empty($category) || is_wp_error($category) )
  451. return false;
  452. if ( empty($feed) )
  453. $feed = get_default_feed();
  454. $permalink_structure = get_option('permalink_structure');
  455. if ( '' == $permalink_structure ) {
  456. $link = trailingslashit( get_option('home') ) . "?feed=$feed&amp;cat=" . $cat_id;
  457. } else {
  458. $link = get_category_link($cat_id);
  459. if( $feed == get_default_feed() )
  460. $feed_link = 'feed';
  461. else
  462. $feed_link = "feed/$feed";
  463. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  464. }
  465. $link = apply_filters('category_feed_link', $link, $feed);
  466. return $link;
  467. }
  468. /**
  469. * Retrieve permalink for feed of tag.
  470. *
  471. * @since 2.3.0
  472. *
  473. * @param int $tag_id Tag ID.
  474. * @param string $feed Optional. Feed type.
  475. * @return string
  476. */
  477. function get_tag_feed_link($tag_id, $feed = '') {
  478. $tag_id = (int) $tag_id;
  479. $tag = get_tag($tag_id);
  480. if ( empty($tag) || is_wp_error($tag) )
  481. return false;
  482. $permalink_structure = get_option('permalink_structure');
  483. if ( empty($feed) )
  484. $feed = get_default_feed();
  485. if ( '' == $permalink_structure ) {
  486. $link = get_option('home') . "?feed=$feed&amp;tag=" . $tag->slug;
  487. } else {
  488. $link = get_tag_link($tag->term_id);
  489. if ( $feed == get_default_feed() )
  490. $feed_link = 'feed';
  491. else
  492. $feed_link = "feed/$feed";
  493. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  494. }
  495. $link = apply_filters('tag_feed_link', $link, $feed);
  496. return $link;
  497. }
  498. /**
  499. * Retrieve edit tag link.
  500. *
  501. * @since 2.7.0
  502. *
  503. * @param int $tag_id Tag ID
  504. * @return string
  505. */
  506. function get_edit_tag_link( $tag_id = 0, $taxonomy = 'post_tag' ) {
  507. $tag = get_term($tag_id, $taxonomy);
  508. if ( !current_user_can('manage_categories') )
  509. return;
  510. $location = admin_url('edit-tags.php?action=edit&amp;taxonomy=' . $taxonomy . '&amp;tag_ID=' . $tag->term_id);
  511. return apply_filters( 'get_edit_tag_link', $location );
  512. }
  513. /**
  514. * Display or retrieve edit tag link with formatting.
  515. *
  516. * @since 2.7.0
  517. *
  518. * @param string $link Optional. Anchor text.
  519. * @param string $before Optional. Display before edit link.
  520. * @param string $after Optional. Display after edit link.
  521. * @param int|object $tag Tag object or ID
  522. * @return string|null HTML content, if $echo is set to false.
  523. */
  524. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  525. $tag = get_term($tag, 'post_tag');
  526. if ( !current_user_can('manage_categories') )
  527. return;
  528. if ( empty($link) )
  529. $link = __('Edit This');
  530. $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit tag' ) . '">' . $link . '</a>';
  531. echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
  532. }
  533. /**
  534. * Retrieve the permalink for the feed of the search results.
  535. *
  536. * @since 2.5.0
  537. *
  538. * @param string $search_query Optional. Search query.
  539. * @param string $feed Optional. Feed type.
  540. * @return string
  541. */
  542. function get_search_feed_link($search_query = '', $feed = '') {
  543. if ( empty($search_query) )
  544. $search = attribute_escape(get_search_query());
  545. else
  546. $search = attribute_escape(stripslashes($search_query));
  547. if ( empty($feed) )
  548. $feed = get_default_feed();
  549. $link = get_option('home') . "?s=$search&amp;feed=$feed";
  550. $link = apply_filters('search_feed_link', $link);
  551. return $link;
  552. }
  553. /**
  554. * Retrieve the permalink for the comments feed of the search results.
  555. *
  556. * @since 2.5.0
  557. *
  558. * @param string $search_query Optional. Search query.
  559. * @param string $feed Optional. Feed type.
  560. * @return string
  561. */
  562. function get_search_comments_feed_link($search_query = '', $feed = '') {
  563. if ( empty($search_query) )
  564. $search = attribute_escape(get_search_query());
  565. else
  566. $search = attribute_escape(stripslashes($search_query));
  567. if ( empty($feed) )
  568. $feed = get_default_feed();
  569. $link = get_option('home') . "?s=$search&amp;feed=comments-$feed";
  570. $link = apply_filters('search_feed_link', $link);
  571. return $link;
  572. }
  573. /**
  574. * Retrieve edit posts link for post.
  575. *
  576. * Can be used within the WordPress loop or outside of it. Can be used with
  577. * pages, posts, attachments, and revisions.
  578. *
  579. * @since 2.3.0
  580. *
  581. * @param int $id Optional. Post ID.
  582. * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
  583. * @return string
  584. */
  585. function get_edit_post_link( $id = 0, $context = 'display' ) {
  586. if ( !$post = &get_post( $id ) )
  587. return;
  588. if ( 'display' == $context )
  589. $action = 'action=edit&amp;';
  590. else
  591. $action = 'action=edit&';
  592. switch ( $post->post_type ) :
  593. case 'page' :
  594. if ( !current_user_can( 'edit_page', $post->ID ) )
  595. return;
  596. $file = 'page';
  597. $var = 'post';
  598. break;
  599. case 'attachment' :
  600. if ( !current_user_can( 'edit_post', $post->ID ) )
  601. return;
  602. $file = 'media';
  603. $var = 'attachment_id';
  604. break;
  605. case 'revision' :
  606. if ( !current_user_can( 'edit_post', $post->ID ) )
  607. return;
  608. $file = 'revision';
  609. $var = 'revision';
  610. $action = '';
  611. break;
  612. default :
  613. if ( !current_user_can( 'edit_post', $post->ID ) )
  614. return;
  615. $file = 'post';
  616. $var = 'post';
  617. break;
  618. endswitch;
  619. return apply_filters( 'get_edit_post_link', admin_url("$file.php?{$action}$var=$post->ID"), $post->ID, $context );
  620. }
  621. /**
  622. * Retrieve edit posts link for post.
  623. *
  624. * @since 1.0.0
  625. *
  626. * @param string $link Optional. Anchor text.
  627. * @param string $before Optional. Display before edit link.
  628. * @param string $after Optional. Display after edit link.
  629. */
  630. function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
  631. global $post;
  632. if ( $post->post_type == 'page' ) {
  633. if ( !current_user_can( 'edit_page', $post->ID ) )
  634. return;
  635. } else {
  636. if ( !current_user_can( 'edit_post', $post->ID ) )
  637. return;
  638. }
  639. $link = '<a class="post-edit-link" href="' . get_edit_post_link( $post->ID ) . '" title="' . attribute_escape( __( 'Edit post' ) ) . '">' . $link . '</a>';
  640. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  641. }
  642. /**
  643. * Retrieve edit comment link.
  644. *
  645. * @since 2.3.0
  646. *
  647. * @param int $comment_id Optional. Comment ID.
  648. * @return string
  649. */
  650. function get_edit_comment_link( $comment_id = 0 ) {
  651. $comment = &get_comment( $comment_id );
  652. $post = &get_post( $comment->comment_post_ID );
  653. if ( $post->post_type == 'page' ) {
  654. if ( !current_user_can( 'edit_page', $post->ID ) )
  655. return;
  656. } else {
  657. if ( !current_user_can( 'edit_post', $post->ID ) )
  658. return;
  659. }
  660. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  661. return apply_filters( 'get_edit_comment_link', $location );
  662. }
  663. /**
  664. * Display or retrieve edit comment link with formatting.
  665. *
  666. * @since 1.0.0
  667. *
  668. * @param string $link Optional. Anchor text.
  669. * @param string $before Optional. Display before edit link.
  670. * @param string $after Optional. Display after edit link.
  671. * @return string|null HTML content, if $echo is set to false.
  672. */
  673. function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
  674. global $comment, $post;
  675. if ( $post->post_type == 'attachment' ) {
  676. } elseif ( $post->post_type == 'page' ) {
  677. if ( !current_user_can( 'edit_page', $post->ID ) )
  678. return;
  679. } else {
  680. if ( !current_user_can( 'edit_post', $post->ID ) )
  681. return;
  682. }
  683. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
  684. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  685. }
  686. /**
  687. * Display edit bookmark (literally a URL external to blog) link.
  688. *
  689. * @since 2.7.0
  690. *
  691. * @param int $link Optional. Bookmark ID.
  692. * @return string
  693. */
  694. function get_edit_bookmark_link( $link = 0 ) {
  695. $link = get_bookmark( $link );
  696. if ( !current_user_can('manage_links') )
  697. return;
  698. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  699. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  700. }
  701. /**
  702. * Display edit bookmark (literally a URL external to blog) link anchor content.
  703. *
  704. * @since 2.7.0
  705. *
  706. * @param string $link Optional. Anchor text.
  707. * @param string $before Optional. Display before edit link.
  708. * @param string $after Optional. Display after edit link.
  709. * @param int $bookmark Optional. Bookmark ID.
  710. */
  711. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  712. $bookmark = get_bookmark($bookmark);
  713. if ( !current_user_can('manage_links') )
  714. return;
  715. if ( empty($link) )
  716. $link = __('Edit This');
  717. $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit link' ) . '">' . $link . '</a>';
  718. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  719. }
  720. // Navigation links
  721. /**
  722. * Retrieve previous post link that is adjacent to current post.
  723. *
  724. * @since 1.5.0
  725. *
  726. * @param bool $in_same_cat Optional. Whether link should be in same category.
  727. * @param string $excluded_categories Optional. Excluded categories IDs.
  728. * @return string
  729. */
  730. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  731. return get_adjacent_post($in_same_cat, $excluded_categories);
  732. }
  733. /**
  734. * Retrieve next post link that is adjacent to current post.
  735. *
  736. * @since 1.5.0
  737. *
  738. * @param bool $in_same_cat Optional. Whether link should be in same category.
  739. * @param string $excluded_categories Optional. Excluded categories IDs.
  740. * @return string
  741. */
  742. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  743. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  744. }
  745. /**
  746. * Retrieve adjacent post link.
  747. *
  748. * Can either be next or previous post link.
  749. *
  750. * @since 2.5.0
  751. *
  752. * @param bool $in_same_cat Optional. Whether link should be in same category.
  753. * @param string $excluded_categories Optional. Excluded categories IDs.
  754. * @param bool $previous Optional. Whether to retrieve previous post.
  755. * @return string
  756. */
  757. function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
  758. global $post, $wpdb;
  759. if( empty($post) || !is_single() || is_attachment() )
  760. return null;
  761. $current_post_date = $post->post_date;
  762. $join = '';
  763. $posts_in_ex_cats_sql = '';
  764. if ( $in_same_cat || !empty($excluded_categories) ) {
  765. $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";
  766. if ( $in_same_cat ) {
  767. $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
  768. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  769. }
  770. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  771. if ( !empty($excluded_categories) ) {
  772. $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
  773. if ( !empty($cat_array) ) {
  774. $excluded_categories = array_diff($excluded_categories, $cat_array);
  775. $posts_in_ex_cats_sql = '';
  776. }
  777. if ( !empty($excluded_categories) ) {
  778. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  779. }
  780. }
  781. }
  782. $adjacent = $previous ? 'previous' : 'next';
  783. $op = $previous ? '<' : '>';
  784. $order = $previous ? 'DESC' : 'ASC';
  785. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  786. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
  787. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  788. return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  789. }
  790. /**
  791. * Display previous post link that is adjacent to the current post.
  792. *
  793. * @since 1.5.0
  794. *
  795. * @param string $format Optional. Link anchor format.
  796. * @param string $link Optional. Link permalink format.
  797. * @param bool $in_same_cat Optional. Whether link should be in same category.
  798. * @param string $excluded_categories Optional. Excluded categories IDs.
  799. */
  800. function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  801. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
  802. }
  803. /**
  804. * Display next post link that is adjacent to the current post.
  805. *
  806. * @since 1.5.0
  807. *
  808. * @param string $format Optional. Link anchor format.
  809. * @param string $link Optional. Link permalink format.
  810. * @param bool $in_same_cat Optional. Whether link should be in same category.
  811. * @param string $excluded_categories Optional. Excluded categories IDs.
  812. */
  813. function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  814. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
  815. }
  816. /**
  817. * Display adjacent post link.
  818. *
  819. * Can be either next post link or previous.
  820. *
  821. * @since 2.5.0
  822. *
  823. * @param string $format Link anchor format.
  824. * @param string $link Link permalink format.
  825. * @param bool $in_same_cat Optional. Whether link should be in same category.
  826. * @param string $excluded_categories Optional. Excluded categories IDs.
  827. * @param bool $previous Optional, default is true. Whether display link to previous post.
  828. */
  829. function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
  830. if ( $previous && is_attachment() )
  831. $post = & get_post($GLOBALS['post']->post_parent);
  832. else
  833. $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
  834. if ( !$post )
  835. return;
  836. $title = $post->post_title;
  837. if ( empty($post->post_title) )
  838. $title = $previous ? __('Previous Post') : __('Next Post');
  839. $title = apply_filters('the_title', $title, $post);
  840. $date = mysql2date(get_option('date_format'), $post->post_date);
  841. $string = '<a href="'.get_permalink($post).'">';
  842. $link = str_replace('%title', $title, $link);
  843. $link = str_replace('%date', $date, $link);
  844. $link = $string . $link . '</a>';
  845. $format = str_replace('%link', $link, $format);
  846. $adjacent = $previous ? 'previous' : 'next';
  847. echo apply_filters( "{$adjacent}_post_link", $format, $link );
  848. }
  849. /**
  850. * Retrieve get links for page numbers.
  851. *
  852. * @since 1.5.0
  853. *
  854. * @param int $pagenum Optional. Page ID.
  855. * @return string
  856. */
  857. function get_pagenum_link($pagenum = 1) {
  858. global $wp_rewrite;
  859. $pagenum = (int) $pagenum;
  860. $request = remove_query_arg( 'paged' );
  861. $home_root = parse_url(get_option('home'));
  862. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  863. $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  864. $request = preg_replace('|^'. $home_root . '|', '', $request);
  865. $request = preg_replace('|^/+|', '', $request);
  866. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  867. $base = trailingslashit( get_bloginfo( 'home' ) );
  868. if ( $pagenum > 1 ) {
  869. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  870. } else {
  871. $result = $base . $request;
  872. }
  873. } else {
  874. $qs_regex = '|\?.*?$|';
  875. preg_match( $qs_regex, $request, $qs_match );
  876. if ( !empty( $qs_match[0] ) ) {
  877. $query_string = $qs_match[0];
  878. $request = preg_replace( $qs_regex, '', $request );
  879. } else {
  880. $query_string = '';
  881. }
  882. $request = preg_replace( '|page/\d+/?$|', '', $request);
  883. $request = preg_replace( '|^index\.php|', '', $request);
  884. $request = ltrim($request, '/');
  885. $base = trailingslashit( get_bloginfo( 'url' ) );
  886. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  887. $base .= 'index.php/';
  888. if ( $pagenum > 1 ) {
  889. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
  890. }
  891. $result = $base . $request . $query_string;
  892. }
  893. $result = apply_filters('get_pagenum_link', $result);
  894. return $result;
  895. }
  896. /**
  897. * Retrieve next posts pages link.
  898. *
  899. * Backported from 2.1.3 to 2.0.10.
  900. *
  901. * @since 2.0.10
  902. *
  903. * @param int $max_page Optional. Max pages.
  904. * @return string
  905. */
  906. function get_next_posts_page_link($max_page = 0) {
  907. global $paged;
  908. if ( !is_single() ) {
  909. if ( !$paged )
  910. $paged = 1;
  911. $nextpage = intval($paged) + 1;
  912. if ( !$max_page || $max_page >= $nextpage )
  913. return get_pagenum_link($nextpage);
  914. }
  915. }
  916. /**
  917. * Display or return the next posts pages link.
  918. *
  919. * @since 0.71
  920. *
  921. * @param int $max_page Optional. Max pages.
  922. * @param boolean $echo Optional. Echo or return;
  923. */
  924. function next_posts( $max_page = 0, $echo = true ) {
  925. $output = clean_url( get_next_posts_page_link( $max_page ) );
  926. if ( $echo )
  927. echo $output;
  928. else
  929. return $output;
  930. }
  931. /**
  932. * Return the next posts pages link.
  933. *
  934. * @since 2.7.0
  935. *
  936. * @param string $label Content for link text.
  937. * @param int $max_page Optional. Max pages.
  938. * @return string|null
  939. */
  940. function get_next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  941. global $paged, $wp_query;
  942. if ( !$max_page ) {
  943. $max_page = $wp_query->max_num_pages;
  944. }
  945. if ( !$paged )
  946. $paged = 1;
  947. $nextpage = intval($paged) + 1;
  948. if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
  949. $attr = apply_filters( 'next_posts_link_attributes', '' );
  950. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  951. }
  952. }
  953. /**
  954. * Display the next posts pages link.
  955. *
  956. * @since 0.71
  957. * @uses get_next_posts_link()
  958. *
  959. * @param string $label Content for link text.
  960. * @param int $max_page Optional. Max pages.
  961. */
  962. function next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  963. echo get_next_posts_link( $label, $max_page );
  964. }
  965. /**
  966. * Retrieve previous post pages link.
  967. *
  968. * Will only return string, if not on a single page or post.
  969. *
  970. * Backported to 2.0.10 from 2.1.3.
  971. *
  972. * @since 2.0.10
  973. *
  974. * @return string|null
  975. */
  976. function get_previous_posts_page_link() {
  977. global $paged;
  978. if ( !is_single() ) {
  979. $nextpage = intval($paged) - 1;
  980. if ( $nextpage < 1 )
  981. $nextpage = 1;
  982. return get_pagenum_link($nextpage);
  983. }
  984. }
  985. /**
  986. * Display or return the previous posts pages link.
  987. *
  988. * @since 0.71
  989. *
  990. * @param boolean $echo Optional. Echo or return;
  991. */
  992. function previous_posts( $echo = true ) {
  993. $output = clean_url( get_previous_posts_page_link() );
  994. if ( $echo )
  995. echo $output;
  996. else
  997. return $output;
  998. }
  999. /**
  1000. * Return the previous posts pages link.
  1001. *
  1002. * @since 2.7.0
  1003. *
  1004. * @param string $label Optional. Previous page link text.
  1005. * @return string|null
  1006. */
  1007. function get_previous_posts_link( $label = '&laquo; Previous Page' ) {
  1008. global $paged;
  1009. if ( !is_single() && $paged > 1 ) {
  1010. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1011. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a>';
  1012. }
  1013. }
  1014. /**
  1015. * Display the previous posts page link.
  1016. *
  1017. * @since 0.71
  1018. * @uses get_previous_posts_link()
  1019. *
  1020. * @param string $label Optional. Previous page link text.
  1021. */
  1022. function previous_posts_link( $label = '&laquo; Previous Page' ) {
  1023. echo get_previous_posts_link( $label );
  1024. }
  1025. /**
  1026. * Display post pages link navigation for previous and next pages.
  1027. *
  1028. * @since 0.71
  1029. *
  1030. * @param string $sep Optional. Separator for posts navigation links.
  1031. * @param string $prelabel Optional. Label for previous pages.
  1032. * @param string $nxtlabel Optional Label for next pages.
  1033. */
  1034. function posts_nav_link( $sep = ' &#8212; ', $prelabel = '&laquo; Previous Page', $nxtlabel = 'Next Page &raquo;' ) {
  1035. global $wp_query;
  1036. if ( !is_singular() ) {
  1037. $max_num_pages = $wp_query->max_num_pages;
  1038. $paged = get_query_var('paged');
  1039. //only have sep if there's both prev and next results
  1040. if ($paged < 2 || $paged >= $max_num_pages) {
  1041. $sep = '';
  1042. }
  1043. if ( $max_num_pages > 1 ) {
  1044. previous_posts_link($prelabel);
  1045. echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $sep);
  1046. next_posts_link($nxtlabel);
  1047. }
  1048. }
  1049. }
  1050. /**
  1051. * Retrieve page numbers links.
  1052. *
  1053. * @since 2.7.0
  1054. *
  1055. * @param int $pagenum Optional. Page number.
  1056. * @return string
  1057. */
  1058. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1059. global $post, $wp_rewrite;
  1060. $pagenum = (int) $pagenum;
  1061. $result = get_permalink( $post->ID );
  1062. if ( 'newest' == get_option('default_comments_page') ) {
  1063. if ( $pagenum != $max_page ) {
  1064. if ( $wp_rewrite->using_permalinks() )
  1065. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1066. else
  1067. $result = add_query_arg( 'cpage', $pagenum, $result );
  1068. }
  1069. } elseif ( $pagenum > 1 ) {
  1070. if ( $wp_rewrite->using_permalinks() )
  1071. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1072. else
  1073. $result = add_query_arg( 'cpage', $pagenum, $result );
  1074. }
  1075. $result .= '#comments';
  1076. $result = apply_filters('get_comments_pagenum_link', $result);
  1077. return $result;
  1078. }
  1079. /**
  1080. * Return the link to next comments pages.
  1081. *
  1082. * @since 2.7.1
  1083. *
  1084. * @param string $label Optional. Label for link text.
  1085. * @param int $max_page Optional. Max page.
  1086. * @return string|null
  1087. */
  1088. function get_next_comments_link( $label = '', $max_page = 0 ) {
  1089. global $wp_query;
  1090. if ( !is_singular() || !get_option('page_comments') )
  1091. return;
  1092. $page = get_query_var('cpage');
  1093. $nextpage = intval($page) + 1;
  1094. if ( empty($max_page) )
  1095. $max_page = $wp_query->max_num_comment_pages;
  1096. if ( empty($max_page) )
  1097. $max_page = get_comment_pages_count();
  1098. if ( $nextpage > $max_page )
  1099. return;
  1100. if ( empty($label) )
  1101. $label = __('Newer Comments &raquo;');
  1102. return '<a href="' . clean_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  1103. }
  1104. /**
  1105. * Display the link to next comments pages.
  1106. *
  1107. * @since 2.7.0
  1108. *
  1109. * @param string $label Optional. Label for link text.
  1110. * @param int $max_page Optional. Max page.
  1111. */
  1112. function next_comments_link( $label = '', $max_page = 0 ) {
  1113. echo get_next_comments_link( $label, $max_page );
  1114. }
  1115. /**
  1116. * Return the previous comments page link.
  1117. *
  1118. * @since 2.7.1
  1119. *
  1120. * @param string $label Optional. Label for comments link text.
  1121. * @return string|null
  1122. */
  1123. function get_previous_comments_link( $label = '' ) {
  1124. if ( !is_singular() || !get_option('page_comments') )
  1125. return;
  1126. $page = get_query_var('cpage');
  1127. if ( intval($page) <= 1 )
  1128. return;
  1129. $prevpage = intval($page) - 1;
  1130. if ( empty($label) )
  1131. $label = __('&laquo; Older Comments');
  1132. return '<a href="' . clean_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  1133. }
  1134. /**
  1135. * Display the previous comments page link.
  1136. *
  1137. * @since 2.7.0
  1138. *
  1139. * @param string $label Optional. Label for comments link text.
  1140. */
  1141. function previous_comments_link( $label = '' ) {
  1142. echo get_previous_comments_link( $label );
  1143. }
  1144. /**
  1145. * Create pagination links for the comments on the current post.
  1146. *
  1147. * @see paginate_links()
  1148. * @since 2.7.0
  1149. *
  1150. * @param string|array $args Optional args. See paginate_links.
  1151. * @return string Markup for pagination links.
  1152. */
  1153. function paginate_comments_links($args = array()) {
  1154. global $wp_query, $wp_rewrite;
  1155. if ( !is_singular() || !get_option('page_comments') )
  1156. return;
  1157. $page = get_query_var('cpage');
  1158. if ( !$page )
  1159. $page = 1;
  1160. $max_page = get_comment_pages_count();
  1161. $defaults = array(
  1162. 'base' => add_query_arg( 'cpage', '%#%' ),
  1163. 'format' => '',
  1164. 'total' => $max_page,
  1165. 'current' => $page,
  1166. 'echo' => true,
  1167. 'add_fragment' => '#comments'
  1168. );
  1169. if ( $wp_rewrite->using_permalinks() )
  1170. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
  1171. $args = wp_parse_args( $args, $defaults );
  1172. $page_links = paginate_links( $args );
  1173. if ( $args['echo'] )
  1174. echo $page_links;
  1175. else
  1176. return $page_links;
  1177. }
  1178. /**
  1179. * Retrieve shortcut link.
  1180. *
  1181. * Use this in 'a' element 'href' attribute.
  1182. *
  1183. * @since 2.6.0
  1184. *
  1185. * @return string
  1186. */
  1187. function get_shortcut_link() {
  1188. $link = "javascript:
  1189. var d=document,
  1190. w=window,
  1191. e=w.getSelection,
  1192. k=d.getSelection,
  1193. x=d.selection,
  1194. s=(e?e():(k)?k():(x?x.createRange().text:0)),
  1195. f='" . admin_url('press-this.php') . "',
  1196. l=d.location,
  1197. e=encodeURIComponent,
  1198. g=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=2';
  1199. function a(){
  1200. if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=720,height=570')){
  1201. l.href=g;
  1202. }
  1203. }";
  1204. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
  1205. $link .= 'setTimeout(a,0);';
  1206. else
  1207. $link .= 'a();';
  1208. $link .= "void(0);";
  1209. $link = str_replace(array("\r", "\n", "\t"), '', $link);
  1210. return apply_filters('shortcut_link', $link);
  1211. }
  1212. /**
  1213. * Retrieve the site url.
  1214. *
  1215. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  1216. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1217. * overridden.
  1218. *
  1219. * @package WordPress
  1220. * @since 2.6.0
  1221. *
  1222. * @param string $path Optional. Path relative to the site url.
  1223. * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
  1224. * @return string Site url link with optional path appended.
  1225. */
  1226. function site_url($path = '', $scheme = null) {
  1227. // should the list of allowed schemes be maintained elsewhere?
  1228. $orig_scheme = $scheme;
  1229. if ( !in_array($scheme, array('http', 'https')) ) {
  1230. if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
  1231. $scheme = 'https';
  1232. elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
  1233. $scheme = 'https';
  1234. elseif ( ('admin' == $scheme) && force_ssl_admin() )
  1235. $scheme = 'https';
  1236. else
  1237. $scheme = ( is_ssl() ? 'https' : 'http' );
  1238. }
  1239. $url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );
  1240. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1241. $url .= '/' . ltrim($path, '/');
  1242. return apply_filters('site_url', $url, $path, $orig_scheme);
  1243. }
  1244. /**
  1245. * Retrieve the url to the admin area.
  1246. *
  1247. * @package WordPress
  1248. * @since 2.6.0
  1249. *
  1250. * @param string $path Optional path relative to the admin url
  1251. * @return string Admin url link with optional path appended
  1252. */
  1253. function admin_url($path = '') {
  1254. $url = site_url('wp-admin/', 'admin');
  1255. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1256. $url .= ltrim($path, '/');
  1257. return $url;
  1258. }
  1259. /**
  1260. * Retrieve the url to the includes directory.
  1261. *
  1262. * @package WordPress
  1263. * @since 2.6.0
  1264. *
  1265. * @param string $path Optional. Path relative to the includes url.
  1266. * @return string Includes url link with optional path appended.
  1267. */
  1268. function includes_url($path = '') {
  1269. $url = site_url() . '/' . WPINC . '/';
  1270. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1271. $url .= ltrim($path, '/');
  1272. return $url;
  1273. }
  1274. /**
  1275. * Retrieve the url to the content directory.
  1276. *
  1277. * @package WordPress
  1278. * @since 2.6.0
  1279. *
  1280. * @param string $path Optional. Path relative to the content url.
  1281. * @return string Content url link with optional path appended.
  1282. */
  1283. function content_url($path = '') {
  1284. $scheme = ( is_ssl() ? 'https' : 'http' );
  1285. $url = WP_CONTENT_URL;
  1286. if ( 0 === strpos($url, 'http') ) {
  1287. if ( is_ssl() )
  1288. $url = str_replace( 'http://', "{$scheme}://", $url );
  1289. }
  1290. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1291. $url .= '/' . ltrim($path, '/');
  1292. return $url;
  1293. }
  1294. /**
  1295. * Retrieve the url to the plugins directory or to a specific file within that directory.
  1296. * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
  1297. *
  1298. * @package WordPress
  1299. * @since 2.6.0
  1300. *
  1301. * @param string $path Optional. Path relative to the plugins url.
  1302. * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
  1303. * @return string Plugins url link with optional path appended.
  1304. */
  1305. function plugins_url($path = '', $plugin = '') {
  1306. $scheme = ( is_ssl() ? 'https' : 'http' );
  1307. $url = WP_PLUGIN_URL;
  1308. if ( 0 === strpos($url, 'http') ) {
  1309. if ( is_ssl() )
  1310. $url = str_replace( 'http://', "{$scheme}://", $url );
  1311. }
  1312. if ( !empty($plugin) && is_string($plugin) )
  1313. {
  1314. $folder = dirname(plugin_basename($plugin));
  1315. if ('.' != $folder)
  1316. $url .= '/' . ltrim($folder, '/');
  1317. }
  1318. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1319. $url .= '/' . ltrim($path, '/');
  1320. return $url;
  1321. }
  1322. ?>