Changeset 1516 for trunk/wp-includes/classes.php
- Timestamp:
- 08/08/2004 03:31:50 PM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/classes.php
r1491 r1516 1 1 <?php 2 2 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 3 class 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 13 21 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; 22 36 } 23 37 24 38 function parse_query ($query) { 25 parse_str($query);26 39 $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; 66 82 } 67 83 68 84 // If year, month, day, hour, minute, and second are set, a single 69 85 // 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; 80 96 } else { 81 if (stristr($ cat,'-')) {82 $this-> category = false;97 if (stristr($qv['cat'],'-')) { 98 $this->is_category = false; 83 99 } 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; 90 106 } 91 107 92 108 // 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; 99 115 } 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 = '') { 123 512 if (! empty($query)) { 124 $this-> parse_query($query);513 $this->query($query); 125 514 } 126 515 } … … 128 517 129 518 // Make a global instance. 130 if (! isset($wp_query _state)) {131 $wp_query _state = new WP_Query_State();519 if (! isset($wp_query)) { 520 $wp_query = new WP_Query(); 132 521 } 133 522
Note: See TracChangeset
for help on using the changeset viewer.