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