Changeset 38275 for trunk/src/wp-includes/class-wp-comment-query.php
- Timestamp:
- 08/18/2016 06:20:55 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-comment-query.php
r38121 r38275 141 141 return false; 142 142 } 143 144 /** 145 * @since 4.7.0 146 * @access protected 147 * @var wpdb 148 */ 149 protected $db; 143 150 144 151 /** … … 261 268 */ 262 269 public function __construct( $query = '' ) { 270 $this->db = $GLOBALS['wpdb']; 271 263 272 $this->query_var_defaults = array( 264 273 'author_email' => '', … … 364 373 * @access public 365 374 * 366 * @global wpdb $wpdb WordPress database abstraction object.367 *368 375 * @return int|array List of comments or number of found comments if `$count` argument is true. 369 376 */ 370 377 public function get_comments() { 371 global $wpdb;372 373 378 $this->parse_query(); 374 379 … … 389 394 $this->meta_query->parse_query_vars( $this->query_vars ); 390 395 if ( ! empty( $this->meta_query->queries ) ) { 391 $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $ wpdb->comments, 'comment_ID', $this );396 $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this ); 392 397 } 393 398 … … 481 486 * @since 4.4.0 482 487 * @access protected 483 *484 * @global wpdb $wpdb WordPress database abstraction object.485 488 */ 486 489 protected function get_comment_ids() { 487 global $wpdb;488 489 490 // Assemble clauses related to 'comment_approved'. 490 491 $approved_clauses = array(); … … 515 516 516 517 default : 517 $status_clauses[] = $ wpdb->prepare( "comment_approved = %s", $status );518 $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status ); 518 519 break; 519 520 } … … 538 539 // Numeric values are assumed to be user ids. 539 540 if ( is_numeric( $unapproved_identifier ) ) { 540 $approved_clauses[] = $ wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );541 $approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); 541 542 542 543 // Otherwise we match against email addresses. 543 544 } else { 544 $approved_clauses[] = $ wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );545 $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); 545 546 } 546 547 } … … 601 602 // If no valid clauses were found, order by comment_date_gmt. 602 603 if ( empty( $orderby_array ) ) { 603 $orderby_array[] = " $wpdb->comments.comment_date_gmt $order";604 $orderby_array[] = "{$this->db->comments}.comment_date_gmt $order"; 604 605 } 605 606 … … 634 635 } 635 636 636 $orderby_array[] = " $wpdb->comments.comment_ID $comment_ID_order";637 $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order"; 637 638 } 638 639 639 640 $orderby = implode( ', ', $orderby_array ); 640 641 } else { 641 $orderby = " $wpdb->comments.comment_date_gmt $order";642 $orderby = "{$this->db->comments}.comment_date_gmt $order"; 642 643 } 643 644 … … 656 657 $fields = 'COUNT(*)'; 657 658 } else { 658 $fields = " $wpdb->comments.comment_ID";659 $fields = "{$this->db->comments}.comment_ID"; 659 660 } 660 661 661 662 $post_id = absint( $this->query_vars['post_id'] ); 662 663 if ( ! empty( $post_id ) ) { 663 $this->sql_clauses['where']['post_id'] = $ wpdb->prepare( 'comment_post_ID = %d', $post_id );664 $this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id ); 664 665 } 665 666 666 667 // Parse comment IDs for an IN clause. 667 668 if ( ! empty( $this->query_vars['comment__in'] ) ) { 668 $this->sql_clauses['where']['comment__in'] = " $wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';669 $this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 669 670 } 670 671 671 672 // Parse comment IDs for a NOT IN clause. 672 673 if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 673 $this->sql_clauses['where']['comment__not_in'] = " $wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';674 $this->sql_clauses['where']['comment__not_in'] = "{$this->db->comments}.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 674 675 } 675 676 … … 695 696 696 697 if ( '' !== $this->query_vars['author_email'] ) { 697 $this->sql_clauses['where']['author_email'] = $ wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );698 $this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); 698 699 } 699 700 700 701 if ( '' !== $this->query_vars['author_url'] ) { 701 $this->sql_clauses['where']['author_url'] = $ wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );702 $this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] ); 702 703 } 703 704 704 705 if ( '' !== $this->query_vars['karma'] ) { 705 $this->sql_clauses['where']['karma'] = $ wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );706 $this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); 706 707 } 707 708 … … 734 735 735 736 default: 736 $comment_types[ $operator ][] = $ wpdb->prepare( '%s', $type );737 $comment_types[ $operator ][] = $this->db->prepare( '%s', $type ); 737 738 break; 738 739 } … … 751 752 752 753 if ( '' !== $parent ) { 753 $this->sql_clauses['where']['parent'] = $ wpdb->prepare( 'comment_parent = %d', $parent );754 $this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent ); 754 755 } 755 756 … … 757 758 $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 758 759 } elseif ( '' !== $this->query_vars['user_id'] ) { 759 $this->sql_clauses['where']['user_id'] = $ wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );760 $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] ); 760 761 } 761 762 … … 781 782 // $field_value may be an array. 782 783 $esses = array_fill( 0, count( (array) $field_value ), '%s' ); 783 $this->sql_clauses['where'][ $field_name ] = $ wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );784 $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); 784 785 } 785 786 } … … 802 803 803 804 $esses = array_fill( 0, count( $q_values ), '%s' ); 804 $this->sql_clauses['where'][ $field_name ] = $ wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );805 $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values ); 805 806 } 806 807 } … … 831 832 832 833 if ( $join_posts_table ) { 833 $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";834 $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID"; 834 835 } 835 836 … … 841 842 842 843 if ( ! $this->query_vars['count'] ) { 843 $groupby = "{$ wpdb->comments}.comment_ID";844 $groupby = "{$this->db->comments}.comment_ID"; 844 845 } 845 846 } … … 890 891 891 892 $this->sql_clauses['select'] = "SELECT $found_rows $fields"; 892 $this->sql_clauses['from'] = "FROM $wpdb->comments$join";893 $this->sql_clauses['from'] = "FROM {$this->db->comments} $join"; 893 894 $this->sql_clauses['groupby'] = $groupby; 894 895 $this->sql_clauses['orderby'] = $orderby; … … 898 899 899 900 if ( $this->query_vars['count'] ) { 900 return intval( $ wpdb->get_var( $this->request ) );901 return intval( $this->db->get_var( $this->request ) ); 901 902 } else { 902 $comment_ids = $ wpdb->get_col( $this->request );903 $comment_ids = $this->db->get_col( $this->request ); 903 904 return array_map( 'intval', $comment_ids ); 904 905 } … … 911 912 * @since 4.6.0 912 913 * @access private 913 *914 * @global wpdb $wpdb WordPress database abstraction object.915 914 */ 916 915 private function set_found_comments() { 917 global $wpdb;918 919 916 if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { 920 917 /** … … 928 925 $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this ); 929 926 930 $this->found_comments = (int) $ wpdb->get_var( $found_comments_query );927 $this->found_comments = (int) $this->db->get_var( $found_comments_query ); 931 928 } 932 929 } … … 944 941 */ 945 942 protected function fill_descendants( $comments ) { 946 global $wpdb;947 948 943 $levels = array( 949 944 0 => wp_list_pluck( $comments, 'comment_ID' ), … … 997 992 if ( $uncached_parent_ids ) { 998 993 $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')'; 999 $level_comments = $ wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );994 $level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" ); 1000 995 1001 996 // Cache parent-child relationships. … … 1068 1063 * @access protected 1069 1064 * 1070 * @global wpdb $wpdb WordPress database abstraction object.1071 *1072 1065 * @param string $string 1073 1066 * @param array $cols … … 1075 1068 */ 1076 1069 protected function get_search_sql( $string, $cols ) { 1077 global $wpdb; 1078 1079 $like = '%' . $wpdb->esc_like( $string ) . '%'; 1070 $like = '%' . $this->db->esc_like( $string ) . '%'; 1080 1071 1081 1072 $searches = array(); 1082 1073 foreach ( $cols as $col ) { 1083 $searches[] = $ wpdb->prepare( "$col LIKE %s", $like );1074 $searches[] = $this->db->prepare( "$col LIKE %s", $like ); 1084 1075 } 1085 1076 … … 1093 1084 * @access protected 1094 1085 * 1095 * @global wpdb $wpdb WordPress database abstraction object.1096 *1097 1086 * @param string $orderby Alias for the field to order by. 1098 1087 * @return string|false Value to used in the ORDER clause. False otherwise. 1099 1088 */ 1100 1089 protected function parse_orderby( $orderby ) { 1101 global $wpdb;1102 1103 1090 $allowed_keys = array( 1104 1091 'comment_agent', … … 1132 1119 $parsed = false; 1133 1120 if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { 1134 $parsed = " $wpdb->commentmeta.meta_value";1121 $parsed = "{$this->db->commentmeta}.meta_value"; 1135 1122 } elseif ( $orderby == 'meta_value_num' ) { 1136 $parsed = " $wpdb->commentmeta.meta_value+0";1123 $parsed = "{$this->db->commentmeta}.meta_value+0"; 1137 1124 } elseif ( $orderby == 'comment__in' ) { 1138 1125 $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) ); 1139 $parsed = "FIELD( {$ wpdb->comments}.comment_ID, $comment__in )";1126 $parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )"; 1140 1127 } elseif ( in_array( $orderby, $allowed_keys ) ) { 1141 1128 … … 1144 1131 $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 1145 1132 } else { 1146 $parsed = " $wpdb->comments.$orderby";1133 $parsed = "{$this->db->comments}.$orderby"; 1147 1134 } 1148 1135 }
Note: See TracChangeset
for help on using the changeset viewer.