Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r42201 r42343  
    243243     *                                                   `$fields` is 'ids' or 'counts'. Accepts 'threaded',
    244244     *                                                   'flat', or false. Default: false.
    245      *     @type string       $cache_domain              Unique cache key to be produced when this query is stored in
     245     *     @type string       $cache_domain              Unique cache key to be produced when this query is stored in
    246246     *                                                   an object cache. Default is 'core'.
    247247     *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
     
    253253    public function __construct( $query = '' ) {
    254254        $this->query_var_defaults = array(
    255             'author_email' => '',
    256             'author_url' => '',
    257             'author__in' => '',
    258             'author__not_in' => '',
    259             'include_unapproved' => '',
    260             'fields' => '',
    261             'ID' => '',
    262             'comment__in' => '',
    263             'comment__not_in' => '',
    264             'karma' => '',
    265             'number' => '',
    266             'offset' => '',
    267             'no_found_rows' => true,
    268             'orderby' => '',
    269             'order' => 'DESC',
    270             'paged' => 1,
    271             'parent' => '',
    272             'parent__in' => '',
    273             'parent__not_in' => '',
    274             'post_author__in' => '',
    275             'post_author__not_in' => '',
    276             'post_ID' => '',
    277             'post_id' => 0,
    278             'post__in' => '',
    279             'post__not_in' => '',
    280             'post_author' => '',
    281             'post_name' => '',
    282             'post_parent' => '',
    283             'post_status' => '',
    284             'post_type' => '',
    285             'status' => 'all',
    286             'type' => '',
    287             'type__in' => '',
    288             'type__not_in' => '',
    289             'user_id' => '',
    290             'search' => '',
    291             'count' => false,
    292             'meta_key' => '',
    293             'meta_value' => '',
    294             'meta_query' => '',
    295             'date_query' => null, // See WP_Date_Query
    296             'hierarchical' => false,
    297             'cache_domain' => 'core',
     255            'author_email'              => '',
     256            'author_url'                => '',
     257            'author__in'                => '',
     258            'author__not_in'            => '',
     259            'include_unapproved'        => '',
     260            'fields'                    => '',
     261            'ID'                        => '',
     262            'comment__in'               => '',
     263            'comment__not_in'           => '',
     264            'karma'                     => '',
     265            'number'                    => '',
     266            'offset'                    => '',
     267            'no_found_rows'             => true,
     268            'orderby'                   => '',
     269            'order'                     => 'DESC',
     270            'paged'                     => 1,
     271            'parent'                    => '',
     272            'parent__in'                => '',
     273            'parent__not_in'            => '',
     274            'post_author__in'           => '',
     275            'post_author__not_in'       => '',
     276            'post_ID'                   => '',
     277            'post_id'                   => 0,
     278            'post__in'                  => '',
     279            'post__not_in'              => '',
     280            'post_author'               => '',
     281            'post_name'                 => '',
     282            'post_parent'               => '',
     283            'post_status'               => '',
     284            'post_type'                 => '',
     285            'status'                    => 'all',
     286            'type'                      => '',
     287            'type__in'                  => '',
     288            'type__not_in'              => '',
     289            'user_id'                   => '',
     290            'search'                    => '',
     291            'count'                     => false,
     292            'meta_key'                  => '',
     293            'meta_value'                => '',
     294            'meta_query'                => '',
     295            'date_query'                => null, // See WP_Date_Query
     296            'hierarchical'              => false,
     297            'cache_domain'              => 'core',
    298298            'update_comment_meta_cache' => true,
    299299            'update_comment_post_cache' => false,
     
    309309     *
    310310     * @since 4.2.0 Extracted from WP_Comment_Query::query().
    311      *
    312311     *
    313312     * @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct()
     
    388387        unset( $_args['fields'] );
    389388
    390         $key = md5( serialize( $_args ) );
     389        $key          = md5( serialize( $_args ) );
    391390        $last_changed = wp_cache_get_last_changed( 'comment' );
    392391
     
    482481        // 'status' accepts an array or a comma-separated string.
    483482        $status_clauses = array();
    484         $statuses = $this->query_vars['status'];
     483        $statuses       = $this->query_vars['status'];
    485484        if ( ! is_array( $statuses ) ) {
    486485            $statuses = preg_split( '/[\s,]+/', $statuses );
     
    491490            foreach ( $statuses as $status ) {
    492491                switch ( $status ) {
    493                     case 'hold' :
     492                    case 'hold':
    494493                        $status_clauses[] = "comment_approved = '0'";
    495494                        break;
    496495
    497                     case 'approve' :
     496                    case 'approve':
    498497                        $status_clauses[] = "comment_approved = '1'";
    499498                        break;
    500499
    501                     case 'all' :
    502                     case '' :
     500                    case 'all':
     501                    case '':
    503502                        $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
    504503                        break;
    505504
    506                     default :
    507                         $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
     505                    default:
     506                        $status_clauses[] = $wpdb->prepare( 'comment_approved = %s', $status );
    508507                        break;
    509508                }
     
    530529                    $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
    531530
    532                 // Otherwise we match against email addresses.
     531                    // Otherwise we match against email addresses.
    533532                } else {
    534533                    $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     
    556555                preg_split( '/[,\s]/', $this->query_vars['orderby'] );
    557556
    558             $orderby_array = array();
     557            $orderby_array            = array();
    559558            $found_orderby_comment_ID = false;
    560559            foreach ( $ordersby as $_key => $_value ) {
     
    565564                if ( is_int( $_key ) ) {
    566565                    $_orderby = $_value;
    567                     $_order = $order;
     566                    $_order   = $order;
    568567                } else {
    569568                    $_orderby = $_key;
    570                     $_order = $_value;
     569                    $_order   = $_value;
    571570                }
    572571
     
    634633        $number = absint( $this->query_vars['number'] );
    635634        $offset = absint( $this->query_vars['offset'] );
    636         $paged = absint( $this->query_vars['paged'] );
     635        $paged  = absint( $this->query_vars['paged'] );
    637636
    638637        if ( ! empty( $number ) ) {
     
    699698        // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
    700699        $raw_types = array(
    701             'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
     700            'IN'     => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
    702701            'NOT IN' => (array) $this->query_vars['type__not_in'],
    703702        );
     
    711710                    // An empty translates to 'all', for backward compatibility
    712711                    case '':
    713                     case 'all' :
     712                    case 'all':
    714713                        break;
    715714
     
    732731            if ( ! empty( $comment_types[ $operator ] ) ) {
    733732                $types_sql = implode( ', ', $comment_types[ $operator ] );
    734                 $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)";
     733                $this->sql_clauses['where'][ 'comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)";
    735734            }
    736735        }
     
    764763        // If any post-related query vars are passed, join the posts table.
    765764        $join_posts_table = false;
    766         $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
    767         $post_fields = array_filter( $plucked );
     765        $plucked          = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
     766        $post_fields      = array_filter( $plucked );
    768767
    769768        if ( ! empty( $post_fields ) ) {
     
    771770            foreach ( $post_fields as $field_name => $field_value ) {
    772771                // $field_value may be an array.
    773                 $esses = array_fill( 0, count( (array) $field_value ), '%s' );
     772                $esses                                     = array_fill( 0, count( (array) $field_value ), '%s' );
    774773                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    775774            }
     
    792791                $join_posts_table = true;
    793792
    794                 $esses = array_fill( 0, count( $q_values ), '%s' );
    795                 $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
     793                $esses                                     = array_fill( 0, count( $q_values ), '%s' );
     794                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $q_values );
    796795            }
    797796        }
     
    809808        // Post author IDs for an IN clause.
    810809        if ( ! empty( $this->query_vars['post_author__in'] ) ) {
    811             $join_posts_table = true;
     810            $join_posts_table                              = true;
    812811            $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
    813812        }
     
    815814        // Post author IDs for a NOT IN clause.
    816815        if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
    817             $join_posts_table = true;
     816            $join_posts_table                                  = true;
    818817            $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
    819818        }
     
    837836
    838837        if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
    839             $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
     838            $this->date_query                         = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
    840839            $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
    841840        }
     
    854853        $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
    855854
    856         $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
    857         $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
    858         $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
    859         $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
    860         $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
    861         $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
     855        $fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
     856        $join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
     857        $where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
     858        $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
     859        $limits  = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
     860        $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
    862861
    863862        $this->filtered_where_clause = $where;
     
    942941        );
    943942
    944         $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
     943        $key          = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    945944        $last_changed = wp_cache_get_last_changed( 'comment' );
    946945
    947946        // Fetch an entire level of the descendant tree at a time.
    948         $level = 0;
     947        $level        = 0;
    949948        $exclude_keys = array( 'parent', 'parent__in', 'parent__not_in' );
    950949        do {
    951950            // Parent-child relationships may be cached. Only query for those that are not.
    952             $child_ids = $uncached_parent_ids = array();
     951            $child_ids   = $uncached_parent_ids = array();
    953952            $_parent_ids = $levels[ $level ];
    954953            foreach ( $_parent_ids as $parent_id ) {
    955                 $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
     954                $cache_key        = "get_comment_child_ids:$parent_id:$key:$last_changed";
    956955                $parent_child_ids = wp_cache_get( $cache_key, 'comment' );
    957956                if ( false !== $parent_child_ids ) {
     
    980979                foreach ( $level_comments as $level_comment ) {
    981980                    $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
    982                     $child_ids[] = $level_comment->comment_ID;
     981                    $child_ids[]                                    = $level_comment->comment_ID;
    983982                }
    984983
     
    10161015                if ( ! isset( $ref[ $c->comment_parent ] ) ) {
    10171016                    $threaded_comments[ $_c->comment_ID ] = $_c;
    1018                     $ref[ $_c->comment_ID ] = $threaded_comments[ $_c->comment_ID ];
    1019 
    1020                 // Otherwise, set it as a child of its parent.
     1017                    $ref[ $_c->comment_ID ]               = $threaded_comments[ $_c->comment_ID ];
     1018
     1019                    // Otherwise, set it as a child of its parent.
    10211020                } else {
    10221021
     
    10601059        }
    10611060
    1062         return ' AND (' . implode(' OR ', $searches) . ')';
     1061        return ' AND (' . implode( ' OR ', $searches ) . ')';
    10631062    }
    10641063
     
    11121111        } elseif ( $orderby == 'comment__in' ) {
    11131112            $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
    1114             $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
     1113            $parsed      = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
    11151114        } elseif ( in_array( $orderby, $allowed_keys ) ) {
    11161115
    11171116            if ( isset( $meta_query_clauses[ $orderby ] ) ) {
    11181117                $meta_clause = $meta_query_clauses[ $orderby ];
    1119                 $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
     1118                $parsed      = sprintf( 'CAST(%s.meta_value AS %s)', esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    11201119            } else {
    11211120                $parsed = "$wpdb->comments.$orderby";
Note: See TracChangeset for help on using the changeset viewer.