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

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

Prev/next query filters. Props mdwaffe. fixes #2415

  • 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_type == 'page' )
46         return get_page_link($post->ID);
47     elseif ($post->post_type == 'attachment')
48         return get_attachment_link($post->ID);
49
50     $permalink = get_option('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_option('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
81     } else { // if they're not using the fancy permalink option
82         $permalink = get_option('home') . '/?p=' . $post->ID;
83         return apply_filters('post_link', $permalink, $post);
84     }
85 }
86
87 // get permalink from post ID
88 function post_permalink($post_id = 0, $mode = '') { // $mode legacy
89     return get_permalink($post_id);
90 }
91
92 // Respects page_on_front.  Use this one.
93 function get_page_link($id = false) {
94     global $post;
95
96     if ( !$id )
97         $id = $post->ID;
98
99     if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
100         $link = get_option('home');
101     else
102         $link = _get_page_link( $id );
103
104     return apply_filters('page_link', $link, $id);
105 }
106
107 // Ignores page_on_front.  Internal use only.
108 function _get_page_link( $id = false ) {
109     global $post, $wp_rewrite;
110
111     if ( !$id )
112         $id = $post->ID;
113
114     $pagestruct = $wp_rewrite->get_page_permastruct();
115
116     if ( '' != $pagestruct && 'draft' != $post->post_status ) {
117         $link = get_page_uri($id);
118         $link = str_replace('%pagename%', $link, $pagestruct);
119         $link = get_option('home') . "/$link/";
120     } else {
121         $link = get_option('home') . "/?page_id=$id";
122     }
123
124     return apply_filters( '_get_page_link', $link, $id );
125 }
126
127 function get_attachment_link($id = false) {
128     global $post, $wp_rewrite;
129
130     $link = false;
131
132     if (! $id) {
133         $id = $post->ID;
134     }
135
136     $object = get_post($id);
137     if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
138         $parent = get_post($object->post_parent);
139         if ( 'page' == $parent->post_type )
140             $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
141         else
142             $parentlink = get_permalink( $object->post_parent );
143         if (! strstr($parentlink, '?') )
144             $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
145     }
146
147     if (! $link ) {
148         $link = get_bloginfo('home') . "/?attachment_id=$id";
149     }
150
151     return apply_filters('attachment_link', $link, $id);
152 }
153
154 function get_year_link($year) {
155     global $wp_rewrite;
156     if ( !$year )
157         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
158     $yearlink = $wp_rewrite->get_year_permastruct();
159     if ( !empty($yearlink) ) {
160         $yearlink = str_replace('%year%', $year, $yearlink);
161         return apply_filters('year_link', get_option('home') . trailingslashit($yearlink), $year);
162     } else {
163         return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
164     }
165 }
166
167 function get_month_link($year, $month) {
168     global $wp_rewrite;
169     if ( !$year )
170         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
171     if ( !$month )
172         $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
173     $monthlink = $wp_rewrite->get_month_permastruct();
174     if ( !empty($monthlink) ) {
175         $monthlink = str_replace('%year%', $year, $monthlink);
176         $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
177         return apply_filters('month_link', get_option('home') . trailingslashit($monthlink), $year, $month);
178     } else {
179         return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
180     }
181 }
182
183 function get_day_link($year, $month, $day) {
184     global $wp_rewrite;
185     if ( !$year )
186         $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
187     if ( !$month )
188         $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
189     if ( !$day )
190         $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
191
192     $daylink = $wp_rewrite->get_day_permastruct();
193     if ( !empty($daylink) ) {
194         $daylink = str_replace('%year%', $year, $daylink);
195         $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
196         $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
197         return apply_filters('day_link', get_option('home') . trailingslashit($daylink), $year, $month, $day);
198     } else {
199         return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
200     }
201 }
202
203 function get_feed_link($feed='rss2') {
204     global $wp_rewrite;
205     $do_perma = 0;
206     $feed_url = get_option('siteurl');
207     $comment_feed_url = $feed_url;
208
209     $permalink = $wp_rewrite->get_feed_permastruct();
210     if ( '' != $permalink ) {
211         if ( false !== strpos($feed, 'comments_') ) {
212             $feed = str_replace('comments_', '', $feed);
213             $permalink = $wp_rewrite->get_comment_feed_permastruct();
214         }
215
216         if ( 'rss2' == $feed )
217             $feed = '';
218
219         $permalink = str_replace('%feed%', $feed, $permalink);
220         $permalink = preg_replace('#/+#', '/', "/$permalink/");
221         $output get_option('home') . $permalink;
222     } else {
223         if ( false !== strpos($feed, 'comments_') )
224             $feed = str_replace('comments_', 'comments-', $feed);
225
226         $output = get_option('home') . "/?feed={$feed}";
227     }
228
229     return apply_filters('feed_link', $output, $feed);
230 }
231
232 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
233     global $post;
234
235     if ( is_attachment() )
236         return;
237
238     if( $post->post_type == 'page' ) {
239         if ( ! current_user_can('edit_page', $post->ID) )
240             return;
241         $file = 'page';
242     } else {
243         if ( ! current_user_can('edit_post', $post->ID) )
244             return;
245         $file = 'post';
246     }
247
248     $location = get_option('siteurl') . "/wp-admin/{$file}.php?action=edit&amp;post=$post->ID";
249     echo $before . "<a href=\"$location\">$link</a>" . $after;
250 }
251
252 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
253     global $post, $comment;
254
255     if( $post->post_type == 'page' ){
256         if ( ! current_user_can('edit_page', $post->ID) )
257             return;
258     } else {
259         if ( ! current_user_can('edit_post', $post->ID) )
260             return;
261     }
262
263     $location = get_option('siteurl') . "/wp-admin/comment.php?action=editcomment&amp;c=$comment->comment_ID";
264     echo $before . "<a href='$location'>$link</a>" . $after;
265 }
266
267 // Navigation links
268
269 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
270     global $post, $wpdb;
271
272     if( !is_single() || is_attachment() )
273         return null;
274
275     $current_post_date = $post->post_date;
276
277     $join = '';
278     if ( $in_same_cat ) {
279         $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
280         $cat_array = get_the_category($post->ID);
281         $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
282         for ( $i = 1; $i < (count($cat_array)); $i++ ) {
283             $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
284         }
285         $join .= ')';
286     }
287
288     $sql_exclude_cats = '';
289     if ( !empty($excluded_categories) ) {
290         $blah = explode(' and ', $excluded_categories);
291         foreach ( $blah as $category ) {
292             $category = intval($category);
293             $sql_cat_ids = " OR pc.category_ID = '$category'";
294         }
295         $posts_in_ex_cats = $wpdb->get_col("SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id=p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID");
296         $posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
297     }
298
299     $join  = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories );
300     $where = apply_filters( 'get_previous_post_where', "WHERE post_date < '$current_post_date' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql", $in_same_cat, $excluded_categories );
301     $sort  = apply_filters( 'get_previous_post_sort', 'ORDER BY post_date DESC LIMIT 1' );
302
303     return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join $where $sort");
304 }
305
306 function get_next_post($in_same_cat = false, $excluded_categories = '') {
307     global $post, $wpdb;
308
309     if( !is_single() || is_attachment() )
310         return null;
311
312     $current_post_date = $post->post_date;
313
314     $join = '';
315     if ( $in_same_cat ) {
316         $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
317         $cat_array = get_the_category($post->ID);
318         $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
319         for ( $i = 1; $i < (count($cat_array)); $i++ ) {
320             $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
321         }
322         $join .= ')';
323     }
324
325     $sql_exclude_cats = '';
326     if ( !empty($excluded_categories) ) {
327         $blah = explode(' and ', $excluded_categories);
328         foreach ( $blah as $category ) {
329             $category = intval($category);
330             $sql_cat_ids = " OR pc.category_ID = '$category'";
331         }
332         $posts_in_ex_cats = $wpdb->get_col("SELECT p.ID from $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id = p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID");
333         $posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
334     }
335
336     $join  = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories );
337     $where = apply_filters( 'get_next_post_where', "WHERE post_date > '$current_post_date' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql AND ID != $post->ID", $in_same_cat, $excluded_categories );
338     $sort  = apply_filters( 'get_next_post_sort', 'ORDER BY post_date ASC LIMIT 1' );
339
340     return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join $where $sort");
341 }
342
343
344 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
345
346     if ( is_attachment() )
347         $post = & get_post($GLOBALS['post']->post_parent);
348     else
349         $post = get_previous_post($in_same_cat, $excluded_categories);
350
351     if ( !$post )
352         return;
353
354     $title = apply_filters('the_title', $post->post_title, $post);
355     $string = '<a href="'.get_permalink($post->ID).'">';
356     $link = str_replace('%title', $title, $link);
357     $link = $pre . $string . $link . '</a>';
358
359     $format = str_replace('%link', $link, $format);
360
361     echo $format;
362 }
363
364 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
365     $post = get_next_post($in_same_cat, $excluded_categories);
366
367     if ( !$post )
368         return;
369
370     $title = apply_filters('the_title', $post->post_title, $post);
371     $string = '<a href="'.get_permalink($post->ID).'">';
372     $link = str_replace('%title', $title, $link);
373     $link = $string . $link . '</a>';
374     $format = str_replace('%link', $link, $format);
375
376     echo $format;
377 }
378
379 function get_pagenum_link($pagenum = 1) {
380     global $wp_rewrite;
381
382     $qstr = wp_specialchars($_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_option('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_option('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 URL.
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(