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

Revision 3627, 14.8 kB (checked in by ryan, 3 years ago)

Security back ports from masquerade and MarkJaquith?.

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