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

Revision 3333, 15.4 kB (checked in by ryan, 3 years ago)

Edit link does not belong on attachments. fixes #2119

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