wp/wp-includes/class-wp-site-query.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    57 
    57 
    58 	/**
    58 	/**
    59 	 * Date query container.
    59 	 * Date query container.
    60 	 *
    60 	 *
    61 	 * @since 4.6.0
    61 	 * @since 4.6.0
    62 	 * @var object WP_Date_Query
    62 	 * @var WP_Date_Query A date query instance.
    63 	 */
    63 	 */
    64 	public $date_query = false;
    64 	public $date_query = false;
    65 
    65 
    66 	/**
    66 	/**
    67 	 * Query vars set by the user.
    67 	 * Query vars set by the user.
   196 			'lang__in'               => '',
   196 			'lang__in'               => '',
   197 			'lang__not_in'           => '',
   197 			'lang__not_in'           => '',
   198 			'search'                 => '',
   198 			'search'                 => '',
   199 			'search_columns'         => array(),
   199 			'search_columns'         => array(),
   200 			'count'                  => false,
   200 			'count'                  => false,
   201 			'date_query'             => null, // See WP_Date_Query
   201 			'date_query'             => null, // See WP_Date_Query.
   202 			'update_site_cache'      => true,
   202 			'update_site_cache'      => true,
   203 			'update_site_meta_cache' => true,
   203 			'update_site_meta_cache' => true,
   204 			'meta_query'             => '',
   204 			'meta_query'             => '',
   205 			'meta_key'               => '',
   205 			'meta_key'               => '',
   206 			'meta_value'             => '',
   206 			'meta_value'             => '',
   243 	 * Sets up the WordPress query for retrieving sites.
   243 	 * Sets up the WordPress query for retrieving sites.
   244 	 *
   244 	 *
   245 	 * @since 4.6.0
   245 	 * @since 4.6.0
   246 	 *
   246 	 *
   247 	 * @param string|array $query Array or URL query string of parameters.
   247 	 * @param string|array $query Array or URL query string of parameters.
   248 	 * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
   248 	 * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
   249 	 *                   or the number of sites when 'count' is passed as a query var.
   249 	 *                   or the number of sites when 'count' is passed as a query var.
   250 	 */
   250 	 */
   251 	public function query( $query ) {
   251 	public function query( $query ) {
   252 		$this->query_vars = wp_parse_args( $query );
   252 		$this->query_vars = wp_parse_args( $query );
   253 
   253 
   259 	 *
   259 	 *
   260 	 * @since 4.6.0
   260 	 * @since 4.6.0
   261 	 *
   261 	 *
   262 	 * @global wpdb $wpdb WordPress database abstraction object.
   262 	 * @global wpdb $wpdb WordPress database abstraction object.
   263 	 *
   263 	 *
   264 	 * @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',
   265 	 *                   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.
   266 	 */
   266 	 */
   267 	public function get_sites() {
   267 	public function get_sites() {
   268 		global $wpdb;
   268 		global $wpdb;
   269 
   269 
   286 		$this->meta_query->parse_query_vars( $this->query_vars );
   286 		$this->meta_query->parse_query_vars( $this->query_vars );
   287 		if ( ! empty( $this->meta_query->queries ) ) {
   287 		if ( ! empty( $this->meta_query->queries ) ) {
   288 			$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
   288 			$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
   289 		}
   289 		}
   290 
   290 
   291 		$site_ids = null;
   291 		$site_data = null;
   292 
   292 
   293 		/**
   293 		/**
   294 		 * Filter the sites array before the query takes place.
   294 		 * Filter the site data before the get_sites query takes place.
   295 		 *
   295 		 *
   296 		 * Return a non-null value to bypass WordPress's default site queries.
   296 		 * Return a non-null value to bypass WordPress's default site queries.
   297 		 *
   297 		 *
       
   298 		 * The expected return type from this filter depends on the value passed in the request query_vars:
       
   299 		 * When `$this->query_vars['count']` is set, the filter should return the site count as an int.
       
   300 		 * When `'ids' === $this->query_vars['fields']`, the filter should return an array of site IDs.
       
   301 		 * Otherwise the filter should return an array of WP_Site objects.
   298 		 *
   302 		 *
   299 		 * @since 5.2.0
   303 		 * @since 5.2.0
   300 		 *
   304 		 *
   301 		 * @param array|null    $site_ids Return an array of site data to short-circuit WP's site query,
   305 		 * @param array|int|null $site_data Return an array of site data to short-circuit WP's site query,
   302 		 *                                or null to allow WP to run its normal queries.
   306 		 *                                  the site count as an integer if `$this->query_vars['count']` is set,
   303 		 * @param WP_Site_Query $this The WP_Site_Query instance, passed by reference.
   307 		 *                                  or null to run the normal queries.
       
   308 		 * @param WP_Site_Query  $this      The WP_Site_Query instance, passed by reference.
   304 		 */
   309 		 */
   305 		$site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) );
   310 		$site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
   306 
   311 
   307 		if ( null === $site_ids ) {
   312 		if ( null !== $site_data ) {
   308 
   313 			return $site_data;
   309 			// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   314 		}
   310 			$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   315 
   311 
   316 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   312 			// Ignore the $fields argument as the queried result will be the same regardless.
   317 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   313 			unset( $_args['fields'] );
   318 
   314 
   319 		// Ignore the $fields argument as the queried result will be the same regardless.
   315 			$key          = md5( serialize( $_args ) );
   320 		unset( $_args['fields'] );
   316 			$last_changed = wp_cache_get_last_changed( 'sites' );
   321 
   317 
   322 		$key          = md5( serialize( $_args ) );
   318 			$cache_key   = "get_sites:$key:$last_changed";
   323 		$last_changed = wp_cache_get_last_changed( 'sites' );
   319 			$cache_value = wp_cache_get( $cache_key, 'sites' );
   324 
   320 
   325 		$cache_key   = "get_sites:$key:$last_changed";
   321 			if ( false === $cache_value ) {
   326 		$cache_value = wp_cache_get( $cache_key, 'sites' );
   322 				$site_ids = $this->get_site_ids();
   327 
   323 				if ( $site_ids ) {
   328 		if ( false === $cache_value ) {
   324 					$this->set_found_sites();
   329 			$site_ids = $this->get_site_ids();
   325 				}
   330 			if ( $site_ids ) {
   326 
   331 				$this->set_found_sites();
   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'];
       
   335 			}
   332 			}
       
   333 
       
   334 			$cache_value = array(
       
   335 				'site_ids'    => $site_ids,
       
   336 				'found_sites' => $this->found_sites,
       
   337 			);
       
   338 			wp_cache_add( $cache_key, $cache_value, 'sites' );
       
   339 		} else {
       
   340 			$site_ids          = $cache_value['site_ids'];
       
   341 			$this->found_sites = $cache_value['found_sites'];
   336 		}
   342 		}
   337 
   343 
   338 		if ( $this->found_sites && $this->query_vars['number'] ) {
   344 		if ( $this->found_sites && $this->query_vars['number'] ) {
   339 			$this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
   345 			$this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
   340 		}
   346 		}
   345 			return intval( $site_ids );
   351 			return intval( $site_ids );
   346 		}
   352 		}
   347 
   353 
   348 		$site_ids = array_map( 'intval', $site_ids );
   354 		$site_ids = array_map( 'intval', $site_ids );
   349 
   355 
   350 		if ( 'ids' == $this->query_vars['fields'] ) {
   356 		if ( 'ids' === $this->query_vars['fields'] ) {
   351 			$this->sites = $site_ids;
   357 			$this->sites = $site_ids;
   352 
   358 
   353 			return $this->sites;
   359 			return $this->sites;
   354 		}
   360 		}
   355 
   361 
   359 		}
   365 		}
   360 
   366 
   361 		// Fetch full site objects from the primed cache.
   367 		// Fetch full site objects from the primed cache.
   362 		$_sites = array();
   368 		$_sites = array();
   363 		foreach ( $site_ids as $site_id ) {
   369 		foreach ( $site_ids as $site_id ) {
   364 			if ( $_site = get_site( $site_id ) ) {
   370 			$_site = get_site( $site_id );
       
   371 			if ( $_site ) {
   365 				$_sites[] = $_site;
   372 				$_sites[] = $_site;
   366 			}
   373 			}
   367 		}
   374 		}
   368 
   375 
   369 		/**
   376 		/**
   687 	/**
   694 	/**
   688 	 * Used internally to generate an SQL string for searching across multiple columns.
   695 	 * Used internally to generate an SQL string for searching across multiple columns.
   689 	 *
   696 	 *
   690 	 * @since 4.6.0
   697 	 * @since 4.6.0
   691 	 *
   698 	 *
   692 	 * @global wpdb  $wpdb WordPress database abstraction object.
   699 	 * @global wpdb $wpdb WordPress database abstraction object.
   693 	 *
   700 	 *
   694 	 * @param string   $string  Search string.
   701 	 * @param string   $string  Search string.
   695 	 * @param string[] $columns Array of columns to search.
   702 	 * @param string[] $columns Array of columns to search.
   696 	 * @return string Search SQL.
   703 	 * @return string Search SQL.
   697 	 */
   704 	 */