Changeset 34542
- Timestamp:
- 09/25/2015 02:08:50 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-comment-query.php
r34411 r34542 43 43 */ 44 44 protected $meta_query_clauses; 45 46 /** 47 * SQL query clauses. 48 * 49 * @since 4.4.0 50 * @access protected 51 * @var array 52 */ 53 protected $sql_clauses = array( 54 'select' => '', 55 'from' => '', 56 'where' => array(), 57 'groupby' => '', 58 'orderby' => '', 59 'limits' => '', 60 ); 45 61 46 62 /** … … 358 374 global $wpdb; 359 375 360 $groupby = '';361 $where = array();362 363 376 // Assemble clauses related to 'comment_approved'. 364 377 $approved_clauses = array(); … … 424 437 if ( ! empty( $approved_clauses ) ) { 425 438 if ( 1 === count( $approved_clauses ) ) { 426 $ where[] = $approved_clauses[0];439 $this->sql_clauses['where']['approved'] = $approved_clauses[0]; 427 440 } else { 428 $ where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';441 $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 429 442 } 430 443 } … … 525 538 $limits = 'LIMIT ' . $number; 526 539 } 527 } else {528 $limits = '';529 540 } 530 541 … … 535 546 } 536 547 537 $join = '';538 539 548 $post_id = absint( $this->query_vars['post_id'] ); 540 549 if ( ! empty( $post_id ) ) { 541 $ where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );550 $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); 542 551 } 543 552 544 553 // Parse comment IDs for an IN clause. 545 554 if ( ! empty( $this->query_vars['comment__in'] ) ) { 546 $ where[] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';555 $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 547 556 } 548 557 549 558 // Parse comment IDs for a NOT IN clause. 550 559 if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 551 $ where[] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';560 $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 552 561 } 553 562 554 563 // Parse comment parent IDs for an IN clause. 555 564 if ( ! empty( $this->query_vars['parent__in'] ) ) { 556 $ where[] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )';565 $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )'; 557 566 } 558 567 559 568 // Parse comment parent IDs for a NOT IN clause. 560 569 if ( ! empty( $this->query_vars['parent__not_in'] ) ) { 561 $ where[] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )';570 $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )'; 562 571 } 563 572 564 573 // Parse comment post IDs for an IN clause. 565 574 if ( ! empty( $this->query_vars['post__in'] ) ) { 566 $ where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';575 $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; 567 576 } 568 577 569 578 // Parse comment post IDs for a NOT IN clause. 570 579 if ( ! empty( $this->query_vars['post__not_in'] ) ) { 571 $ where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';580 $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; 572 581 } 573 582 574 583 if ( '' !== $this->query_vars['author_email'] ) { 575 $ where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );584 $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); 576 585 } 577 586 578 587 if ( '' !== $this->query_vars['karma'] ) { 579 $ where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );588 $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); 580 589 } 581 590 … … 615 624 if ( ! empty( $comment_types[ $operator ] ) ) { 616 625 $types_sql = implode( ', ', $comment_types[ $operator ] ); 617 $ where[] = "comment_type $operator ($types_sql)";626 $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)"; 618 627 } 619 628 } 620 629 621 630 if ( '' !== $this->query_vars['parent'] ) { 622 $ where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );631 $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] ); 623 632 } 624 633 625 634 if ( is_array( $this->query_vars['user_id'] ) ) { 626 $ where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';635 $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 627 636 } elseif ( '' !== $this->query_vars['user_id'] ) { 628 $ where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );637 $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); 629 638 } 630 639 … … 636 645 637 646 // Strip leading 'AND'. 638 $ where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );647 $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql ); 639 648 } 640 649 … … 649 658 // $field_value may be an array. 650 659 $esses = array_fill( 0, count( (array) $field_value ), '%s' ); 651 $ where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );660 $this->sql_clauses['where']['post_fields'] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); 652 661 } 653 662 } … … 655 664 // Comment author IDs for an IN clause. 656 665 if ( ! empty( $this->query_vars['author__in'] ) ) { 657 $ where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';666 $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; 658 667 } 659 668 660 669 // Comment author IDs for a NOT IN clause. 661 670 if ( ! empty( $this->query_vars['author__not_in'] ) ) { 662 $ where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';671 $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; 663 672 } 664 673 … … 666 675 if ( ! empty( $this->query_vars['post_author__in'] ) ) { 667 676 $join_posts_table = true; 668 $ where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';677 $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; 669 678 } 670 679 … … 672 681 if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { 673 682 $join_posts_table = true; 674 $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 675 } 683 $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 684 } 685 686 $join = ''; 676 687 677 688 if ( $join_posts_table ) { 678 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";689 $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; 679 690 } 680 691 … … 683 694 684 695 // Strip leading 'AND'. 685 $ where[] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );696 $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); 686 697 687 698 if ( ! $this->query_vars['count'] ) { … … 693 704 if ( ! empty( $date_query ) && is_array( $date_query ) ) { 694 705 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); 695 $ where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );696 } 697 698 $where = implode( ' AND ', $ where);706 $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() ); 707 } 708 709 $where = implode( ' AND ', $this->sql_clauses['where'] ); 699 710 700 711 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); … … 728 739 } 729 740 730 $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits"; 741 $this->sql_clauses['select'] = "SELECT $fields"; 742 $this->sql_clauses['from'] = "FROM $wpdb->comments $join"; 743 $this->sql_clauses['groupby'] = $groupby; 744 $this->sql_clauses['orderby'] = $orderby; 745 $this->sql_clauses['limits'] = $limits; 746 747 $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 731 748 732 749 if ( $this->query_vars['count'] ) {
Note: See TracChangeset
for help on using the changeset viewer.