wp/wp-includes/class-wp-network-query.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    89 	 * @param string|array $query {
    89 	 * @param string|array $query {
    90 	 *     Optional. Array or query string of network query parameters. Default empty.
    90 	 *     Optional. Array or query string of network query parameters. Default empty.
    91 	 *
    91 	 *
    92 	 *     @type array        $network__in          Array of network IDs to include. Default empty.
    92 	 *     @type array        $network__in          Array of network IDs to include. Default empty.
    93 	 *     @type array        $network__not_in      Array of network IDs to exclude. Default empty.
    93 	 *     @type array        $network__not_in      Array of network IDs to exclude. Default empty.
    94  	 *     @type bool         $count                Whether to return a network count (true) or array of network objects.
    94 	 *     @type bool         $count                Whether to return a network count (true) or array of network objects.
    95  	 *                                              Default false.
    95 	 *                                              Default false.
    96  	 *     @type string       $fields               Network fields to return. Accepts 'ids' (returns an array of network IDs)
    96 	 *     @type string       $fields               Network fields to return. Accepts 'ids' (returns an array of network IDs)
    97  	 *                                              or empty (returns an array of complete network objects). Default empty.
    97 	 *                                              or empty (returns an array of complete network objects). Default empty.
    98  	 *     @type int          $number               Maximum number of networks to retrieve. Default empty (no limit).
    98 	 *     @type int          $number               Maximum number of networks to retrieve. Default empty (no limit).
    99  	 *     @type int          $offset               Number of networks to offset the query. Used to build LIMIT clause.
    99 	 *     @type int          $offset               Number of networks to offset the query. Used to build LIMIT clause.
   100  	 *                                              Default 0.
   100 	 *                                              Default 0.
   101  	 *     @type bool         $no_found_rows        Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   101 	 *     @type bool         $no_found_rows        Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   102  	 *     @type string|array $orderby              Network status or array of statuses. Accepts 'id', 'domain', 'path',
   102 	 *     @type string|array $orderby              Network status or array of statuses. Accepts 'id', 'domain', 'path',
   103  	 *                                              'domain_length', 'path_length' and 'network__in'. Also accepts false,
   103 	 *                                              'domain_length', 'path_length' and 'network__in'. Also accepts false,
   104  	 *                                              an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
   104 	 *                                              an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
   105  	 *     @type string       $order                How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
   105 	 *     @type string       $order                How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
   106  	 *     @type string       $domain               Limit results to those affiliated with a given domain. Default empty.
   106 	 *     @type string       $domain               Limit results to those affiliated with a given domain. Default empty.
   107  	 *     @type array        $domain__in           Array of domains to include affiliated networks for. Default empty.
   107 	 *     @type array        $domain__in           Array of domains to include affiliated networks for. Default empty.
   108  	 *     @type array        $domain__not_in       Array of domains to exclude affiliated networks for. Default empty.
   108 	 *     @type array        $domain__not_in       Array of domains to exclude affiliated networks for. Default empty.
   109  	 *     @type string       $path                 Limit results to those affiliated with a given path. Default empty.
   109 	 *     @type string       $path                 Limit results to those affiliated with a given path. Default empty.
   110  	 *     @type array        $path__in             Array of paths to include affiliated networks for. Default empty.
   110 	 *     @type array        $path__in             Array of paths to include affiliated networks for. Default empty.
   111  	 *     @type array        $path__not_in         Array of paths to exclude affiliated networks for. Default empty.
   111 	 *     @type array        $path__not_in         Array of paths to exclude affiliated networks for. Default empty.
   112  	 *     @type string       $search               Search term(s) to retrieve matching networks for. Default empty.
   112 	 *     @type string       $search               Search term(s) to retrieve matching networks for. Default empty.
   113 	 *     @type bool         $update_network_cache Whether to prime the cache for found networks. Default true.
   113 	 *     @type bool         $update_network_cache Whether to prime the cache for found networks. Default true.
   114 	 * }
   114 	 * }
   115 	 */
   115 	 */
   116 	public function __construct( $query = '' ) {
   116 	public function __construct( $query = '' ) {
   117 		$this->query_var_defaults = array(
   117 		$this->query_var_defaults = array(
   142 	/**
   142 	/**
   143 	 * Parses arguments passed to the network query with default query parameters.
   143 	 * Parses arguments passed to the network query with default query parameters.
   144 	 *
   144 	 *
   145 	 * @since 4.6.0
   145 	 * @since 4.6.0
   146 	 *
   146 	 *
   147 	 *
       
   148 	 * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct()
   147 	 * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct()
   149 	 */
   148 	 */
   150 	public function parse_query( $query = '' ) {
   149 	public function parse_query( $query = '' ) {
   151 		if ( empty( $query ) ) {
   150 		if ( empty( $query ) ) {
   152 			$query = $this->query_vars;
   151 			$query = $this->query_vars;
   196 		 *
   195 		 *
   197 		 * @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).
   198 		 */
   197 		 */
   199 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   198 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   200 
   199 
   201 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   200 		$network_ids = null;
   202 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   201 
   203 
   202 		/**
   204 		// Ignore the $fields argument as the queried result will be the same regardless.
   203 		 * Filter the sites array before the query takes place.
   205 		unset( $_args['fields'] );
   204 		 *
   206 
   205 		 * Return a non-null value to bypass WordPress's default site queries.
   207 		$key = md5( serialize( $_args ) );
   206 		 *
   208 		$last_changed = wp_cache_get_last_changed( 'networks' );
   207 		 *
   209 
   208 		 * @since 5.2.0
   210 		$cache_key = "get_network_ids:$key:$last_changed";
   209 		 *
   211 		$cache_value = wp_cache_get( $cache_key, 'networks' );
   210 		 * @param array|null       $site_ids Return an array of site data to short-circuit WP's site query,
   212 
   211 		 *                                   or null to allow WP to run its normal queries.
   213 		if ( false === $cache_value ) {
   212 		 * @param WP_Network_Query $this     The WP_Network_Query instance, passed by reference.
   214 			$network_ids = $this->get_network_ids();
   213 		 */
   215 			if ( $network_ids ) {
   214 		$network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) );
   216 				$this->set_found_networks();
   215 
       
   216 		if ( null === $network_ids ) {
       
   217 
       
   218 			// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
       
   219 			$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
       
   220 
       
   221 			// Ignore the $fields argument as the queried result will be the same regardless.
       
   222 			unset( $_args['fields'] );
       
   223 
       
   224 			$key          = md5( serialize( $_args ) );
       
   225 			$last_changed = wp_cache_get_last_changed( 'networks' );
       
   226 
       
   227 			$cache_key   = "get_network_ids:$key:$last_changed";
       
   228 			$cache_value = wp_cache_get( $cache_key, 'networks' );
       
   229 
       
   230 			if ( false === $cache_value ) {
       
   231 				$network_ids = $this->get_network_ids();
       
   232 				if ( $network_ids ) {
       
   233 					$this->set_found_networks();
       
   234 				}
       
   235 
       
   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'];
   217 			}
   244 			}
   218 
       
   219 			$cache_value = array(
       
   220 				'network_ids' => $network_ids,
       
   221 				'found_networks' => $this->found_networks,
       
   222 			);
       
   223 			wp_cache_add( $cache_key, $cache_value, 'networks' );
       
   224 		} else {
       
   225 			$network_ids = $cache_value['network_ids'];
       
   226 			$this->found_networks = $cache_value['found_networks'];
       
   227 		}
   245 		}
   228 
   246 
   229 		if ( $this->found_networks && $this->query_vars['number'] ) {
   247 		if ( $this->found_networks && $this->query_vars['number'] ) {
   230 			$this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
   248 			$this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
   231 		}
   249 		}
   258 		/**
   276 		/**
   259 		 * Filters the network query results.
   277 		 * Filters the network query results.
   260 		 *
   278 		 *
   261 		 * @since 4.6.0
   279 		 * @since 4.6.0
   262 		 *
   280 		 *
   263 		 * @param array            $_networks An array of WP_Network objects.
   281 		 * @param WP_Network[]     $_networks An array of WP_Network objects.
   264 		 * @param WP_Network_Query $this      Current instance of WP_Network_Query (passed by reference).
   282 		 * @param WP_Network_Query $this      Current instance of WP_Network_Query (passed by reference).
   265 		 */
   283 		 */
   266 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   284 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   267 
   285 
   268 		// Convert to WP_Network instances
   286 		// Convert to WP_Network instances
   299 					continue;
   317 					continue;
   300 				}
   318 				}
   301 
   319 
   302 				if ( is_int( $_key ) ) {
   320 				if ( is_int( $_key ) ) {
   303 					$_orderby = $_value;
   321 					$_orderby = $_value;
   304 					$_order = $order;
   322 					$_order   = $order;
   305 				} else {
   323 				} else {
   306 					$_orderby = $_key;
   324 					$_orderby = $_key;
   307 					$_order = $_value;
   325 					$_order   = $_value;
   308 				}
   326 				}
   309 
   327 
   310 				$parsed = $this->parse_orderby( $_orderby );
   328 				$parsed = $this->parse_orderby( $_orderby );
   311 
   329 
   312 				if ( ! $parsed ) {
   330 				if ( ! $parsed ) {
   326 			$orderby = "$wpdb->site.id $order";
   344 			$orderby = "$wpdb->site.id $order";
   327 		}
   345 		}
   328 
   346 
   329 		$number = absint( $this->query_vars['number'] );
   347 		$number = absint( $this->query_vars['number'] );
   330 		$offset = absint( $this->query_vars['offset'] );
   348 		$offset = absint( $this->query_vars['offset'] );
       
   349 		$limits = '';
   331 
   350 
   332 		if ( ! empty( $number ) ) {
   351 		if ( ! empty( $number ) ) {
   333 			if ( $offset ) {
   352 			if ( $offset ) {
   334 				$limits = 'LIMIT ' . $offset . ',' . $number;
   353 				$limits = 'LIMIT ' . $offset . ',' . $number;
   335 			} else {
   354 			} else {
   391 
   410 
   392 		$join = '';
   411 		$join = '';
   393 
   412 
   394 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   413 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   395 
   414 
       
   415 		$groupby = '';
       
   416 
   396 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   417 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   397 
   418 
   398 		/**
   419 		/**
   399 		 * Filters the network query clauses.
   420 		 * Filters the network query clauses.
   400 		 *
   421 		 *
   401 		 * @since 4.6.0
   422 		 * @since 4.6.0
   402 		 *
   423 		 *
   403 		 * @param array            $pieces A compacted array of network query clauses.
   424 		 * @param string[]         $pieces An associative array of network query clauses.
   404 		 * @param WP_Network_Query $this   Current instance of WP_Network_Query (passed by reference).
   425 		 * @param WP_Network_Query $this   Current instance of WP_Network_Query (passed by reference).
   405 		 */
   426 		 */
   406 		$clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) );
   427 		$clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) );
   407 
   428 
   408 		$fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   429 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   409 		$join = isset( $clauses['join'] ) ? $clauses['join'] : '';
   430 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   410 		$where = isset( $clauses['where'] ) ? $clauses['where'] : '';
   431 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   411 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   432 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   412 		$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
   433 		$limits  = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
   413 		$groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
   434 		$groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
   414 
   435 
   415 		if ( $where ) {
   436 		if ( $where ) {
   416 			$where = 'WHERE ' . $where;
   437 			$where = 'WHERE ' . $where;
   417 		}
   438 		}
   477 	 *
   498 	 *
   478 	 * @since 4.6.0
   499 	 * @since 4.6.0
   479 	 *
   500 	 *
   480 	 * @global wpdb  $wpdb WordPress database abstraction object.
   501 	 * @global wpdb  $wpdb WordPress database abstraction object.
   481 	 *
   502 	 *
   482 	 * @param string $string  Search string.
   503 	 * @param string   $string  Search string.
   483 	 * @param array  $columns Columns to search.
   504 	 * @param string[] $columns Array of columns to search.
   484 	 *
   505 	 *
   485 	 * @return string Search SQL.
   506 	 * @return string Search SQL.
   486 	 */
   507 	 */
   487 	protected function get_search_sql( $string, $columns ) {
   508 	protected function get_search_sql( $string, $columns ) {
   488 		global $wpdb;
   509 		global $wpdb;
   517 		);
   538 		);
   518 
   539 
   519 		$parsed = false;
   540 		$parsed = false;
   520 		if ( $orderby == 'network__in' ) {
   541 		if ( $orderby == 'network__in' ) {
   521 			$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   542 			$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
   522 			$parsed = "FIELD( {$wpdb->site}.id, $network__in )";
   543 			$parsed      = "FIELD( {$wpdb->site}.id, $network__in )";
   523 		} elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
   544 		} elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
   524 			$field = substr( $orderby, 0, -7 );
   545 			$field  = substr( $orderby, 0, -7 );
   525 			$parsed = "CHAR_LENGTH($wpdb->site.$field)";
   546 			$parsed = "CHAR_LENGTH($wpdb->site.$field)";
   526 		} elseif ( in_array( $orderby, $allowed_keys ) ) {
   547 		} elseif ( in_array( $orderby, $allowed_keys ) ) {
   527 			$parsed = "$wpdb->site.$orderby";
   548 			$parsed = "$wpdb->site.$orderby";
   528 		}
   549 		}
   529 
   550