root/tags/2.0.4/wp-includes/classes.php

Revision 3958, 50.6 kB (checked in by ryan, 2 years ago)

Don't allow negative values when paging. fixes #2893

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 class WP_Query {
4     var $query;
5     var $query_vars;
6     var $queried_object;
7     var $queried_object_id;
8     var $request;
9
10     var $posts;
11     var $post_count = 0;
12     var $current_post = -1;
13     var $in_the_loop = false;
14     var $post;
15
16     var $is_single = false;
17     var $is_preview = false;
18     var $is_page = false;
19     var $is_archive = false;
20     var $is_date = false;
21     var $is_year = false;
22     var $is_month = false;
23     var $is_day = false;
24     var $is_time = false;
25     var $is_author = false;
26     var $is_category = false;
27     var $is_search = false;
28     var $is_feed = false;
29     var $is_trackback = false;
30     var $is_home = false;
31     var $is_404 = false;
32     var $is_comments_popup = false;
33     var $is_admin = false;
34     var $is_attachment = false;
35
36     function init_query_flags() {
37         $this->is_single = false;
38         $this->is_page = false;
39         $this->is_archive = false;
40         $this->is_date = false;
41         $this->is_year = false;
42         $this->is_month = false;
43         $this->is_day = false;
44         $this->is_time = false;
45         $this->is_author = false;
46         $this->is_category = false;
47         $this->is_search = false;
48         $this->is_feed = false;
49         $this->is_trackback = false;
50         $this->is_home = false;
51         $this->is_404 = false;
52         $this->is_paged = false;
53         $this->is_admin = false;
54         $this->is_attachment = false;
55     }
56     
57     function init () {
58         unset($this->posts);
59         unset($this->query);
60         unset($this->query_vars);
61         unset($this->queried_object);
62         unset($this->queried_object_id);
63         $this->post_count = 0;
64         $this->current_post = -1;
65         $this->in_the_loop = false;
66         
67         $this->init_query_flags();
68     }
69
70     // Reparse the query vars.
71     function parse_query_vars() {
72         $this->parse_query('');
73     }
74
75     // Parse a query string and set query type booleans.
76     function parse_query ($query) {
77         if ( !empty($query) || !isset($this->query) ) {
78             $this->init();
79             parse_str($query, $qv);
80             $this->query = $query;
81             $this->query_vars = $qv;
82         }
83
84         if ('404' == $qv['error']) {
85             $this->is_404 = true;
86             if ( !empty($query) ) {
87                 do_action('parse_query', array(&$this));
88             }
89             return;
90         }
91
92         $qv['m'] =  (int) $qv['m'];
93         $qv['p'] =  (int) $qv['p'];
94
95         // Compat.  Map subpost to attachment.
96         if ( '' != $qv['subpost'] )
97             $qv['attachment'] = $qv['subpost'];
98         if ( '' != $qv['subpost_id'] )
99             $qv['attachment_id'] = $qv['subpost_id'];
100             
101         if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) {
102             $this->is_single = true;
103             $this->is_attachment = true;
104         } elseif ('' != $qv['name']) {
105             $this->is_single = true;
106         } elseif ( $qv['p'] ) {
107             $this->is_single = true;
108         } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) {
109             // If year, month, day, hour, minute, and second are set, a single
110             // post is being queried.       
111             $this->is_single = true;
112         } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) {
113             $this->is_page = true;
114             $this->is_single = false;
115         } elseif (!empty($qv['s'])) {
116             $this->is_search = true;
117             switch ($qv['show_post_type']) {
118             case 'page' :
119                 $this->is_page = true;
120                 break;
121             case 'attachment' :
122                 $this->is_attachment = true;
123                 break;
124             }
125         } else {
126         // Look for archive queries.  Dates, categories, authors.
127
128             if ( (int) $qv['second']) {
129                 $this->is_time = true;
130                 $this->is_date = true;
131             }
132
133             if ( (int) $qv['minute']) {
134                 $this->is_time = true;
135                 $this->is_date = true;
136             }
137
138             if ( (int) $qv['hour']) {
139                 $this->is_time = true;
140                 $this->is_date = true;
141             }
142
143             if ( (int) $qv['day']) {
144                 if (! $this->is_date) {
145                     $this->is_day = true;
146                     $this->is_date = true;
147                 }
148             }
149
150             if ( (int)  $qv['monthnum']) {
151                 if (! $this->is_date) {
152                     $this->is_month = true;
153                     $this->is_date = true;
154                 }
155             }
156
157             if ( (int)  $qv['year']) {
158                 if (! $this->is_date) {
159                     $this->is_year = true;
160                     $this->is_date = true;
161                 }
162             }
163
164             if ( (int)  $qv['m']) {
165                 $this->is_date = true;
166                 if (strlen($qv['m']) > 9) {
167                     $this->is_time = true;
168                 } else if (strlen($qv['m']) > 7) {
169                     $this->is_day = true;
170                 } else if (strlen($qv['m']) > 5) {
171                     $this->is_month = true;
172                 } else {
173                     $this->is_year = true;
174                 }
175             }
176
177             if ('' != $qv['w']) {
178                 $this->is_date = true;
179             }
180
181             if (empty($qv['cat']) || ($qv['cat'] == '0')) {
182                 $this->is_category = false;
183             } else {
184                 if (stristr($qv['cat'],'-')) {
185                     $this->is_category = false;
186                 } else {
187                     $this->is_category = true;
188                 }
189             }
190
191             if ('' != $qv['category_name']) {
192                 $this->is_category = true;
193             }
194             
195             if ((empty($qv['author'])) || ($qv['author'] == '0')) {
196                 $this->is_author = false;
197             } else {
198                 $this->is_author = true;
199             }
200
201             if ('' != $qv['author_name']) {
202                 $this->is_author = true;
203             }
204
205             if ( ($this->is_date || $this->is_author || $this->is_category)) {
206                 $this->is_archive = true;
207             }
208
209             if ( 'attachment' == $qv['show_post_type'] ) {
210                 $this->is_attachment = true;
211             }
212         }
213
214         if ('' != $qv['feed']) {
215             $this->is_feed = true;
216         }
217
218         if ('' != $qv['tb']) {
219             $this->is_trackback = true;
220         }
221
222         if ('' != $qv['paged']) {
223             $this->is_paged = true;
224         }
225
226         if ('' != $qv['comments_popup']) {
227             $this->is_comments_popup = true;
228         }
229         
230         //if we're previewing inside the write screen
231         if ('' != $qv['preview']) {
232             $this->is_preview = true;
233         }
234
235         if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
236             $this->is_admin = true;
237         }
238
239         if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) {
240             $this->is_home = true;
241         }
242
243         if ( !empty($query) ) {
244             do_action('parse_query', array(&$this));
245         }
246     }
247
248     function set_404() {
249         $this->init_query_flags();
250         $this->is_404 = true;   
251     }
252     
253     function get($query_var) {
254         if (isset($this->query_vars[$query_var])) {
255             return $this->query_vars[$query_var];
256         }
257
258         return '';
259     }
260
261     function set($query_var, $value) {
262         $this->query_vars[$query_var] = $value;
263     }
264
265     function &get_posts() {
266         global $wpdb, $pagenow, $user_ID;
267
268         do_action('pre_get_posts', array(&$this));
269
270         // Shorthand.
271         $q = $this->query_vars;   
272
273         // First let's clear some variables
274         $whichcat = '';
275         $whichauthor = '';
276         $whichpage = '';
277         $result = '';
278         $where = '';
279         $limits = '';
280         $distinct = '';
281         $join = '';
282
283         if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
284             $q['posts_per_page'] = get_settings('posts_per_page');
285         if ( !isset($q['what_to_show']) )
286             $q['what_to_show'] = get_settings('what_to_show');
287         if ( isset($q['showposts']) && $q['showposts'] ) {
288             $q['showposts'] = (int) $q['showposts'];
289             $q['posts_per_page'] = $q['showposts'];
290         }
291         if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
292             $q['posts_per_page'] = $q['posts_per_archive_page'];
293         if ( !isset($q['nopaging']) ) {
294             if ($q['posts_per_page'] == -1) {
295                 $q['nopaging'] = true;
296             } else {
297                 $q['nopaging'] = false;
298             }
299         }
300         if ( $this->is_feed ) {
301             $q['posts_per_page'] = get_settings('posts_per_rss');
302             $q['what_to_show'] = 'posts';
303         }
304
305         if (isset($q['page'])) {
306             $q['page'] = trim($q['page'], '/');
307             $q['page'] = (int) $q['page'];
308             $q['page'] = abs($q['page']);
309         }
310     
311         $add_hours = intval(get_settings('gmt_offset'));
312         $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
313         $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
314
315         // If a month is specified in the querystring, load that month
316         if ( (int) $q['m'] ) {
317             $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
318             $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
319             if (strlen($q['m'])>5)
320                 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
321             if (strlen($q['m'])>7)
322                 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
323             if (strlen($q['m'])>9)
324                 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
325             if (strlen($q['m'])>11)
326                 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
327             if (strlen($q['m'])>13)
328                 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
329         }
330
331         if ( (int) $q['hour'] ) {
332             $q['hour'] = '' . intval($q['hour']);
333             $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
334         }
335
336         if ( (int) $q['minute'] ) {
337             $q['minute'] = '' . intval($q['minute']);
338             $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
339         }
340
341         if ( (int) $q['second'] ) {
342             $q['second'] = '' . intval($q['second']);
343             $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
344         }
345
346         if ( (int) $q['year'] ) {
347             $q['year'] = '' . intval($q['year']);
348             $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
349         }
350
351         if ( (int) $q['monthnum'] ) {
352             $q['monthnum'] = '' . intval($q['monthnum']);
353             $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
354         }
355
356         if ( (int) $q['day'] ) {
357             $q['day'] = '' . intval($q['day']);
358             $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
359         }
360
361         // Compat.  Map subpost to attachment.
362         if ( '' != $q['subpost'] )
363             $q['attachment'] = $q['subpost'];
364         if ( '' != $q['subpost_id'] )
365             $q['attachment_id'] = $q['subpost_id'];
366
367         if ('' != $q['name']) {
368             $q['name'] = sanitize_title($q['name']);
369             $where .= " AND post_name = '" . $q['name'] . "'";
370         } else if ('' != $q['pagename']) {
371             $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
372             $page_paths = '/' . trim($q['pagename'], '/');
373             $q['pagename'] = sanitize_title(basename($page_paths));
374             $q['name'] = $q['pagename'];
375             $page_paths = explode('/', $page_paths);
376             foreach($page_paths as $pathdir)
377                 $page_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
378                 
379             $all_page_ids = get_all_page_ids();
380             $reqpage = 0;
381             if (is_array($all_page_ids)) { foreach ( $all_page_ids as $page_id ) {
382                 $page = get_page($page_id);
383                 if ( $page->fullpath == $page_path ) {
384                     $reqpage = $page_id;
385                     break;
386                 }
387             } }
388             
389             $where .= " AND (ID = '$reqpage')";
390         } elseif ('' != $q['attachment']) {
391             $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
392             $attach_paths = '/' . trim($q['attachment'], '/');
393             $q['attachment'] = sanitize_title(basename($attach_paths));
394             $q['name'] = $q['attachment'];
395             $where .= " AND post_name = '" . $q['attachment'] . "'";
396         }
397
398         if ( (int) $q['w'] ) {
399             $q['w'] = ''.intval($q['w']);
400             $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
401         }
402
403         if ( intval($q['comments_popup']) )
404             $q['p'] = intval($q['comments_popup']);
405
406         // If a attachment is requested by number, let it supercede any post number.
407         if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) )
408             $q['p'] = (int) $q['attachment_id'];
409
410         // If a post number is specified, load that post
411         if (($q['p'] != '') && intval($q['p']) != 0) {
412             $q['p'] =  (int) $q['p'];
413             $where = ' AND ID = ' . $q['p'];
414         }
415
416         if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) {
417             $q['page_id'] = intval($q['page_id']);
418             $q