Changeset 30084
- Timestamp:
- 10/29/2014 02:21:10 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment.php
r30026 r30084 303 303 'post_status' => '', 304 304 'post_type' => '', 305 'status' => ' ',305 'status' => '', 306 306 'type' => '', 307 307 'user_id' => '', … … 344 344 } 345 345 346 347 346 348 // Assemble clauses related to 'comment_approved'. 347 349 $approved_clauses = array(); 348 $status = $this->query_vars['status']; 349 if ( 'hold' == $status ) { 350 $approved_clauses[] = "comment_approved = '0'"; 351 } elseif ( 'approve' == $status ) { 352 $approved_clauses[] = "comment_approved = '1'"; 353 } elseif ( ! empty( $status ) && 'all' != $status ) { 354 $approved_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 355 } else { 356 $approved_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 350 351 // 'status' accepts an array or a comma-separated string. 352 $status_clauses = array(); 353 $statuses = $this->query_vars['status']; 354 if ( ! is_array( $statuses ) ) { 355 $statuses = preg_split( '/[\s,]+/', $statuses ); 356 } 357 358 // Remove empty statuses. 359 $statuses = array_filter( $statuses ); 360 361 // 'any' overrides other statuses. 362 if ( ! in_array( 'any', $statuses ) ) { 363 foreach ( $statuses as $status ) { 364 switch ( $status ) { 365 case 'hold' : 366 $status_clauses[] = "comment_approved = '0'"; 367 break; 368 369 case 'approve' : 370 $status_clauses[] = "comment_approved = '1'"; 371 break; 372 373 case 'all' : 374 $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 375 break; 376 377 default : 378 $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 379 break; 380 } 381 } 382 383 if ( ! empty( $status_clauses ) ) { 384 $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; 385 } 357 386 } 358 387 … … 380 409 381 410 // Collapse comment_approved clauses into a single OR-separated clause. 382 if ( 1 === count( $approved_clauses ) ) { 383 $approved = $approved_clauses[0]; 384 } else { 385 $approved = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 411 if ( ! empty( $approved_clauses ) ) { 412 if ( 1 === count( $approved_clauses ) ) { 413 $where[] = $approved_clauses[0]; 414 } else { 415 $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 416 } 386 417 } 387 418 … … 458 489 459 490 $join = ''; 460 $where = $approved;461 491 462 492 $post_id = absint( $this->query_vars['post_id'] ); 463 493 if ( ! empty( $post_id ) ) { 464 $where .= $wpdb->prepare( ' ANDcomment_post_ID = %d', $post_id );494 $wherecomment_post_ID = %d', $post_id ); 465 495 } 466 496 467 497 // Parse comment IDs for an IN clause. 468 498 if ( ! empty( $this->query_vars['comment__in'] ) ) { 469 $where .= ' ANDcomment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';499 $wherecomment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 470 500 } 471 501 472 502 // Parse comment IDs for a NOT IN clause. 473 503 if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 474 $where .= ' ANDcomment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';504 $wherecomment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 475 505 } 476 506 477 507 // Parse comment post IDs for an IN clause. 478 508 if ( ! empty( $this->query_vars['post__in'] ) ) { 479 $where .= ' ANDcomment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';509 $wherecomment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; 480 510 } 481 511 482 512 // Parse comment post IDs for a NOT IN clause. 483 513 if ( ! empty( $this->query_vars['post__not_in'] ) ) { 484 $where .= ' ANDcomment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';514 $wherecomment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; 485 515 } 486 516 487 517 if ( '' !== $this->query_vars['author_email'] ) { 488 $where .= $wpdb->prepare( ' ANDcomment_author_email = %s', $this->query_vars['author_email'] );518 $wherecomment_author_email = %s', $this->query_vars['author_email'] ); 489 519 } 490 520 491 521 if ( '' !== $this->query_vars['karma'] ) { 492 $where .= $wpdb->prepare( ' ANDcomment_karma = %d', $this->query_vars['karma'] );522 $wherecomment_karma = %d', $this->query_vars['karma'] ); 493 523 } 494 524 495 525 if ( 'comment' == $this->query_vars['type'] ) { 496 $where .= " ANDcomment_type = ''";526 $wherecomment_type = ''"; 497 527 } elseif( 'pings' == $this->query_vars['type'] ) { 498 $where .= ' ANDcomment_type IN ("pingback", "trackback")';528 $wherecomment_type IN ("pingback", "trackback")'; 499 529 } elseif ( ! empty( $this->query_vars['type'] ) ) { 500 $where .= $wpdb->prepare( ' ANDcomment_type = %s', $this->query_vars['type'] );530 $wherecomment_type = %s', $this->query_vars['type'] ); 501 531 } 502 532 503 533 if ( '' !== $this->query_vars['parent'] ) { 504 $where .= $wpdb->prepare( ' ANDcomment_parent = %d', $this->query_vars['parent'] );534 $wherecomment_parent = %d', $this->query_vars['parent'] ); 505 535 } 506 536 507 537 if ( is_array( $this->query_vars['user_id'] ) ) { 508 $where .= ' ANDuser_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';538 $whereuser_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 509 539 } elseif ( '' !== $this->query_vars['user_id'] ) { 510 $where .= $wpdb->prepare( ' ANDuser_id = %d', $this->query_vars['user_id'] );540 $whereuser_id = %d', $this->query_vars['user_id'] ); 511 541 } 512 542 513 543 if ( '' !== $this->query_vars['search'] ) { 514 $ where .= $this->get_search_sql(544 $= $this->get_search_sql( 515 545 $this->query_vars['search'], 516 546 array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) 517 547 ); 548 549 550 518 551 } 519 552 … … 526 559 $join_posts_table = true; 527 560 foreach ( $post_fields as $field_name => $field_value ) { 528 $where .= $wpdb->prepare( " AND{$wpdb->posts}.{$field_name} = %s", $field_value );561 $where {$wpdb->posts}.{$field_name} = %s", $field_value ); 529 562 } 530 563 } … … 532 565 // Comment author IDs for an IN clause. 533 566 if ( ! empty( $this->query_vars['author__in'] ) ) { 534 $where .= ' ANDuser_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';567 $whereuser_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; 535 568 } 536 569 537 570 // Comment author IDs for a NOT IN clause. 538 571 if ( ! empty( $this->query_vars['author__not_in'] ) ) { 539 $where .= ' ANDuser_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';572 $whereuser_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; 540 573 } 541 574 … … 543 576 if ( ! empty( $this->query_vars['post_author__in'] ) ) { 544 577 $join_posts_table = true; 545 $where .= ' ANDpost_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';578 $wherepost_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; 546 579 } 547 580 … … 549 582 if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { 550 583 $join_posts_table = true; 551 $where .= ' ANDpost_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';584 $wherepost_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 552 585 } 553 586 … … 559 592 $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 560 593 $join .= $clauses['join']; 561 $where .= $clauses['where']; 594 595 // Strip leading 'AND'. 596 $where[] = preg_replace( '/^\s*AND\s*/', '', $clauses['where'] ); 562 597 563 598 if ( ! $this->query_vars['count'] ) { … … 569 604 if ( ! empty( $date_query ) && is_array( $date_query ) ) { 570 605 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); 571 $where .= $date_query_object->get_sql(); 572 } 606 $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() ); 607 } 608 609 $where = implode( ' AND ', $where ); 573 610 574 611 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' ); … … 591 628 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 592 629 630 631 632 633 593 634 if ( $groupby ) { 594 635 $groupby = 'GROUP BY ' . $groupby; … … 599 640 } 600 641 601 $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE$where $groupby $orderby $limits";642 $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits"; 602 643 603 644 if ( $this->query_vars['count'] ) { -
trunk/tests/phpunit/tests/comment/query.php
r30026 r30084 74 74 75 75 $this->assertEqualSets( array( $c1, $c3 ), $found ); 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 76 140 } 77 141
Note: See TracChangeset
for help on using the changeset viewer.