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

/wp-includes/link-template.php

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

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