PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/link-template.php

https://github.com/ianloic/wordpress
PHP | 1810 lines | 1006 code | 235 blank | 569 comment | 233 complexity | 9b67eb09b9e2010bdd3f6346f6a1ce68 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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 = trailingslashit(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 = trailingslashit(get_option('home')) . "$link";
  207. $link = user_trailingslashit($link, 'page');
  208. } else {
  209. $link = trailingslashit(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 = trailingslashit(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', trailingslashit(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', trailingslashit(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', trailingslashit(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 = trailingslashit(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 = trailingslashit(get_option('home')) . "?feed=$feed&amp;page_id=$post_id";
  374. else
  375. $url = trailingslashit(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 apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
  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 = trailingslashit(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 = trailingslashit(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 = esc_attr(get_search_query());
  545. else
  546. $search = esc_attr(stripslashes($search_query));
  547. if ( empty($feed) )
  548. $feed = get_default_feed();
  549. $link = trailingslashit(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 = esc_attr(get_search_query());
  565. else
  566. $search = esc_attr(stripslashes($search_query));
  567. if ( empty($feed) )
  568. $feed = get_default_feed();
  569. $link = trailingslashit(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. * Display edit post 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. * @param int $id Optional. Post ID.
  630. */
  631. function edit_post_link( $link = 'Edit This', $before = '', $after = '', $id = 0 ) {
  632. if ( !$post = &get_post( $id ) )
  633. return;
  634. if ( !$url = get_edit_post_link( $post->ID ) )
  635. return;
  636. $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( __( 'Edit post' ) ) . '">' . $link . '</a>';
  637. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  638. }
  639. /**
  640. * Retrieve edit comment link.
  641. *
  642. * @since 2.3.0
  643. *
  644. * @param int $comment_id Optional. Comment ID.
  645. * @return string
  646. */
  647. function get_edit_comment_link( $comment_id = 0 ) {
  648. $comment = &get_comment( $comment_id );
  649. $post = &get_post( $comment->comment_post_ID );
  650. if ( $post->post_type == 'page' ) {
  651. if ( !current_user_can( 'edit_page', $post->ID ) )
  652. return;
  653. } else {
  654. if ( !current_user_can( 'edit_post', $post->ID ) )
  655. return;
  656. }
  657. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  658. return apply_filters( 'get_edit_comment_link', $location );
  659. }
  660. /**
  661. * Display or retrieve edit comment link with formatting.
  662. *
  663. * @since 1.0.0
  664. *
  665. * @param string $link Optional. Anchor text.
  666. * @param string $before Optional. Display before edit link.
  667. * @param string $after Optional. Display after edit link.
  668. * @return string|null HTML content, if $echo is set to false.
  669. */
  670. function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
  671. global $comment, $post;
  672. if ( $post->post_type == 'attachment' ) {
  673. } elseif ( $post->post_type == 'page' ) {
  674. if ( !current_user_can( 'edit_page', $post->ID ) )
  675. return;
  676. } else {
  677. if ( !current_user_can( 'edit_post', $post->ID ) )
  678. return;
  679. }
  680. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
  681. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  682. }
  683. /**
  684. * Display edit bookmark (literally a URL external to blog) link.
  685. *
  686. * @since 2.7.0
  687. *
  688. * @param int $link Optional. Bookmark ID.
  689. * @return string
  690. */
  691. function get_edit_bookmark_link( $link = 0 ) {
  692. $link = get_bookmark( $link );
  693. if ( !current_user_can('manage_links') )
  694. return;
  695. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  696. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  697. }
  698. /**
  699. * Display edit bookmark (literally a URL external to blog) link anchor content.
  700. *
  701. * @since 2.7.0
  702. *
  703. * @param string $link Optional. Anchor text.
  704. * @param string $before Optional. Display before edit link.
  705. * @param string $after Optional. Display after edit link.
  706. * @param int $bookmark Optional. Bookmark ID.
  707. */
  708. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  709. $bookmark = get_bookmark($bookmark);
  710. if ( !current_user_can('manage_links') )
  711. return;
  712. if ( empty($link) )
  713. $link = __('Edit This');
  714. $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit link' ) . '">' . $link . '</a>';
  715. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  716. }
  717. // Navigation links
  718. /**
  719. * Retrieve previous post link that is adjacent to current post.
  720. *
  721. * @since 1.5.0
  722. *
  723. * @param bool $in_same_cat Optional. Whether link should be in same category.
  724. * @param string $excluded_categories Optional. Excluded categories IDs.
  725. * @return string
  726. */
  727. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  728. return get_adjacent_post($in_same_cat, $excluded_categories);
  729. }
  730. /**
  731. * Retrieve next post link that is adjacent to current post.
  732. *
  733. * @since 1.5.0
  734. *
  735. * @param bool $in_same_cat Optional. Whether link should be in same category.
  736. * @param string $excluded_categories Optional. Excluded categories IDs.
  737. * @return string
  738. */
  739. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  740. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  741. }
  742. /**
  743. * Retrieve adjacent post link.
  744. *
  745. * Can either be next or previous post link.
  746. *
  747. * @since 2.5.0
  748. *
  749. * @param bool $in_same_cat Optional. Whether link should be in same category.
  750. * @param string $excluded_categories Optional. Excluded categories IDs.
  751. * @param bool $previous Optional. Whether to retrieve previous post.
  752. * @return string
  753. */
  754. function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
  755. global $post, $wpdb;
  756. if ( empty($post) || !is_single() || is_attachment() )
  757. return null;
  758. $current_post_date = $post->post_date;
  759. $join = '';
  760. $posts_in_ex_cats_sql = '';
  761. if ( $in_same_cat || !empty($excluded_categories) ) {
  762. $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";
  763. if ( $in_same_cat ) {
  764. $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
  765. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  766. }
  767. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  768. if ( !empty($excluded_categories) ) {
  769. $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
  770. if ( !empty($cat_array) ) {
  771. $excluded_categories = array_diff($excluded_categories, $cat_array);
  772. $posts_in_ex_cats_sql = '';
  773. }
  774. if ( !empty($excluded_categories) ) {
  775. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  776. }
  777. }
  778. }
  779. $adjacent = $previous ? 'previous' : 'next';
  780. $op = $previous ? '<' : '>';
  781. $order = $previous ? 'DESC' : 'ASC';
  782. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  783. $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 );
  784. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  785. $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
  786. $query_key = 'adjacent_post_' . md5($query);
  787. $result = wp_cache_get($query_key, 'counts');
  788. if ( false !== $result )
  789. return $result;
  790. $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  791. if ( null === $result )
  792. $result = '';
  793. wp_cache_set($query_key, $result, 'counts');
  794. return $result;
  795. }
  796. /**
  797. * Get adjacent post relational link.
  798. *
  799. * Can either be next or previous post relational link.
  800. *
  801. * @since 2.8.0
  802. *
  803. * @param string $title Optional. Link title format.
  804. * @param bool $in_same_cat Optional. Whether link should be in same category.
  805. * @param string $excluded_categories Optional. Excluded categories IDs.
  806. * @param bool $previous Optional, default is true. Whether display link to previous post.
  807. * @return string
  808. */
  809. function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
  810. if ( $previous && is_attachment() )
  811. $post = & get_post($GLOBALS['post']->post_parent);
  812. else
  813. $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
  814. if ( empty($post) )
  815. return;
  816. if ( empty($post->post_title) )
  817. $post->post_title = $previous ? __('Previous Post') : __('Next Post');
  818. $date = mysql2date(get_option('date_format'), $post->post_date);
  819. $title = str_replace('%title', $post->post_title, $title);
  820. $title = str_replace('%date', $date, $title);
  821. $title = apply_filters('the_title', $title, $post);
  822. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  823. $link .= esc_attr( $title );
  824. $link .= "' href='" . get_permalink($post) . "' />\n";
  825. $adjacent = $previous ? 'previous' : 'next';
  826. return apply_filters( "{$adjacent}_post_rel_link", $link );
  827. }
  828. /**
  829. * Display relational links for the posts adjacent to the current post.
  830. *
  831. * @since 2.8.0
  832. *
  833. * @param string $title Optional. Link title format.
  834. * @param bool $in_same_cat Optional. Whether link should be in same category.
  835. * @param string $excluded_categories Optional. Excluded categories IDs.
  836. */
  837. function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  838. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  839. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  840. }
  841. /**
  842. * Display relational link for the next post adjacent to the current post.
  843. *
  844. * @since 2.8.0
  845. *
  846. * @param string $title Optional. Link title format.
  847. * @param bool $in_same_cat Optional. Whether link should be in same category.
  848. * @param string $excluded_categories Optional. Excluded categories IDs.
  849. */
  850. function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  851. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  852. }
  853. /**
  854. * Display relational link for the previous post adjacent to the current post.
  855. *
  856. * @since 2.8.0
  857. *
  858. * @param string $title Optional. Link title format.
  859. * @param bool $in_same_cat Optional. Whether link should be in same category.
  860. * @param string $excluded_categories Optional. Excluded categories IDs.
  861. */
  862. function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  863. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  864. }
  865. /**
  866. * Retrieve boundary post.
  867. *
  868. * Boundary being either the first or last post by publish date within the contraitns specified
  869. * by in same category or excluded categories.
  870. *
  871. * @since 2.8.0
  872. *
  873. * @param bool $in_same_cat Optional. Whether returned post should be in same category.
  874. * @param string $excluded_categories Optional. Excluded categories IDs.
  875. * @param bool $previous Optional. Whether to retrieve first post.
  876. * @return object
  877. */
  878. function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
  879. global $post, $wpdb;
  880. if ( empty($post) || !is_single() || is_attachment() )
  881. return null;
  882. $cat_array = array();
  883. $excluded_categories = array();
  884. if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
  885. if ( !empty($in_same_cat) ) {
  886. $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
  887. }
  888. if ( !empty($excluded_categories) ) {
  889. $excluded_categories = array_map('intval', explode(',', $excluded_categories));
  890. if ( !empty($cat_array) )
  891. $excluded_categories = array_diff($excluded_categories, $cat_array);
  892. $inverse_cats = array();
  893. foreach ( $excluded_categories as $excluded_category)
  894. $inverse_cats[] = $excluded_category * -1;
  895. $excluded_categories = $inverse_cats;
  896. }
  897. }
  898. $categories = implode(',', array_merge($cat_array, $excluded_categories) );
  899. $order = $start ? 'ASC' : 'DESC';
  900. return get_posts( array('numberposts' => 1, 'order' => $order, 'orderby' => 'ID', 'category' => $categories) );
  901. }
  902. /**
  903. * Get boundary post relational link.
  904. *
  905. * Can either be start or end post relational link.
  906. *
  907. * @since 2.8.0
  908. *
  909. * @param string $title Optional. Link title format.
  910. * @param bool $in_same_cat Optional. Whether link should be in same category.
  911. * @param string $excluded_categories Optional. Excluded categories IDs.
  912. * @param bool $start Optional, default is true. Whether display link to first post.
  913. * @return string
  914. */
  915. function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
  916. $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
  917. // Even though we limited get_posts to return only 1 item it still returns an array of objects.
  918. $post = $posts[0];
  919. if ( empty($post) )
  920. return;
  921. if ( empty($post->post_title) )
  922. $post->post_title = $start ? __('First Post') : __('Last Post');
  923. $date = mysql2date(get_option('date_format'), $post->post_date);
  924. $title = str_replace('%title', $post->post_title, $title);
  925. $title = str_replace('%date', $date, $title);
  926. $title = apply_filters('the_title', $title, $post);
  927. $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
  928. $link .= esc_attr($title);
  929. $link .= "' href='" . get_permalink($post) . "' />\n";
  930. $boundary = $start ? 'start' : 'end';
  931. return apply_filters( "{$boundary}_post_rel_link", $link );
  932. }
  933. /**
  934. * Display relational link for the first post.
  935. *
  936. * @since 2.8.0
  937. *
  938. * @param string $title Optional. Link title format.
  939. * @param bool $in_same_cat Optional. Whether link should be in same category.
  940. * @param string $excluded_categories Optional. Excluded categories IDs.
  941. */
  942. function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  943. echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
  944. }
  945. /**
  946. * Get site index relational link.
  947. *
  948. * @since 2.8.0
  949. *
  950. * @return string
  951. */
  952. function get_index_rel_link() {
  953. $link = "<link rel='index' title='" . esc_attr(get_bloginfo('name')) . "' href='" . get_bloginfo('siteurl') . "' />\n";
  954. return apply_filters( "index_rel_link", $link );
  955. }
  956. /**
  957. * Display relational link for the site index.
  958. *
  959. * @since 2.8.0
  960. */
  961. function index_rel_link() {
  962. echo get_index_rel_link();
  963. }
  964. /**
  965. * Get parent post relational link.
  966. *
  967. * @since 2.8.0
  968. *
  969. * @param string $title Optional. Link title format.
  970. * @return string
  971. */
  972. function get_parent_post_rel_link($title = '%title') {
  973. if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
  974. $post = & get_post($GLOBALS['post']->post_parent);
  975. if ( empty($post) )
  976. return;
  977. $date = mysql2date(get_option('date_format'), $post->post_date);
  978. $title = str_replace('%title', $post->post_title, $title);
  979. $title = str_replace('%date', $date, $title);
  980. $title = apply_filters('the_title', $title, $post);
  981. $link = "<link rel='up' title='";
  982. $link .= esc_attr( $title );
  983. $link .= "' href='" . get_permalink($post) . "' />\n";
  984. return apply_filters( "parent_post_rel_link", $link );
  985. }
  986. /**
  987. * Display relational link for parent item
  988. *
  989. * @since 2.8.0
  990. */
  991. function parent_post_rel_link($title = '%title') {
  992. echo get_parent_post_rel_link($title);
  993. }
  994. /**
  995. * Display previous post link that is adjacent to the current post.
  996. *
  997. * @since 1.5.0
  998. *
  999. * @param string $format Optional. Link anchor format.
  1000. * @param string $link Optional. Link permalink format.
  1001. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1002. * @param string $excluded_categories Optional. Excluded categories IDs.
  1003. */
  1004. function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1005. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
  1006. }
  1007. /**
  1008. * Display next post link that is adjacent to the current post.
  1009. *
  1010. * @since 1.5.0
  1011. *
  1012. * @param string $format Optional. Link anchor format.
  1013. * @param string $link Optional. Link permalink format.
  1014. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1015. * @param string $excluded_categories Optional. Excluded categories IDs.
  1016. */
  1017. function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
  1018. adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
  1019. }
  1020. /**
  1021. * Display adjacent post link.
  1022. *
  1023. * Can be either next post link or previous.
  1024. *
  1025. * @since 2.5.0
  1026. *
  1027. * @param string $format Link anchor format.
  1028. * @param string $link Link permalink format.
  1029. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1030. * @param string $excluded_categories Optional. Excluded categories IDs.
  1031. * @param bool $previous Optional, default is true. Whether display link to previous post.
  1032. */
  1033. function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1034. if ( $previous && is_attachment() )
  1035. $post = & get_post($GLOBALS['post']->post_parent);
  1036. else
  1037. $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
  1038. if ( !$post )
  1039. return;
  1040. $title = $post->post_title;
  1041. if ( empty($post->post_title) )
  1042. $title = $previous ? __('Previous Post') : __('Next Post');
  1043. $title = apply_filters('the_title', $title, $post);
  1044. $date = mysql2date(get_option('date_format'), $post->post_date);
  1045. $string = '<a href="'.get_permalink($post).'">';
  1046. $link = str_replace('%title', $title, $link);
  1047. $link = str_replace('%date', $date, $link);
  1048. $link = $string . $link . '</a>';
  1049. $format = str_replace('%link', $link, $format);
  1050. $adjacent = $previous ? 'previous' : 'next';
  1051. echo apply_filters( "{$adjacent}_post_link", $format, $link );
  1052. }
  1053. /**
  1054. * Retrieve get links for page numbers.
  1055. *
  1056. * @since 1.5.0
  1057. *
  1058. * @param int $pagenum Optional. Page ID.
  1059. * @return string
  1060. */
  1061. function get_pagenum_link($pagenum = 1) {
  1062. global $wp_rewrite;
  1063. $pagenum = (int) $pagenum;
  1064. $request = remove_query_arg( 'paged' );
  1065. $home_root = parse_url(get_option('home'));
  1066. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1067. $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  1068. $request = preg_replace('|^'. $home_root . '|', '', $request);
  1069. $request = preg_replace('|^/+|', '', $request);
  1070. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1071. $base = trailingslashit( get_bloginfo( 'home' ) );
  1072. if ( $pagenum > 1 ) {
  1073. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1074. } else {
  1075. $result = $base . $request;
  1076. }
  1077. } else {
  1078. $qs_regex = '|\?.*?$|';
  1079. preg_match( $qs_regex, $request, $qs_match );
  1080. if ( !empty( $qs_match[0] ) ) {
  1081. $query_string = $qs_match[0];
  1082. $request = preg_replace( $qs_regex, '', $request );
  1083. } else {
  1084. $query_string = '';
  1085. }
  1086. $request = preg_replace( '|page/\d+/?$|', '', $request);
  1087. $request = preg_replace( '|^index\.php|', '', $request);
  1088. $request = ltrim($request, '/');
  1089. $base = trailingslashit( get_bloginfo( 'url' ) );
  1090. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1091. $base .= 'index.php/';
  1092. if ( $pagenum > 1 ) {
  1093. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
  1094. }
  1095. $result = $base . $request . $query_string;
  1096. }
  1097. $result = apply_filters('get_pagenum_link', $result);
  1098. return $result;
  1099. }
  1100. /**
  1101. * Retrieve next posts pages link.
  1102. *
  1103. * Backported from 2.1.3 to 2.0.10.
  1104. *
  1105. * @since 2.0.10
  1106. *
  1107. * @param int $max_page Optional. Max pages.
  1108. * @return string
  1109. */
  1110. function get_next_posts_page_link($max_page = 0) {
  1111. global $paged;
  1112. if ( !is_single() ) {
  1113. if ( !$paged )
  1114. $paged = 1;
  1115. $nextpage = intval($paged) + 1;
  1116. if ( !$max_page || $max_page >= $nextpage )
  1117. return get_pagenum_link($nextpage);
  1118. }
  1119. }
  1120. /**
  1121. * Display or return the next posts pages link.
  1122. *
  1123. * @since 0.71
  1124. *
  1125. * @param int $max_page Optional. Max pages.
  1126. * @param boolean $echo Optional. Echo or return;
  1127. */
  1128. function next_posts( $max_page = 0, $echo = true ) {
  1129. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1130. if ( $echo )
  1131. echo $output;
  1132. else
  1133. return $output;
  1134. }
  1135. /**
  1136. * Return the next posts pages link.
  1137. *
  1138. * @since 2.7.0
  1139. *
  1140. * @param string $label Content for link text.
  1141. * @param int $max_page Optional. Max pages.
  1142. * @return string|null
  1143. */
  1144. function get_next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  1145. global $paged, $wp_query;
  1146. if ( !$max_page ) {
  1147. $max_page = $wp_query->max_num_pages;
  1148. }
  1149. if ( !$paged )
  1150. $paged = 1;
  1151. $nextpage = intval($paged) + 1;
  1152. if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
  1153. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1154. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  1155. }
  1156. }
  1157. /**
  1158. * Display the next posts pages link.
  1159. *
  1160. * @since 0.71
  1161. * @uses get_next_posts_link()
  1162. *
  1163. * @param string $label Content for link text.
  1164. * @param int $max_page Optional. Max pages.
  1165. */
  1166. function next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
  1167. echo get_next_posts_link( $label, $max_page );
  1168. }
  1169. /**
  1170. * Retrieve previous post pages link.
  1171. *
  1172. * Will only return string, if not on a single page or post.
  1173. *
  1174. * Backported to 2.0.10 from 2.1.3.
  1175. *
  1176. * @since 2.0.10
  1177. *
  1178. * @return string|null
  1179. */
  1180. function get_previous_posts_page_link() {
  1181. global $paged;
  1182. if ( !is_single() ) {
  1183. $nextpage = intval($paged) - 1;
  1184. if ( $nextpage < 1 )
  1185. $nextpage = 1;
  1186. return get_pagenum_link($nextpage);
  1187. }
  1188. }
  1189. /**
  1190. * Display or return the previous posts pages link.
  1191. *
  1192. * @since 0.71
  1193. *
  1194. * @param boolean $echo Optional. Echo or return;
  1195. */
  1196. function previous_posts( $echo = true ) {
  1197. $output = esc_url( get_previous_posts_page_link() );
  1198. if ( $echo )
  1199. echo $output;
  1200. else
  1201. return $output;
  1202. }
  1203. /**
  1204. * Return the previous posts pages link.
  1205. *
  1206. * @since 2.7.0
  1207. *
  1208. * @param string $label Optional. Previous page link text.
  1209. * @return string|null
  1210. */
  1211. function get_previous_posts_link( $label = '&laquo; Previous Page' ) {
  1212. global $paged;
  1213. if ( !is_single() && $paged > 1 ) {
  1214. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1215. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a>';
  1216. }
  1217. }
  1218. /**
  1219. * Display the previous posts page link.
  1220. *
  1221. * @since 0.71
  1222. * @uses get_previous_posts_link()
  1223. *
  1224. * @param string $label Optional. Previous page link text.
  1225. */
  1226. function previous_posts_link( $label = '&laquo; Previous Page' ) {
  1227. echo get_previous_posts_link( $label );
  1228. }
  1229. /**
  1230. * Return post pages link navigation for previous and next pages.
  1231. *
  1232. * @since 2.8
  1233. *
  1234. * @param string|array $args Optional args.
  1235. * @return string The posts link navigation.
  1236. */
  1237. function get_posts_nav_link( $args = array() ) {
  1238. global $wp_query;
  1239. $return = '';
  1240. if ( !is_singular() ) {
  1241. $defaults = array(
  1242. 'sep' => ' &#8212; ',
  1243. 'prelabel' => __('&laquo; Previous Page'),
  1244. 'nxtlabel' => __('Next Page &raquo;'),
  1245. );
  1246. $args = wp_parse_args( $args, $defaults );
  1247. $max_num_pages = $wp_query->max_num_pages;
  1248. $paged = get_query_var('paged');
  1249. //only have sep if there's both prev and next results
  1250. if ($paged < 2 || $paged >= $max_num_pages) {
  1251. $args['sep'] = '';
  1252. }
  1253. if ( $max_num_pages > 1 ) {
  1254. $return = get_previous_posts_link($args['prelabel']);
  1255. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $args['sep']);
  1256. $return .= get_next_posts_link($args['nxtlabel']);
  1257. }
  1258. }
  1259. return $return;
  1260. }
  1261. /**
  1262. * Display post pages link navigation for previous and next pages.
  1263. *
  1264. * @since 0.71
  1265. *
  1266. * @param string $sep Optional. Separator for posts navigation links.
  1267. * @param string $prelabel Optional. Label for previous pages.
  1268. * @param string $nxtlabel Optional Label for next pages.
  1269. */
  1270. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1271. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1272. echo get_posts_nav_link($args);
  1273. }
  1274. /**
  1275. * Retrieve page numbers links.
  1276. *
  1277. * @since 2.7.0
  1278. *
  1279. * @param int $pagenum Optional. Page number.
  1280. * @return string
  1281. */
  1282. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  1283. global $post, $wp_rewrite;
  1284. $pagenum = (int) $pagenum;
  1285. $result = get_permalink( $post->ID );
  1286. if ( 'newest' == get_option('default_comments_page') ) {
  1287. if ( $pagenum != $max_page ) {
  1288. if ( $wp_rewrite->using_permalinks() )
  1289. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1290. else
  1291. $result = add_query_arg( 'cpage', $pagenum, $result );
  1292. }
  1293. } elseif ( $pagenum > 1 ) {
  1294. if ( $wp_rewrite->using_permalinks() )
  1295. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  1296. else
  1297. $result = add_query_arg( 'cpage', $pagenum, $result );
  1298. }
  1299. $result .= '#comments';
  1300. $result = apply_filters('get_comments_pagenum_link', $result);
  1301. return $result;
  1302. }
  1303. /**
  1304. * Return the link to next comments pages.
  1305. *
  1306. * @since 2.7.1
  1307. *
  1308. * @param string $label Optional. Label for link text.
  1309. * @param int $max_page Optional. Max page.
  1310. * @return string|null
  1311. */
  1312. function get_next_comments_link( $label = '', $max_page = 0 ) {
  1313. global $wp_query;
  1314. if ( !is_singular() || !get_option('page_comments') )
  1315. return;
  1316. $page = get_query_var('cpage');
  1317. $nextpage = intval($page) + 1;
  1318. if ( empty($max_page) )
  1319. $max_page = $wp_query->max_num_comment_pages;
  1320. if ( empty($max_page) )
  1321. $max_page = get_comment_pages_count();
  1322. if ( $nextpage > $max_page )
  1323. return;
  1324. if ( empty($label) )
  1325. $label = __('Newer Comments &raquo;');
  1326. return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  1327. }
  1328. /**
  1329. * Display the link to next comments pages.
  1330. *
  1331. * @since 2.7.0
  1332. *
  1333. * @param string $label Optional. Label for link text.
  1334. * @param int $max_page Optional. Max page.
  1335. */
  1336. function next_comments_link( $label = '', $max_page = 0 ) {
  1337. echo get_next_comments_link( $label, $max_page );
  1338. }
  1339. /**
  1340. * Return the previous comments page link.
  1341. *
  1342. * @since 2.7.1
  1343. *
  1344. * @param string $label Optional. Label for comments link text.
  1345. * @return string|null
  1346. */
  1347. function get_previous_comments_link( $label = '' ) {
  1348. if ( !is_singular() || !get_option('page_comments') )
  1349. return;
  1350. $page = get_query_var('cpage');
  1351. if ( intval($page) <= 1 )
  1352. return;
  1353. $prevpage = intval($page) - 1;
  1354. if ( empty($label) )
  1355. $label = __('&laquo; Older Comments');
  1356. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
  1357. }
  1358. /**
  1359. * Display the previous comments page link.
  1360. *
  1361. * @since 2.7.0
  1362. *
  1363. * @param string $label Optional. Label for comments link text.
  1364. */
  1365. function previous_comments_link( $label = '' ) {
  1366. echo get_previous_comments_link( $label );
  1367. }
  1368. /**
  1369. * Create pagination links for the comments on the current post.
  1370. *
  1371. * @see paginate_links()
  1372. * @since 2.7.0
  1373. *
  1374. * @param string|array $args Optional args. See paginate_links.
  1375. * @return string Markup for pagination links.
  1376. */
  1377. function paginate_comments_links($args = array()) {
  1378. global $wp_query, $wp_rewrite;
  1379. if ( !is_singular() || !get_option('page_comments') )
  1380. return;
  1381. $page = get_query_var('cpage');
  1382. if ( !$page )
  1383. $page = 1;
  1384. $max_page = get_comment_pages_count();
  1385. $defaults = array(
  1386. 'base' => add_query_arg( 'cpage', '%#%' ),
  1387. 'format' => '',
  1388. 'total' => $max_page,
  1389. 'current' => $page,
  1390. 'echo' => true,
  1391. 'add_fragment' => '#comments'
  1392. );
  1393. if ( $wp_rewrite->using_permalinks() )
  1394. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
  1395. $args = wp_parse_args( $args, $defaults );
  1396. $page_links = paginate_links( $args );
  1397. if ( $args['echo'] )
  1398. echo $page_links;
  1399. else
  1400. return $page_links;
  1401. }
  1402. /**
  1403. * Retrieve shortcut link.
  1404. *
  1405. * Use this in 'a' element 'href' attribute.
  1406. *
  1407. * @since 2.6.0
  1408. *
  1409. * @return string
  1410. */
  1411. function get_shortcut_link() {
  1412. $link = "javascript:
  1413. var d=document,
  1414. w=window,
  1415. e=w.getSelection,
  1416. k=d.getSelection,
  1417. x=d.selection,
  1418. s=(e?e():(k)?k():(x?x.createRange().text:0)),
  1419. f='" . admin_url('press-this.php') . "',
  1420. l=d.location,
  1421. e=encodeURIComponent,
  1422. g=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=2';
  1423. function a(){
  1424. if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=720,height=570')){
  1425. l.href=g;
  1426. }
  1427. }";
  1428. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
  1429. $link .= 'setTimeout(a,0);';
  1430. else
  1431. $link .= 'a();';
  1432. $link .= "void(0);";
  1433. $link = str_replace(array("\r", "\n", "\t"), '', $link);
  1434. return apply_filters('shortcut_link', $link);
  1435. }
  1436. /**
  1437. * Retrieve the site url.
  1438. *
  1439. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  1440. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  1441. * overridden.
  1442. *
  1443. * @package WordPress
  1444. * @since 2.6.0
  1445. *
  1446. * @param string $path Optional. Path relative to the site url.
  1447. * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
  1448. * @return string Site url link with optional path appended.
  1449. */
  1450. function site_url($path = '', $scheme = null) {
  1451. // should the list of allowed schemes be maintained elsewhere?
  1452. $orig_scheme = $scheme;
  1453. if ( !in_array($scheme, array('http', 'https')) ) {
  1454. if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
  1455. $scheme = 'https';
  1456. elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
  1457. $scheme = 'https';
  1458. elseif ( ('admin' == $scheme) && force_ssl_admin() )
  1459. $scheme = 'https';
  1460. else
  1461. $scheme = ( is_ssl() ? 'https' : 'http' );
  1462. }
  1463. $url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );
  1464. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1465. $url .= '/' . ltrim($path, '/');
  1466. return apply_filters('site_url', $url, $path, $orig_scheme);
  1467. }
  1468. /**
  1469. * Retrieve the url to the admin area.
  1470. *
  1471. * @package WordPress
  1472. * @since 2.6.0
  1473. *
  1474. * @param string $path Optional path relative to the admin url
  1475. * @return string Admin url link with optional path appended
  1476. */
  1477. function admin_url($path = '') {
  1478. $url = site_url('wp-admin/', 'admin');
  1479. if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
  1480. $url .= ltrim($path, '/');
  1481. return apply_filters('admin_url', $url, $path);
  1482. }
  1483. /**
  1484. * Retrieve the url to the includes directory.
  1485. *
  1486. * @package WordPress
  1487. * @since 2.6.0
  1488. *
  1489. * @param string $path Optional. Path relative to the includes url.
  1490. * @return string Includes url link with optional path appended.
  1491. */
  1492. function includes_url($path = '') {
  1493. $url = site_url() . '/' . WPINC . '/'

Large files files are truncated, but you can click here to view the full file