PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

https://github.com/webgefrickel/frckl-init-wordpress
PHP | 2506 lines | 1851 code | 191 blank | 464 comment | 190 complexity | 6296f624c3db0f59985602b1007a4ea8 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-2.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 esc_url( 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 URL with or without a trailing slash.
  29. * @param string $type_of_url 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, post_type_archive
  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. $post = get_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|WP_Post $id Optional. Post ID or post object, defaults to the current post.
  72. * @param bool $leavename Optional. Whether to keep post name or page name, defaults to false.
  73. * @return string|bool The permalink URL or false if post does not exist.
  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) )
  97. return false;
  98. if ( $post->post_type == 'page' )
  99. return get_page_link($post->ID, $leavename, $sample);
  100. elseif ( $post->post_type == 'attachment' )
  101. return get_attachment_link( $post->ID, $leavename );
  102. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
  103. return get_post_permalink($post->ID, $leavename, $sample);
  104. $permalink = get_option('permalink_structure');
  105. $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);
  106. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  107. $unixtime = strtotime($post->post_date);
  108. $category = '';
  109. if ( strpos($permalink, '%category%') !== false ) {
  110. $cats = get_the_category($post->ID);
  111. if ( $cats ) {
  112. usort($cats, '_usort_terms_by_ID'); // order by ID
  113. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  114. $category_object = get_term( $category_object, 'category' );
  115. $category = $category_object->slug;
  116. if ( $parent = $category_object->parent )
  117. $category = get_category_parents($parent, false, '/', true) . $category;
  118. }
  119. // show default category in permalinks, without
  120. // having to assign it explicitly
  121. if ( empty($category) ) {
  122. $default_category = get_term( get_option( 'default_category' ), 'category' );
  123. $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  124. }
  125. }
  126. $author = '';
  127. if ( strpos($permalink, '%author%') !== false ) {
  128. $authordata = get_userdata($post->post_author);
  129. $author = $authordata->user_nicename;
  130. }
  131. $date = explode(" ",date('Y m d H i s', $unixtime));
  132. $rewritereplace =
  133. array(
  134. $date[0],
  135. $date[1],
  136. $date[2],
  137. $date[3],
  138. $date[4],
  139. $date[5],
  140. $post->post_name,
  141. $post->ID,
  142. $category,
  143. $author,
  144. $post->post_name,
  145. );
  146. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
  147. $permalink = user_trailingslashit($permalink, 'single');
  148. } else { // if they're not using the fancy permalink option
  149. $permalink = home_url('?p=' . $post->ID);
  150. }
  151. return apply_filters('post_link', $permalink, $post, $leavename);
  152. }
  153. /**
  154. * Retrieve the permalink for a post with a custom post type.
  155. *
  156. * @since 3.0.0
  157. *
  158. * @param int $id Optional. Post ID.
  159. * @param bool $leavename Optional, defaults to false. Whether to keep post name.
  160. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  161. * @return string
  162. */
  163. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  164. global $wp_rewrite;
  165. $post = get_post($id);
  166. if ( is_wp_error( $post ) )
  167. return $post;
  168. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
  169. $slug = $post->post_name;
  170. $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  171. $post_type = get_post_type_object($post->post_type);
  172. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
  173. if ( ! $leavename ) {
  174. if ( $post_type->hierarchical )
  175. $slug = get_page_uri($id);
  176. $post_link = str_replace("%$post->post_type%", $slug, $post_link);
  177. }
  178. $post_link = home_url( user_trailingslashit($post_link) );
  179. } else {
  180. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
  181. $post_link = add_query_arg($post_type->query_var, $slug, '');
  182. else
  183. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
  184. $post_link = home_url($post_link);
  185. }
  186. return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);
  187. }
  188. /**
  189. * Retrieve permalink from post ID.
  190. *
  191. * @since 1.0.0
  192. *
  193. * @param int $post_id Optional. Post ID.
  194. * @param mixed $deprecated Not used.
  195. * @return string
  196. */
  197. function post_permalink( $post_id = 0, $deprecated = '' ) {
  198. if ( !empty( $deprecated ) )
  199. _deprecated_argument( __FUNCTION__, '1.3' );
  200. return get_permalink($post_id);
  201. }
  202. /**
  203. * Retrieve the permalink for current page or page ID.
  204. *
  205. * Respects page_on_front. Use this one.
  206. *
  207. * @since 1.5.0
  208. *
  209. * @param int|object $post Optional. Post ID or object.
  210. * @param bool $leavename Optional, defaults to false. Whether to keep page name.
  211. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  212. * @return string
  213. */
  214. function get_page_link( $post = false, $leavename = false, $sample = false ) {
  215. $post = get_post( $post );
  216. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
  217. $link = home_url('/');
  218. else
  219. $link = _get_page_link( $post, $leavename, $sample );
  220. return apply_filters( 'page_link', $link, $post->ID, $sample );
  221. }
  222. /**
  223. * Retrieve the page permalink.
  224. *
  225. * Ignores page_on_front. Internal use only.
  226. *
  227. * @since 2.1.0
  228. * @access private
  229. *
  230. * @param int|object $post Optional. Post ID or object.
  231. * @param bool $leavename Optional. Leave name.
  232. * @param bool $sample Optional. Sample permalink.
  233. * @return string
  234. */
  235. function _get_page_link( $post = false, $leavename = false, $sample = false ) {
  236. global $wp_rewrite;
  237. $post = get_post( $post );
  238. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  239. $link = $wp_rewrite->get_page_permastruct();
  240. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
  241. if ( ! $leavename ) {
  242. $link = str_replace('%pagename%', get_page_uri( $post ), $link);
  243. }
  244. $link = home_url($link);
  245. $link = user_trailingslashit($link, 'page');
  246. } else {
  247. $link = home_url( '?page_id=' . $post->ID );
  248. }
  249. return apply_filters( '_get_page_link', $link, $post->ID );
  250. }
  251. /**
  252. * Retrieve permalink for attachment.
  253. *
  254. * This can be used in the WordPress Loop or outside of it.
  255. *
  256. * @since 2.0.0
  257. *
  258. * @param int|object $post Optional. Post ID or object.
  259. * @param bool $leavename Optional. Leave name.
  260. * @return string
  261. */
  262. function get_attachment_link( $post = null, $leavename = false ) {
  263. global $wp_rewrite;
  264. $link = false;
  265. $post = get_post( $post );
  266. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
  267. if ( $wp_rewrite->using_permalinks() && $parent ) {
  268. if ( 'page' == $parent->post_type )
  269. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
  270. else
  271. $parentlink = get_permalink( $post->post_parent );
  272. if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
  273. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
  274. else
  275. $name = $post->post_name;
  276. if ( strpos($parentlink, '?') === false )
  277. $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
  278. if ( ! $leavename )
  279. $link = str_replace( '%postname%', $name, $link );
  280. }
  281. if ( ! $link )
  282. $link = home_url( '/?attachment_id=' . $post->ID );
  283. return apply_filters( 'attachment_link', $link, $post->ID );
  284. }
  285. /**
  286. * Retrieve the permalink for the year archives.
  287. *
  288. * @since 1.5.0
  289. *
  290. * @param int|bool $year False for current year or year for permalink.
  291. * @return string
  292. */
  293. function get_year_link($year) {
  294. global $wp_rewrite;
  295. if ( !$year )
  296. $year = gmdate('Y', current_time('timestamp'));
  297. $yearlink = $wp_rewrite->get_year_permastruct();
  298. if ( !empty($yearlink) ) {
  299. $yearlink = str_replace('%year%', $year, $yearlink);
  300. return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
  301. } else {
  302. return apply_filters('year_link', home_url('?m=' . $year), $year);
  303. }
  304. }
  305. /**
  306. * Retrieve the permalink for the month archives with year.
  307. *
  308. * @since 1.0.0
  309. *
  310. * @param bool|int $year False for current year. Integer of year.
  311. * @param bool|int $month False for current month. Integer of month.
  312. * @return string
  313. */
  314. function get_month_link($year, $month) {
  315. global $wp_rewrite;
  316. if ( !$year )
  317. $year = gmdate('Y', current_time('timestamp'));
  318. if ( !$month )
  319. $month = gmdate('m', current_time('timestamp'));
  320. $monthlink = $wp_rewrite->get_month_permastruct();
  321. if ( !empty($monthlink) ) {
  322. $monthlink = str_replace('%year%', $year, $monthlink);
  323. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
  324. return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
  325. } else {
  326. return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
  327. }
  328. }
  329. /**
  330. * Retrieve the permalink for the day archives with year and month.
  331. *
  332. * @since 1.0.0
  333. *
  334. * @param bool|int $year False for current year. Integer of year.
  335. * @param bool|int $month False for current month. Integer of month.
  336. * @param bool|int $day False for current day. Integer of day.
  337. * @return string
  338. */
  339. function get_day_link($year, $month, $day) {
  340. global $wp_rewrite;
  341. if ( !$year )
  342. $year = gmdate('Y', current_time('timestamp'));
  343. if ( !$month )
  344. $month = gmdate('m', current_time('timestamp'));
  345. if ( !$day )
  346. $day = gmdate('j', current_time('timestamp'));
  347. $daylink = $wp_rewrite->get_day_permastruct();
  348. if ( !empty($daylink) ) {
  349. $daylink = str_replace('%year%', $year, $daylink);
  350. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
  351. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
  352. return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
  353. } else {
  354. return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
  355. }
  356. }
  357. /**
  358. * Display the permalink for the feed type.
  359. *
  360. * @since 3.0.0
  361. *
  362. * @param string $anchor The link's anchor text.
  363. * @param string $feed Optional, defaults to default feed. Feed type.
  364. */
  365. function the_feed_link( $anchor, $feed = '' ) {
  366. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  367. echo apply_filters( 'the_feed_link', $link, $feed );
  368. }
  369. /**
  370. * Retrieve the permalink for the feed type.
  371. *
  372. * @since 1.5.0
  373. *
  374. * @param string $feed Optional, defaults to default feed. Feed type.
  375. * @return string
  376. */
  377. function get_feed_link($feed = '') {
  378. global $wp_rewrite;
  379. $permalink = $wp_rewrite->get_feed_permastruct();
  380. if ( '' != $permalink ) {
  381. if ( false !== strpos($feed, 'comments_') ) {
  382. $feed = str_replace('comments_', '', $feed);
  383. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  384. }
  385. if ( get_default_feed() == $feed )
  386. $feed = '';
  387. $permalink = str_replace('%feed%', $feed, $permalink);
  388. $permalink = preg_replace('#/+#', '/', "/$permalink");
  389. $output = home_url( user_trailingslashit($permalink, 'feed') );
  390. } else {
  391. if ( empty($feed) )
  392. $feed = get_default_feed();
  393. if ( false !== strpos($feed, 'comments_') )
  394. $feed = str_replace('comments_', 'comments-', $feed);
  395. $output = home_url("?feed={$feed}");
  396. }
  397. return apply_filters('feed_link', $output, $feed);
  398. }
  399. /**
  400. * Retrieve the permalink for the post comments feed.
  401. *
  402. * @since 2.2.0
  403. *
  404. * @param int $post_id Optional. Post ID.
  405. * @param string $feed Optional. Feed type.
  406. * @return string
  407. */
  408. function get_post_comments_feed_link($post_id = 0, $feed = '') {
  409. $post_id = absint( $post_id );
  410. if ( ! $post_id )
  411. $post_id = get_the_ID();
  412. if ( empty( $feed ) )
  413. $feed = get_default_feed();
  414. if ( '' != get_option('permalink_structure') ) {
  415. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
  416. $url = _get_page_link( $post_id );
  417. else
  418. $url = get_permalink($post_id);
  419. $url = trailingslashit($url) . 'feed';
  420. if ( $feed != get_default_feed() )
  421. $url .= "/$feed";
  422. $url = user_trailingslashit($url, 'single_feed');
  423. } else {
  424. $type = get_post_field('post_type', $post_id);
  425. if ( 'page' == $type )
  426. $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
  427. else
  428. $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
  429. }
  430. return apply_filters('post_comments_feed_link', $url);
  431. }
  432. /**
  433. * Display the comment feed link for a post.
  434. *
  435. * Prints out the comment feed link for a post. Link text is placed in the
  436. * anchor. If no link text is specified, default text is used. If no post ID is
  437. * specified, the current post is used.
  438. *
  439. * @package WordPress
  440. * @subpackage Feed
  441. * @since 2.5.0
  442. *
  443. * @param string $link_text Descriptive text.
  444. * @param int $post_id Optional post ID. Default to current post.
  445. * @param string $feed Optional. Feed format.
  446. * @return string Link to the comment feed for the current post.
  447. */
  448. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  449. $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
  450. if ( empty($link_text) )
  451. $link_text = __('Comments Feed');
  452. echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
  453. }
  454. /**
  455. * Retrieve the feed link for a given author.
  456. *
  457. * Returns a link to the feed for all posts by a given author. A specific feed
  458. * can be requested or left blank to get the default feed.
  459. *
  460. * @package WordPress
  461. * @subpackage Feed
  462. * @since 2.5.0
  463. *
  464. * @param int $author_id ID of an author.
  465. * @param string $feed Optional. Feed type.
  466. * @return string Link to the feed for the author specified by $author_id.
  467. */
  468. function get_author_feed_link( $author_id, $feed = '' ) {
  469. $author_id = (int) $author_id;
  470. $permalink_structure = get_option('permalink_structure');
  471. if ( empty($feed) )
  472. $feed = get_default_feed();
  473. if ( '' == $permalink_structure ) {
  474. $link = home_url("?feed=$feed&amp;author=" . $author_id);
  475. } else {
  476. $link = get_author_posts_url($author_id);
  477. if ( $feed == get_default_feed() )
  478. $feed_link = 'feed';
  479. else
  480. $feed_link = "feed/$feed";
  481. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  482. }
  483. $link = apply_filters('author_feed_link', $link, $feed);
  484. return $link;
  485. }
  486. /**
  487. * Retrieve the feed link for a category.
  488. *
  489. * Returns a link to the feed for all posts in a given category. A specific feed
  490. * can be requested or left blank to get the default feed.
  491. *
  492. * @package WordPress
  493. * @subpackage Feed
  494. * @since 2.5.0
  495. *
  496. * @param int $cat_id ID of a category.
  497. * @param string $feed Optional. Feed type.
  498. * @return string Link to the feed for the category specified by $cat_id.
  499. */
  500. function get_category_feed_link($cat_id, $feed = '') {
  501. return get_term_feed_link($cat_id, 'category', $feed);
  502. }
  503. /**
  504. * Retrieve the feed link for a term.
  505. *
  506. * Returns a link to the feed for all posts in a given term. A specific feed
  507. * can be requested or left blank to get the default feed.
  508. *
  509. * @since 3.0
  510. *
  511. * @param int $term_id ID of a category.
  512. * @param string $taxonomy Optional. Taxonomy of $term_id
  513. * @param string $feed Optional. Feed type.
  514. * @return string Link to the feed for the term specified by $term_id and $taxonomy.
  515. */
  516. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  517. $term_id = ( int ) $term_id;
  518. $term = get_term( $term_id, $taxonomy );
  519. if ( empty( $term ) || is_wp_error( $term ) )
  520. return false;
  521. if ( empty( $feed ) )
  522. $feed = get_default_feed();
  523. $permalink_structure = get_option( 'permalink_structure' );
  524. if ( '' == $permalink_structure ) {
  525. if ( 'category' == $taxonomy ) {
  526. $link = home_url("?feed=$feed&amp;cat=$term_id");
  527. }
  528. elseif ( 'post_tag' == $taxonomy ) {
  529. $link = home_url("?feed=$feed&amp;tag=$term->slug");
  530. } else {
  531. $t = get_taxonomy( $taxonomy );
  532. $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
  533. }
  534. } else {
  535. $link = get_term_link( $term_id, $term->taxonomy );
  536. if ( $feed == get_default_feed() )
  537. $feed_link = 'feed';
  538. else
  539. $feed_link = "feed/$feed";
  540. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  541. }
  542. if ( 'category' == $taxonomy )
  543. $link = apply_filters( 'category_feed_link', $link, $feed );
  544. elseif ( 'post_tag' == $taxonomy )
  545. $link = apply_filters( 'tag_feed_link', $link, $feed );
  546. else
  547. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  548. return $link;
  549. }
  550. /**
  551. * Retrieve permalink for feed of tag.
  552. *
  553. * @since 2.3.0
  554. *
  555. * @param int $tag_id Tag ID.
  556. * @param string $feed Optional. Feed type.
  557. * @return string
  558. */
  559. function get_tag_feed_link($tag_id, $feed = '') {
  560. return get_term_feed_link($tag_id, 'post_tag', $feed);
  561. }
  562. /**
  563. * Retrieve edit tag link.
  564. *
  565. * @since 2.7.0
  566. *
  567. * @param int $tag_id Tag ID
  568. * @param string $taxonomy Taxonomy
  569. * @return string
  570. */
  571. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  572. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  573. }
  574. /**
  575. * Display or retrieve edit tag link with formatting.
  576. *
  577. * @since 2.7.0
  578. *
  579. * @param string $link Optional. Anchor text.
  580. * @param string $before Optional. Display before edit link.
  581. * @param string $after Optional. Display after edit link.
  582. * @param object $tag Tag object.
  583. * @return string HTML content.
  584. */
  585. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  586. $link = edit_term_link( $link, '', '', $tag, false );
  587. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  588. }
  589. /**
  590. * Retrieve edit term url.
  591. *
  592. * @since 3.1.0
  593. *
  594. * @param int $term_id Term ID
  595. * @param string $taxonomy Taxonomy
  596. * @param string $object_type The object type
  597. * @return string
  598. */
  599. function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
  600. $tax = get_taxonomy( $taxonomy );
  601. if ( !current_user_can( $tax->cap->edit_terms ) )
  602. return;
  603. $term = get_term( $term_id, $taxonomy );
  604. $args = array(
  605. 'action' => 'edit',
  606. 'taxonomy' => $taxonomy,
  607. 'tag_ID' => $term->term_id,
  608. );
  609. if ( $object_type )
  610. $args['post_type'] = $object_type;
  611. $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
  612. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  613. }
  614. /**
  615. * Display or retrieve edit term link with formatting.
  616. *
  617. * @since 3.1.0
  618. *
  619. * @param string $link Optional. Anchor text.
  620. * @param string $before Optional. Display before edit link.
  621. * @param string $after Optional. Display after edit link.
  622. * @param object $term Term object.
  623. * @return string HTML content.
  624. */
  625. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  626. if ( is_null( $term ) )
  627. $term = get_queried_object();
  628. if ( ! $term )
  629. return;
  630. $tax = get_taxonomy( $term->taxonomy );
  631. if ( ! current_user_can( $tax->cap->edit_terms ) )
  632. return;
  633. if ( empty( $link ) )
  634. $link = __('Edit This');
  635. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
  636. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  637. if ( $echo )
  638. echo $link;
  639. else
  640. return $link;
  641. }
  642. /**
  643. * Retrieve permalink for search.
  644. *
  645. * @since 3.0.0
  646. * @param string $query Optional. The query string to use. If empty the current query is used.
  647. * @return string
  648. */
  649. function get_search_link( $query = '' ) {
  650. global $wp_rewrite;
  651. if ( empty($query) )
  652. $search = get_search_query( false );
  653. else
  654. $search = stripslashes($query);
  655. $permastruct = $wp_rewrite->get_search_permastruct();
  656. if ( empty( $permastruct ) ) {
  657. $link = home_url('?s=' . urlencode($search) );
  658. } else {
  659. $search = urlencode($search);
  660. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
  661. $link = str_replace( '%search%', $search, $permastruct );
  662. $link = home_url( user_trailingslashit( $link, 'search' ) );
  663. }
  664. return apply_filters( 'search_link', $link, $search );
  665. }
  666. /**
  667. * Retrieve the permalink for the feed of the search results.
  668. *
  669. * @since 2.5.0
  670. *
  671. * @param string $search_query Optional. Search query.
  672. * @param string $feed Optional. Feed type.
  673. * @return string
  674. */
  675. function get_search_feed_link($search_query = '', $feed = '') {
  676. global $wp_rewrite;
  677. $link = get_search_link($search_query);
  678. if ( empty($feed) )
  679. $feed = get_default_feed();
  680. $permastruct = $wp_rewrite->get_search_permastruct();
  681. if ( empty($permastruct) ) {
  682. $link = add_query_arg('feed', $feed, $link);
  683. } else {
  684. $link = trailingslashit($link);
  685. $link .= "feed/$feed/";
  686. }
  687. $link = apply_filters('search_feed_link', $link, $feed, 'posts');
  688. return $link;
  689. }
  690. /**
  691. * Retrieve the permalink for the comments feed of the search results.
  692. *
  693. * @since 2.5.0
  694. *
  695. * @param string $search_query Optional. Search query.
  696. * @param string $feed Optional. Feed type.
  697. * @return string
  698. */
  699. function get_search_comments_feed_link($search_query = '', $feed = '') {
  700. global $wp_rewrite;
  701. if ( empty($feed) )
  702. $feed = get_default_feed();
  703. $link = get_search_feed_link($search_query, $feed);
  704. $permastruct = $wp_rewrite->get_search_permastruct();
  705. if ( empty($permastruct) )
  706. $link = add_query_arg('feed', 'comments-' . $feed, $link);
  707. else
  708. $link = add_query_arg('withcomments', 1, $link);
  709. $link = apply_filters('search_feed_link', $link, $feed, 'comments');
  710. return $link;
  711. }
  712. /**
  713. * Retrieve the permalink for a post type archive.
  714. *
  715. * @since 3.1.0
  716. *
  717. * @param string $post_type Post type
  718. * @return string
  719. */
  720. function get_post_type_archive_link( $post_type ) {
  721. global $wp_rewrite;
  722. if ( ! $post_type_obj = get_post_type_object( $post_type ) )
  723. return false;
  724. if ( ! $post_type_obj->has_archive )
  725. return false;
  726. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  727. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  728. if ( $post_type_obj->rewrite['with_front'] )
  729. $struct = $wp_rewrite->front . $struct;
  730. else
  731. $struct = $wp_rewrite->root . $struct;
  732. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  733. } else {
  734. $link = home_url( '?post_type=' . $post_type );
  735. }
  736. return apply_filters( 'post_type_archive_link', $link, $post_type );
  737. }
  738. /**
  739. * Retrieve the permalink for a post type archive feed.
  740. *
  741. * @since 3.1.0
  742. *
  743. * @param string $post_type Post type
  744. * @param string $feed Optional. Feed type
  745. * @return string
  746. */
  747. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  748. $default_feed = get_default_feed();
  749. if ( empty( $feed ) )
  750. $feed = $default_feed;
  751. if ( ! $link = get_post_type_archive_link( $post_type ) )
  752. return false;
  753. $post_type_obj = get_post_type_object( $post_type );
  754. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
  755. $link = trailingslashit( $link );
  756. $link .= 'feed/';
  757. if ( $feed != $default_feed )
  758. $link .= "$feed/";
  759. } else {
  760. $link = add_query_arg( 'feed', $feed, $link );
  761. }
  762. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  763. }
  764. /**
  765. * Retrieve edit posts link for post.
  766. *
  767. * Can be used within the WordPress loop or outside of it. Can be used with
  768. * pages, posts, attachments, and revisions.
  769. *
  770. * @since 2.3.0
  771. *
  772. * @param int $id Optional. Post ID.
  773. * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
  774. * @return string
  775. */
  776. function get_edit_post_link( $id = 0, $context = 'display' ) {
  777. if ( ! $post = get_post( $id ) )
  778. return;
  779. if ( 'revision' === $post->post_type )
  780. $action = '';
  781. elseif ( 'display' == $context )
  782. $action = '&amp;action=edit';
  783. else
  784. $action = '&action=edit';
  785. $post_type_object = get_post_type_object( $post->post_type );
  786. if ( !$post_type_object )
  787. return;
  788. if ( !current_user_can( 'edit_post', $post->ID ) )
  789. return;
  790. return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
  791. }
  792. /**
  793. * Display edit post link for post.
  794. *
  795. * @since 1.0.0
  796. *
  797. * @param string $link Optional. Anchor text.
  798. * @param string $before Optional. Display before edit link.
  799. * @param string $after Optional. Display after edit link.
  800. * @param int $id Optional. Post ID.
  801. */
  802. function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  803. if ( !$post = get_post( $id ) )
  804. return;
  805. if ( !$url = get_edit_post_link( $post->ID ) )
  806. return;
  807. if ( null === $link )
  808. $link = __('Edit This');
  809. $post_type_obj = get_post_type_object( $post->post_type );
  810. $link = '<a class="post-edit-link" href="' . $url . '">' . $link . '</a>';
  811. echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
  812. }
  813. /**
  814. * Retrieve delete posts link for post.
  815. *
  816. * Can be used within the WordPress loop or outside of it, with any post type.
  817. *
  818. * @since 2.9.0
  819. *
  820. * @param int $id Optional. Post ID.
  821. * @param string $deprecated Not used.
  822. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  823. * @return string
  824. */
  825. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  826. if ( ! empty( $deprecated ) )
  827. _deprecated_argument( __FUNCTION__, '3.0' );
  828. if ( !$post = get_post( $id ) )
  829. return;
  830. $post_type_object = get_post_type_object( $post->post_type );
  831. if ( !$post_type_object )
  832. return;
  833. if ( !current_user_can( 'delete_post', $post->ID ) )
  834. return;
  835. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  836. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  837. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  838. }
  839. /**
  840. * Retrieve edit comment link.
  841. *
  842. * @since 2.3.0
  843. *
  844. * @param int $comment_id Optional. Comment ID.
  845. * @return string
  846. */
  847. function get_edit_comment_link( $comment_id = 0 ) {
  848. $comment = get_comment( $comment_id );
  849. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  850. return;
  851. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  852. return apply_filters( 'get_edit_comment_link', $location );
  853. }
  854. /**
  855. * Display or retrieve edit comment link with formatting.
  856. *
  857. * @since 1.0.0
  858. *
  859. * @param string $link Optional. Anchor text.
  860. * @param string $before Optional. Display before edit link.
  861. * @param string $after Optional. Display after edit link.
  862. * @return string|null HTML content, if $echo is set to false.
  863. */
  864. function edit_comment_link( $link = null, $before = '', $after = '' ) {
  865. global $comment;
  866. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  867. return;
  868. if ( null === $link )
  869. $link = __('Edit This');
  870. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $link . '</a>';
  871. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
  872. }
  873. /**
  874. * Display edit bookmark (literally a URL external to blog) link.
  875. *
  876. * @since 2.7.0
  877. *
  878. * @param int $link Optional. Bookmark ID.
  879. * @return string
  880. */
  881. function get_edit_bookmark_link( $link = 0 ) {
  882. $link = get_bookmark( $link );
  883. if ( !current_user_can('manage_links') )
  884. return;
  885. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  886. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  887. }
  888. /**
  889. * Display edit bookmark (literally a URL external to blog) link anchor content.
  890. *
  891. * @since 2.7.0
  892. *
  893. * @param string $link Optional. Anchor text.
  894. * @param string $before Optional. Display before edit link.
  895. * @param string $after Optional. Display after edit link.
  896. * @param int $bookmark Optional. Bookmark ID.
  897. */
  898. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  899. $bookmark = get_bookmark($bookmark);
  900. if ( !current_user_can('manage_links') )
  901. return;
  902. if ( empty($link) )
  903. $link = __('Edit This');
  904. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
  905. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  906. }
  907. /**
  908. * Retrieve edit user link
  909. *
  910. * @since 3.5.0
  911. *
  912. * @param int $user_id Optional. User ID. Defaults to the current user.
  913. * @return string URL to edit user page or empty string.
  914. */
  915. function get_edit_user_link( $user_id = null ) {
  916. if ( ! $user_id )
  917. $user_id = get_current_user_id();
  918. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
  919. return '';
  920. $user = get_userdata( $user_id );
  921. if ( ! $user )
  922. return '';
  923. if ( get_current_user_id() == $user->ID )
  924. $link = get_edit_profile_url( $user->ID );
  925. else
  926. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  927. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  928. }
  929. // Navigation links
  930. /**
  931. * Retrieve previous post that is adjacent to current post.
  932. *
  933. * @since 1.5.0
  934. *
  935. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  936. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  937. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  938. */
  939. function get_previous_post($in_same_cat = false, $excluded_categories = '') {
  940. return get_adjacent_post($in_same_cat, $excluded_categories);
  941. }
  942. /**
  943. * Retrieve next post that is adjacent to current post.
  944. *
  945. * @since 1.5.0
  946. *
  947. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  948. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  949. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  950. */
  951. function get_next_post($in_same_cat = false, $excluded_categories = '') {
  952. return get_adjacent_post($in_same_cat, $excluded_categories, false);
  953. }
  954. /**
  955. * Retrieve adjacent post.
  956. *
  957. * Can either be next or previous post.
  958. *
  959. * @since 2.5.0
  960. *
  961. * @param bool $in_same_cat Optional. Whether post should be in a same category.
  962. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  963. * @param bool $previous Optional. Whether to retrieve previous post.
  964. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  965. */
  966. function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
  967. global $wpdb;
  968. if ( ! $post = get_post() )
  969. return null;
  970. $current_post_date = $post->post_date;
  971. $join = '';
  972. $posts_in_ex_cats_sql = '';
  973. if ( $in_same_cat || ! empty( $excluded_categories ) ) {
  974. $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";
  975. if ( $in_same_cat ) {
  976. if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
  977. return '';
  978. $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
  979. if ( ! $cat_array || is_wp_error( $cat_array ) )
  980. return '';
  981. $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  982. }
  983. $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
  984. if ( ! empty( $excluded_categories ) ) {
  985. if ( ! is_array( $excluded_categories ) ) {
  986. // back-compat, $excluded_categories used to be IDs separated by " and "
  987. if ( strpos( $excluded_categories, ' and ' ) !== false ) {
  988. _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
  989. $excluded_categories = explode( ' and ', $excluded_categories );
  990. } else {
  991. $excluded_categories = explode( ',', $excluded_categories );
  992. }
  993. }
  994. $excluded_categories = array_map( 'intval', $excluded_categories );
  995. if ( ! empty( $cat_array ) ) {
  996. $excluded_categories = array_diff($excluded_categories, $cat_array);
  997. $posts_in_ex_cats_sql = '';
  998. }
  999. if ( !empty($excluded_categories) ) {
  1000. $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  1001. }
  1002. }
  1003. }
  1004. $adjacent = $previous ? 'previous' : 'next';
  1005. $op = $previous ? '<' : '>';
  1006. $order = $previous ? 'DESC' : 'ASC';
  1007. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
  1008. $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 );
  1009. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  1010. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1011. $query_key = 'adjacent_post_' . md5($query);
  1012. $result = wp_cache_get($query_key, 'counts');
  1013. if ( false !== $result ) {
  1014. if ( $result )
  1015. $result = get_post( $result );
  1016. return $result;
  1017. }
  1018. $result = $wpdb->get_var( $query );
  1019. if ( null === $result )
  1020. $result = '';
  1021. wp_cache_set($query_key, $result, 'counts');
  1022. if ( $result )
  1023. $result = get_post( $result );
  1024. return $result;
  1025. }
  1026. /**
  1027. * Get adjacent post relational link.
  1028. *
  1029. * Can either be next or previous post relational link.
  1030. *
  1031. * @since 2.8.0
  1032. *
  1033. * @param string $title Optional. Link title format.
  1034. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1035. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1036. * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
  1037. * @return string
  1038. */
  1039. function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
  1040. if ( $previous && is_attachment() && $post = get_post() )
  1041. $post = get_post( $post->post_parent );
  1042. else
  1043. $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
  1044. if ( empty($post) )
  1045. return;
  1046. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
  1047. if ( empty( $post_title ) )
  1048. $post_title = $previous ? __('Previous Post') : __('Next Post');
  1049. $date = mysql2date(get_option('date_format'), $post->post_date);
  1050. $title = str_replace('%title', $post_title, $title);
  1051. $title = str_replace('%date', $date, $title);
  1052. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1053. $link .= esc_attr( $title );
  1054. $link .= "' href='" . get_permalink($post) . "' />\n";
  1055. $adjacent = $previous ? 'previous' : 'next';
  1056. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1057. }
  1058. /**
  1059. * Display relational links for the posts adjacent to the current post.
  1060. *
  1061. * @since 2.8.0
  1062. *
  1063. * @param string $title Optional. Link title format.
  1064. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1065. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1066. */
  1067. function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1068. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1069. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1070. }
  1071. /**
  1072. * Display relational links for the posts adjacent to the current post for single post pages.
  1073. *
  1074. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1075. * @since 3.0.0
  1076. *
  1077. */
  1078. function adjacent_posts_rel_link_wp_head() {
  1079. if ( !is_singular() || is_attachment() )
  1080. return;
  1081. adjacent_posts_rel_link();
  1082. }
  1083. /**
  1084. * Display relational link for the next post adjacent to the current post.
  1085. *
  1086. * @since 2.8.0
  1087. *
  1088. * @param string $title Optional. Link title format.
  1089. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1090. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1091. */
  1092. function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1093. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
  1094. }
  1095. /**
  1096. * Display relational link for the previous post adjacent to the current post.
  1097. *
  1098. * @since 2.8.0
  1099. *
  1100. * @param string $title Optional. Link title format.
  1101. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1102. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1103. */
  1104. function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
  1105. echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
  1106. }
  1107. /**
  1108. * Retrieve boundary post.
  1109. *
  1110. * Boundary being either the first or last post by publish date within the constraints specified
  1111. * by $in_same_cat or $excluded_categories.
  1112. *
  1113. * @since 2.8.0
  1114. *
  1115. * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
  1116. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1117. * @param bool $start Optional. Whether to retrieve first or last post.
  1118. * @return object
  1119. */
  1120. function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
  1121. $post = get_post();
  1122. if ( ! $post || ! is_single() || is_attachment() )
  1123. return null;
  1124. $cat_array = array();
  1125. if( ! is_array( $excluded_categories ) )
  1126. $excluded_categories = explode( ',', $excluded_categories );
  1127. if ( $in_same_cat || ! empty( $excluded_categories ) ) {
  1128. if ( $in_same_cat )
  1129. $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
  1130. if ( ! empty( $excluded_categories ) ) {
  1131. $excluded_categories = array_map( 'intval', $excluded_categories );
  1132. $excluded_categories = array_diff( $excluded_categories, $cat_array );
  1133. $inverse_cats = array();
  1134. foreach ( $excluded_categories as $excluded_category )
  1135. $inverse_cats[] = $excluded_category * -1;
  1136. $excluded_categories = $inverse_cats;
  1137. }
  1138. }
  1139. $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
  1140. $order = $start ? 'ASC' : 'DESC';
  1141. return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
  1142. }
  1143. /*
  1144. * Get previous post link that is adjacent to the current post.
  1145. *
  1146. * @since 3.7.0
  1147. *
  1148. * @param string $format Optional. Link anchor format.
  1149. * @param string $link Optional. Link permalink format.
  1150. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1151. * @param string $excluded_categories Optional. Excluded categories IDs.
  1152. * @return string
  1153. */
  1154. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
  1155. return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true );
  1156. }
  1157. /**
  1158. * Display previous post link that is adjacent to the current post.
  1159. *
  1160. * @since 1.5.0
  1161. * @uses get_previous_post_link()
  1162. *
  1163. * @param string $format Optional. Link anchor format.
  1164. * @param string $link Optional. Link permalink format.
  1165. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1166. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1167. */
  1168. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
  1169. echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_categories );
  1170. }
  1171. /**
  1172. * Get previous post link that is adjacent to the current post.
  1173. *
  1174. * @since 3.7.0
  1175. * @uses get_next_post_link()
  1176. *
  1177. * @param string $format Optional. Link anchor format.
  1178. * @param string $link Optional. Link permalink format.
  1179. * @param bool $in_same_cat Optional. Whether link should be in same category.
  1180. * @param string $excluded_categories Optional. Excluded categories IDs.
  1181. * @return string
  1182. */
  1183. function get_next_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
  1184. return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false );
  1185. }
  1186. /**
  1187. * Display next post link that is adjacent to the current post.
  1188. *
  1189. * @since 1.5.0
  1190. *
  1191. * @param string $format Optional. Link anchor format.
  1192. * @param string $link Optional. Link permalink format.
  1193. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1194. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1195. */
  1196. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
  1197. echo get_next_post_link( $format, $link, $in_same_cat, $excluded_categories );
  1198. }
  1199. /**
  1200. * Get adjacent post link.
  1201. *
  1202. * Can be either next post link or previous.
  1203. *
  1204. * @since 3.7.0
  1205. *
  1206. * @param string $format Link anchor format.
  1207. * @param string $link Link permalink format.
  1208. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1209. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1210. * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
  1211. * @return string
  1212. */
  1213. function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
  1214. if ( $previous && is_attachment() )
  1215. $post = get_post( get_post()->post_parent );
  1216. else
  1217. $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
  1218. if ( ! $post ) {
  1219. $output = '';
  1220. } else {
  1221. $title = $post->post_title;
  1222. if ( empty( $post->post_title ) )
  1223. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1224. /** This filter is documented in wp-includes/post-template.php */
  1225. $title = apply_filters( 'the_title', $title, $post->ID );
  1226. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1227. $rel = $previous ? 'prev' : 'next';
  1228. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
  1229. $inlink = str_replace( '%title', $title, $link );
  1230. $inlink = str_replace( '%date', $date, $inlink );
  1231. $inlink = $string . $inlink . '</a>';
  1232. $output = str_replace( '%link', $inlink, $format );
  1233. }
  1234. $adjacent = $previous ? 'previous' : 'next';
  1235. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
  1236. }
  1237. /**
  1238. * Display adjacent post link.
  1239. *
  1240. * Can be either next post link or previous.
  1241. *
  1242. * @since 2.5.0
  1243. * @uses get_adjacent_post_link()
  1244. *
  1245. * @param string $format Link anchor format.
  1246. * @param string $link Link permalink format.
  1247. * @param bool $in_same_cat Optional. Whether link should be in a same category.
  1248. * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  1249. * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
  1250. * @return string
  1251. */
  1252. function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
  1253. echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, $previous );
  1254. }
  1255. /**
  1256. * Retrieve links for page numbers.
  1257. *
  1258. * @since 1.5.0
  1259. *
  1260. * @param int $pagenum Optional. Page ID.
  1261. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1262. * Otherwise, prepares the URL with esc_url_raw().
  1263. * @return string
  1264. */
  1265. function get_pagenum_link($pagenum = 1, $escape = true ) {
  1266. global $wp_rewrite;
  1267. $pagenum = (int) $pagenum;
  1268. $request = remove_query_arg( 'paged' );
  1269. $home_root = parse_url(home_url());
  1270. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1271. $home_root = preg_quote( $home_root, '|' );
  1272. $request = preg_replace('|^'. $home_root . '|i', '', $request);
  1273. $request = preg_replace('|^/+|', '', $request);
  1274. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1275. $base = trailingslashit( get_bloginfo( 'url' ) );
  1276. if ( $pagenum > 1 ) {
  1277. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1278. } else {
  1279. $result = $base . $request;
  1280. }
  1281. } else {
  1282. $qs_regex = '|\?.*?$|';
  1283. preg_match( $qs_regex, $request, $qs_match );
  1284. if ( !empty( $qs_match[0] ) ) {
  1285. $query_string = $qs_match[0];
  1286. $request = preg_replace( $qs_regex, '', $request );
  1287. } else {
  1288. $query_string = '';
  1289. }
  1290. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1291. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
  1292. $request = ltrim($request, '/');
  1293. $base = trailingslashit( get_bloginfo( 'url' ) );
  1294. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1295. $base .= $wp_rewrite->index . '/';
  1296. if ( $pagenum > 1 ) {
  1297. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1298. }
  1299. $result = $base . $request . $query_string;
  1300. }
  1301. $result = apply_filters('get_pagenum_link', $result);
  1302. if ( $escape )
  1303. return esc_url( $result );
  1304. else
  1305. return esc_url_raw( $result );
  1306. }
  1307. /**
  1308. * Retrieve next posts page link.
  1309. *
  1310. * Backported from 2.1.3 to 2.0.10.
  1311. *
  1312. * @since 2.0.10
  1313. *
  1314. * @param int $max_page Optional. Max pages.
  1315. * @return string
  1316. */
  1317. function get_next_posts_page_link($max_page = 0) {
  1318. global $paged;
  1319. if ( !is_single() ) {
  1320. if ( !$paged )
  1321. $paged = 1;
  1322. $nextpage = intval($paged) + 1;
  1323. if ( !$max_page || $max_page >= $nextpage )
  1324. return get_pagenum_link($nextpage);
  1325. }
  1326. }
  1327. /**
  1328. * Display or return the next posts page link.
  1329. *
  1330. * @since 0.71
  1331. *
  1332. * @param int $max_page Optional. Max pages.
  1333. * @param boolean $echo Optional. Echo or return;
  1334. */
  1335. function next_posts( $max_page = 0, $echo = true ) {
  1336. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1337. if ( $echo )
  1338. echo $output;
  1339. else
  1340. return $output;
  1341. }
  1342. /**
  1343. * Return the next posts page link.
  1344. *
  1345. * @since 2.7.0
  1346. *
  1347. * @param string $label Content for link text.
  1348. * @param int $max_page Optional. Max pages.
  1349. * @return string|null
  1350. */
  1351. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1352. global $paged, $wp_query;
  1353. if ( !$max_page )
  1354. $max_page = $wp_query->max_num_pages;
  1355. if ( !$paged )
  1356. $paged = 1;
  1357. $nextpage = intval($paged) + 1;
  1358. if ( null === $label )
  1359. $label = __( 'Next Page &raquo;' );
  1360. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1361. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1362. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1363. }
  1364. }
  1365. /**
  1366. * Display the next posts page link.
  1367. *
  1368. * @since 0.71
  1369. * @uses get_next_posts_link()
  1370. *
  1371. * @param string $label Content for link text.
  1372. * @param int $max_page Optional. Max pages.
  1373. */
  1374. function next_posts_link( $label = null, $max_page = 0 ) {
  1375. echo get_next_posts_link( $label, $max_page );
  1376. }
  1377. /**
  1378. * Retrieve previous posts page link.
  1379. *
  1380. * Will only return string, if not on a single page or post.
  1381. *
  1382. * Backported to 2.0.10 from 2.1.3.
  1383. *
  1384. * @since 2.0.10
  1385. *
  1386. * @return string|null
  1387. */
  1388. function get_previous_posts_page_link() {
  1389. global $paged;
  1390. if ( !is_single() ) {
  1391. $nextpage = intval($paged) - 1;
  1392. if ( $nextpage < 1 )
  1393. $nextpage = 1;
  1394. return get_pagenum_link($nextpage);
  1395. }
  1396. }
  1397. /**
  1398. * Display or return the previous posts page link.
  1399. *
  1400. * @since 0.71
  1401. *
  1402. * @param boolean $echo Optional. Echo or return…

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