[ Index ] |
|
Code source de WordPress 2.1.2 |
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 &query_posts($query) { 14 unset($GLOBALS['wp_query']); 15 $GLOBALS['wp_query'] =& new WP_Query(); 16 return $GLOBALS['wp_query']->query($query); 17 } 18 19 /* 20 * Query type checks. 21 */ 22 23 function is_admin () { 24 global $wp_query; 25 26 return ( $wp_query->is_admin || strstr($_SERVER['REQUEST_URI'], 'wp-admin/') ); 27 } 28 29 function is_archive () { 30 global $wp_query; 31 32 return $wp_query->is_archive; 33 } 34 35 function is_attachment () { 36 global $wp_query; 37 38 return $wp_query->is_attachment; 39 } 40 41 function is_author ($author = '') { 42 global $wp_query; 43 44 if ( !$wp_query->is_author ) 45 return false; 46 47 if ( empty($author) ) 48 return true; 49 50 $author_obj = $wp_query->get_queried_object(); 51 52 if ( $author == $author_obj->ID ) 53 return true; 54 elseif ( $author == $author_obj->nickname ) 55 return true; 56 elseif ( $author == $author_obj->user_nicename ) 57 return true; 58 59 return false; 60 } 61 62 function is_category ($category = '') { 63 global $wp_query; 64 65 if ( !$wp_query->is_category ) 66 return false; 67 68 if ( empty($category) ) 69 return true; 70 71 $cat_obj = $wp_query->get_queried_object(); 72 73 if ( $category == $cat_obj->cat_ID ) 74 return true; 75 else if ( $category == $cat_obj->cat_name ) 76 return true; 77 elseif ( $category == $cat_obj->category_nicename ) 78 return true; 79 80 return false; 81 } 82 83 function is_comments_popup () { 84 global $wp_query; 85 86 return $wp_query->is_comments_popup; 87 } 88 89 function is_date () { 90 global $wp_query; 91 92 return $wp_query->is_date; 93 } 94 95 function is_day () { 96 global $wp_query; 97 98 return $wp_query->is_day; 99 } 100 101 function is_feed () { 102 global $wp_query; 103 104 return $wp_query->is_feed; 105 } 106 107 function is_home () { 108 global $wp_query; 109 110 return $wp_query->is_home; 111 } 112 113 function is_month () { 114 global $wp_query; 115 116 return $wp_query->is_month; 117 } 118 119 function is_page ($page = '') { 120 global $wp_query; 121 122 if ( !$wp_query->is_page ) 123 return false; 124 125 if ( empty($page) ) 126 return true; 127 128 $page_obj = $wp_query->get_queried_object(); 129 130 if ( $page == $page_obj->ID ) 131 return true; 132 elseif ( $page == $page_obj->post_title ) 133 return true; 134 else if ( $page == $page_obj->post_name ) 135 return true; 136 137 return false; 138 } 139 140 function is_paged () { 141 global $wp_query; 142 143 return $wp_query->is_paged; 144 } 145 146 function is_plugin_page() { 147 global $plugin_page; 148 149 if ( isset($plugin_page) ) 150 return true; 151 152 return false; 153 } 154 155 function is_preview() { 156 global $wp_query; 157 158 return $wp_query->is_preview; 159 } 160 161 function is_robots() { 162 global $wp_query; 163 164 return $wp_query->is_robots; 165 } 166 167 function is_search () { 168 global $wp_query; 169 170 return $wp_query->is_search; 171 } 172 173 function is_single ($post = '') { 174 global $wp_query; 175 176 if ( !$wp_query->is_single ) 177 return false; 178 179 if ( empty( $post) ) 180 return true; 181 182 $post_obj = $wp_query->get_queried_object(); 183 184 if ( $post == $post_obj->ID ) 185 return true; 186 elseif ( $post == $post_obj->post_title ) 187 return true; 188 elseif ( $post == $post_obj->post_name ) 189 return true; 190 191 return false; 192 } 193 194 function is_singular() { 195 global $wp_query; 196 197 return $wp_query->is_singular; 198 } 199 200 function is_time () { 201 global $wp_query; 202 203 return $wp_query->is_time; 204 } 205 206 function is_trackback () { 207 global $wp_query; 208 209 return $wp_query->is_trackback; 210 } 211 212 function is_year () { 213 global $wp_query; 214 215 return $wp_query->is_year; 216 } 217 218 function is_404 () { 219 global $wp_query; 220 221 return $wp_query->is_404; 222 } 223 224 /* 225 * The Loop. Post loop control. 226 */ 227 228 function have_posts() { 229 global $wp_query; 230 231 return $wp_query->have_posts(); 232 } 233 234 function in_the_loop() { 235 global $wp_query; 236 237 return $wp_query->in_the_loop; 238 } 239 240 function rewind_posts() { 241 global $wp_query; 242 243 return $wp_query->rewind_posts(); 244 } 245 246 function the_post() { 247 global $wp_query; 248 249 $wp_query->the_post(); 250 } 251 252 /* 253 * WP_Query 254 */ 255 256 class WP_Query { 257 var $query; 258 var $query_vars = array(); 259 var $queried_object; 260 var $queried_object_id; 261 var $request; 262 263 var $posts; 264 var $post_count = 0; 265 var $current_post = -1; 266 var $in_the_loop = false; 267 var $post; 268 269 var $found_posts = 0; 270 var $max_num_pages = 0; 271 272 var $is_single = false; 273 var $is_preview = false; 274 var $is_page = false; 275 var $is_archive = false; 276 var $is_date = false; 277 var $is_year = false; 278 var $is_month = false; 279 var $is_day = false; 280 var $is_time = false; 281 var $is_author = false; 282 var $is_category = false; 283 var $is_search = false; 284 var $is_feed = false; 285 var $is_trackback = false; 286 var $is_home = false; 287 var $is_404 = false; 288 var $is_comments_popup = false; 289 var $is_admin = false; 290 var $is_attachment = false; 291 var $is_singular = false; 292 var $is_robots = false; 293 var $is_posts_page = false; 294 295 function init_query_flags() { 296 $this->is_single = false; 297 $this->is_page = false; 298 $this->is_archive = false; 299 $this->is_date = false; 300 $this->is_year = false; 301 $this->is_month = false; 302 $this->is_day = false; 303 $this->is_time = false; 304 $this->is_author = false; 305 $this->is_category = false; 306 $this->is_search = false; 307 $this->is_feed = false; 308 $this->is_trackback = false; 309 $this->is_home = false; 310 $this->is_404 = false; 311 $this->is_paged = false; 312 $this->is_admin = false; 313 $this->is_attachment = false; 314 $this->is_singular = false; 315 $this->is_robots = false; 316 $this->is_posts_page = false; 317 } 318 319 function init () { 320 unset($this->posts); 321 unset($this->query); 322 $this->query_vars = array(); 323 unset($this->queried_object); 324 unset($this->queried_object_id); 325 $this->post_count = 0; 326 $this->current_post = -1; 327 $this->in_the_loop = false; 328 329 $this->init_query_flags(); 330 } 331 332 // Reparse the query vars. 333 function parse_query_vars() { 334 $this->parse_query(''); 335 } 336 337 function fill_query_vars($array) { 338 $keys = array( 339 'error' 340 , 'm' 341 , 'p' 342 , 'subpost' 343 , 'subpost_id' 344 , 'attachment' 345 , 'attachment_id' 346 , 'name' 347 , 'hour' 348 , 'static' 349 , 'pagename' 350 , 'page_id' 351 , 'second' 352 , 'minute' 353 , 'hour' 354 , 'day' 355 , 'monthnum' 356 , 'year' 357 , 'w' 358 , 'category_name' 359 , 'author_name' 360 , 'feed' 361 , 'tb' 362 , 'paged' 363 , 'comments_popup' 364 , 'preview' 365 ); 366 367 foreach ($keys as $key) { 368 if ( !isset($array[$key])) 369 $array[$key] = ''; 370 } 371 372 return $array; 373 } 374 375 // Parse a query string and set query type booleans. 376 function parse_query ($query) { 377 if ( !empty($query) || !isset($this->query) ) { 378 $this->init(); 379 if ( is_array($query) ) 380 $qv = & $query; 381 else 382 parse_str($query, $qv); 383 $this->query = $query; 384 $this->query_vars = $qv; 385 } 386 387 $qv = $this->fill_query_vars($qv); 388 389 if ( ! empty($qv['robots']) ) { 390 $this->is_robots = true; 391 return; 392 } 393 394 if ('404' == $qv['error']) { 395 $this->is_404 = true; 396 if ( !empty($query) ) { 397 do_action_ref_array('parse_query', array(&$this)); 398 } 399 return; 400 } 401 402 $qv['m'] = (int) $qv['m']; 403 $qv['p'] = (int) $qv['p']; 404 405 // Compat. Map subpost to attachment. 406 if ( '' != $qv['subpost'] ) 407 $qv['attachment'] = $qv['subpost']; 408 if ( '' != $qv['subpost_id'] ) 409 $qv['attachment_id'] = $qv['subpost_id']; 410 411 if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) { 412 $this->is_single = true; 413 $this->is_attachment = true; 414 } elseif ('' != $qv['name']) { 415 $this->is_single = true; 416 } elseif ( $qv['p'] ) { 417 $this->is_single = true; 418 } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) { 419 // If year, month, day, hour, minute, and second are set, a single 420 // post is being queried. 421 $this->is_single = true; 422 } elseif ('' != $qv['static'] || '' != $qv['pagename'] || (int) $qv['page_id']) { 423 $this->is_page = true; 424 $this->is_single = false; 425 } elseif (!empty($qv['s'])) { 426 $this->is_search = true; 427 } else { 428 // Look for archive queries. Dates, categories, authors. 429 430 if ( (int) $qv['second']) { 431 $this->is_time = true; 432 $this->is_date = true; 433 } 434 435 if ( (int) $qv['minute']) { 436 $this->is_time = true; 437 $this->is_date = true; 438 } 439 440 if ( (int) $qv['hour']) { 441 $this->is_time = true; 442 $this->is_date = true; 443 } 444 445 if ( (int) $qv['day']) { 446 if (! $this->is_date) { 447 $this->is_day = true; 448 $this->is_date = true; 449 } 450 } 451 452 if ( (int) $qv['monthnum']) { 453 if (! $this->is_date) { 454 $this->is_month = true; 455 $this->is_date = true; 456 } 457 } 458 459 if ( (int) $qv['year']) { 460 if (! $this->is_date) { 461 $this->is_year = true; 462 $this->is_date = true; 463 } 464 } 465 466 if ( (int) $qv['m']) { 467 $this->is_date = true; 468 if (strlen($qv['m']) > 9) { 469 $this->is_time = true; 470 } else if (strlen($qv['m']) > 7) { 471 $this->is_day = true; 472 } else if (strlen($qv['m']) > 5) { 473 $this->is_month = true; 474 } else { 475 $this->is_year = true; 476 } 477 } 478 479 if ('' != $qv['w']) { 480 $this->is_date = true; 481 } 482 483 if (empty($qv['cat']) || ($qv['cat'] == '0')) { 484 $this->is_category = false; 485 } else { 486 if (stristr($qv['cat'],'-')) { 487 $this->is_category = false; 488 } else { 489 $this->is_category = true; 490 } 491 } 492 493 if ('' != $qv['category_name']) { 494 $this->is_category = true; 495 } 496 497 if ((empty($qv['author'])) || ($qv['author'] == '0')) { 498 $this->is_author = false; 499 } else { 500 $this->is_author = true; 501 } 502 503 if ('' != $qv['author_name']) { 504 $this->is_author = true; 505 } 506 507 if ( ($this->is_date || $this->is_author || $this->is_category)) { 508 $this->is_archive = true; 509 } 510 } 511 512 if ('' != $qv['feed']) { 513 $this->is_feed = true; 514 } 515 516 if ('' != $qv['tb']) { 517 $this->is_trackback = true; 518 } 519 520 if ('' != $qv['paged']) { 521 $this->is_paged = true; 522 } 523 524 if ('' != $qv['comments_popup']) { 525 $this->is_comments_popup = true; 526 } 527 528 //if we're previewing inside the write screen 529 if ('' != $qv['preview']) { 530 $this->is_preview = true; 531 } 532 533 if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { 534 $this->is_admin = true; 535 } 536 537 if ( $this->is_single || $this->is_page || $this->is_attachment ) 538 $this->is_singular = true; 539 540 if ( ! ($this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) { 541 $this->is_home = true; 542 } 543 544 if ( !empty($query) ) { 545 do_action_ref_array('parse_query', array(&$this)); 546 } 547 } 548 549 function set_404() { 550 $is_feed = $this->is_feed; 551 552 $this->init_query_flags(); 553 $this->is_404 = true; 554 555 $this->is_feed = $is_feed; 556 } 557 558 function get($query_var) { 559 if (isset($this->query_vars[$query_var])) { 560 return $this->query_vars[$query_var]; 561 } 562 563 return ''; 564 } 565 566 function set($query_var, $value) { 567 $this->query_vars[$query_var] = $value; 568 } 569 570 function &get_posts() { 571 global $wpdb, $pagenow, $user_ID; 572 573 do_action_ref_array('pre_get_posts', array(&$this)); 574 575 // Shorthand. 576 $q = &$this->query_vars; 577 578 $q = $this->fill_query_vars($q); 579 580 // First let's clear some variables 581 $distinct = ''; 582 $whichcat = ''; 583 $whichauthor = ''; 584 $whichpage = ''; 585 $result = ''; 586 $where = ''; 587 $limits = ''; 588 $join = ''; 589 $search = ''; 590 $groupby = ''; 591 592 if ( !isset($q['post_type']) ) 593 $q['post_type'] = 'post'; 594 $post_type = $q['post_type']; 595 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) 596 $q['posts_per_page'] = get_option('posts_per_page'); 597 if ( isset($q['showposts']) && $q['showposts'] ) { 598 $q['showposts'] = (int) $q['showposts']; 599 $q['posts_per_page'] = $q['showposts']; 600 } 601 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) 602 $q['posts_per_page'] = $q['posts_per_archive_page']; 603 if ( !isset($q['nopaging']) ) { 604 if ($q['posts_per_page'] == -1) { 605 $q['nopaging'] = true; 606 } else { 607 $q['nopaging'] = false; 608 } 609 } 610 if ( $this->is_feed ) { 611 $q['posts_per_page'] = get_option('posts_per_rss'); 612 $q['nopaging'] = false; 613 } 614 $q['posts_per_page'] = (int) $q['posts_per_page']; 615 if ( $q['posts_per_page'] < -1 ) 616 $q['posts_per_page'] = abs($q['posts_per_page']); 617 else if ( $q['posts_per_page'] == 0 ) 618 $q['posts_per_page'] = 1; 619 620 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) { 621 $this->is_page = true; 622 $this->is_home = false; 623 $q['page_id'] = get_option('page_on_front'); 624 } 625 626 if (isset($q['page'])) { 627 $q['page'] = trim($q['page'], '/'); 628 $q['page'] = (int) $q['page']; 629 $q['page'] = abs($q['page']); 630 } 631 632 $add_hours = intval(get_option('gmt_offset')); 633 $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours)); 634 $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; 635 636 // If a month is specified in the querystring, load that month 637 if ( (int) $q['m'] ) { 638 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); 639 $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); 640 if (strlen($q['m'])>5) 641 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); 642 if (strlen($q['m'])>7) 643 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); 644 if (strlen($q['m'])>9) 645 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); 646 if (strlen($q['m'])>11) 647 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); 648 if (strlen($q['m'])>13) 649 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); 650 } 651 652 if ( (int) $q['hour'] ) { 653 $q['hour'] = '' . intval($q['hour']); 654 $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; 655 } 656 657 if ( (int) $q['minute'] ) { 658 $q['minute'] = '' . intval($q['minute']); 659 $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; 660 } 661 662 if ( (int) $q['second'] ) { 663 $q['second'] = '' . intval($q['second']); 664 $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; 665 } 666 667 if ( (int) $q['year'] ) { 668 $q['year'] = '' . intval($q['year']); 669 $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; 670 } 671 672 if ( (int) $q['monthnum'] ) { 673 $q['monthnum'] = '' . intval($q['monthnum']); 674 $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; 675 } 676 677 if ( (int) $q['day'] ) { 678 $q['day'] = '' . intval($q['day']); 679 $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; 680 } 681 682 // Compat. Map subpost to attachment. 683 if ( '' != $q['subpost'] ) 684 $q['attachment'] = $q['subpost']; 685 if ( '' != $q['subpost_id'] ) 686 $q['attachment_id'] = $q['subpost_id']; 687 688 if ('' != $q['name']) { 689 $q['name'] = sanitize_title($q['name']); 690 $where .= " AND post_name = '" . $q['name'] . "'"; 691 } else if ('' != $q['pagename']) { 692 $reqpage = get_page_by_path($q['pagename']); 693 if ( !empty($reqpage) ) 694 $reqpage = $reqpage->ID; 695 else 696 $reqpage = 0; 697 698 if ( ('page' == get_option('show_on_front') ) && ( $reqpage == get_option('page_for_posts') ) ) { 699 $this->is_singular = false; 700 $this->is_page = false; 701 $this->is_home = true; 702 $this->is_posts_page = true; 703 } else { 704 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); 705 $page_paths = '/' . trim($q['pagename'], '/'); 706 $q['pagename'] = sanitize_title(basename($page_paths)); 707 $q['name'] = $q['pagename']; 708 $where .= " AND (ID = '$reqpage')"; 709 } 710 } elseif ('' != $q['attachment']) { 711 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); 712 $attach_paths = '/' . trim($q['attachment'], '/'); 713 $q['attachment'] = sanitize_title(basename($attach_paths)); 714 $q['name'] = $q['attachment']; 715 $where .= " AND post_name = '" . $q['attachment'] . "'"; 716 } 717 718 if ( (int) $q['w'] ) { 719 $q['w'] = ''.intval($q['w']); 720 $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; 721 } 722 723 if ( intval($q['comments_popup']) ) 724 $q['p'] = intval($q['comments_popup']); 725 726 // If a attachment is requested by number, let it supercede any post number. 727 if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) ) 728 $q['p'] = (int) $q['attachment_id']; 729 730 // If a post number is specified, load that post 731 if (($q['p'] != '') && intval($q['p']) != 0) { 732 $q['p'] = (int) $q['p']; 733 $where = ' AND ID = ' . $q['p']; 734 } 735 736 if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) { 737 $q['page_id'] = intval($q['page_id']); 738 if ( ('page' == get_option('show_on_front') ) && ( $q['page_id'] == get_option('page_for_posts') ) ) { 739 $this->is_singular = false; 740 $this->is_page = false; 741 $this->is_home = true; 742 $this->is_posts_page = true; 743 } else { 744 $q['p'] = $q['page_id']; 745 $where = ' AND ID = '.$q['page_id']; 746 } 747 } 748 749 // If a search pattern is specified, load the posts that match 750 if (!empty($q['s'])) { 751 // added slashes screw with quote grouping when done early, so done later 752 $q['s'] = stripslashes($q['s']); 753 if ($q['sentence']) { 754 $q['search_terms'] = array($q['s']); 755 } 756 else { 757 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches); 758 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]); 759 } 760 $n = ($q['exact']) ? '' : '%'; 761 $searchand = ''; 762 foreach((array)$q['search_terms'] as $term) { 763 $term = addslashes_gpc($term); 764 $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))"; 765 $searchand = ' AND '; 766 } 767 $term = addslashes_gpc($q['s']); 768 if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ) 769 $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')"; 770 771 if ( !empty($search) ) 772 $search = " AND ({$search}) "; 773 } 774 775 // Category stuff 776 777 if ((empty($q['cat'])) || ($q['cat'] == '0') || 778 // Bypass cat checks if fetching specific posts 779 ( $this->is_single || $this->is_page )) { 780 $whichcat=''; 781 } else { 782 $q['cat'] = ''.urldecode($q['cat']).''; 783 $q['cat'] = addslashes_gpc($q['cat']); 784 $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) "; 785 $cat_array = preg_split('/[,\s]+/', $q['cat']); 786 $in_cats = $out_cats = $out_posts = ''; 787 foreach ( $cat_array as $cat ) { 788 $cat = intval($cat); 789 $in = strstr($cat, '-') ? false : true; 790 $cat = trim($cat, '-'); 791 if ( $in ) 792 $in_cats .= "$cat, " . get_category_children($cat, '', ', '); 793 else 794 $out_cats .= "$cat, " . get_category_children($cat, '', ', '); 795 } 796 $in_cats = substr($in_cats, 0, -2); 797 $out_cats = substr($out_cats, 0, -2); 798 if ( strlen($in_cats) > 0 ) 799 $in_cats = " AND $wpdb->post2cat.category_id IN ($in_cats)"; 800 if ( strlen($out_cats) > 0 ) { 801 $ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE $wpdb->post2cat.category_id IN ($out_cats)"); 802 if ( is_array($ids) && count($ids > 0) ) { 803 foreach ( $ids as $id ) 804 $out_posts .= "$id, "; 805 $out_posts = substr($out_posts, 0, -2); 806 } 807 if ( strlen($out_posts) > 0 ) 808 $out_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)"; 809 else 810 $out_cats = ''; 811 } 812 $whichcat = $in_cats . $out_cats; 813 $groupby = "{$wpdb->posts}.ID"; 814 } 815 816 // Category stuff for nice URLs 817 if ('' != $q['category_name']) { 818 $reqcat = get_category_by_path($q['category_name']); 819 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name']))); 820 $cat_paths = '/' . trim($q['category_name'], '/'); 821 $q['category_name'] = sanitize_title(basename($cat_paths)); 822 823 $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); 824 $q['category_name'] = sanitize_title(basename($cat_paths)); 825 $cat_paths = explode('/', $cat_paths); 826 $cat_path = ''; 827 foreach ( (array) $cat_paths as $pathdir ) 828 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); 829 830 //if we don't match the entire hierarchy fallback on just matching the nicename 831 if ( empty($reqcat) ) 832 $reqcat = get_category_by_path($q['category_name'], false); 833 834 if ( !empty($reqcat) ) 835 $reqcat = $reqcat->cat_ID; 836 else 837 $reqcat = 0; 838 839 $q['cat'] = $reqcat; 840 841 $tables = ", $wpdb->post2cat, $wpdb->categories"; 842 $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) "; 843 $whichcat = " AND category_id IN ({$q['cat']}, "; 844 $whichcat .= get_category_children($q['cat'], '', ', '); 845 $whichcat = substr($whichcat, 0, -2); 846 $whichcat .= ")"; 847 $groupby = "{$wpdb->posts}.ID"; 848 } 849 850 // Author/user stuff 851 852 if ((empty($q['author'])) || ($q['author'] == '0')) { 853 $whichauthor=''; 854 } else { 855 $q['author'] = ''.urldecode($q['author']).''; 856 $q['author'] = addslashes_gpc($q['author']); 857 if (stristr($q['author'], '-')) { 858 $eq = '!='; 859 $andor = 'AND'; 860 $q['author'] = explode('-', $q['author']); 861 $q['author'] = ''.intval($q['author'][1]); 862 } else { 863 $eq = '='; 864 $andor = 'OR'; 865 } 866 $author_array = preg_split('/[,\s]+/', $q['author']); 867 $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]); 868 for ($i = 1; $i < (count($author_array)); $i = $i + 1) { 869 $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); 870 } 871 $whichauthor .= ')'; 872 } 873 874 // Author stuff for nice URLs 875 876 if ('' != $q['author_name']) { 877 if (stristr($q['author_name'],'/')) { 878 $q['author_name'] = explode('/',$q['author_name']); 879 if ($q['author_name'][count($q['author_name'])-1]) { 880 $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash 881 } else { 882 $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash 883 } 884 } 885 $q['author_name'] = sanitize_title($q['author_name']); 886 $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'"); 887 $whichauthor .= ' AND (post_author = '.intval($q['author']).')'; 888 } 889 890 $where .= $search.$whichcat.$whichauthor; 891 892 if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) { 893 $q['order']='DESC'; 894 } 895 896 // Order by 897 if (empty($q['orderby'])) { 898 $q['orderby'] = 'post_date '.$q['order']; 899 } else { 900 // Used to filter values 901 $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order'); 902 $q['orderby'] = urldecode($q['orderby']); 903 $q['orderby'] = addslashes_gpc($q['orderby']); 904 $orderby_array = explode(' ',$q['orderby']); 905 if ( empty($orderby_array) ) 906 $orderby_array[] = $q['orderby']; 907 $q['orderby'] = ''; 908 for ($i = 0; $i < count($orderby_array); $i++) { 909 // Only allow certain values for safety 910 $orderby = $orderby_array[$i]; 911 if ( 'menu_order' != $orderby ) 912 $orderby = 'post_' . $orderby; 913 if ( in_array($orderby_array[$i], $allowed_keys) ) 914 $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}"; 915 } 916 if ( empty($q['orderby']) ) 917 $q['orderby'] = 'post_date '.$q['order']; 918 } 919 920 if ( $this->is_attachment ) { 921 $where .= " AND (post_type = 'attachment')"; 922 } elseif ($this->is_page) { 923 $where .= " AND (post_type = 'page')"; 924 } elseif ($this->is_single) { 925 $where .= " AND (post_type = 'post')"; 926 } else { 927 $where .= " AND (post_type = '$post_type' AND (post_status = 'publish'"; 928 929 if ( is_admin() ) 930 $where .= " OR post_status = 'future' OR post_status = 'draft'"; 931 932 if ( is_user_logged_in() ) { 933 if ( 'post' == $post_type ) 934 $cap = 'edit_private_posts'; 935 else 936 $cap = 'edit_private_pages'; 937 938 if ( current_user_can($cap) ) 939 $where .= " OR post_status = 'private'"; 940 else 941 $where .= " OR post_author = $user_ID AND post_status = 'private'"; 942 } 943 944 $where .= '))'; 945 } 946 947 // Apply filters on where and join prior to paging so that any 948 // manipulations to them are reflected in the paging by day queries. 949 $where = apply_filters('posts_where', $where); 950 $join = apply_filters('posts_join', $join); 951 952 // Paging 953 if (empty($q['nopaging']) && !$this->is_singular) { 954 $page = abs(intval($q['paged'])); 955 if (empty($page)) { 956 $page = 1; 957 } 958 959 if ( empty($q['offset']) ) { 960 $pgstrt = ''; 961 $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', '; 962 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; 963 } else { // we're ignoring $page and using 'offset' 964 $q['offset'] = abs(intval($q['offset'])); 965 $pgstrt = $q['offset'] . ', '; 966 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; 967 } 968 } 969 970 // Apply post-paging filters on where and join. Only plugins that 971 // manipulate paging queries should use these hooks. 972 $where = apply_filters('posts_where_paged', $where); 973 $groupby = apply_filters('posts_groupby', $groupby); 974 if ( ! empty($groupby) ) 975 $groupby = 'GROUP BY ' . $groupby; 976 $join = apply_filters('posts_join_paged', $join); 977 $orderby = apply_filters('posts_orderby', $q['orderby']); 978 $distinct = apply_filters('posts_distinct', $distinct); 979 $fields = apply_filters('posts_fields', "$wpdb->posts.*"); 980 $limits = apply_filters( 'post_limits', $limits ); 981 $found_rows = ''; 982 if ( !empty($limits) ) 983 $found_rows = 'SQL_CALC_FOUND_ROWS'; 984 985 $request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby ORDER BY $orderby $limits"; 986 $this->request = apply_filters('posts_request', $request); 987 988 $this->posts = $wpdb->get_results($this->request); 989 if ( !empty($limits) ) { 990 $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' ); 991 $this->found_posts = $wpdb->get_var( $found_posts_query ); 992 $this->found_posts = apply_filters( 'found_posts', $this->found_posts ); 993 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 994 } 995 // Check post status to determine if post should be displayed. 996 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { 997 $status = get_post_status($this->posts[0]); 998 //$type = get_post_type($this->posts[0]); 999 if ( ('publish' != $status) ) { 1000 if ( ! is_user_logged_in() ) { 1001 // User must be logged in to view unpublished posts. 1002 $this->posts = array(); 1003 } else { 1004 if ('draft' == $status) { 1005 // User must have edit permissions on the draft to preview. 1006 if (! current_user_can('edit_post', $this->posts[0]->ID)) { 1007 $this->posts = array(); 1008 } else { 1009 $this->is_preview = true; 1010 $this->posts[0]->post_date = current_time('mysql'); 1011 } 1012 } else if ('future' == $status) { 1013 $this->is_preview = true; 1014 if (!current_user_can('edit_post', $this->posts[0]->ID)) { 1015 $this->posts = array ( ); 1016 } 1017 } else { 1018 if (! current_user_can('read_post', $this->posts[0]->ID)) 1019 $this->posts = array(); 1020 } 1021 } 1022 } 1023 } 1024 1025 $this->posts = apply_filters('the_posts', $this->posts); 1026 1027 update_post_caches($this->posts); 1028 1029 $this->post_count = count($this->posts); 1030 if ($this->post_count > 0) { 1031 $this->post = $this->posts[0]; 1032 } 1033 1034 return $this->posts; 1035 } 1036 1037 function next_post() { 1038 1039 $this->current_post++; 1040 1041 $this->post = $this->posts[$this->current_post]; 1042 return $this->post; 1043 } 1044 1045 function the_post() { 1046 global $post; 1047 $this->in_the_loop = true; 1048 $post = $this->next_post(); 1049 setup_postdata($post); 1050 1051 if ( $this->current_post == 0 ) // loop has just started 1052 do_action('loop_start'); 1053 } 1054 1055 function have_posts() { 1056 if ($this->current_post + 1 < $this->post_count) { 1057 return true; 1058 } elseif ($this->current_post + 1 == $this->post_count) { 1059 do_action('loop_end'); 1060 // Do some cleaning up after the loop 1061 $this->rewind_posts(); 1062 } 1063 1064 $this->in_the_loop = false; 1065 return false; 1066 } 1067 1068 function rewind_posts() { 1069 $this->current_post = -1; 1070 if ($this->post_count > 0) { 1071 $this->post = $this->posts[0]; 1072 } 1073 } 1074 1075 function &query($query) { 1076 $this->parse_query($query); 1077 return $this->get_posts(); 1078 } 1079 1080 function get_queried_object() { 1081 if (isset($this->queried_object)) { 1082 return $this->queried_object; 1083 } 1084 1085 $this->queried_object = NULL; 1086 $this->queried_object_id = 0; 1087 1088 if ($this->is_category) { 1089 $cat = $this->get('cat'); 1090 $category = &get_category($cat); 1091 $this->queried_object = &$category; 1092 $this->queried_object_id = $cat; 1093 } else if ($this->is_posts_page) { 1094 $this->queried_object = & get_page(get_option('page_for_posts')); 1095 $this->queried_object_id = $this->queried_object->ID; 1096 } else if ($this->is_single) { 1097 $this->queried_object = $this->post; 1098 $this->queried_object_id = $this->post->ID; 1099 } else if ($this->is_page) { 1100 $this->queried_object = $this->post; 1101 $this->queried_object_id = $this->post->ID; 1102 } else if ($this->is_author) { 1103 $author_id = $this->get('author'); 1104 $author = get_userdata($author_id); 1105 $this->queried_object = $author; 1106 $this->queried_object_id = $author_id; 1107 } 1108 1109 return $this->queried_object; 1110 } 1111 1112 function get_queried_object_id() { 1113 $this->get_queried_object(); 1114 1115 if (isset($this->queried_object_id)) { 1116 return $this->queried_object_id; 1117 } 1118 1119 return 0; 1120 } 1121 1122 function WP_Query ($query = '') { 1123 if (! empty($query)) { 1124 $this->query($query); 1125 } 1126 } 1127 } 1128 1129 1130 // Redirect old slugs 1131 function wp_old_slug_redirect () { 1132 global $wp_query; 1133 if ( is_404() && '' != $wp_query->query_vars['name'] ) : 1134 global $wpdb; 1135 1136 $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'"; 1137 1138 // if year, monthnum, or day have been specified, make our query more precise 1139 // just in case there are multiple identical _wp_old_slug values 1140 if ( '' != $wp_query->query_vars['year'] ) 1141 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'"; 1142 if ( '' != $wp_query->query_vars['monthnum'] ) 1143 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'"; 1144 if ( '' != $wp_query->query_vars['day'] ) 1145 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'"; 1146 1147 $id = (int) $wpdb->get_var($query); 1148 1149 if ( !$id ) 1150 return; 1151 1152 $link = get_permalink($id); 1153 1154 if ( !$link ) 1155 return; 1156 1157 wp_redirect($link, '301'); // Permanent redirect 1158 exit; 1159 endif; 1160 } 1161 1162 1163 // 1164 // Private helper functions 1165 // 1166 1167 // Setup global post data. 1168 function setup_postdata($post) { 1169 global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query; 1170 global $pagenow; 1171 1172 $id = $post->ID; 1173 1174 $authordata = get_userdata($post->post_author); 1175 1176 $day = mysql2date('d.m.y', $post->post_date); 1177 $currentmonth = mysql2date('m', $post->post_date); 1178 $numpages = 1; 1179 $page = get_query_var('page'); 1180 if ( !$page ) 1181 $page = 1; 1182 if ( is_single() || is_page() ) 1183 $more = 1; 1184 $content = $post->post_content; 1185 if ( preg_match('/<!--nextpage-->/', $content) ) { 1186 if ( $page > 1 ) 1187 $more = 1; 1188 $multipage = 1; 1189 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content); 1190 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content); 1191 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content); 1192 $pages = explode('<!--nextpage-->', $content); 1193 $numpages = count($pages); 1194 } else { 1195 $pages[0] = $post->post_content; 1196 $multipage = 0; 1197 } 1198 return true; 1199 } 1200 1201 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |