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