wp/wp-includes/class-wp-network-query.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    87 	 * @since 4.6.0
    87 	 * @since 4.6.0
    88 	 *
    88 	 *
    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 int[]        $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 int[]        $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).
   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 string[]     $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 string[]     $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 string[]     $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 string[]     $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 = '' ) {
   198 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   198 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
   199 
   199 
   200 		$network_data = null;
   200 		$network_data = null;
   201 
   201 
   202 		/**
   202 		/**
   203 		 * Filter the network data before the query takes place.
   203 		 * Filters the network data before the query takes place.
   204 		 *
   204 		 *
   205 		 * Return a non-null value to bypass WordPress's default network queries.
   205 		 * Return a non-null value to bypass WordPress' default network queries.
   206 		 *
   206 		 *
   207 		 * The expected return type from this filter depends on the value passed in the request query_vars.
   207 		 * The expected return type from this filter depends on the value passed
   208 		 * When `$this->query_vars['count']` is set, the filter should return the network count as an int.
   208 		 * in the request query vars:
   209 		 * When `'ids' === $this->query_vars['fields']`, the filter should return an array of network IDs.
   209 		 * - When `$this->query_vars['count']` is set, the filter should return
   210 		 * Otherwise the filter should return an array of WP_Network objects.
   210 		 *   the network count as an integer.
       
   211 		 * - When `'ids' === $this->query_vars['fields']`, the filter should return
       
   212 		 *   an array of network IDs.
       
   213 		 * - Otherwise the filter should return an array of WP_Network objects.
       
   214 		 *
       
   215 		 * Note that if the filter returns an array of network data, it will be assigned
       
   216 		 * to the `networks` property of the current WP_Network_Query instance.
       
   217 		 *
       
   218 		 * Filtering functions that require pagination information are encouraged to set
       
   219 		 * the `found_networks` and `max_num_pages` properties of the WP_Network_Query object,
       
   220 		 * passed to the filter by reference. If WP_Network_Query does not perform a database
       
   221 		 * query, it will not have enough information to generate these values itself.
   211 		 *
   222 		 *
   212 		 * @since 5.2.0
   223 		 * @since 5.2.0
   213 		 *
   224 		 * @since 5.6.0 The returned array of network data is assigned to the `networks` property
   214 		 * @param array|null       $network_data Return an array of network data to short-circuit WP's network query,
   225 		 *              of the current WP_Network_Query instance.
       
   226 		 *
       
   227 		 * @param array|int|null   $network_data Return an array of network data to short-circuit WP's network query,
   215 		 *                                       the network count as an integer if `$this->query_vars['count']` is set,
   228 		 *                                       the network count as an integer if `$this->query_vars['count']` is set,
   216 		 *                                       or null to allow WP to run its normal queries.
   229 		 *                                       or null to allow WP to run its normal queries.
   217 		 * @param WP_Network_Query $this         The WP_Network_Query instance, passed by reference.
   230 		 * @param WP_Network_Query $query        The WP_Network_Query instance, passed by reference.
   218 		 */
   231 		 */
   219 		$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );
   232 		$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );
   220 
   233 
   221 		if ( null !== $network_data ) {
   234 		if ( null !== $network_data ) {
       
   235 			if ( is_array( $network_data ) && ! $this->query_vars['count'] ) {
       
   236 				$this->networks = $network_data;
       
   237 			}
       
   238 
   222 			return $network_data;
   239 			return $network_data;
   223 		}
   240 		}
   224 
   241 
   225 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   242 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   226 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   243 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   255 		}
   272 		}
   256 
   273 
   257 		// If querying for a count only, there's nothing more to do.
   274 		// If querying for a count only, there's nothing more to do.
   258 		if ( $this->query_vars['count'] ) {
   275 		if ( $this->query_vars['count'] ) {
   259 			// $network_ids is actually a count in this case.
   276 			// $network_ids is actually a count in this case.
   260 			return intval( $network_ids );
   277 			return (int) $network_ids;
   261 		}
   278 		}
   262 
   279 
   263 		$network_ids = array_map( 'intval', $network_ids );
   280 		$network_ids = array_map( 'intval', $network_ids );
   264 
   281 
   265 		if ( 'ids' === $this->query_vars['fields'] ) {
   282 		if ( 'ids' === $this->query_vars['fields'] ) {
   284 		 * Filters the network query results.
   301 		 * Filters the network query results.
   285 		 *
   302 		 *
   286 		 * @since 4.6.0
   303 		 * @since 4.6.0
   287 		 *
   304 		 *
   288 		 * @param WP_Network[]     $_networks An array of WP_Network objects.
   305 		 * @param WP_Network[]     $_networks An array of WP_Network objects.
   289 		 * @param WP_Network_Query $this      Current instance of WP_Network_Query (passed by reference).
   306 		 * @param WP_Network_Query $query     Current instance of WP_Network_Query (passed by reference).
   290 		 */
   307 		 */
   291 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   308 		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
   292 
   309 
   293 		// Convert to WP_Network instances.
   310 		// Convert to WP_Network instances.
   294 		$this->networks = array_map( 'get_network', $_networks );
   311 		$this->networks = array_map( 'get_network', $_networks );
   427 		 * Filters the network query clauses.
   444 		 * Filters the network query clauses.
   428 		 *
   445 		 *
   429 		 * @since 4.6.0
   446 		 * @since 4.6.0
   430 		 *
   447 		 *
   431 		 * @param string[]         $pieces An associative array of network query clauses.
   448 		 * @param string[]         $pieces An associative array of network query clauses.
   432 		 * @param WP_Network_Query $this   Current instance of WP_Network_Query (passed by reference).
   449 		 * @param WP_Network_Query $query  Current instance of WP_Network_Query (passed by reference).
   433 		 */
   450 		 */
   434 		$clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) );
   451 		$clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) );
   435 
   452 
   436 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   453 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   437 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   454 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   464 		$this->sql_clauses['limits']  = $limits;
   481 		$this->sql_clauses['limits']  = $limits;
   465 
   482 
   466 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   483 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   467 
   484 
   468 		if ( $this->query_vars['count'] ) {
   485 		if ( $this->query_vars['count'] ) {
   469 			return intval( $wpdb->get_var( $this->request ) );
   486 			return (int) $wpdb->get_var( $this->request );
   470 		}
   487 		}
   471 
   488 
   472 		$network_ids = $wpdb->get_col( $this->request );
   489 		$network_ids = $wpdb->get_col( $this->request );
   473 
   490 
   474 		return array_map( 'intval', $network_ids );
   491 		return array_map( 'intval', $network_ids );