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-meta-query.php

    r41688 r42343  
    101101     * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
    102102     *
    103      *
    104103     * @param array $meta_query {
    105104     *     Array of meta query clauses. When first-order clauses or sub-clauses use strings as
     
    126125     */
    127126    public function __construct( $meta_query = false ) {
    128         if ( !$meta_query )
     127        if ( ! $meta_query ) {
    129128            return;
     129        }
    130130
    131131        if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
     
    162162                continue;
    163163
    164             // First-order clause.
     164                // First-order clause.
    165165            } elseif ( $this->is_first_order_clause( $query ) ) {
    166166                if ( isset( $query['value'] ) && array() === $query['value'] ) {
     
    170170                $clean_queries[ $key ] = $query;
    171171
    172             // Otherwise, it's a nested query, so we recurse.
     172                // Otherwise, it's a nested query, so we recurse.
    173173            } else {
    174174                $cleaned_query = $this->sanitize_query( $query );
     
    187187        if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
    188188            $clean_queries['relation'] = 'OR';
    189             $this->has_or_relation = true;
    190 
    191         /*
    192          * If there is only a single clause, call the relation 'OR'.
    193          * This value will not actually be used to join clauses, but it
    194          * simplifies the logic around combining key-only queries.
    195          */
     189            $this->has_or_relation     = true;
     190
     191            /*
     192            * If there is only a single clause, call the relation 'OR'.
     193            * This value will not actually be used to join clauses, but it
     194            * simplifies the logic around combining key-only queries.
     195            */
    196196        } elseif ( 1 === count( $clean_queries ) ) {
    197197            $clean_queries['relation'] = 'OR';
    198198
    199         // Default to AND.
     199            // Default to AND.
    200200        } else {
    201201            $clean_queries['relation'] = 'AND';
     
    276276     */
    277277    public function get_cast_for_type( $type = '' ) {
    278         if ( empty( $type ) )
     278        if ( empty( $type ) ) {
    279279            return 'CHAR';
     280        }
    280281
    281282        $meta_type = strtoupper( $type );
    282283
    283         if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
     284        if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) ) {
    284285            return 'CHAR';
    285 
    286         if ( 'NUMERIC' == $meta_type )
     286        }
     287
     288        if ( 'NUMERIC' == $meta_type ) {
    287289            $meta_type = 'SIGNED';
     290        }
    288291
    289292        return $meta_type;
     
    365368         */
    366369        $queries = $this->queries;
    367         $sql = $this->get_sql_for_query( $queries );
     370        $sql     = $this->get_sql_for_query( $queries );
    368371
    369372        if ( ! empty( $sql['where'] ) ) {
     
    405408        $indent = '';
    406409        for ( $i = 0; $i < $depth; $i++ ) {
    407             $indent .= "  ";
     410            $indent .= '  ';
    408411        }
    409412
     
    427430
    428431                    $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
    429                 // This is a subquery, so we recurse.
     432                    // This is a subquery, so we recurse.
    430433                } else {
    431434                    $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
     
    483486        $sql_chunks = array(
    484487            'where' => array(),
    485             'join' => array(),
     488            'join'  => array(),
    486489        );
    487490
     
    492495        }
    493496
    494         if ( ! in_array( $clause['compare'], array(
    495             '=', '!=', '>', '>=', '<', '<=',
    496             'LIKE', 'NOT LIKE',
    497             'IN', 'NOT IN',
    498             'BETWEEN', 'NOT BETWEEN',
    499             'EXISTS', 'NOT EXISTS',
    500             'REGEXP', 'NOT REGEXP', 'RLIKE'
    501         ) ) ) {
     497        if ( ! in_array(
     498            $clause['compare'], array(
     499                '=',
     500                '!=',
     501                '>',
     502                '>=',
     503                '<',
     504                '<=',
     505                'LIKE',
     506                'NOT LIKE',
     507                'IN',
     508                'NOT IN',
     509                'BETWEEN',
     510                'NOT BETWEEN',
     511                'EXISTS',
     512                'NOT EXISTS',
     513                'REGEXP',
     514                'NOT REGEXP',
     515                'RLIKE',
     516            )
     517        ) ) {
    502518            $clause['compare'] = '=';
    503519        }
     
    511527        $alias = $this->find_compatible_table_alias( $clause, $parent_query );
    512528        if ( false === $alias ) {
    513             $i = count( $this->table_aliases );
     529            $i     = count( $this->table_aliases );
    514530            $alias = $i ? 'mt' . $i : $this->meta_table;
    515531
     
    520536                $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
    521537
    522             // All other JOIN clauses.
     538                // All other JOIN clauses.
    523539            } else {
    524540                $join .= " INNER JOIN $this->meta_table";
     
    528544
    529545            $this->table_aliases[] = $alias;
    530             $sql_chunks['join'][] = $join;
     546            $sql_chunks['join'][]  = $join;
    531547        }
    532548
     
    535551
    536552        // Determine the data type.
    537         $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
    538         $meta_type  = $this->get_cast_for_type( $_meta_type );
     553        $_meta_type     = isset( $clause['type'] ) ? $clause['type'] : '';
     554        $meta_type      = $this->get_cast_for_type( $_meta_type );
    539555        $clause['cast'] = $meta_type;
    540556
     
    545561
    546562        // Ensure unique clause keys, so none are overwritten.
    547         $iterator = 1;
     563        $iterator        = 1;
    548564        $clause_key_base = $clause_key;
    549565        while ( isset( $this->clauses[ $clause_key ] ) ) {
     
    579595
    580596            switch ( $meta_compare ) {
    581                 case 'IN' :
    582                 case 'NOT IN' :
     597                case 'IN':
     598                case 'NOT IN':
    583599                    $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
    584                     $where = $wpdb->prepare( $meta_compare_string, $meta_value );
     600                    $where               = $wpdb->prepare( $meta_compare_string, $meta_value );
    585601                    break;
    586602
    587                 case 'BETWEEN' :
    588                 case 'NOT BETWEEN' :
     603                case 'BETWEEN':
     604                case 'NOT BETWEEN':
    589605                    $meta_value = array_slice( $meta_value, 0, 2 );
    590                     $where = $wpdb->prepare( '%s AND %s', $meta_value );
     606                    $where      = $wpdb->prepare( '%s AND %s', $meta_value );
    591607                    break;
    592608
    593                 case 'LIKE' :
    594                 case 'NOT LIKE' :
     609                case 'LIKE':
     610                case 'NOT LIKE':
    595611                    $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
    596                     $where = $wpdb->prepare( '%s', $meta_value );
     612                    $where      = $wpdb->prepare( '%s', $meta_value );
    597613                    break;
    598614
    599615                // EXISTS with a value is interpreted as '='.
    600                 case 'EXISTS' :
     616                case 'EXISTS':
    601617                    $meta_compare = '=';
    602                     $where = $wpdb->prepare( '%s', $meta_value );
     618                    $where        = $wpdb->prepare( '%s', $meta_value );
    603619                    break;
    604620
    605621                // 'value' is ignored for NOT EXISTS.
    606                 case 'NOT EXISTS' :
     622                case 'NOT EXISTS':
    607623                    $where = '';
    608624                    break;
    609625
    610                 default :
     626                default:
    611627                    $where = $wpdb->prepare( '%s', $meta_value );
    612628                    break;
     
    688704                $compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' );
    689705
    690             // Clauses joined by AND with "negative" operators share a join only if they also share a key.
     706                // Clauses joined by AND with "negative" operators share a join only if they also share a key.
    691707            } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) {
    692708                $compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );
     
    711727         * @param object      $this         WP_Meta_Query object.
    712728         */
    713         return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
     729        return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this );
    714730    }
    715731
Note: See TracChangeset for help on using the changeset viewer.