root/branches/2.0/wp-includes/template-functions-links.php

Revision 5100, 15.5 kB (checked in by ryan, 2 years ago)

More int casts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3
4 function the_permalink() {
5     echo apply_filters('the_permalink', get_permalink());
6 }
7
8
9 function permalink_link() { // For backwards compatibility
10     echo apply_filters('the_permalink', get_permalink());
11 }
12
13
14 function permalink_anchor($mode = 'id') {
15     global $post;
16     switch ( strtolower($mode) ) {
17         case 'title':
18             $title = sanitize_title($post->post_title) . '-' . $id;
19             echo '<a id="'.$title.'"></a>';
20             break;
21         case 'id':
22         default:
23             echo '<a id="post-' . $post->ID . '"></a>';
24             break;
25     }
26 }
27
28
29 function get_permalink($id = 0) {
30     $rewritecode = array(
31         '%year%',
32         '%monthnum%',
33         '%day%',
34         '%hour%',
35         '%minute%',
36         '%second%',
37         '%postname%',
38         '%post_id%',
39         '%category%',
40         '%author%',
41         '%pagename%'
42     );
43
44     $post = &get_post($id);
45     if ( $post->post_status == 'static' )
46         return get_page_link($post->ID);
47     elseif ($post->post_status == 'attachment')
48         return get_attachment_link($post->ID);
49
50     $permalink = get_settings('permalink_structure');
51
52     if ( '' != $permalink && 'draft' != $post->post_status ) {
53         $unixtime = strtotime($post->post_date);
54
55         $category = '';
56         if ( strstr($permalink, '%category%') ) {
57             $cats = get_the_category($post->ID);
58             $category = $cats[0]->category_nicename;
59             if ( $parent=$cats[0]->category_parent )
60                 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
61         }
62
63         $authordata = get_userdata($post->post_author);
64         $author = $authordata->user_nicename;
65         $date = explode(" ",date('Y m d H i s', $unixtime));
66         $rewritereplace =
67         array(
68             $date[0],
69             $date[1],
70             $date[2],
71             $date[3],
72             $date[4],
73             $date[5],
74             $post->post_name,
75             $post->ID,
76             $category,
77             $author,
78             $post->post_name,
79         );
80         return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
81     } else { // if they're not using the fancy permalink option
82         $permalink = get_settings('home') . '/?p=' . $post->ID;
83         return apply_filters('post_link', $permalink, $post);
84     }
85 }
86
87 function get_page_link($id = false) {
88     global $post, $wp_rewrite;
89
90     $id = (int) $id;
91     if ( !$id )
92         $id = (int) $post->ID;
93
94     $pagestruct = $wp_rewrite->get_page_permastruct();
95
96     if ( '' != $pagestruct ) {
97         $link = get_page_uri($id);
98         $link = str_replace('%pagename%', $link, $pagestruct);
99         $link = get_settings('home') . "/$link/";
100     } else {
101         $link = get_settings('home') . "/?page_id=$id";
102     }
103
104     return apply_filters('page_link', $link, $id);
105 }
106
107 function get_attachment_link($id = false) {
108     global $post, $wp_rewrite;
109
110     $link = false;
111
112     if (! $id) {
113         $id = (int) $post->ID;
114     }
115
116     $object = get_post($id);
117     if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) {
118         $parent = get_post($object->post_parent);
119         $parentlink = get_permalink($object->post_parent);
120         if (! strstr($parentlink, '?') )
121             $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
122     }
123
124     if (! $link ) {
125         $link = get_bloginfo('home') . "/?attachment_id=$id";
126     }
127
128     return apply_filters('attachment_link', $link, $id);
129 }
130
131 function get_year_link($year) {
132     global $wp_rewrite;
133     if ( !$year )
134         $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
135     $yearlink = $wp_rewrite->get_year_permastruct();
136     if ( !empty($yearlink) ) {
137         $yearlink = str_replace('%year%', $year, $yearlink);
138         return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year);
139     } else {
140         return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year);
141     }
142 }
143
144 function get_month_link($year, $month) {
145     global $wp_rewrite;
146     if ( !$year )
147         $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
148     if ( !$month )
149         $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
150     $monthlink = $wp_rewrite->get_month_permastruct();
151     if ( !empty($monthlink) ) {
152         $monthlink = str_replace('%year%', $year, $monthlink);
153         $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
154         return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month);
155     } else {
156         return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
157     }
158 }
159
160 function get_day_link($year, $month, $day) {
161     global $wp_rewrite;
162     if ( !$year )
163         $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
164     if ( !$month )
165         $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
166     if ( !$day )
167         $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
168
169     $daylink = $wp_rewrite->get_day_permastruct();
170     if ( !empty($daylink) ) {
171         $daylink = str_replace('%year%', $year, $daylink);
172         $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
173         $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
174         return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day);
175     } else {
176         return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
177     }
178 }
179
180 function get_feed_link($feed='rss2') {
181     global $wp_rewrite;
182     $do_perma = 0;
183     $feed_url = get_settings('siteurl');
184     $comment_feed_url = $feed_url;
185
186     $permalink = $wp_rewrite->get_feed_permastruct();
187     if ( '' != $permalink ) {
188         if ( false !== strpos($feed, 'comments_') ) {
189             $feed = str_replace('comments_', '', $feed);
190             $permalink = $wp_rewrite->get_comment_feed_permastruct();
191         }
192
193         if ( 'rss2' == $feed )
194             $feed = '';
195
196         $permalink = str_replace('%feed%', $feed, $permalink);
197         $permalink = preg_replace('#/+#', '/', "/$permalink/");
198         $output get_settings('home') . $permalink;
199     } else {
200         if ( false !== strpos($feed, 'comments_') )
201             $feed = str_replace('comments_', 'comments-', $feed);
202
203         $output = get_settings('home') . "/?feed={$feed}";
204     }
205
206     return apply_filters('feed_link', $output, $feed);
207 }
208
209 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
210     global $post;
211
212     if ( ! current_user_can('edit_post', $post->ID) )
213         return;
214
215     if ( is_attachment() )
216         return;
217     else
218         $file = 'post';
219
220     $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&amp;post=$post->ID";
221     echo $before . "<a href=\"$location\">$link</a>" . $after;
222 }
223
224 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
225     global $post, $comment;
226
227     if ( ! current_user_can('edit_post', $post->ID) )
228         return;
229
230     $location = get_settings('siteurl') . "/wp-admin/post.php?action=editcomment&amp;comment=$comment->comment_ID";
231     echo $before . "<a href='$location'>$link</a>" . $after;
232 }
233
234 // Navigation links
235
236 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
237     global $post, $wpdb;
238
239     if( !is_single() || is_attachment() )
240         return null;
241
242     $current_post_date = $post->post_date;
243
244     $join = '';
245     if ( $in_same_cat ) {
246         $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
247         $cat_array = get_the_category($post->ID);
248         $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
249         for ( $i = 1; $i < (count($cat_array)); $i++ ) {
250             $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
251         }
252         $join .= ')';
253     }
254
255     $sql_exclude_cats = '';
256     if ( !empty($excluded_categories) ) {
257         $blah = explode('and', $excluded_categories);
258         foreach ( $blah as $category ) {
259             $category = intval($category);
260             $sql_exclude_cats .= " AND post_category != $category";
261         }
262     }
263
264     return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
265 }
266
267 function get_next_post($in_same_cat = false, $excluded_categories = '') {
268     global $post, $wpdb;
269
270     if( !is_single() || is_attachment() )
271         return null;
272
273     $current_post_date = $post->post_date;
274     
275     $join = '';
276     if ( $in_same_cat ) {
277         $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
278         $cat_array = get_the_category($post->ID);
279         $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
280         for ( $i = 1; $i < (count($cat_array)); $i++ ) {
281             $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
282         }
283         $join .= ')';
284     }
285
286     $sql_exclude_cats = '';
287     if ( !empty($excluded_categories) ) {
288         $blah = explode('and', $excluded_categories);
289         foreach ( $blah as $category ) {
290             $category = intval($category);
291             $sql_exclude_cats .= " AND post_category != $category";
292         }
293     }
294
295     $now = current_time('mysql');
296     
297     return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
298 }
299
300
301 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
302
303     if ( is_attachment() )
304         $post = & get_post($GLOBALS['post']->post_parent);
305     else
306         $post = get_previous_post($in_same_cat, $excluded_categories);
307
308     if ( !$post )
309         return;
310
311     $title = apply_filters('the_title', $post->post_title, $post);
312     $string = '<a href="'.get_permalink($post->ID).'">';
313     $link = str_replace('%title', $title, $link);
314     $link = $pre . $string . $link . '</a>';
315
316     $format = str_replace('%link', $link, $format);
317
318     echo $format;       
319 }
320
321 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
322     $post = get_next_post($in_same_cat, $excluded_categories);
323
324     if ( !$post )
325         return;
326
327     $title = apply_filters('the_title', $post->post_title, $post);
328     $string = '<a href="'.get_permalink($post->ID).'">';
329     $link = str_replace('%title', $title, $link);
330     $link = $string . $link . '</a>';
331     $format = str_replace('%link', $link, $format);
332
333     echo $format;       
334 }
335
336
337 // Deprecated.    Use previous_post_link().
338 function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
339
340     if ( empty($in_same_cat) || 'no' == $in_same_cat )
341         $in_same_cat = false;
342     else
343         $in_same_cat = true;
344
345     $post = get_previous_post($in_same_cat, $excluded_categories);
346
347     if ( !$post )
348         return;
349
350     $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
351     if ( 'yes' == $title )
352         $string .= apply_filters('the_title', $post->post_title, $post);
353     $string .= '</a>';
354     $format = str_replace('%', $string, $format);
355     echo $format;
356 }
357
358 // Deprecated.    Use next_post_link().
359 function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
360
361     if ( empty($in_same_cat) || 'no' == $in_same_cat )
362         $in_same_cat = false;
363     else
364         $in_same_cat = true;
365
366     $post = get_next_post($in_same_cat, $excluded_categories);
367
368     if ( !$post    )
369         return;
370
371     $string = '<a href="'.get_permalink($post->ID).'">'.$next;
372     if ( 'yes' == $title )
373         $string .= apply_filters('the_title', $post->post_title, $nextpost);
374     $string .= '</a>';
375     $format = str_replace('%', $string, $format);
376     echo $format;
377 }
378
379 function get_pagenum_link($pagenum = 1) {
380     global $wp_rewrite;
381
382     $qstr = $_SERVER['REQUEST_URI'];
383
384     $page_querystring = "paged";
385     $page_modstring = "page/";
386     $page_modregex = "page/?";
387     $permalink = 0;
388
389     $home_root = parse_url(get_settings('home'));
390     $home_root = $home_root['path'];
391     $home_root = trailingslashit($home_root);
392     $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
393     $qstr = preg_replace('|^/+|', '', $qstr);
394
395     $index = $_SERVER['PHP_SELF'];
396     $index = preg_replace('|^'. $home_root . '|', '', $index);
397     $index = preg_replace('|^/+|', '', $index);
398
399     // if we already have a QUERY style page string
400     if ( stristr( $qstr, $page_querystring ) ) {
401         $replacement = "$page_querystring=$pagenum";
402         $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
403         // if we already have a mod_rewrite style page string
404     } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
405         $permalink = 1;
406         $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
407
408         // if we don't have a page string at all ...
409         // lets see what sort of URL we have...
410     } else {
411         // we need to know the way queries are being written
412         // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
413         if ( stristr( $qstr, '?' ) ) {
414             // so append the query string (using &, since we already have ?)
415             $qstr .=    '&amp;' . $page_querystring . '=' . $pagenum;
416             // otherwise, it could be rewritten, OR just the default index ...
417         } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) {
418             $permalink = 1;
419             $index = $wp_rewrite->index;
420             // If it's not a path info permalink structure, trim the index.
421             if ( !$wp_rewrite->using_index_permalinks() ) {
422                 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
423             } else {
424                 // If using path info style permalinks, make sure the index is in
425                 // the URI.
426                 if ( strpos($qstr, $index) === false )
427                     $qstr = '/' . $index . $qstr;
428             }
429
430             $qstr =    trailingslashit($qstr) . $page_modstring . $pagenum;
431         } else {
432             $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
433         }
434     }
435
436     $qstr = preg_replace('|^/+|', '', $qstr);
437     if ( $permalink )
438         $qstr = trailingslashit($qstr);
439     $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', trailingslashit( get_settings('home') ) . $qstr );
440     
441     // show