Make WordPress Core

Ticket #20597: comment-query-by-category.patch

File comment-query-by-category.patch, 2.8 KB (added by sambauers, 13 years ago)
  • wp-includes/comment.php

     
    222222                        'type' => '',
    223223                        'user_id' => '',
    224224                        'search' => '',
    225                         'count' => false
     225                        'count' => false,
     226                        'cat' => false,
     227                        'category__in' => array(),
     228                        'category__not_in' => array()
    226229                );
    227230
    228231                $this->query_vars = wp_parse_args( $query_vars, $defaults );
     
    301304                else
    302305                        $fields = '*';
    303306
     307                if ( empty( $category__in ) ) {
     308                        $category__in = array();
     309                }
     310                if ( empty( $category__not_in ) ) {
     311                        $category__not_in = array();
     312                }
     313
     314                if ( !empty($cat) && '0' != $cat ) {
     315                        $cat = ''.urldecode($cat).'';
     316                        $cat = addslashes_gpc($cat);
     317                        $cat_array = preg_split('/[,\s]+/', $cat);
     318                        $cat = '';
     319                        $req_cats = array();
     320                        foreach ( (array) $cat_array as $cat_id ) {
     321                                $cat_id = intval($cat_id);
     322                                $req_cats[] = $cat_id;
     323                                $in = ($cat_id > 0);
     324                                $cat_id = abs($cat_id);
     325                                if ( $in ) {
     326                                        $category__in[] = $cat_id;
     327                                        $category__in = array_merge( $category__in, get_term_children($cat_id, 'category') );
     328                                } else {
     329                                        $category__not_in[] = $cat_id;
     330                                        $category__not_in = array_merge( $category__not_in, get_term_children($cat_id, 'category') );
     331                                }
     332                        }
     333                        $cat = implode(',', $req_cats);
     334                }
     335
    304336                $join = '';
    305337                $where = $approved;
    306338
     
    330362                        foreach( $post_fields as $field_name => $field_value )
    331363                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
    332364                }
     365                if ( ! empty( $category__in ) || ! empty( $category__not_in ) ) {
     366                        if ( empty( $join ) ) {
     367                                $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     368                        }
     369                        $join .= " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
     370                        $join .= " INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
     371                        $where .= " AND $wpdb->term_taxonomy.taxonomy = 'category'";
    333372
     373                        if ( ! empty( $category__in ) ) {
     374                                $category__in = array_map( 'absint', array_unique( (array) $category__in ) );
     375                                $category__in_query = implode( ',', $category__in );
     376                                $where .= $wpdb->prepare( " AND $wpdb->term_taxonomy.term_id IN ($category__in_query)" );
     377                        }
     378
     379                        if ( ! empty( $category__not_in ) ) {
     380                                $category__not_in = array_map( 'absint', array_unique( (array) $category__not_in ) );
     381                                $category__not_in_query = implode( ',', $category__not_in );
     382                                $where .= $wpdb->prepare( " AND $wpdb->term_taxonomy.term_id NOT IN ($category__not_in_query)" );
     383                        }
     384                }
     385
    334386                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
    335387                $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
    336388                foreach ( $pieces as $piece )