wp/wp-includes/class-wp-network-query.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
   167 	 * Sets up the WordPress query for retrieving networks.
   167 	 * Sets up the WordPress query for retrieving networks.
   168 	 *
   168 	 *
   169 	 * @since 4.6.0
   169 	 * @since 4.6.0
   170 	 *
   170 	 *
   171 	 * @param string|array $query Array or URL query string of parameters.
   171 	 * @param string|array $query Array or URL query string of parameters.
   172 	 * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids',
   172 	 * @return array|int List of WP_Network objects, a list of network IDs when 'fields' is set to 'ids',
   173 	 *                   or the number of networks when 'count' is passed as a query var.
   173 	 *                   or the number of networks when 'count' is passed as a query var.
   174 	 */
   174 	 */
   175 	public function query( $query ) {
   175 	public function query( $query ) {
   176 		$this->query_vars = wp_parse_args( $query );
   176 		$this->query_vars = wp_parse_args( $query );
   177 		return $this->get_networks();
   177 		return $this->get_networks();
   180 	/**
   180 	/**
   181 	 * Gets a list of networks matching the query vars.
   181 	 * Gets a list of networks matching the query vars.
   182 	 *
   182 	 *
   183 	 * @since 4.6.0
   183 	 * @since 4.6.0
   184 	 *
   184 	 *
   185 	 * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids',
   185 	 * @return array|int List of WP_Network objects, a list of network IDs when 'fields' is set to 'ids',
   186 	 *                   or the number of networks when 'count' is passed as a query var.
   186 	 *                   or the number of networks when 'count' is passed as a query var.
   187 	 */
   187 	 */
   188 	public function get_networks() {
   188 	public function get_networks() {
   189 		$this->parse_query();
   189 		$this->parse_query();
   190 
   190 
   195 		 *
   195 		 *
   196 		 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference).
   196 		 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference).
   197 		 */
   197 		 */
   198 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   198 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   199 
   199 
   200 		$network_ids = null;
   200 		$network_data = null;
   201 
   201 
   202 		/**
   202 		/**
   203 		 * Filter the sites array before the query takes place.
   203 		 * Filter the network data before the query takes place.
   204 		 *
   204 		 *
   205 		 * Return a non-null value to bypass WordPress's default site queries.
   205 		 * Return a non-null value to bypass WordPress's default network queries.
   206 		 *
   206 		 *
       
   207 		 * The expected return type from this filter depends on the value passed in the request query_vars.
       
   208 		 * When `$this->query_vars['count']` is set, the filter should return the network count as an int.
       
   209 		 * When `'ids' === $this->query_vars['fields']`, the filter should return an array of network IDs.
       
   210 		 * Otherwise the filter should return an array of WP_Network objects.
   207 		 *
   211 		 *
   208 		 * @since 5.2.0
   212 		 * @since 5.2.0
   209 		 *
   213 		 *
   210 		 * @param array|null       $site_ids Return an array of site data to short-circuit WP's site query,
   214 		 * @param array|null       $network_data Return an array of network data to short-circuit WP's network query,
   211 		 *                                   or null to allow WP to run its normal queries.
   215 		 *                                       the network count as an integer if `$this->query_vars['count']` is set,
   212 		 * @param WP_Network_Query $this     The WP_Network_Query instance, passed by reference.
   216 		 *                                       or null to allow WP to run its normal queries.
       
   217 		 * @param WP_Network_Query $this         The WP_Network_Query instance, passed by reference.
   213 		 */
   218 		 */
   214 		$network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) );
   219 		$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );
   215 
   220 
   216 		if ( null === $network_ids ) {
   221 		if ( null !== $network_data ) {
   217 
   222 			return $network_data;
   218 			// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   223 		}
   219 			$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   224 
   220 
   225 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   221 			// Ignore the $fields argument as the queried result will be the same regardless.
   226 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   222 			unset( $_args['fields'] );
   227 
   223 
   228 		// Ignore the $fields argument as the queried result will be the same regardless.
   224 			$key          = md5( serialize( $_args ) );
   229 		unset( $_args['fields'] );
   225 			$last_changed = wp_cache_get_last_changed( 'networks' );
   230 
   226 
   231 		$key          = md5( serialize( $_args ) );
   227 			$cache_key   = "get_network_ids:$key:$last_changed";
   232 		$last_changed = wp_cache_get_last_changed( 'networks' );
   228 			$cache_value = wp_cache_get( $cache_key, 'networks' );
   233 
   229 
   234 		$cache_key   = "get_network_ids:$key:$last_changed";
   230 			if ( false === $cache_value ) {
   235 		$cache_value = wp_cache_get( $cache_key, 'networks' );
   231 				$network_ids = $this->get_network_ids();
   236 
   232 				if ( $network_ids ) {
   237 		if ( false === $cache_value ) {
   233 					$this->set_found_networks();
   238 			$network_ids = $this->get_network_ids();
   234 				}
   239 			if ( $network_ids ) {
   235 
   240 				$this->set_found_networks();
   236 				$cache_value = array(
       
   237 					'network_ids'    => $network_ids,
       
   238 					'found_networks' => $this->found_networks,
       
   239 				);
       
   240 				wp_cache_add( $cache_key, $cache_value, 'networks' );
       
   241 			} else {
       
   242 				$network_ids          = $cache_value['network_ids'];
       
   243 				$this->found_networks = $cache_value['found_networks'];
       
   244 			}
   241 			}
       
   242 
       
   243 			$cache_value = array(
       
   244 				'network_ids'    => $network_ids,
       
   245 				'found_networks' => $this->found_networks,
       
   246 			);
       
   247 			wp_cache_add( $cache_key, $cache_value, 'networks' );
       
   248 		} else {
       
   249 			$network_ids          = $cache_value['network_ids'];
       
   250 			$this->found_networks = $cache_value['found_networks'];
   245 		}
   251 		}
   246 
   252 
   247 		if ( $this->found_networks && $this->query_vars['number'] ) {
   253 		if ( $this->found_networks && $this->query_vars['number'] ) {
   248 			$this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
   254 			$this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
   249 		}
   255 		}
   254 			return intval( $network_ids );
   260 			return intval( $network_ids );
   255 		}
   261 		}
   256 
   262 
   257 		$network_ids = array_map( 'intval', $network_ids );
   263 		$network_ids = array_map( 'intval', $network_ids );
   258 
   264 
   259 		if ( 'ids' == $this->query_vars['fields'] ) {
   265 		if ( 'ids' === $this->query_vars['fields'] ) {
   260 			$this->networks = $network_ids;
   266 			$this->networks = $network_ids;
   261 			return $this->networks;
   267 			return $this->networks;
   262 		}
   268 		}
   263 
   269 
   264 		if ( $this->query_vars['update_network_cache'] ) {
   270 		if ( $this->query_vars['update_network_cache'] ) {
   266 		}
   272 		}
   267 
   273 
   268 		// Fetch full network objects from the primed cache.
   274 		// Fetch full network objects from the primed cache.
   269 		$_networks = array();
   275 		$_networks = array();
   270 		foreach ( $network_ids as $network_id ) {
   276 		foreach ( $network_ids as $network_id ) {
   271 			if ( $_network = get_network( $network_id ) ) {
   277 			$_network = get_network( $network_id );
       
   278 			if ( $_network ) {
   272 				$_networks[] = $_network;
   279 				$_networks[] = $_network;
   273 			}
   280 			}
   274 		}
   281 		}
   275 
   282 
   276 		/**
   283 		/**
   281 		 * @param WP_Network[]     $_networks An array of WP_Network objects.
   288 		 * @param WP_Network[]     $_networks An array of WP_Network objects.
   282 		 * @param WP_Network_Query $this      Current instance of WP_Network_Query (passed by reference).
   289 		 * @param WP_Network_Query $this      Current instance of WP_Network_Query (passed by reference).
   283 		 */
   290 		 */
   284 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   291 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   285 
   292 
   286 		// Convert to WP_Network instances
   293 		// Convert to WP_Network instances.
   287 		$this->networks = array_map( 'get_network', $_networks );
   294 		$this->networks = array_map( 'get_network', $_networks );
   288 
   295 
   289 		return $this->networks;
   296 		return $this->networks;
   290 	}
   297 	}
   291 
   298 
   496 	/**
   503 	/**
   497 	 * Used internally to generate an SQL string for searching across multiple columns.
   504 	 * Used internally to generate an SQL string for searching across multiple columns.
   498 	 *
   505 	 *
   499 	 * @since 4.6.0
   506 	 * @since 4.6.0
   500 	 *
   507 	 *
   501 	 * @global wpdb  $wpdb WordPress database abstraction object.
   508 	 * @global wpdb $wpdb WordPress database abstraction object.
   502 	 *
   509 	 *
   503 	 * @param string   $string  Search string.
   510 	 * @param string   $string  Search string.
   504 	 * @param string[] $columns Array of columns to search.
   511 	 * @param string[] $columns Array of columns to search.
   505 	 *
       
   506 	 * @return string Search SQL.
   512 	 * @return string Search SQL.
   507 	 */
   513 	 */
   508 	protected function get_search_sql( $string, $columns ) {
   514 	protected function get_search_sql( $string, $columns ) {
   509 		global $wpdb;
   515 		global $wpdb;
   510 
   516 
   536 			'domain',
   542 			'domain',
   537 			'path',
   543 			'path',
   538 		);
   544 		);
   539 
   545 
   540 		$parsed = false;
   546 		$parsed = false;
   541 		if ( $orderby == 'network__in' ) {
   547 		if ( 'network__in' === $orderby ) {
   542 			$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   548 			$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   543 			$parsed      = "FIELD( {$wpdb->site}.id, $network__in )";
   549 			$parsed      = "FIELD( {$wpdb->site}.id, $network__in )";
   544 		} elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
   550 		} elseif ( 'domain_length' === $orderby || 'path_length' === $orderby ) {
   545 			$field  = substr( $orderby, 0, -7 );
   551 			$field  = substr( $orderby, 0, -7 );
   546 			$parsed = "CHAR_LENGTH($wpdb->site.$field)";
   552 			$parsed = "CHAR_LENGTH($wpdb->site.$field)";
   547 		} elseif ( in_array( $orderby, $allowed_keys ) ) {
   553 		} elseif ( in_array( $orderby, $allowed_keys, true ) ) {
   548 			$parsed = "$wpdb->site.$orderby";
   554 			$parsed = "$wpdb->site.$orderby";
   549 		}
   555 		}
   550 
   556 
   551 		return $parsed;
   557 		return $parsed;
   552 	}
   558 	}