Make WordPress Core

Ticket #42284: 42284.diff

File 42284.diff, 5.3 KB (added by spacedmonkey, 9 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    143143                $this->query_var_defaults = array(
    144144                        'fields'            => '',
    145145                        'ID'                => '',
    146                         'site__in'          => '',
     146                        'site__in'          => array(),
    147147                        'site__not_in'      => '',
    148148                        'number'            => 100,
    149149                        'offset'            => '',
     
    151151                        'orderby'           => 'id',
    152152                        'order'             => 'ASC',
    153153                        'network_id'        => 0,
    154                         'network__in'       => '',
     154                        'network__in'       => array(),
    155155                        'network__not_in'   => '',
    156156                        'domain'            => '',
    157                         'domain__in'        => '',
     157                        'domain__in'        => array(),
    158158                        'domain__not_in'    => '',
    159159                        'path'              => '',
    160                         'path__in'          => '',
     160                        'path__in'          => array(),
    161161                        'path__not_in'      => '',
    162162                        'public'            => null,
    163163                        'archived'          => null,
     
    165165                        'spam'              => null,
    166166                        'deleted'           => null,
    167167                        'lang_id'           => null,
    168                         'lang__in'          => '',
     168                        'lang__in'          => array(),
    169169                        'lang__not_in'      => '',
    170170                        'search'            => '',
    171171                        'search_columns'    => array(),
     
    206206        }
    207207
    208208        /**
     209         * @param string $query
     210         */
     211        public function tidy_query( $query = '' ) {
     212                if ( empty( $query ) ) {
     213                        $query = $this->query_vars;
     214                }
     215                $query = wp_parse_args( $query );
     216
     217                $list = array(
     218                        'ID'         => 'site__in',
     219                        'network_id' => 'network__in',
     220                        'domain'     => 'domain__in',
     221                        'path'       => 'path__in',
     222                        'lang_id'    => 'lang__in'
     223                );
     224
     225                foreach ( $list as $key => $value ) {
     226                        if ( empty( $query[ $key ] ) ) {
     227                                unset( $query[ $key ] );
     228                                continue;
     229                        }
     230                        if ( empty( $query[ $value ] ) ||  ! is_array( $query[ $value ] ) ) {
     231                                $query[ $value ] = array();
     232                        }
     233
     234                        if ( ! in_array( $query[ $key ], $query[ $value ] ) ) {
     235                                $query[ $value ][] = $query[ $key ];
     236                        }
     237
     238                        unset( $query[ $key ] );
     239                }
     240               
     241                $this->query_vars = $query;
     242        }
     243
     244        /**
    209245         * Sets up the WordPress query for retrieving sites.
    210246         *
    211247         * @since 4.6.0
     
    240276                 */
    241277                do_action_ref_array( 'pre_get_sites', array( &$this ) );
    242278
     279                $this->tidy_query();
     280
    243281                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    244282                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    245283
     
    388426                }
    389427
    390428                // Parse site IDs for an IN clause.
    391                 $site_id = absint( $this->query_vars['ID'] );
    392                 if ( ! empty( $site_id ) ) {
    393                         $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
    394                 }
    395 
    396                 // Parse site IDs for an IN clause.
    397429                if ( ! empty( $this->query_vars['site__in'] ) ) {
    398430                        $this->sql_clauses['where']['site__in'] = "blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
    399431                }
     
    403435                        $this->sql_clauses['where']['site__not_in'] = "blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
    404436                }
    405437
    406                 $network_id = absint( $this->query_vars['network_id'] );
    407 
    408                 if ( ! empty( $network_id ) ) {
    409                         $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
    410                 }
    411 
    412438                // Parse site network IDs for an IN clause.
    413439                if ( ! empty( $this->query_vars['network__in'] ) ) {
    414440                        $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
     
    419445                        $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
    420446                }
    421447
    422                 if ( ! empty( $this->query_vars['domain'] ) ) {
    423                         $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
    424                 }
    425 
    426448                // Parse site domain for an IN clause.
    427449                if ( is_array( $this->query_vars['domain__in'] ) ) {
    428450                        $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
     
    433455                        $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
    434456                }
    435457
    436                 if ( ! empty( $this->query_vars['path'] ) ) {
    437                         $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
    438                 }
    439 
    440458                // Parse site path for an IN clause.
    441459                if ( is_array( $this->query_vars['path__in'] ) ) {
    442460                        $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
     
    472490                        $this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
    473491                }
    474492
    475                 if ( is_numeric( $this->query_vars['lang_id'] ) ) {
    476                         $lang_id = absint( $this->query_vars['lang_id'] );
    477                         $this->sql_clauses['where']['lang_id'] = $wpdb->prepare( "lang_id = %d ", $lang_id );
    478                 }
    479 
    480493                // Parse site language IDs for an IN clause.
    481494                if ( ! empty( $this->query_vars['lang__in'] ) ) {
    482495                        $this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';