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

/source/mydroid/external/webkit/WebKitSite/blog/wp-includes/link-template.php

https://bitbucket.org/tfzxyinhao/kindle-fire
PHP | 1809 lines | 1005 code | 235 blank | 569 comment | 234 complexity | 687cfc21bcc3b13c6f1d8ec022e8ba45 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, 0BSD, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.0, BSD-3-Clause, LGPL-2.1, Apache-2.0, AGPL-3.0, GPL-3.0

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

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