Changeset 1516

Show
Ignore:
Timestamp:
08/08/04 15:31:50 (4 years ago)
Author:
rboren
Message:

WP Query class.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/index.php

    r1500 r1516  
    3232 
    3333<div id="content"> 
    34 <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?> 
     34<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    3535 
    3636<?php the_date('','<h2>','</h2>'); ?> 
     
    5757<?php comments_template(); // Get wp-comments.php template ?> 
    5858 
    59 <?php endforeach; else: ?> 
     59<?php endwhile; else: ?> 
    6060<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
    6161<?php endif; ?> 
  • trunk/wp-blog-header.php

    r1491 r1516  
    142142$query_string = apply_filters('query_string', $query_string); 
    143143 
    144 $wp_query_state->parse_query($query_string); 
    145  
    146144update_category_cache(); 
    147145 
  • trunk/wp-includes/classes.php

    r1491 r1516  
    11<?php 
    22 
    3 class WP_Query_State { 
    4     var $single = false; 
    5     var $archive = false; 
    6     var $date = false; 
    7     var $author = false; 
    8     var $category = false; 
    9     var $search = false; 
    10     var $feed = false; 
    11     var $home = false; 
    12      
     3class WP_Query { 
     4    var $query; 
     5    var $query_vars; 
     6 
     7    var $posts; 
     8    var $post_count = 0; 
     9    var $current_post = -1; 
     10    var $post; 
     11 
     12    var $is_single = false; 
     13    var $is_archive = false; 
     14    var $is_date = false; 
     15    var $is_author = false; 
     16    var $is_category = false; 
     17    var $is_search = false; 
     18    var $is_feed = false; 
     19    var $is_home = false; 
     20 
    1321    function init () { 
    14         $this->single = false; 
    15         $this->archive = false; 
    16         $this->date = false; 
    17         $this->author = false; 
    18         $this->category = false; 
    19         $this->search = false; 
    20         $this->feed = false; 
    21         $this->home = false; 
     22        $this->is_single = false; 
     23        $this->is_archive = false; 
     24        $this->is_date = false; 
     25        $this->is_author = false; 
     26        $this->is_category = false; 
     27        $this->is_search = false; 
     28        $this->is_feed = false; 
     29        $this->is_home = false; 
     30 
     31        unset($this->posts); 
     32        unset($this->query); 
     33        unset($this->query_vars); 
     34        $this->post_count = 0; 
     35        $this->current_post = -1; 
    2236    } 
    2337 
    2438    function parse_query ($query) { 
    25         parse_str($query); 
    2639        $this->init(); 
    27  
    28         if ('' != $name) { 
    29             $this->single = true; 
    30         } 
    31  
    32         if (($p != '') && ($p != 'all')) { 
    33             $this->single = true; 
    34         } 
    35  
    36         if ('' != $m) { 
    37             $this->date = true; 
    38         } 
    39  
    40         if ('' != $hour) { 
    41             $this->date = true;             
    42         } 
    43  
    44         if ('' != $minute) { 
    45             $this->date = true; 
    46         } 
    47  
    48         if ('' != $second) { 
    49             $this->date = true; 
    50         } 
    51  
    52         if ('' != $year) { 
    53             $this->date = true; 
    54         } 
    55  
    56         if ('' != $monthnum) { 
    57             $this->date = true; 
    58         } 
    59  
    60         if ('' != $day) { 
    61             $this->date = true; 
    62         } 
    63  
    64         if ('' != $w) { 
    65             $this->date = true; 
     40        parse_str($query, $qv); 
     41        $this->query = $query; 
     42        $this->query_vars = $qv; 
     43 
     44        if ('' != $qv['name']) { 
     45            $this->is_single = true; 
     46        } 
     47 
     48        if (($qv['p'] != '') && ($qv['p'] != 'all')) { 
     49            $this->is_single = true;             
     50        } 
     51 
     52        if ('' != $qv['m']) { 
     53            $this->is_date = true; 
     54        } 
     55 
     56        if ('' != $qv['hour']) { 
     57            $this->is_date = true;             
     58        } 
     59 
     60        if ('' != $qv['minute']) { 
     61            $this->is_date = true; 
     62        } 
     63 
     64        if ('' != $qv['second']) { 
     65            $this->is_date = true; 
     66        } 
     67 
     68        if ('' != $qv['year']) { 
     69            $this->is_date = true; 
     70        } 
     71 
     72        if ('' != $qv['monthnum']) { 
     73            $this->is_date = true; 
     74        } 
     75 
     76        if ('' != $qv['day']) { 
     77            $this->is_date = true; 
     78        } 
     79 
     80        if ('' != $qv['w']) { 
     81            $this->is_date = true; 
    6682        } 
    6783 
    6884        // If year, month, day, hour, minute, and second are set, a single  
    6985        // post is being queried.         
    70         if (('' != $hour) && ('' != $minute) &&('' != $second) && ('' != $year) && ('' != $monthnum) && ('' != $day)) { 
    71             $this->single = true; 
    72         } 
    73  
    74         if (!empty($s)) { 
    75             $this->search = true; 
    76         } 
    77  
    78         if (empty($cat) || ($cat == 'all') || ($cat == '0')) { 
    79             $this->category = false; 
     86        if (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) { 
     87            $this->is_single = true; 
     88        } 
     89 
     90        if (!empty($qv['s'])) { 
     91            $this->is_search = true; 
     92        } 
     93 
     94        if (empty($qv['cat']) || ($qv['cat'] == 'all') || ($qv['cat'] == '0')) { 
     95            $this->is_category = false; 
    8096        } else { 
    81             if (stristr($cat,'-')) { 
    82                 $this->category = false; 
     97            if (stristr($qv['cat'],'-')) { 
     98                $this->is_category = false; 
    8399            } else { 
    84                 $this->category = true; 
    85             } 
    86         } 
    87  
    88         if ('' != $category_name) { 
    89             $this->category = true; 
     100                $this->is_category = true; 
     101            } 
     102        } 
     103 
     104        if ('' != $qv['category_name']) { 
     105            $this->is_category = true; 
    90106        } 
    91107             
    92108        // single, date, and search override category. 
    93         if ($this->single || $this->date || $this->search) { 
    94             $this->category = false;                 
    95         } 
    96  
    97         if ((empty($author)) || ($author == 'all') || ($author == '0')) { 
    98             $this->author = false; 
     109        if ($this->is_single || $this->is_date || $this->is_search) { 
     110            $this->is_category = false;                 
     111        } 
     112 
     113        if ((empty($qv['author'])) || ($qv['author'] == 'all') || ($qv['author'] == '0')) { 
     114            $this->is_author = false; 
    99115        } else { 
    100             $this->author = true; 
    101         } 
    102  
    103         if ('' != $author_name) { 
    104             $this->author = true; 
    105         } 
    106  
    107         if ('' != $feed) { 
    108             $this->feed = true; 
    109         } 
    110  
    111         if ( ($this->date || $this->author || $this->category) 
    112              && (! $this->single)) { 
    113             $this->archive = true; 
    114         } 
    115  
    116         if ( ! ($this->archive || $this->single || $this->search || $this->feed)) { 
    117             $this->home = true; 
    118         } 
    119  
    120     } 
    121  
    122     function WP_Query_State ($query = '') { 
     116            $this->is_author = true; 
     117        } 
     118 
     119        if ('' != $qv['author_name']) { 
     120            $this->is_author = true; 
     121        } 
     122 
     123        if ('' != $qv['feed']) { 
     124            $this->is_feed = true; 
     125            $this->feed = $qv['feed']; 
     126        } 
     127 
     128        if ( ($this->is_date || $this->is_author || $this->is_category) 
     129             && (! $this->is_single)) { 
     130            $this->is_archive = true; 
     131        } 
     132 
     133        if ( ! ($this->is_archive || $this->is_single || $this->is_search || $this->is_feed)) { 
     134            $this->is_home = true; 
     135        } 
     136    } 
     137 
     138    function get($query_var) { 
     139        if (isset($this->query_vars[$query_var])) { 
     140            return $this->query_vars[$query_var]; 
     141        } 
     142 
     143        return ''; 
     144    } 
     145 
     146    function get_posts() { 
     147        global $wpdb, $pagenow, $request, $user_ID; 
     148 
     149    // Shorthand. 
     150    $q = $this->query_vars;  
     151 
     152        // First let's clear some variables 
     153        $whichcat = ''; 
     154        $whichauthor = ''; 
     155        $result = ''; 
     156        $where = ''; 
     157        $limits = ''; 
     158        $distinct = ''; 
     159        $join = ''; 
     160 
     161    if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) 
     162      $q['posts_per_page'] = get_settings('posts_per_page'); 
     163    if ( !isset($q['what_to_show']) ) 
     164      $q['what_to_show'] = get_settings('what_to_show'); 
     165    if ( isset($q['showposts']) && $q['showposts'] ) { 
     166      $q['showposts'] = (int) $q['showposts']; 
     167      $q['posts_per_page'] = $q['showposts']; 
     168    } 
     169 
     170        $add_hours = intval(get_settings('gmt_offset')); 
     171        $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); 
     172        $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; 
     173 
     174        // If a month is specified in the querystring, load that month 
     175        if ('' != $q['m']) { 
     176            $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); 
     177            $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); 
     178            if (strlen($q['m'])>5) 
     179                $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); 
     180            if (strlen($q['m'])>7) 
     181                $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); 
     182            if (strlen($q['m'])>9) 
     183                $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); 
     184            if (strlen($q['m'])>11) 
     185                $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); 
     186            if (strlen($q['m'])>13) 
     187                $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); 
     188        } 
     189 
     190        if ('' != $q['hour']) { 
     191            $q['hour'] = '' . intval($q['hour']); 
     192            $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; 
     193        } 
     194 
     195        if ('' != $q['minute']) { 
     196            $q['minute'] = '' . intval($q['minute']); 
     197            $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; 
     198        } 
     199 
     200        if ('' != $q['second']) { 
     201            $q['second'] = '' . intval($q['second']); 
     202            $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; 
     203        } 
     204 
     205        if ('' != $q['year']) { 
     206            $q['year'] = '' . intval($q['year']); 
     207            $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; 
     208        } 
     209 
     210        if ('' != $q['monthnum']) { 
     211            $q['monthnum'] = '' . intval($q['monthnum']); 
     212            $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; 
     213        } 
     214 
     215        if ('' != $q['day']) { 
     216            $q['day'] = '' . intval($q['day']); 
     217            $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; 
     218        } 
     219 
     220        if ('' != $q['name']) { 
     221            $q['name'] = preg_replace('/[^a-z0-9-_]/', '', $q['name']); 
     222            $where .= " AND post_name = '" . $q['name'] . "'"; 
     223        } 
     224 
     225        if ('' != $q['w']) { 
     226            $q['w'] = ''.intval($q['w']); 
     227            $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; 
     228        } 
     229 
     230        // If a post number is specified, load that post 
     231        if (($q['p'] != '') && ($q['p'] != 'all')) { 
     232            $q['p'] = intval($q['p']); 
     233            $where = ' AND ID = '.$q['p']; 
     234        } 
     235 
     236        // If a search pattern is specified, load the posts that match 
     237        if (!empty($q['s'])) { 
     238            $q['s'] = addslashes_gpc($q['s']); 
     239            $search = ' AND ('; 
     240            $q['s'] = preg_replace('/, +/', ' ', $q['s']); 
     241            $q['s'] = str_replace(',', ' ', $q['s']); 
     242            $q['s'] = str_replace('"', ' ', $q['s']); 
     243            $q['s'] = trim($q['s']); 
     244            if ($q['exact']) { 
     245                $n = ''; 
     246            } else { 
     247                $n = '%'; 
     248            } 
     249            if (!$q['sentence']) { 
     250                $s_array = explode(' ',$q['s']); 
     251                $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))'; 
     252                for ( $i = 1; $i < count($s_array); $i = $i + 1) { 
     253                    $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))'; 
     254                } 
     255                $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')'; 
     256                $search .= ')'; 
     257            } else { 
     258                $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))'; 
     259            } 
     260        } 
     261 
     262        // Category stuff 
     263 
     264        if ((empty($q['cat'])) || ($q['cat'] == 'all') || ($q['cat'] == '0') ||  
     265            // Bypass cat checks if fetching specific posts 
     266            ( 
     267             intval($q['year']) || intval($q['monthnum']) || intval($q['day']) || intval($q['w']) || 
     268             intval($q['p']) || !empty($q['name']) || !empty($q['s']) 
     269             ) 
     270            ) { 
     271            $whichcat=''; 
     272        } else { 
     273            $q['cat'] = ''.urldecode($q['cat']).''; 
     274            $q['cat'] = addslashes_gpc($q['cat']); 
     275            if (stristr($q['cat'],'-')) { 
     276                // Note: if we have a negative, we ignore all the positives. It must 
     277                // always mean 'everything /except/ this one'. We should be able to do 
     278                // multiple negatives but we don't :-( 
     279                $eq = '!='; 
     280                $andor = 'AND'; 
     281                $q['cat'] = explode('-',$q['cat']); 
     282                $q['cat'] = intval($q['cat'][1]); 
     283            } else { 
     284                $eq = '='; 
     285                $andor = 'OR'; 
     286            } 
     287            $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) "; 
     288            $cat_array = explode(' ',$q['cat']); 
     289            $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]); 
     290            $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' '); 
     291            for ($i = 1; $i < (count($cat_array)); $i = $i + 1) { 
     292                $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]); 
     293                $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' '); 
     294            } 
     295            $whichcat .= ')'; 
     296            if ($eq == '!=') { 
     297                $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category. 
     298            } 
     299        } 
     300 
     301        // Category stuff for nice URIs 
     302 
     303        if ('' != $q['category_name']) { 
     304            if (stristr($q['category_name'],'/')) { 
     305                $q['category_name'] = explode('/',$q['category_name']); 
     306                if ($q['category_name'][count($q['category_name'])-1]) { 
     307                    $q['category_name'] = $q['category_name'][count($q['category_name'])-1]; // no trailing slash 
     308                } else { 
     309                    $q['category_name'] = $q['category_name'][count($q['category_name'])-2]; // there was a trailling slash 
     310                } 
     311            } 
     312            $q['category_name'] = preg_replace('|[^a-z0-9-_]|i', '', $q['category_name']); 
     313            $tables = ", $wpdb->post2cat, $wpdb->categories"; 
     314            $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; 
     315            $whichcat = " AND (category_nicename = '" . $q['category_name'] . "'"; 
     316            $q['cat'] = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '" . $q['category_name'] . "'"); 
     317            $whichcat .= get_category_children($q['cat'], " OR category_id = "); 
     318            $whichcat .= ")"; 
     319        } 
     320 
     321        // Author/user stuff 
     322 
     323        if ((empty($q['author'])) || ($q['author'] == 'all') || ($q['author'] == '0')) { 
     324            $whichauthor=''; 
     325        } else { 
     326            $q['author'] = ''.urldecode($q['author']).''; 
     327            $q['author'] = addslashes_gpc($q['author']); 
     328            if (stristr($q['author'], '-')) { 
     329                $eq = '!='; 
     330                $andor = 'AND'; 
     331                $q['author'] = explode('-', $q['author']); 
     332                $q['author'] = ''.intval($q['author'][1]); 
     333            } else { 
     334                $eq = '='; 
     335                $andor = 'OR'; 
     336            } 
     337            $author_array = explode(' ', $q['author']); 
     338            $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]); 
     339            for ($i = 1; $i < (count($author_array)); $i = $i + 1) { 
     340                $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); 
     341            } 
     342            $whichauthor .= ')'; 
     343        } 
     344 
     345        // Author stuff for nice URIs 
     346 
     347        if ('' != $q['author_name']) { 
     348            if (stristr($q['author_name'],'/')) { 
     349                $q['author_name'] = explode('/',$q['author_name']); 
     350                if ($q['author_name'][count($q['author_name'])-1]) { 
     351                    $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash 
     352                } else { 
     353                    $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash 
     354                } 
     355            } 
     356            $q['author_name'] = preg_replace('|[^a-z0-9-_]|', '', strtolower($q['author_name'])); 
     357            $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'"); 
     358            $whichauthor .= ' AND (post_author = '.intval($q['author']).')'; 
     359        } 
     360 
     361        $where .= $search.$whichcat.$whichauthor; 
     362 
     363        if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) { 
     364            $q['order']='DESC'; 
     365        } 
     366 
     367        // Order by 
     368        if (empty($q['orderby'])) { 
     369            $q['orderby']='date '.$q['order']; 
     370        } else { 
     371            // Used to filter values 
     372            $allowed_keys = array('author','date','category','title'); 
     373            $q['orderby'] = urldecode($q['orderby']); 
     374            $q['orderby'] = addslashes_gpc($q['orderby']); 
     375            $orderby_array = explode(' ',$q['orderby']); 
     376            if (!in_array($orderby_array[0],$allowed_keys)) { 
     377                $orderby_array[0] = 'date'; 
     378            } 
     379            $q['orderby'] = $orderby_array[0].' '.$q['order']; 
     380            if (count($orderby_array)>1) { 
     381                for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) { 
     382                    // Only allow certain values for safety 
     383                    if (in_array($orderby_array[$i],$allowed_keys)) { 
     384                        $q['orderby'] .= ',post_'.$orderby_array[$i].' '.$q['order']; 
     385                    } 
     386                } 
     387            } 
     388        } 
     389 
     390        if ((!$whichcat) && (!$q['m']) && (!$q['p']) && (!$q['w']) && (!$q['s']) && empty($q['poststart']) && empty($q['postend'])) { 
     391            if ($q['what_to_show'] == 'posts') { 
     392                $limits = ' LIMIT '.$q['posts_per_page']; 
     393            } elseif ($q['what_to_show'] == 'days' && empty($q['monthnum']) && empty($q['year']) && empty($q['day'])) { 
     394                $lastpostdate = get_lastpostdate(); 
     395                $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate); 
     396                $lastpostdate = mysql2date('U',$lastpostdate); 
     397                $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($q['posts_per_page']-1) * 86400))); 
     398                $where .= " AND post_date > '$otherdate'"; 
     399            } 
     400        } 
     401 
     402        if ( !empty($q['postend']) && ($q['postend'] > $q['poststart']) && (!$q['m']) && empty($q['monthnum']) && empty($q['year']) && empty($q['day']) &&(!$q['w']) && (!$whichcat) && (!$q['s']) && (!$q['p'])) { 
     403            if ($q['what_to_show'] == 'posts') { 
     404                $q['poststart'] = intval($q['poststart']); 
     405                $q['postend'] = intval($q['postend']); 
     406                $limposts = $q['postend'] - $q['poststart']; 
     407                $limits = ' LIMIT '.$q['poststart'].','.$limposts; 
     408            } elseif ($q['what_to_show'] == 'days') { 
     409                $q['poststart'] = intval($q['poststart']); 
     410                $q['postend'] = intval($q['postend']); 
     411                $limposts = $q['postend'] - $q['poststart']; 
     412                $lastpostdate = get_lastpostdate(); 
     413                $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate); 
     414                $lastpostdate = mysql2date('U',$lastpostdate); 
     415                $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($q['poststart'] -1) * 86400))); 
     416                $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($q['postend'] -1) * 86400))); 
     417                $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'"; 
     418            } 
     419        } else { 
     420            if (($q['what_to_show'] == 'posts') && (! is_single()) && (!$q['more'])) { 
     421                if ($pagenow != 'post.php') { 
     422                    $pgstrt = ''; 
     423                    if ($q['paged']) { 
     424                        $pgstrt = (intval($q['paged']) -1) * $q['posts_per_page'] . ', '; 
     425                    } 
     426                    $limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; 
     427                } else { 
     428                    if (($q['m']) || ($q['p']) || ($q['w']) || ($q['s']) || ($whichcat)) { 
     429                        $limits = ''; 
     430                    } else { 
     431                        $pgstrt = ''; 
     432                        if ($q['paged']) { 
     433                            $pgstrt = (intval($q['paged']) -1) * $q['posts_per_page'] . ', '; 
     434                        } 
     435                        $limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; 
     436                    } 
     437                } 
     438            } 
     439            elseif (($q['m']) || ($q['p']) || ($q['w']) || ($q['s']) || ($whichcat) || ($q['author']) || $q['monthnum'] || $q['year'] || $q['day']) { 
     440                $limits = ''; 
     441            } 
     442        } 
     443 
     444        if ($q['p'] == 'all') { 
     445            $where = ''; 
     446        } 
     447 
     448        $now = gmdate('Y-m-d H:i:59'); 
     449 
     450        if ($pagenow != 'post.php' && $pagenow != 'edit.php') { 
     451            if ((empty($q['poststart'])) || (empty($q['postend'])) || !($q['postend'] > $q['poststart'])) { 
     452                $where .= " AND post_date_gmt <= '$now'"; 
     453            } 
     454 
     455            $distinct = 'DISTINCT'; 
     456        } 
     457        $where .= ' AND (post_status = "publish"'; 
     458 
     459        // Get private posts 
     460        if (isset($user_ID) && ('' != intval($user_ID))) 
     461            $where .= " OR post_author = $user_ID AND post_status != 'draft')"; 
     462        else 
     463            $where .= ')'; 
     464 
     465        $where = apply_filters('posts_where', $where); 
     466        $where .= " GROUP BY $wpdb->posts.ID"; 
     467        $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1".$where." ORDER BY post_" . $q['orderby'] . " $limits"; 
     468 
     469 
     470        if ($q['preview']) { 
     471            $request = 'SELECT 1-1'; // dummy mysql query for the preview 
     472            // little funky fix for IEwin, rawk on that code 
     473            $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT))); 
     474            if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) { 
     475                $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content); 
     476            } 
     477        } 
     478 
     479        $this->posts = $wpdb->get_results($request); 
     480        $this->posts = apply_filters('the_posts', $this->posts); 
     481        $this->post_count = count($this->posts); 
     482        if ($this->post_count > 0) { 
     483            $this->post = $posts[0]; 
     484        } 
     485 
     486        update_post_caches($this->posts); 
     487        return $this->posts; 
     488    } 
     489 
     490    function next_post() { 
     491         
     492        $this->current_post++; 
     493 
     494        $this->post = $this->posts[$this->current_post]; 
     495        return $this->post; 
     496    } 
     497 
     498    function have_posts() { 
     499        if ($this->current_post + 1 < $this->post_count) { 
     500            return true; 
     501        } 
     502 
     503        return false; 
     504    } 
     505     
     506    function query($query) { 
     507        $this->parse_query($query); 
     508        return $this->get_posts(); 
     509    } 
     510 
     511    function WP_Query ($query = '') { 
    123512        if (! empty($query)) { 
    124             $this->parse_query($query); 
     513            $this->query($query); 
    125514        } 
    126515    } 
     
    128517 
    129518// Make a global instance. 
    130 if (! isset($wp_query_state)) { 
    131     $wp_query_state = new WP_Query_State(); 
     519if (! isset($wp_query)) { 
     520    $wp_query = new WP_Query(); 
    132521} 
    133522 
  • trunk/wp-includes/functions.php

    <
    r1513 r1516  
    10371037 
    10381038 
    1039 function start_wp() { 
    1040    global $post, $id, $postdata, $authordata, $day, $preview, $page, $pages, $multipage, $more, $numpages
     1039function start_wp($use_wp_query = false) { 
     1040  global $post, $id, $postdata, $authordata, $day, $preview, $page, $pages, $multipage, $more, $numpages, $wp_query
    10411041    global $pagenow; 
     1042 
     1043    if ($use_wp_query) { 
     1044      $post = $wp_query->next_post(); 
     1045    } else { 
     1046      $wp_query->next_post(); 
     1047    } 
     1048 
    10421049    if (!$preview) { 
    10431050        $id = $post->ID; 
     
    14321439 
    14331440function query_posts($query) { 
    1434     global $wpdb, $pagenow, $request, $user_ID; 
    1435  
    1436     parse_str($query); 
    1437  
    1438     // First let's clear some variables 
    1439     $whichcat = ''; 
    1440     $whichauthor = ''; 
    1441     $result = ''; 
    1442     $where = ''; 
    1443     $limits = ''; 
    1444     $distinct = ''; 
    1445     $join = ''; 
    1446  
    1447     $add_hours = intval(get_settings('gmt_offset')); 
    1448     $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); 
    1449     $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; 
    1450  
    1451     // If a month is specified in the querystring, load that month 
    1452     if ('' != $m) { 
    1453         $m = '' . preg_replace('|[^0-9]|', '', $m); 
    1454         $where .= ' AND YEAR(post_date)=' . substr($m, 0, 4); 
    1455         if (strlen($m)>5) 
    1456             $where .= ' AND MONTH(post_date)=' . substr($m, 4, 2); 
    1457         if (strlen($m)>7) 
    1458             $where .= ' AND DAYOFMONTH(post_date)=' . substr($m, 6, 2); 
    1459         if (strlen($m)>9) 
    1460             $where .= ' AND HOUR(post_date)=' . substr($m, 8, 2); 
    1461         if (strlen($m)>11) 
    1462             $where .= ' AND MINUTE(post_date)=' . substr($m, 10, 2); 
    1463         if (strlen($m)>13) 
    1464             $where .= ' AND SECOND(post_date)=' . substr($m, 12, 2); 
    1465     } 
    1466  
    1467     if ('' != $hour) { 
    1468         $hour = '' . intval($hour); 
    1469         $where .= " AND HOUR(post_date)='$hour'"; 
    1470     } 
    1471  
    1472     if ('' != $minute) { 
    1473         $minute = '' . intval($minute); 
    1474         $where .= " AND MINUTE(post_date)='$minute'"; 
    1475     } 
    1476  
    1477     if ('' != $second) { 
    1478         $second = '' . intval($second); 
    1479         $where .= " AND SECOND(post_date)='$second'"; 
    1480     } 
    1481  
    1482     if ('' != $year) { 
    1483         $year = '' . intval($year); 
    1484         $where .= " AND YEAR(post_date)='$year'"; 
    1485     } 
    1486  
    1487     if ('' != $monthnum) { 
    1488         $monthnum = '' . intval($monthnum); 
    1489         $where .= " AND MONTH(post_date)='$monthnum'"; 
    1490     } 
    1491  
    1492     if ('' != $day) { 
    1493         $day = '' . intval($day); 
    1494         $where .= " AND DAYOFMONTH(post_date)='$day'"; 
    1495     } 
    1496  
    1497     if ('' != $name) { 
    1498         $name = preg_replace('/[^a-z0-9-_]/', '', $name); 
    1499         $where .= " AND post_name = '$name'"; 
    1500     } 
    1501  
    1502     if ('' != $w) { 
    1503         $w = ''.intval($w); 
    1504         $where .= " AND WEEK(post_date, 1)='$w'"; 
    1505     } 
    1506  
    1507     // If a post number is specified, load that post 
    1508     if (($p != '') && ($p != 'all')) { 
    1509         $p = intval($p); 
    1510         $where = ' AND ID = '.$p; 
    1511     } 
    1512  
    1513     // If a search pattern is specified, load the posts that match 
    1514     if (!empty($s)) { 
    1515         $s = addslashes_gpc($s); 
    1516         $search = ' AND ('; 
    1517         $s = preg_replace('/, +/', ' ', $s); 
    1518         $s = str_replace(',', ' ', $s); 
    1519         $s = str_replace('"', ' ', $s); 
    1520         $s = trim($s); 
    1521         if ($exact) { 
    1522             $n = ''; 
    1523         } else { 
    1524             $n = '%'; 
    1525         } 
    1526         if (!$sentence) { 
    1527             $s_array = explode(' ',$s); 
    1528             $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))'; 
    1529             for ( $i = 1; $i < count($s_array); $i = $i + 1) { 
    1530                 $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))'; 
    1531             } 
    1532             $search .= ' OR (post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')'; 
    1533             $search .= ')'; 
    1534         } else { 
    1535             $search = ' AND ((post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\'))'; 
    1536         } 
    1537     } 
    1538  
    1539     // Category stuff 
    1540  
    1541     if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||  
    1542         // Bypass cat checks if fetching specific posts 
    1543         ( 
    1544          intval($year) || intval($monthnum) || intval($day) || intval($w) || 
    1545          intval($p) || !empty($name) || !empty($s) 
    1546          ) 
    1547         ) { 
    1548         $whichcat=''; 
    1549     } else { 
    1550         $cat = ''.urldecode($cat).''; 
    1551         $cat = addslashes_gpc($cat); 
    1552         if (stristr($cat,'-')) { 
    1553             // Note: if we have a negative, we ignore all the positives. It must 
    1554             // always mean 'everything /except/ this one'. We should be able to do 
    1555             // multiple negatives but we don't :-( 
    1556             $eq = '!='; 
    1557             $andor = 'AND'; 
    1558             $cat = explode('-',$cat); 
    1559             $cat = intval($cat[1]); 
    1560         } else { 
    1561             $eq = '='; 
    1562             $andor = 'OR'; 
    1563         } 
    1564         $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) "; 
    1565         $cat_array = explode(' ',$cat); 
    1566         $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]); 
    1567         $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' '); 
    1568         for ($i = 1; $i < (count($cat_array)); $i = $i + 1) { 
    1569             $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]); 
    1570             $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' '); 
    1571         } 
    1572         $whichcat .= ')'; 
    1573         if ($eq == '!=') { 
    1574             $cat = '-'.$cat; // Put back the knowledge that we are excluding a category. 
    1575         } 
    1576     } 
    1577  
    1578     // Category stuff for nice URIs 
    1579  
    1580     if ('' != $category_name) { 
    1581         if (stristr($category_name,'/')) { 
    1582             $category_name = explode('/',$category_name); 
    1583             if ($category_name[count($category_name)-1]) { 
    1584                 $category_name = $category_name[count($category_name)-1]; // no trailing slash 
    1585             } else { 
    1586                 $category_name = $category_name[count($category_name)-2]; // there was a trailling slash 
    1587             } 
    1588         } 
    1589         $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name); 
    1590         $tables = ", $wpdb->post2cat, $wpdb->categories"; 
    1591         $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; 
    1592         $whichcat = " AND (category_nicename = '$category_name'"; 
    1593         $cat = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_name'"); 
    1594         $whichcat .= get_category_children($cat, " OR category_id = "); 
    1595         $whichcat .= ")"; 
    1596     } 
    1597  
    1598     // Author/user stuff 
    1599  
    1600     if ((empty($author)) || ($author == 'all') || ($author == '0')) { 
    1601         $whichauthor=''; 
    1602     } else { 
    1603         $author = ''.urldecode($author).''; 
    1604         $author = addslashes_gpc($author); 
    1605         if (stristr($author, '-')) { 
    1606             $eq = '!='; 
    1607             $andor = 'AND'; 
    1608             $author = explode('-', $author); 
    1609             $author = ''.intval($author[1]); 
    1610         } else { 
    1611             $eq = '='; 
    1612             $andor = 'OR'; 
    1613         } 
    1614         $author_array = explode(' ', $author); 
    1615         $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]); 
    1616         for ($i = 1; $i < (count($author_array)); $i = $i + 1) { 
    1617             $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); 
    1618         } 
    1619         $whichauthor .= ')'; 
    1620     } 
    1621  
    1622     // Author stuff for nice URIs 
    1623  
    1624     if ('' != $author_name) { 
    1625         if (stristr($author_name,'/')) { 
    1626             $author_name = explode('/',$author_name); 
    1627             if ($author_name[count($author_name)-1]) { 
    1628                 $author_name = $author_name[count($author_name)-1];#no trailing slash 
    1629             } else { 
    1630                 $author_name = $author_name[count($author_name)-2];#there was a trailling slash 
    1631             } 
    1632         } 
    1633         $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name)); 
    1634         $author = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$author_name."'"); 
    1635         $whichauthor .= ' AND (post_author = '.intval($author).')'; 
    1636     } 
    1637  
    1638     $where .= $search.$whichcat.$whichauthor; 
    1639  
    1640     if ((empty($order)) || ((strtoupper($order) != 'ASC') && (strtoupper($order) != 'DESC'))) { 
    1641         $order='DESC'; 
    1642     } 
    1643  
    1644     // Order by 
    1645     if (empty($orderby)) { 
    1646         $orderby='date '.$order; 
    1647     } else { 
    1648         // Used to filter values 
    1649         $allowed_keys = array('author','date','category','title'); 
    1650         $orderby = urldecode($orderby); 
    1651         $orderby = addslashes_gpc($orderby); 
    1652         $orderby_array = explode(' ',$orderby); 
    1653         if (!in_array($orderby_array[0],$allowed_keys)) { 
    1654             $orderby_array[0] = 'date'; 
    1655         } 
    1656         $orderby = $orderby_array[0].' '.$order; 
    1657         if (count($orderby_array)>1) { 
    1658             for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) { 
    1659                 // Only allow certain values for safety 
    1660                 if (in_array($orderby_array[$i],$allowed_keys)) { 
    1661                     $orderby .= ',post_'.$orderby_array[$i].' '.$order; 
    1662                 } 
    1663             } 
    1664         } 
    1665     } 
    1666  
    1667     if ((!$whichcat) && (!$m) && (!$p) && (!$w) && (!$s) && empty($poststart) && empty($postend)) { 
    1668         if ($what_to_show == 'posts') { 
    1669             $limits = ' LIMIT '.$posts_per_page; 
    1670         } elseif ($what_to_show == 'days' && empty($monthnum) && empty($year) && empty($day)) { 
    1671             $lastpostdate = get_lastpostdate(); 
    1672             $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate); 
    1673             $lastpostdate = mysql2date('U',$lastpostdate); 
    1674             $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($posts_per_page-1) * 86400))); 
    1675             $where .= " AND post_date > '$otherdate'"; 
    1676         } 
    1677     } 
    1678  
    1679     if ( !empty($postend) && ($postend > $poststart) && (!$m) && empty($monthnum) && empty($year) && empty($day) &&(!$w) && (!$whichcat) && (!$s) && (!$p)) { 
    1680         if ($what_to_show == 'posts') { 
    1681             $poststart = intval($poststart); 
    1682             $postend = intval($postend); 
    1683             $limposts = $postend - $poststart; 
    1684             $limits = ' LIMIT '.$poststart.','.$limposts; 
    1685         } elseif ($what_to_show == 'days') { 
    1686             $poststart = intval($poststart); 
    1687             $postend = intval($postend); 
    1688             $limposts = $postend - $poststart; 
    1689             $lastpostdate = get_lastpostdate(); 
    1690             $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate); 
    1691             $lastpostdate = mysql2date('U',$lastpostdate); 
    1692             $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400))); 
    1693             $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400))); 
    1694             $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'"; 
    1695         } 
    1696     } else { 
    1697         if (($what_to_show == 'posts') && (! is_single()) && (!$more)) { 
    1698             if ($pagenow != 'post.php') { 
    1699                 $pgstrt = ''; 
    1700                 if ($paged) { 
    1701                     $pgstrt = (intval($paged) -1) * $posts_per_page . ', '; 
    1702                 } 
    1703                 $limits = 'LIMIT '.$pgstrt.$posts_per_page; 
    1704             } else { 
    1705                 if (($m) || ($p) || ($w) || ($s) || ($whichcat)) { 
    1706                     $limits = ''; 
    1707                 } else { 
    1708                     $pgstrt = ''; 
    1709                     if ($paged) { 
    1710                         $pgstrt = (intval($paged) -1) * $posts_per_page . ', '; 
    1711                     } 
    1712                     $limits = 'LIMIT '.$pgstrt.$posts_per_page; 
    1713                 } 
    1714             } 
    1715         } 
    1716         elseif (($m) || ($p) || ($w) || ($s) || ($whichcat) || ($author) || $monthnum || $year || $day) { 
    1717             $limits = ''; 
    1718         } 
    1719     } 
    1720  
    1721     if ($p == 'all') { 
    1722         $where = ''; 
    1723     } 
    1724  
    1725     $now = gmdate('Y-m-d H:i:59'); 
    1726  
    1727     if ($pagenow != 'post.php' && $pagenow != 'edit.php') { 
    1728         if ((empty($poststart)) || (empty($postend)) || !($postend > $poststart)) { 
    1729             $where .= " AND post_date_gmt <= '$now'"; 
    1730         } 
    1731  
    1732         $distinct = 'DISTINCT'; 
    1733     } 
    1734     $where .= ' AND (post_status = "publish"'; 
    1735  
    1736     // Get private posts 
    1737     if (isset($user_ID) && ('' != intval($user_ID))) 
    1738         $where .= " OR post_author = $user_ID AND post_status != 'draft')"; 
    1739     else 
    1740         $where .= ')'; 
    1741  
    1742     $where = apply_filters('posts_where', $where); 
    1743     $where .= " GROUP BY $wpdb->posts.ID"; 
    1744     $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits"; 
    1745  
    1746  
    1747     if ($preview) { 
    1748         $request = 'SELECT 1-1'; // dummy mysql query for the preview 
    1749         // little funky fix for IEwin, rawk on that code 
    1750         $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT))); 
    1751         if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) { 
    1752             $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content); 
    1753         } 
    1754     } 
    1755  
    1756     // error_log("$request"); 
    1757     // echo $request; 
    1758     $posts = $wpdb->get_results($request); 
    1759     $posts = apply_filters('the_posts', $posts); 
    1760  
    1761     update_post_caches($posts); 
    1762     return $posts; 
     1441    global $wp_query; 
     1442 
     1443    return $wp_query->query($query); 
    17631444} 
    17641445 
     
    18531534 
    18541535function is_single () { 
    1855     global $wp_query_state
    1856  
    1857     return $wp_query_state->single; 
     1536    global $wp_query
     1537 
     1538    return $wp_query->is_single;