wp/wp-includes/class-wp-site-query.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    38 		'orderby' => '',
    38 		'orderby' => '',
    39 		'limits'  => '',
    39 		'limits'  => '',
    40 	);
    40 	);
    41 
    41 
    42 	/**
    42 	/**
       
    43 	 * Metadata query container.
       
    44 	 *
       
    45 	 * @since 5.1.0
       
    46 	 * @var WP_Meta_Query
       
    47 	 */
       
    48 	public $meta_query = false;
       
    49 
       
    50 	/**
       
    51 	 * Metadata query clauses.
       
    52 	 *
       
    53 	 * @since 5.1.0
       
    54 	 * @var array
       
    55 	 */
       
    56 	protected $meta_query_clauses;
       
    57 
       
    58 	/**
    43 	 * Date query container.
    59 	 * Date query container.
    44 	 *
    60 	 *
    45 	 * @since 4.6.0
    61 	 * @since 4.6.0
    46 	 * @var object WP_Date_Query
    62 	 * @var object WP_Date_Query
    47 	 */
    63 	 */
    90 	/**
   106 	/**
    91 	 * Sets up the site query, based on the query vars passed.
   107 	 * Sets up the site query, based on the query vars passed.
    92 	 *
   108 	 *
    93 	 * @since 4.6.0
   109 	 * @since 4.6.0
    94 	 * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
   110 	 * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
       
   111 	 * @since 5.1.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
       
   112 	 *              'meta_value', 'meta_type' and 'meta_compare' parameters.
    95 	 *
   113 	 *
    96 	 * @param string|array $query {
   114 	 * @param string|array $query {
    97 	 *     Optional. Array or query string of site query parameters. Default empty.
   115 	 *     Optional. Array or query string of site query parameters. Default empty.
    98 	 *
   116 	 *
    99 	 *     @type array        $site__in          Array of site IDs to include. Default empty.
   117 	 *     @type array        $site__in               Array of site IDs to include. Default empty.
   100 	 *     @type array        $site__not_in      Array of site IDs to exclude. Default empty.
   118 	 *     @type array        $site__not_in           Array of site IDs to exclude. Default empty.
   101 	 *     @type bool         $count             Whether to return a site count (true) or array of site objects.
   119 	 *     @type bool         $count                  Whether to return a site count (true) or array of site objects.
   102 	 *                                           Default false.
   120 	 *                                                Default false.
   103 	 *     @type array        $date_query        Date query clauses to limit sites by. See WP_Date_Query.
   121 	 *     @type array        $date_query             Date query clauses to limit sites by. See WP_Date_Query.
   104 	 *                                           Default null.
   122 	 *                                                Default null.
   105 	 *     @type string       $fields            Site fields to return. Accepts 'ids' (returns an array of site IDs)
   123 	 *     @type string       $fields                 Site fields to return. Accepts 'ids' (returns an array of site IDs)
   106 	 *                                           or empty (returns an array of complete site objects). Default empty.
   124 	 *                                                or empty (returns an array of complete site objects). Default empty.
   107 	 *     @type int          $ID                A site ID to only return that site. Default empty.
   125 	 *     @type int          $ID                     A site ID to only return that site. Default empty.
   108 	 *     @type int          $number            Maximum number of sites to retrieve. Default 100.
   126 	 *     @type int          $number                 Maximum number of sites to retrieve. Default 100.
   109 	 *     @type int          $offset            Number of sites to offset the query. Used to build LIMIT clause.
   127 	 *     @type int          $offset                 Number of sites to offset the query. Used to build LIMIT clause.
   110 	 *                                           Default 0.
   128 	 *                                                Default 0.
   111 	 *     @type bool         $no_found_rows     Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   129 	 *     @type bool         $no_found_rows          Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   112 	 *     @type string|array $orderby           Site status or array of statuses. Accepts 'id', 'domain', 'path',
   130 	 *     @type string|array $orderby                Site status or array of statuses. Accepts 'id', 'domain', 'path',
   113 	 *                                           'network_id', 'last_updated', 'registered', 'domain_length',
   131 	 *                                                'network_id', 'last_updated', 'registered', 'domain_length',
   114 	 *                                           'path_length', 'site__in' and 'network__in'. Also accepts false,
   132 	 *                                                'path_length', 'site__in' and 'network__in'. Also accepts false,
   115 	 *                                           an empty array, or 'none' to disable `ORDER BY` clause.
   133 	 *                                                an empty array, or 'none' to disable `ORDER BY` clause.
   116 	 *                                           Default 'id'.
   134 	 *                                                Default 'id'.
   117 	 *     @type string       $order             How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
   135 	 *     @type string       $order                  How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
   118 	 *     @type int          $network_id        Limit results to those affiliated with a given network ID. If 0,
   136 	 *     @type int          $network_id             Limit results to those affiliated with a given network ID. If 0,
   119 	 *                                           include all networks. Default 0.
   137 	 *                                                include all networks. Default 0.
   120 	 *     @type array        $network__in       Array of network IDs to include affiliated sites for. Default empty.
   138 	 *     @type array        $network__in            Array of network IDs to include affiliated sites for. Default empty.
   121 	 *     @type array        $network__not_in   Array of network IDs to exclude affiliated sites for. Default empty.
   139 	 *     @type array        $network__not_in        Array of network IDs to exclude affiliated sites for. Default empty.
   122 	 *     @type string       $domain            Limit results to those affiliated with a given domain. Default empty.
   140 	 *     @type string       $domain                 Limit results to those affiliated with a given domain. Default empty.
   123 	 *     @type array        $domain__in        Array of domains to include affiliated sites for. Default empty.
   141 	 *     @type array        $domain__in             Array of domains to include affiliated sites for. Default empty.
   124 	 *     @type array        $domain__not_in    Array of domains to exclude affiliated sites for. Default empty.
   142 	 *     @type array        $domain__not_in         Array of domains to exclude affiliated sites for. Default empty.
   125 	 *     @type string       $path              Limit results to those affiliated with a given path. Default empty.
   143 	 *     @type string       $path                   Limit results to those affiliated with a given path. Default empty.
   126 	 *     @type array        $path__in          Array of paths to include affiliated sites for. Default empty.
   144 	 *     @type array        $path__in               Array of paths to include affiliated sites for. Default empty.
   127 	 *     @type array        $path__not_in      Array of paths to exclude affiliated sites for. Default empty.
   145 	 *     @type array        $path__not_in           Array of paths to exclude affiliated sites for. Default empty.
   128 	 *     @type int          $public            Limit results to public sites. Accepts '1' or '0'. Default empty.
   146 	 *     @type int          $public                 Limit results to public sites. Accepts '1' or '0'. Default empty.
   129 	 *     @type int          $archived          Limit results to archived sites. Accepts '1' or '0'. Default empty.
   147 	 *     @type int          $archived               Limit results to archived sites. Accepts '1' or '0'. Default empty.
   130 	 *     @type int          $mature            Limit results to mature sites. Accepts '1' or '0'. Default empty.
   148 	 *     @type int          $mature                 Limit results to mature sites. Accepts '1' or '0'. Default empty.
   131 	 *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
   149 	 *     @type int          $spam                   Limit results to spam sites. Accepts '1' or '0'. Default empty.
   132 	 *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
   150 	 *     @type int          $deleted                Limit results to deleted sites. Accepts '1' or '0'. Default empty.
   133 	 *     @type int          $lang_id           Limit results to a language ID. Default empty.
   151 	 *     @type int          $lang_id                Limit results to a language ID. Default empty.
   134 	 *     @type array        $lang__in          Array of language IDs to include affiliated sites for. Default empty.
   152 	 *     @type array        $lang__in               Array of language IDs to include affiliated sites for. Default empty.
   135 	 *     @type array        $lang__not_in      Array of language IDs to exclude affiliated sites for. Default empty.
   153 	 *     @type array        $lang__not_in           Array of language IDs to exclude affiliated sites for. Default empty.
   136 	 *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
   154 	 *     @type string       $search                 Search term(s) to retrieve matching sites for. Default empty.
   137 	 *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
   155 	 *     @type array        $search_columns         Array of column names to be searched. Accepts 'domain' and 'path'.
   138 	 *                                           Default empty array.
   156 	 *                                                Default empty array.
   139 	 *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default true.
   157 	 *     @type bool         $update_site_cache      Whether to prime the cache for found sites. Default true.
       
   158 	 *     @type bool         $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
       
   159 	 *     @type array        $meta_query             Meta query clauses to limit retrieved sites by. See `WP_Meta_Query`.
       
   160 	 *                                                Default empty.
       
   161 	 *     @type string       $meta_key               Limit sites to those matching a specific metadata key.
       
   162 	 *                                                Can be used in conjunction with `$meta_value`. Default empty.
       
   163 	 *     @type string       $meta_value             Limit sites to those matching a specific metadata value.
       
   164 	 *                                                Usually used in conjunction with `$meta_key`. Default empty.
       
   165 	 *     @type string       $meta_type              Data type that the `$meta_value` column will be CAST to for
       
   166 	 *                                                comparisons. Default empty.
       
   167 	 *     @type string       $meta_compare           Comparison operator to test the `$meta_value`. Default empty.
   140 	 * }
   168 	 * }
   141 	 */
   169 	 */
   142 	public function __construct( $query = '' ) {
   170 	public function __construct( $query = '' ) {
   143 		$this->query_var_defaults = array(
   171 		$this->query_var_defaults = array(
   144 			'fields'            => '',
   172 			'fields'                 => '',
   145 			'ID'                => '',
   173 			'ID'                     => '',
   146 			'site__in'          => '',
   174 			'site__in'               => '',
   147 			'site__not_in'      => '',
   175 			'site__not_in'           => '',
   148 			'number'            => 100,
   176 			'number'                 => 100,
   149 			'offset'            => '',
   177 			'offset'                 => '',
   150 			'no_found_rows'     => true,
   178 			'no_found_rows'          => true,
   151 			'orderby'           => 'id',
   179 			'orderby'                => 'id',
   152 			'order'             => 'ASC',
   180 			'order'                  => 'ASC',
   153 			'network_id'        => 0,
   181 			'network_id'             => 0,
   154 			'network__in'       => '',
   182 			'network__in'            => '',
   155 			'network__not_in'   => '',
   183 			'network__not_in'        => '',
   156 			'domain'            => '',
   184 			'domain'                 => '',
   157 			'domain__in'        => '',
   185 			'domain__in'             => '',
   158 			'domain__not_in'    => '',
   186 			'domain__not_in'         => '',
   159 			'path'              => '',
   187 			'path'                   => '',
   160 			'path__in'          => '',
   188 			'path__in'               => '',
   161 			'path__not_in'      => '',
   189 			'path__not_in'           => '',
   162 			'public'            => null,
   190 			'public'                 => null,
   163 			'archived'          => null,
   191 			'archived'               => null,
   164 			'mature'            => null,
   192 			'mature'                 => null,
   165 			'spam'              => null,
   193 			'spam'                   => null,
   166 			'deleted'           => null,
   194 			'deleted'                => null,
   167 			'lang_id'           => null,
   195 			'lang_id'                => null,
   168 			'lang__in'          => '',
   196 			'lang__in'               => '',
   169 			'lang__not_in'      => '',
   197 			'lang__not_in'           => '',
   170 			'search'            => '',
   198 			'search'                 => '',
   171 			'search_columns'    => array(),
   199 			'search_columns'         => array(),
   172 			'count'             => false,
   200 			'count'                  => false,
   173 			'date_query'        => null, // See WP_Date_Query
   201 			'date_query'             => null, // See WP_Date_Query
   174 			'update_site_cache' => true,
   202 			'update_site_cache'      => true,
       
   203 			'update_site_meta_cache' => true,
       
   204 			'meta_query'             => '',
       
   205 			'meta_key'               => '',
       
   206 			'meta_value'             => '',
       
   207 			'meta_type'              => '',
       
   208 			'meta_compare'           => '',
   175 		);
   209 		);
   176 
   210 
   177 		if ( ! empty( $query ) ) {
   211 		if ( ! empty( $query ) ) {
   178 			$this->query( $query );
   212 			$this->query( $query );
   179 		}
   213 		}
   223 	/**
   257 	/**
   224 	 * Retrieves a list of sites matching the query vars.
   258 	 * Retrieves a list of sites matching the query vars.
   225 	 *
   259 	 *
   226 	 * @since 4.6.0
   260 	 * @since 4.6.0
   227 	 *
   261 	 *
       
   262 	 * @global wpdb $wpdb WordPress database abstraction object.
       
   263 	 *
   228 	 * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
   264 	 * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
   229 	 *                   or the number of sites when 'count' is passed as a query var.
   265 	 *                   or the number of sites when 'count' is passed as a query var.
   230 	 */
   266 	 */
   231 	public function get_sites() {
   267 	public function get_sites() {
       
   268 		global $wpdb;
       
   269 
   232 		$this->parse_query();
   270 		$this->parse_query();
       
   271 
       
   272 		// Parse meta query.
       
   273 		$this->meta_query = new WP_Meta_Query();
       
   274 		$this->meta_query->parse_query_vars( $this->query_vars );
   233 
   275 
   234 		/**
   276 		/**
   235 		 * Fires before sites are retrieved.
   277 		 * Fires before sites are retrieved.
   236 		 *
   278 		 *
   237 		 * @since 4.6.0
   279 		 * @since 4.6.0
   238 		 *
   280 		 *
   239 		 * @param WP_Site_Query $this Current instance of WP_Site_Query (passed by reference).
   281 		 * @param WP_Site_Query $this Current instance of WP_Site_Query (passed by reference).
   240 		 */
   282 		 */
   241 		do_action_ref_array( 'pre_get_sites', array( &$this ) );
   283 		do_action_ref_array( 'pre_get_sites', array( &$this ) );
   242 
   284 
   243 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   285 		// Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
   244 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   286 		$this->meta_query->parse_query_vars( $this->query_vars );
   245 
   287 		if ( ! empty( $this->meta_query->queries ) ) {
   246 		// Ignore the $fields argument as the queried result will be the same regardless.
   288 			$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
   247 		unset( $_args['fields'] );
   289 		}
   248 
   290 
   249 		$key = md5( serialize( $_args ) );
   291 		$site_ids = null;
   250 		$last_changed = wp_cache_get_last_changed( 'sites' );
   292 
   251 
   293 		/**
   252 		$cache_key = "get_sites:$key:$last_changed";
   294 		 * Filter the sites array before the query takes place.
   253 		$cache_value = wp_cache_get( $cache_key, 'sites' );
   295 		 *
   254 
   296 		 * Return a non-null value to bypass WordPress's default site queries.
   255 		if ( false === $cache_value ) {
   297 		 *
   256 			$site_ids = $this->get_site_ids();
   298 		 *
   257 			if ( $site_ids ) {
   299 		 * @since 5.2.0
   258 				$this->set_found_sites();
   300 		 *
       
   301 		 * @param array|null    $site_ids Return an array of site data to short-circuit WP's site query,
       
   302 		 *                                or null to allow WP to run its normal queries.
       
   303 		 * @param WP_Site_Query $this The WP_Site_Query instance, passed by reference.
       
   304 		 */
       
   305 		$site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) );
       
   306 
       
   307 		if ( null === $site_ids ) {
       
   308 
       
   309 			// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
       
   310 			$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
       
   311 
       
   312 			// Ignore the $fields argument as the queried result will be the same regardless.
       
   313 			unset( $_args['fields'] );
       
   314 
       
   315 			$key          = md5( serialize( $_args ) );
       
   316 			$last_changed = wp_cache_get_last_changed( 'sites' );
       
   317 
       
   318 			$cache_key   = "get_sites:$key:$last_changed";
       
   319 			$cache_value = wp_cache_get( $cache_key, 'sites' );
       
   320 
       
   321 			if ( false === $cache_value ) {
       
   322 				$site_ids = $this->get_site_ids();
       
   323 				if ( $site_ids ) {
       
   324 					$this->set_found_sites();
       
   325 				}
       
   326 
       
   327 				$cache_value = array(
       
   328 					'site_ids'    => $site_ids,
       
   329 					'found_sites' => $this->found_sites,
       
   330 				);
       
   331 				wp_cache_add( $cache_key, $cache_value, 'sites' );
       
   332 			} else {
       
   333 				$site_ids          = $cache_value['site_ids'];
       
   334 				$this->found_sites = $cache_value['found_sites'];
   259 			}
   335 			}
   260 
       
   261 			$cache_value = array(
       
   262 				'site_ids' => $site_ids,
       
   263 				'found_sites' => $this->found_sites,
       
   264 			);
       
   265 			wp_cache_add( $cache_key, $cache_value, 'sites' );
       
   266 		} else {
       
   267 			$site_ids = $cache_value['site_ids'];
       
   268 			$this->found_sites = $cache_value['found_sites'];
       
   269 		}
   336 		}
   270 
   337 
   271 		if ( $this->found_sites && $this->query_vars['number'] ) {
   338 		if ( $this->found_sites && $this->query_vars['number'] ) {
   272 			$this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
   339 			$this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
   273 		}
   340 		}
   286 			return $this->sites;
   353 			return $this->sites;
   287 		}
   354 		}
   288 
   355 
   289 		// Prime site network caches.
   356 		// Prime site network caches.
   290 		if ( $this->query_vars['update_site_cache'] ) {
   357 		if ( $this->query_vars['update_site_cache'] ) {
   291 			_prime_site_caches( $site_ids );
   358 			_prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
   292 		}
   359 		}
   293 
   360 
   294 		// Fetch full site objects from the primed cache.
   361 		// Fetch full site objects from the primed cache.
   295 		$_sites = array();
   362 		$_sites = array();
   296 		foreach ( $site_ids as $site_id ) {
   363 		foreach ( $site_ids as $site_id ) {
   302 		/**
   369 		/**
   303 		 * Filters the site query results.
   370 		 * Filters the site query results.
   304 		 *
   371 		 *
   305 		 * @since 4.6.0
   372 		 * @since 4.6.0
   306 		 *
   373 		 *
   307 		 * @param array         $_sites An array of WP_Site objects.
   374 		 * @param WP_Site[]     $_sites An array of WP_Site objects.
   308 		 * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
   375 		 * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
   309 		 */
   376 		 */
   310 		$_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
   377 		$_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
   311 
   378 
   312 		// Convert to WP_Site instances.
   379 		// Convert to WP_Site instances.
   343 					continue;
   410 					continue;
   344 				}
   411 				}
   345 
   412 
   346 				if ( is_int( $_key ) ) {
   413 				if ( is_int( $_key ) ) {
   347 					$_orderby = $_value;
   414 					$_orderby = $_value;
   348 					$_order = $order;
   415 					$_order   = $order;
   349 				} else {
   416 				} else {
   350 					$_orderby = $_key;
   417 					$_orderby = $_key;
   351 					$_order = $_value;
   418 					$_order   = $_value;
   352 				}
   419 				}
   353 
   420 
   354 				$parsed = $this->parse_orderby( $_orderby );
   421 				$parsed = $this->parse_orderby( $_orderby );
   355 
   422 
   356 				if ( ! $parsed ) {
   423 				if ( ! $parsed ) {
   365 				$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
   432 				$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
   366 			}
   433 			}
   367 
   434 
   368 			$orderby = implode( ', ', $orderby_array );
   435 			$orderby = implode( ', ', $orderby_array );
   369 		} else {
   436 		} else {
   370 			$orderby = "blog_id $order";
   437 			$orderby = "{$wpdb->blogs}.blog_id $order";
   371 		}
   438 		}
   372 
   439 
   373 		$number = absint( $this->query_vars['number'] );
   440 		$number = absint( $this->query_vars['number'] );
   374 		$offset = absint( $this->query_vars['offset'] );
   441 		$offset = absint( $this->query_vars['offset'] );
       
   442 		$limits = '';
   375 
   443 
   376 		if ( ! empty( $number ) ) {
   444 		if ( ! empty( $number ) ) {
   377 			if ( $offset ) {
   445 			if ( $offset ) {
   378 				$limits = 'LIMIT ' . $offset . ',' . $number;
   446 				$limits = 'LIMIT ' . $offset . ',' . $number;
   379 			} else {
   447 			} else {
   382 		}
   450 		}
   383 
   451 
   384 		if ( $this->query_vars['count'] ) {
   452 		if ( $this->query_vars['count'] ) {
   385 			$fields = 'COUNT(*)';
   453 			$fields = 'COUNT(*)';
   386 		} else {
   454 		} else {
   387 			$fields = 'blog_id';
   455 			$fields = "{$wpdb->blogs}.blog_id";
   388 		}
   456 		}
   389 
   457 
   390 		// Parse site IDs for an IN clause.
   458 		// Parse site IDs for an IN clause.
   391 		$site_id = absint( $this->query_vars['ID'] );
   459 		$site_id = absint( $this->query_vars['ID'] );
   392 		if ( ! empty( $site_id ) ) {
   460 		if ( ! empty( $site_id ) ) {
   393 			$this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
   461 			$this->sql_clauses['where']['ID'] = $wpdb->prepare( "{$wpdb->blogs}.blog_id = %d", $site_id );
   394 		}
   462 		}
   395 
   463 
   396 		// Parse site IDs for an IN clause.
   464 		// Parse site IDs for an IN clause.
   397 		if ( ! empty( $this->query_vars['site__in'] ) ) {
   465 		if ( ! empty( $this->query_vars['site__in'] ) ) {
   398 			$this->sql_clauses['where']['site__in'] = "blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
   466 			$this->sql_clauses['where']['site__in'] = "{$wpdb->blogs}.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
   399 		}
   467 		}
   400 
   468 
   401 		// Parse site IDs for a NOT IN clause.
   469 		// Parse site IDs for a NOT IN clause.
   402 		if ( ! empty( $this->query_vars['site__not_in'] ) ) {
   470 		if ( ! empty( $this->query_vars['site__not_in'] ) ) {
   403 			$this->sql_clauses['where']['site__not_in'] = "blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
   471 			$this->sql_clauses['where']['site__not_in'] = "{$wpdb->blogs}.blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
   404 		}
   472 		}
   405 
   473 
   406 		$network_id = absint( $this->query_vars['network_id'] );
   474 		$network_id = absint( $this->query_vars['network_id'] );
   407 
   475 
   408 		if ( ! empty( $network_id ) ) {
   476 		if ( ! empty( $network_id ) ) {
   446 		if ( is_array( $this->query_vars['path__not_in'] ) ) {
   514 		if ( is_array( $this->query_vars['path__not_in'] ) ) {
   447 			$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
   515 			$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
   448 		}
   516 		}
   449 
   517 
   450 		if ( is_numeric( $this->query_vars['archived'] ) ) {
   518 		if ( is_numeric( $this->query_vars['archived'] ) ) {
   451 			$archived = absint( $this->query_vars['archived'] );
   519 			$archived                               = absint( $this->query_vars['archived'] );
   452 			$this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %s ", absint( $archived ) );
   520 			$this->sql_clauses['where']['archived'] = $wpdb->prepare( 'archived = %s ', absint( $archived ) );
   453 		}
   521 		}
   454 
   522 
   455 		if ( is_numeric( $this->query_vars['mature'] ) ) {
   523 		if ( is_numeric( $this->query_vars['mature'] ) ) {
   456 			$mature = absint( $this->query_vars['mature'] );
   524 			$mature                               = absint( $this->query_vars['mature'] );
   457 			$this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature );
   525 			$this->sql_clauses['where']['mature'] = $wpdb->prepare( 'mature = %d ', $mature );
   458 		}
   526 		}
   459 
   527 
   460 		if ( is_numeric( $this->query_vars['spam'] ) ) {
   528 		if ( is_numeric( $this->query_vars['spam'] ) ) {
   461 			$spam = absint( $this->query_vars['spam'] );
   529 			$spam                               = absint( $this->query_vars['spam'] );
   462 			$this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam );
   530 			$this->sql_clauses['where']['spam'] = $wpdb->prepare( 'spam = %d ', $spam );
   463 		}
   531 		}
   464 
   532 
   465 		if ( is_numeric( $this->query_vars['deleted'] ) ) {
   533 		if ( is_numeric( $this->query_vars['deleted'] ) ) {
   466 			$deleted = absint( $this->query_vars['deleted'] );
   534 			$deleted                               = absint( $this->query_vars['deleted'] );
   467 			$this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted );
   535 			$this->sql_clauses['where']['deleted'] = $wpdb->prepare( 'deleted = %d ', $deleted );
   468 		}
   536 		}
   469 
   537 
   470 		if ( is_numeric( $this->query_vars['public'] ) ) {
   538 		if ( is_numeric( $this->query_vars['public'] ) ) {
   471 			$public = absint( $this->query_vars['public'] );
   539 			$public                               = absint( $this->query_vars['public'] );
   472 			$this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
   540 			$this->sql_clauses['where']['public'] = $wpdb->prepare( 'public = %d ', $public );
   473 		}
   541 		}
   474 
   542 
   475 		if ( is_numeric( $this->query_vars['lang_id'] ) ) {
   543 		if ( is_numeric( $this->query_vars['lang_id'] ) ) {
   476 			$lang_id = absint( $this->query_vars['lang_id'] );
   544 			$lang_id                               = absint( $this->query_vars['lang_id'] );
   477 			$this->sql_clauses['where']['lang_id'] = $wpdb->prepare( "lang_id = %d ", $lang_id );
   545 			$this->sql_clauses['where']['lang_id'] = $wpdb->prepare( 'lang_id = %d ', $lang_id );
   478 		}
   546 		}
   479 
   547 
   480 		// Parse site language IDs for an IN clause.
   548 		// Parse site language IDs for an IN clause.
   481 		if ( ! empty( $this->query_vars['lang__in'] ) ) {
   549 		if ( ! empty( $this->query_vars['lang__in'] ) ) {
   482 			$this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';
   550 			$this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';
   504 			 *
   572 			 *
   505 			 * The default columns include 'domain' and 'path.
   573 			 * The default columns include 'domain' and 'path.
   506 			 *
   574 			 *
   507 			 * @since 4.6.0
   575 			 * @since 4.6.0
   508 			 *
   576 			 *
   509 			 * @param array         $search_columns Array of column names to be searched.
   577 			 * @param string[]      $search_columns Array of column names to be searched.
   510 			 * @param string        $search         Text being searched.
   578 			 * @param string        $search         Text being searched.
   511 			 * @param WP_Site_Query $this           The current WP_Site_Query instance.
   579 			 * @param WP_Site_Query $this           The current WP_Site_Query instance.
   512 			 */
   580 			 */
   513 			$search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
   581 			$search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
   514 
   582 
   515 			$this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
   583 			$this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
   516 		}
   584 		}
   517 
   585 
   518 		$date_query = $this->query_vars['date_query'];
   586 		$date_query = $this->query_vars['date_query'];
   519 		if ( ! empty( $date_query ) && is_array( $date_query ) ) {
   587 		if ( ! empty( $date_query ) && is_array( $date_query ) ) {
   520 			$this->date_query = new WP_Date_Query( $date_query, 'registered' );
   588 			$this->date_query                         = new WP_Date_Query( $date_query, 'registered' );
   521 			$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
   589 			$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
   522 		}
   590 		}
   523 
   591 
   524 		$join = '';
   592 		$join    = '';
       
   593 		$groupby = '';
       
   594 
       
   595 		if ( ! empty( $this->meta_query_clauses ) ) {
       
   596 			$join .= $this->meta_query_clauses['join'];
       
   597 
       
   598 			// Strip leading 'AND'.
       
   599 			$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
       
   600 
       
   601 			if ( ! $this->query_vars['count'] ) {
       
   602 				$groupby = "{$wpdb->blogs}.blog_id";
       
   603 			}
       
   604 		}
   525 
   605 
   526 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   606 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   527 
   607 
   528 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   608 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   529 
   609 
   530 		/**
   610 		/**
   531 		 * Filters the site query clauses.
   611 		 * Filters the site query clauses.
   532 		 *
   612 		 *
   533 		 * @since 4.6.0
   613 		 * @since 4.6.0
   534 		 *
   614 		 *
   535 		 * @param array         $pieces A compacted array of site query clauses.
   615 		 * @param string[]      $pieces An associative array of site query clauses.
   536 		 * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
   616 		 * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
   537 		 */
   617 		 */
   538 		$clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
   618 		$clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
   539 
   619 
   540 		$fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   620 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   541 		$join = isset( $clauses['join'] ) ? $clauses['join'] : '';
   621 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   542 		$where = isset( $clauses['where'] ) ? $clauses['where'] : '';
   622 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   543 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   623 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   544 		$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
   624 		$limits  = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
   545 		$groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
   625 		$groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
   546 
   626 
   547 		if ( $where ) {
   627 		if ( $where ) {
   548 			$where = 'WHERE ' . $where;
   628 			$where = 'WHERE ' . $where;
   549 		}
   629 		}
   609 	 *
   689 	 *
   610 	 * @since 4.6.0
   690 	 * @since 4.6.0
   611 	 *
   691 	 *
   612 	 * @global wpdb  $wpdb WordPress database abstraction object.
   692 	 * @global wpdb  $wpdb WordPress database abstraction object.
   613 	 *
   693 	 *
   614 	 * @param string $string  Search string.
   694 	 * @param string   $string  Search string.
   615 	 * @param array  $columns Columns to search.
   695 	 * @param string[] $columns Array of columns to search.
   616 	 * @return string Search SQL.
   696 	 * @return string Search SQL.
   617 	 */
   697 	 */
   618 	protected function get_search_sql( $string, $columns ) {
   698 	protected function get_search_sql( $string, $columns ) {
   619 		global $wpdb;
   699 		global $wpdb;
   620 
   700 
   648 		$parsed = false;
   728 		$parsed = false;
   649 
   729 
   650 		switch ( $orderby ) {
   730 		switch ( $orderby ) {
   651 			case 'site__in':
   731 			case 'site__in':
   652 				$site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
   732 				$site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
   653 				$parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
   733 				$parsed   = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
   654 				break;
   734 				break;
   655 			case 'network__in':
   735 			case 'network__in':
   656 				$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   736 				$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   657 				$parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
   737 				$parsed      = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
   658 				break;
   738 				break;
   659 			case 'domain':
   739 			case 'domain':
   660 			case 'last_updated':
   740 			case 'last_updated':
   661 			case 'path':
   741 			case 'path':
   662 			case 'registered':
   742 			case 'registered':
   670 				break;
   750 				break;
   671 			case 'path_length':
   751 			case 'path_length':
   672 				$parsed = 'CHAR_LENGTH(path)';
   752 				$parsed = 'CHAR_LENGTH(path)';
   673 				break;
   753 				break;
   674 			case 'id':
   754 			case 'id':
   675 				$parsed = 'blog_id';
   755 				$parsed = "{$wpdb->blogs}.blog_id";
   676 				break;
   756 				break;
       
   757 		}
       
   758 
       
   759 		if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) {
       
   760 			return $parsed;
       
   761 		}
       
   762 
       
   763 		$meta_clauses = $this->meta_query->get_clauses();
       
   764 		if ( empty( $meta_clauses ) ) {
       
   765 			return $parsed;
       
   766 		}
       
   767 
       
   768 		$primary_meta_query = reset( $meta_clauses );
       
   769 		if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) {
       
   770 			$orderby = 'meta_value';
       
   771 		}
       
   772 
       
   773 		switch ( $orderby ) {
       
   774 			case 'meta_value':
       
   775 				if ( ! empty( $primary_meta_query['type'] ) ) {
       
   776 					$parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
       
   777 				} else {
       
   778 					$parsed = "{$primary_meta_query['alias']}.meta_value";
       
   779 				}
       
   780 				break;
       
   781 			case 'meta_value_num':
       
   782 				$parsed = "{$primary_meta_query['alias']}.meta_value+0";
       
   783 				break;
       
   784 			default:
       
   785 				if ( isset( $meta_clauses[ $orderby ] ) ) {
       
   786 					$meta_clause = $meta_clauses[ $orderby ];
       
   787 					$parsed      = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
       
   788 				}
   677 		}
   789 		}
   678 
   790 
   679 		return $parsed;
   791 		return $parsed;
   680 	}
   792 	}
   681 
   793