root/trunk/wp-includes/query.php

Revision 7906, 48.6 kB (checked in by ryan, 2 days ago)

Query cleanups. Use absint, concat where instead of overwrite, make post_parent independent, sanitize postin and postnot_in. Props mdawaffe. see #6772

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 /*
4  * The Big Query.
5  */
6
7 function get_query_var($var) {
8     global $wp_query;
9
10     return $wp_query->get($var);
11 }
12
13 function set_query_var($var, $value) {
14     global $wp_query;
15
16     return $wp_query->set($var, $value);
17 }
18
19 function &query_posts($query) {
20     unset($GLOBALS['wp_query']);
21     $GLOBALS['wp_query'] =& new WP_Query();
22     return $GLOBALS['wp_query']->query($query);
23 }
24
25 function wp_reset_query() {
26     unset($GLOBALS['wp_query']);
27     $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
28     global $wp_query;
29     if ( !empty($wp_query->post) ) {
30         $GLOBALS['post'] = $wp_query->post;
31         setup_postdata($wp_query->post);
32     }
33 }
34
35 /*
36  * Query type checks.
37  */
38
39 function is_admin () {
40     if ( defined('WP_ADMIN') )
41         return WP_ADMIN;
42     return false;
43 }
44
45 function is_archive () {
46     global $wp_query;
47
48     return $wp_query->is_archive;
49 }
50
51 function is_attachment () {
52     global $wp_query;
53
54     return $wp_query->is_attachment;
55 }
56
57 function is_author ($author = '') {
58     global $wp_query;
59
60     if ( !$wp_query->is_author )
61         return false;
62
63     if ( empty($author) )
64         return true;
65
66     $author_obj = $wp_query->get_queried_object();
67
68     $author = (array) $author;
69
70     if ( in_array( $author_obj->ID, $author ) )
71         return true;
72     elseif ( in_array( $author_obj->nickname, $author ) )
73         return true;
74     elseif ( in_array( $author_obj->user_nicename, $author ) )
75         return true;
76
77     return false;
78 }
79
80 function is_category ($category = '') {
81     global $wp_query;
82
83     if ( !$wp_query->is_category )
84         return false;
85
86     if ( empty($category) )
87         return true;
88
89     $cat_obj = $wp_query->get_queried_object();
90
91     $category = (array) $category;
92
93     if ( in_array( $cat_obj->term_id, $category ) )
94         return true;
95     elseif ( in_array( $cat_obj->name, $category ) )
96         return true;
97     elseif ( in_array( $cat_obj->slug, $category ) )
98         return true;
99
100     return false;
101 }
102
103 function is_tag( $slug = '' ) {
104     global $wp_query;
105
106     if ( !$wp_query->is_tag )
107         return false;
108
109     if ( empty( $slug ) )
110         return true;
111
112     $tag_obj = $wp_query->get_queried_object();
113
114     $slug = (array) $slug;
115
116     if ( in_array( $tag_obj->slug, $slug ) )
117         return true;
118
119     return false;
120 }
121
122 function is_tax( $slug = '' ) {
123     global $wp_query;
124     
125     if ( !$wp_query->is_tax )
126         return false;
127
128     if ( empty($slug) )
129         return true;
130
131     $term = $wp_query->get_queried_object();
132
133     $slug = (array) $slug;
134
135     if ( in_array( $term->slug, $slug ) )
136         return true;
137
138     return false;
139 }
140
141 function is_comments_popup () {
142     global $wp_query;
143
144     return $wp_query->is_comments_popup;
145 }
146
147 function is_date () {
148     global $wp_query;
149
150     return $wp_query->is_date;
151 }
152
153 function is_day () {
154     global $wp_query;
155
156     return $wp_query->is_day;
157 }
158
159 function is_feed () {
160     global $wp_query;
161
162     return $wp_query->is_feed;
163 }
164
165 /**
166  * is_front_page() - Is it the front of the site, whether blog view or a WP Page?
167  *
168  * @since 2.5
169  * @uses is_home
170  * @uses get_option
171  *
172  * @return bool True if front of site
173  */
174 function is_front_page () {
175     // most likely case
176     if ( 'posts' == get_option('show_on_front') && is_home() )
177         return true;
178     elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
179         return true;
180     else
181         return false;
182 }
183
184 /**
185  * is_home() - Is it the blog view homepage?
186  *
187  * @since 2.1
188  * @global object $wp_query
189  *
190  * @return bool True if blog view homepage
191  */
192 function is_home () {
193     global $wp_query;
194
195     return $wp_query->is_home;
196 }
197
198 function is_month () {
199     global $wp_query;
200
201     return $wp_query->is_month;
202 }
203
204 function is_page ($page = '') {
205     global $wp_query;
206
207     if ( !$wp_query->is_page )
208         return false;
209
210     if ( empty($page) )
211         return true;
212
213     $page_obj = $wp_query->get_queried_object();
214
215     $page = (array) $page;
216
217     if ( in_array( $page_obj->ID, $page ) )
218         return true;
219     elseif ( in_array( $page_obj->post_title, $page ) )
220         return true;
221     else if ( in_array( $page_obj->post_name, $page ) )
222         return true;
223
224     return false;
225 }
226
227 function is_paged () {
228     global $wp_query;
229
230     return $wp_query->is_paged;
231 }
232
233 function is_plugin_page() {
234     global $plugin_page;
235
236     if ( isset($plugin_page) )
237         return true;
238
239     return false;
240 }
241
242 function is_preview() {
243     global $wp_query;
244
245     return $wp_query->is_preview;
246 }
247
248 function is_robots() {
249     global $wp_query;
250
251     return $wp_query->is_robots;
252 }
253
254 function is_search () {
255     global $wp_query;
256
257     return $wp_query->is_search;
258 }
259
260 function is_single ($post = '') {
261     global $wp_query;
262
263     if ( !$wp_query->is_single )
264         return false;
265
266     if ( empty( $post) )
267         return true;
268
269     $post_obj = $wp_query->get_queried_object();
270
271     $post = (array) $post;
272
273     if ( in_array( $post_obj->ID, $post ) )
274         return true;
275     elseif ( in_array( $post_obj->post_title, $post ) )
276         return true;
277     elseif ( in_array( $post_obj->post_name, $post ) )
278         return true;
279
280     return false;
281 }
282
283 function is_singular() {
284     global $wp_query;
285
286     return $wp_query->is_singular;
287 }
288
289 function is_time () {
290     global $wp_query;
291
292     return $wp_query->is_time;
293 }
294
295 function is_trackback () {
296     global $wp_query;
297
298     return $wp_query->is_trackback;
299 }
300
301 function is_year () {
302     global $wp_query;
303
304     return $wp_query->is_year;
305 }
306
307 function is_404 () {
308     global $wp_query;
309
310     return $wp_query->is_404;
311 }
312
313 /*
314  * The Loop.  Post loop control.
315  */
316
317 function have_posts() {
318     global $wp_query;
319
320     return $wp_query->have_posts();
321 }
322
323 function in_the_loop() {
324     global $wp_query;
325
326     return $wp_query->in_the_loop;
327 }
328
329 function rewind_posts() {
330     global $wp_query;
331
332     return $wp_query->rewind_posts();
333 }
334
335 function the_post() {
336     global $wp_query;
337
338     $wp_query->the_post();
339 }
340
341 /*
342  * Comments loop.
343  */
344
345 function have_comments() {
346     global $wp_query;
347     return $wp_query->have_comments();
348 }
349
350 function the_comment() {
351     global $wp_query;
352     return $wp_query->the_comment();
353 }
354
355 /*
356  * WP_Query
357  */
358
359 class WP_Query {
360     var $query;
361     var $query_vars = array();
362     var $queried_object;
363     var $queried_object_id;
364     var $request;
365
366     var $posts;
367     var $post_count = 0;
368     var $current_post = -1;
369     var $in_the_loop = false;
370     var $post;
371
372     var $comments;
373     var $comment_count = 0;
374     var $current_comment = -1;
375     var $comment;
376
377     var $found_posts = 0;
378     var $max_num_pages = 0;
379
380     var $is_single = false;
381     var $is_preview = false;
382     var $is_page = false;
383     var $is_archive = false;
384     var $is_date = false;
385     var $is_year = false;
386     var $is_month = false;
387     var $is_day = false;
388     var $is_time = false;
389     var $is_author = false;
390     var $is_category = false;
391     var $is_tag = false;
392     var $is_tax = false;
393     var $is_search = false;
394     var $is_feed = false;
395     var $is_comment_feed = false;
396     var $is_trackback = false;
397     var $is_home = false;
398     var $is_404 = false;
399     var $is_comments_popup = false;
400     var $is_admin = false;
401     var $is_attachment = false;
402     var $is_singular = false;
403     var $is_robots = false;
404     var $is_posts_page = false;
405
406     function init_query_flags() {
407         $this->is_single = false;
408         $this->is_page = false;
409         $this->is_archive = false;
410         $this->is_date = false;
411         $this->is_year = false;
412         $this->is_month = false;
413         $this->is_day = false;
414         $this->is_time = false;
415         $this->is_author = false;
416         $this->is_category = false;
417         $this->is_tag = false;
418         $this->is_tax = false;
419         $this->is_search = false;
420         $this->is_feed = false;
421         $this->is_comment_feed = false;
422         $this->is_trackback = false;
423         $this->is_home = false;
424         $this->is_404 = false;
425         $this->is_paged = false;
426         $this->is_admin = false;
427         $this->is_attachment = false;
428         $this->is_singular = false;
429         $this->is_robots = false;
430         $this->is_posts_page = false;
431     }
432
433     function init () {
434         unset($this->posts);
435         unset($this->query);
436         $this->query_vars = array();
437         unset($this->queried_object);
438         unset($this->queried_object_id);
439         $this->post_count = 0;
440         $this->current_post = -1;
441         $this->in_the_loop = false;
442
443         $this->init_query_flags();
444     }
445
446     // Reparse the query vars.
447     function parse_query_vars() {
448         $this->parse_query('');
449     }
450
451     function fill_query_vars($array) {
452         $keys = array(
453             'error'
454             , 'm'
455             , 'p'
456             , 'post_parent'
457             , 'subpost'
458             , 'subpost_id'
459             , 'attachment'
460             , 'attachment_id'
461             , 'name'
462             , 'hour'
463             , 'static'
464             , 'pagename'
465             , 'page_id'
466             , 'second'
467             , 'minute'
468             , 'hour'
469             , 'day'
470             , 'monthnum'
471             , 'year'
472             , 'w'
473             , 'category_name'
474             , 'tag'
475             , 'tag_id'
476             , 'author_name'
477             , 'feed'
478             , 'tb'
479             , 'paged'
480             , 'comments_popup'
481             , 'meta_key'
482             , 'meta_value'
483             , 'preview'
484         );
485
486         foreach ($keys as $key) {
487             if ( !isset($array[$key]))
488                 $array[$key] = '';
489         }
490
491         $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
492             'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
493
494         foreach ( $array_keys as $key ) {
495             if ( !isset($array[$key]))
496                 $array[$key] = array();
497         }
498         return $array;
499     }
500
501     // Parse a query string and set query type booleans.
502     function parse_query ($query) {
503         if ( !empty($query) || !isset($this->query) ) {
504             $this->init();
505             if ( is_array($query) )
506                 $this->query_vars = $query;
507             else
508                 parse_str($query, $this->query_vars);
509             $this->query = $query;
510         }
511
512         $this->query_vars = $this->fill_query_vars($this->query_vars);
513         $qv = &$this->query_vars;
514
515         if ( ! empty($qv['robots']) )
516             $this->is_robots = true;
517
518         $qv['p'] =  absint($qv['p']);
519         $qv['page_id'] =  absint($qv['page_id']);
520         $qv['year'] = absint($qv['year']);
521         $qv['monthnum'] = absint($qv['monthnum']);
522         $qv['day'] = absint($qv['day']);
523         $qv['w'] = absint($qv['w']);
524         $qv['m'] = absint($qv['m']);
525         $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
526         if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
527         if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
528         if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
529
530         // Compat.  Map subpost to attachment.
531         if ( '' != $qv['subpost'] )
532             $qv['attachment'] = $qv['subpost'];
533         if ( '' != $qv['subpost_id'] )
534             $qv['attachment_id'] = $qv['subpost_id'];
535
536         $qv['attachment_id'] = absint($qv['attachment_id']);
537
538         if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
539             $this->is_single = true;
540             $this->is_attachment = true;
541         } elseif ( '' != $qv['name'] ) {
542             $this->is_single = true;
543         } elseif ( $qv['p'] ) {
544             $this->is_single = true;
545         } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
546             // If year, month, day, hour, minute, and second are set, a single
547             // post is being queried.
548             $this->is_single = true;
549         } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
550             $this->is_page = true;
551             $this->is_single = false;
552         } elseif ( !empty($qv['s']) ) {
553             $this->is_search = true;
554         } else {
555         // Look for archive queries.  Dates, categories, authors.
556
557             if ( '' !== $qv['second'] ) {
558                 $this->is_time = true;
559                 $this->is_date = true;
560             }
561
562             if ( '' !== $qv['minute'] ) {
563                 $this->is_time = true;
564                 $this->is_date = true;
565             }
566
567             if ( '' !== $qv['hour'] ) {
568                 $this->is_time = true;
569                 $this->is_date = true;
570             }
571
572             if ( $qv['day'] ) {
573                 if (! $this->is_date) {
574                     $this->is_day = true;
575                     $this->is_date = true;
576                 }
577             }
578
579             if ( $qv['monthnum'] ) {
580                 if (!