PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

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