root/tags/1.5.1/wp-includes/link-template.php

Revision 1260, 21.1 kB (checked in by donncha, 9 months ago)

Merged with WP 2.5, revision 7806

Line 
1 <?php
2
3
4 function the_permalink() {
5     echo apply_filters('the_permalink', get_permalink());
6 }
7
8
9 /**
10  * Conditionally adds a trailing slash if the permalink structure
11  * has a trailing slash, strips the trailing slash if not
12  * @global object Uses $wp_rewrite
13  * @param $string string a URL with or without a trailing slash
14  * @param $type_of_url string the type of URL being considered (e.g. single, category, etc) for use in the filter
15  * @return string
16  */
17 function user_trailingslashit($string, $type_of_url = '') {
18     global $wp_rewrite;
19     if ( $wp_rewrite->use_trailing_slashes )
20         $string = trailingslashit($string);
21     else
22         $string = untrailingslashit($string);
23
24     // Note that $type_of_url can be one of following:
25     // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
26     $string = apply_filters('user_trailingslashit', $string, $type_of_url);
27     return $string;
28 }
29
30
31 function permalink_anchor($mode = 'id') {
32     global $post;
33     switch ( strtolower($mode) ) {
34         case 'title':
35             $title = sanitize_title($post->post_title) . '-' . $post->ID;
36             echo '<a id="'.$title.'"></a>';
37             break;
38         case 'id':
39         default:
40             echo '<a id="post-' . $post->ID . '"></a>';
41             break;
42     }
43 }
44
45
46 function get_permalink($id = 0, $leavename=false) {
47     $rewritecode = array(
48         '%year%',
49         '%monthnum%',
50         '%day%',
51         '%hour%',
52         '%minute%',
53         '%second%',
54         $leavename? '' : '%postname%',
55         '%post_id%',
56         '%category%',
57         '%author%',
58         $leavename? '' : '%pagename%',
59     );
60
61     $post = &get_post($id);
62
63     if ( empty($post->ID) ) return FALSE;
64
65     if ( $post->post_type == 'page' )
66         return get_page_link($post->ID, $leavename);
67     elseif ($post->post_type == 'attachment')
68         return get_attachment_link($post->ID);
69
70     $permalink = get_option('permalink_structure');
71
72     if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
73         $unixtime = strtotime($post->post_date);
74
75         $category = '';
76         if ( strpos($permalink, '%category%') !== false ) {
77             $cats = get_the_category($post->ID);
78             if ( $cats )
79                 usort($cats, '_usort_terms_by_ID'); // order by ID
80             $category = $cats[0]->slug;
81             if ( $parent=$cats[0]->parent )
82                 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
83
84             // show default category in permalinks, without
85             // having to assign it explicitly
86             if ( empty($category) ) {
87                 $default_category = get_category( get_option( 'default_category' ) );
88                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
89             }
90         }
91
92         $author = '';
93         if ( strpos($permalink, '%author%') !== false ) {
94             $authordata = get_userdata($post->post_author);
95             $author = $authordata->user_nicename;
96         }
97
98         $date = explode(" ",date('Y m d H i s', $unixtime));
99         $rewritereplace =
100         array(
101             $date[0],
102             $date[1],
103             $date[2],
104             $date[3],
105             $date[4],
106             $date[5],
107             $post->post_name,
108             $post->ID,
109             $category,
110             $author,
111             $post->post_name,
112         );
113         $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
114         $permalink = user_trailingslashit($permalink, 'single');
115         return apply_filters('post_link', $permalink, $post);
116     } else { // if they're not using the fancy permalink option
117         $permalink = get_option('home') . '/?p=' . $post->ID;
118         return apply_filters('post_link', $permalink, $post);
119     }
120 }
121
122 // get permalink from post ID
123 function post_permalink($post_id = 0, $deprecated = '') {
124     return get_permalink($post_id);
125 }
126
127 // Respects page_on_front.  Use this one.
128 function get_page_link($id = false, $leavename = false) {
129     global $post;
130
131     $id = (int) $id;
132     if ( !$id )
133         $id = (int) $post->ID;
134
135     if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
136         $link = get_option('home');
137     else
138         $link = _get_page_link( $id , $leavename );
139
140     return apply_filters('page_link', $link, $id);
141 }
142
143 // Ignores page_on_front.  Internal use only.
144 function _get_page_link( $id = false, $leavename = false ) {
145     global $post, $wp_rewrite;
146
147     if ( !$id )
148         $id = (int) $post->ID;
149     else
150         $post = &get_post($id);
151
152     $pagestruct = $wp_rewrite->get_page_permastruct();
153
154     if ( '' != $pagestruct && isset($post->post_status) && 'draft' != $post->post_status ) {
155         $link = get_page_uri($id);
156         $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
157         $link = get_option('home') . "/$link";
158         $link = user_trailingslashit($link, 'page');
159     } else {
160         $link = get_option('home') . "/?page_id=$id";
161     }
162
163     return apply_filters( '_get_page_link', $link, $id );
164 }
165
166 function get_attachment_link($id = false) {
167     global $post, $wp_rewrite;
168
169     $link = false;
170
171     if (! $id) {
172         $id = (int) $post->ID;
173     }
174
175     $object = get_post($id);
176     if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
177         $parent = get_post($object->post_parent);
178         if ( 'page' == $parent->post_type )
179             $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
180         else
181             $parentlink = get_permalink( $object->post_parent );
182         if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
183             $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
184         else
185             $name = $object->post_name;
186         if (strpos($parentlink, '?') === false)
187             $link = trailingslashit($parentlink) . $name . '/';
188     }
189
190     if (! $link ) {
191         $link = get_bloginfo('url') . "/?attachment_id=$id";
192     }
193
194     return apply_filters('attachment_link', $link, $id);
195 }
196
197 function get_year_link($year) {
198     global $wp_rewrite;
199     if ( !$year )
200         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
201     $yearlink = $wp_rewrite->get_year_permastruct();
202     if ( !empty($yearlink) ) {
203         $yearlink = str_replace('%year%', $year, $yearlink);
204         return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
205     } else {
206         return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
207     }
208 }
209
210 function get_month_link($year, $month) {
211     global $wp_rewrite;
212     if ( !$year )
213         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
214     if ( !$month )
215         $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
216     $monthlink = $wp_rewrite->get_month_permastruct();
217     if ( !empty($monthlink) ) {
218         $monthlink = str_replace('%year%', $year, $monthlink);
219         $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
220         return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
221     } else {
222         return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
223     }
224 }
225
226 function get_day_link($year, $month, $day) {
227     global $wp_rewrite;
228     if ( !$year )
229         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
230     if ( !$month )
231         $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
232     if ( !$day )
233         $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
234
235     $daylink = $wp_rewrite->get_day_permastruct();
236     if ( !empty($daylink) ) {
237         $daylink = str_replace('%year%', $year, $daylink);
238         $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
239         $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
240         return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
241     } else {
242         return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
243     }
244 }
245
246 function get_feed_link($feed = '') {
247     global $wp_rewrite;
248
249     $permalink = $wp_rewrite->get_feed_permastruct();
250     if ( '' != $permalink ) {
251         if ( false !== strpos($feed, 'comments_') ) {
252             $feed = str_replace('comments_', '', $feed);
253             $permalink = $wp_rewrite->get_comment_feed_permastruct();
254         }
255
256         if ( get_default_feed() == $feed )
257             $feed = '';
258
259         $permalink = str_replace('%feed%', $feed, $permalink);
260         $permalink = preg_replace('#/+#', '/', "/$permalink");
261         $output get_option('home') . user_trailingslashit($permalink, 'feed');
262     } else {
263         if ( empty($feed) )
264             $feed = get_default_feed();
265
266         if ( false !== strpos($feed, 'comments_') )
267             $feed = str_replace('comments_', 'comments-', $feed);
268
269         $output = get_option('home') . "/?feed={$feed}";
270     }
271
272     return apply_filters('feed_link', $output, $feed);
273 }
274
275 function get_post_comments_feed_link($post_id = '', $feed = '') {
276     global $id;
277
278     if ( empty($post_id) )
279         $post_id = (int) $id;
280
281     if ( empty($feed) )
282         $feed = get_default_feed();
283
284     if ( '' != get_option('permalink_structure') ) {
285         $url = trailingslashit( get_permalink($post_id) ) . 'feed';
286         if ( $feed != get_default_feed() )
287             $url .= "/$feed";
288         $url = user_trailingslashit($url, 'single_feed');
289     } else {
290         $type = get_post_field('post_type', $post_id);
291         if ( 'page' == $type )
292             $url = get_option('home') . "/?feed=$feed&amp;page_id=$post_id";
293         else
294             $url = get_option('home') . "/?feed=$feed&amp;p=$post_id";
295     }
296
297     return apply_filters('post_comments_feed_link', $url);
298 }
299
300 /** post_comments_feed_link() - Output the comment feed link for a post.
301  *
302  * Prints out the comment feed link for a post.  Link text is placed in the
303  * anchor.  If no link text is specified, default text is used.  If no post ID
304  * is specified, the current post is used.
305  *
306  * @package WordPress
307  * @subpackage Feed
308  * @since 2.5
309  *
310  * @param string Descriptive text
311  * @param int Optional post ID.  Default to current post.
312  * @return string Link to the comment feed for the current post
313 */
314 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
315     $url = get_post_comments_feed_link($post_id, $feed);
316     if ( empty($link_text) )
317         $link_text = __('Comments Feed');
318
319     echo "<a href='$url'>$link_text</a>";
320 }
321
322 function get_author_feed_link( $author_id, $feed = '' ) {
323     $author_id = (int) $author_id;
324     $permalink_structure = get_option('permalink_structure');
325
326     if ( empty($feed) )
327         $feed = get_default_feed();
328
329     if ( '' == $permalink_structure ) {
330         $link = get_option('home') . '?feed=rss2&amp;author=' . $author_id;
331     } else {
332         $link = get_author_posts_url($author_id);
333         $link = trailingslashit($link) . user_trailingslashit('feed', 'feed');
334     }
335
336     $link = apply_filters('author_feed_link', $link);
337
338     return $link;
339 }
340
341 /** get_category_feed_link() - Get the feed link for a given category
342  *
343  * Returns a link to the feed for all post in a given category.  A specific feed can be requested
344  * or left blank to get the default feed.
345  *
346  * @package WordPress
347  * @subpackage Feed
348  * @since 2.5
349  *
350  * @param int $cat_id ID of a category
351  * @param string $feed Feed type
352  * @return string Link to the feed for the category specified by $cat_id
353 */
354 function get_category_feed_link($cat_id, $feed = '') {
355     $cat_id = (int) $cat_id;
356
357     $category = get_category($cat_id);
358
359     if ( empty($category) || is_wp_error($category) )
360         return false;
361
362     if ( empty($feed) )
363         $feed = get_default_feed();
364
365     $permalink_structure = get_option('permalink_structure');
366
367     if ( '' == $permalink_structure ) {
368         $link = get_option('home') . "?feed=$feed&amp;cat=" . $cat_id;
369     } else {
370         $link = get_category_link($cat_id);
371         if( $feed == get_default_feed() )
372             $feed_link = 'feed';
373         else
374             $feed_link = "feed/$feed";
375
376         $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
377     }
378
379     $link = apply_filters('category_feed_link', $link, $feed);
380
381     return $link;
382 }
383
384 function get_tag_feed_link($tag_id, $feed = '') {
385     $tag_id = (int) $tag_id;
386
387     $tag = get_tag($tag_id);
388
389     if ( empty($tag) || is_wp_error($tag) )
390         return false;
391
392     $permalink_structure = get_option('permalink_structure');
393
394     if ( empty($feed) )
395         $feed = get_default_feed();
396
397     if ( '' == $permalink_structure ) {
398         $link = get_option('home') . "?feed=$feed&amp;tag=" . $tag->slug;
399     } else {
400         $link = get_tag_link($tag->term_id);
401         if ( $feed == get_default_feed() )
402             $feed_link = 'feed';
403         else
404             $feed_link = "feed/$feed";
405         $link = $link . user_trailingslashit($feed_link, 'feed');
406     }
407
408     $link = apply_filters('tag_feed_link', $link, $feed);
409
410     return $link;
411 }
412
413 function get_search_feed_link($search_query = '', $feed = '') {
414     if ( empty($search_query) )
415         $search = attribute_escape(get_search_query());
416     else
417         $search = attribute_escape(stripslashes($search_query));
418
419     if ( empty($feed) )
420         $feed = get_default_feed();
421
422     $link = get_option('home') . "?s=$search&amp;feed=$feed";
423
424     $link = apply_filters('search_feed_link', $link);
425
426     return $link;
427 }
428
429 function get_search_comments_feed_link($search_query = '', $feed = '') {
430     if ( empty($search_query) )
431         $search = attribute_escape(get_search_query());
432     else
433         $search = attribute_escape(stripslashes($search_query));
434
435     if ( empty($feed) )
436         $feed = get_default_feed();
437
438     $link = get_option('home') . "?s=$search&amp;feed=comments-$feed";
439
440     $link = apply_filters('search_feed_link', $link);
441
442     return $link;
443 }
444
445 function get_edit_post_link( $id = 0 ) {
446     if ( !$post = &get_post( $id ) )
447         return;
448
449     switch ( $post->post_type ) :
450     case 'page' :
451         if ( !current_user_can( 'edit_page', $post->ID ) )
452             return;
453         $file = 'page';
454         $var  = 'post';
455         break;
456     case 'attachment' :
457         if ( !current_user_can( 'edit_post', $post->ID ) )
458             return;
459         $file = 'media';
460         $var  = 'attachment_id';
461         break;
462     default :
463         if ( !current_user_can( 'edit_post', $post->ID ) )
464             return;
465         $file = 'post';
466         $var  = 'post';
467         break;
468     endswitch;
469     
470     return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . "/wp-admin/$file.php?action=edit&amp;$var=$post->ID", $post->ID );
471 }
472
473 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
474     global $post;
475
476     if ( $post->post_type == 'page' ) {
477         if ( !current_user_can( 'edit_page', $post->ID ) )
478             return;
479     } else {
480         if ( !current_user_can( 'edit_post', $post->ID ) )
481             return;
482     }