Make WordPress Core

Ticket #40196: 40196.2.patch

File 40196.2.patch, 7.2 KB (added by ocean90, 8 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    9999         * Sets up the site query, based on the query vars passed.
    100100         *
    101101         * @since 4.6.0
     102         * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
    102103         * @access public
    103104         *
    104105         * @param string|array $query {
     
    138139         *     @type int          $mature            Limit results to mature sites. Accepts '1' or '0'. Default empty.
    139140         *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
    140141         *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
     142         *     @type int          $lang_id           Limit results to a language ID. Default empty.
     143         *     @type array        $lang__in          Array of language IDs to include affiliated sites for. Default empty.
     144         *     @type array        $lang__not_in      Array of language IDs to exclude affiliated sites for. Default empty.
    141145         *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
    142146         *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
    143147         *                                           Default empty array.
     
    169173                        'mature'            => null,
    170174                        'spam'              => null,
    171175                        'deleted'           => null,
     176                        'lang_id'           => null,
     177                        'lang__in'          => '',
     178                        'lang__not_in'      => '',
    172179                        'search'            => '',
    173180                        'search_columns'    => array(),
    174181                        'count'             => false,
     
    471478                        $this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
    472479                }
    473480
     481                if ( is_numeric( $this->query_vars['lang_id'] ) ) {
     482                        $lang_id = absint( $this->query_vars['lang_id'] );
     483                        $this->sql_clauses['where']['lang_id'] = $wpdb->prepare( "lang_id = %d ", $lang_id );
     484                }
     485
     486                // Parse site language IDs for an IN clause.
     487                if ( ! empty( $this->query_vars['lang__in'] ) ) {
     488                        $this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';
     489                }
     490
     491                // Parse site language IDs for a NOT IN clause.
     492                if ( ! empty( $this->query_vars['lang__not_in'] ) ) {
     493                        $this->sql_clauses['where']['lang__not_in'] = 'lang_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__not_in'] ) ) . ' )';
     494                }
     495
    474496                // Falsey search strings are ignored.
    475497                if ( strlen( $this->query_vars['search'] ) ) {
    476498                        $search_columns = array();
  • src/wp-includes/ms-blogs.php

     
    574574 * Retrieves a list of sites matching requested arguments.
    575575 *
    576576 * @since 4.6.0
     577 * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
    577578 *
    578579 * @see WP_Site_Query::parse_query()
    579580 *
     
    614615 *     @type int          $mature            Limit results to mature sites. Accepts '1' or '0'. Default empty.
    615616 *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
    616617 *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
     618 *     @type int          $lang_id           Limit results to a language ID. Default empty.
     619 *     @type array        $lang__in          Array of language IDs to include affiliated sites for. Default empty.
     620 *     @type array        $lang__not_in      Array of language IDs to exclude affiliated sites for. Default empty.
    617621 *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
    618622 *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
    619623 *                                           Default empty array.
  • tests/phpunit/tests/multisite/siteQuery.php

     
    4747                        'www.w.org/'                  => array( 'domain' => 'www.w.org',          'path' => '/' ),
    4848                        'www.w.org/foo/'              => array( 'domain' => 'www.w.org',          'path' => '/foo/' ),
    4949                        'www.w.org/foo/bar/'          => array( 'domain' => 'www.w.org',          'path' => '/foo/bar/' ),
    50                         'www.w.org/make/'             => array( 'domain' => 'www.w.org',          'path' => '/make/' ),
     50                        'www.w.org/make/'             => array( 'domain' => 'www.w.org',          'path' => '/make/', 'meta' => array( 'public' => 1, 'lang_id' => 1 ) ),
    5151                );
    5252
    5353                foreach ( self::$site_ids as &$id ) {
     
    431431                $this->assertEqualSets( array_values( self::$site_ids ), $found );
    432432        }
    433433
     434        public function test_wp_site_query_by_lang_id_with_zero() {
     435                $q = new WP_Site_Query();
     436                $found = $q->query( array(
     437                        'fields'       => 'ids',
     438                        // Exclude main site since we don't have control over it here.
     439                        'site__not_in' => array( 1 ),
     440                        'lang_id'      => 0,
     441                ) );
     442
     443                $this->assertEqualSets( array_diff( array_values( self::$site_ids ), array( self::$site_ids['www.w.org/make/'] ) ), $found );
     444        }
     445
     446        public function test_wp_site_query_by_lang_id() {
     447                $q = new WP_Site_Query();
     448                $found = $q->query( array(
     449                        'fields'       => 'ids',
     450                        'lang_id'      => 1,
     451                ) );
     452
     453                $expected = array(
     454                        self::$site_ids['www.w.org/make/'],
     455                );
     456
     457                $this->assertEqualSets( $expected, $found );
     458        }
     459
     460        public function test_wp_site_query_by_lang_id_with_no_results() {
     461                $q = new WP_Site_Query();
     462                $found = $q->query( array(
     463                        'fields'       => 'ids',
     464                        'lang_id'      => 2,
     465                ) );
     466
     467                $this->assertEmpty( $found );
     468        }
     469
     470        public function test_wp_site_query_by_lang__in() {
     471                $q = new WP_Site_Query();
     472                $found = $q->query( array(
     473                        'fields' => 'ids',
     474                        'lang__in' => array( 1 ),
     475                ) );
     476
     477                $expected = array(
     478                        self::$site_ids['www.w.org/make/'],
     479                );
     480
     481                $this->assertEqualSets( $expected, $found );
     482        }
     483
     484        public function test_wp_site_query_by_lang__in_with_multiple_ids() {
     485                $q = new WP_Site_Query();
     486                $found = $q->query( array(
     487                        'fields' => 'ids',
     488                        // Exclude main site since we don't have control over it here.
     489                        'site__not_in' => array( 1 ),
     490                        'lang__in' => array( 0, 1 ),
     491                ) );
     492
     493                $this->assertEqualSets( array_values( self::$site_ids ), $found );
     494        }
     495
     496        public function test_wp_site_query_by_lang__not_in() {
     497                $q = new WP_Site_Query();
     498                $found = $q->query( array(
     499                        'fields' => 'ids',
     500                        'lang__not_in' => array( 0 ),
     501                ) );
     502
     503                $expected = array(
     504                        self::$site_ids['www.w.org/make/'],
     505                );
     506
     507                $this->assertEqualSets( $expected, $found );
     508        }
     509
     510        public function test_wp_site_query_by_lang__not_in_with_multiple_ids() {
     511                $q = new WP_Site_Query();
     512                $found = $q->query( array(
     513                        'fields' => 'ids',
     514                        'lang__not_in' => array( 0, 1 ),
     515                ) );
     516
     517                $this->assertEmpty( $found );
     518        }
     519
    434520        public function test_wp_site_query_by_search_with_text_in_domain() {
    435521                $q = new WP_Site_Query();
    436522                $found = $q->query( array(