wp/wp-includes/class-wp-site-query.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   107 	 * Sets up the site query, based on the query vars passed.
   107 	 * Sets up the site query, based on the query vars passed.
   108 	 *
   108 	 *
   109 	 * @since 4.6.0
   109 	 * @since 4.6.0
   110 	 * @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',
   111 	 * @since 5.1.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
   112 	 *              'meta_value', 'meta_type' and 'meta_compare' parameters.
   112 	 *              'meta_compare_key', 'meta_value', 'meta_type', and 'meta_compare' parameters.
       
   113 	 * @since 5.3.0 Introduced the 'meta_type_key' parameter.
   113 	 *
   114 	 *
   114 	 * @param string|array $query {
   115 	 * @param string|array $query {
   115 	 *     Optional. Array or query string of site query parameters. Default empty.
   116 	 *     Optional. Array or query string of site query parameters. Default empty.
   116 	 *
   117 	 *
   117 	 *     @type int[]        $site__in               Array of site IDs to include. Default empty.
   118 	 *     @type int[]           $site__in               Array of site IDs to include. Default empty.
   118 	 *     @type int[]        $site__not_in           Array of site IDs to exclude. Default empty.
   119 	 *     @type int[]           $site__not_in           Array of site IDs to exclude. Default empty.
   119 	 *     @type bool         $count                  Whether to return a site count (true) or array of site objects.
   120 	 *     @type bool            $count                  Whether to return a site count (true) or array of site objects.
   120 	 *                                                Default false.
   121 	 *                                                   Default false.
   121 	 *     @type array        $date_query             Date query clauses to limit sites by. See WP_Date_Query.
   122 	 *     @type array           $date_query             Date query clauses to limit sites by. See WP_Date_Query.
   122 	 *                                                Default null.
   123 	 *                                                   Default null.
   123 	 *     @type string       $fields                 Site fields to return. Accepts 'ids' (returns an array of site IDs)
   124 	 *     @type string          $fields                 Site fields to return. Accepts 'ids' (returns an array of site IDs)
   124 	 *                                                or empty (returns an array of complete site objects). Default empty.
   125 	 *                                                   or empty (returns an array of complete site objects). Default empty.
   125 	 *     @type int          $ID                     A site ID to only return that site. Default empty.
   126 	 *     @type int             $ID                     A site ID to only return that site. Default empty.
   126 	 *     @type int          $number                 Maximum number of sites to retrieve. Default 100.
   127 	 *     @type int             $number                 Maximum number of sites to retrieve. Default 100.
   127 	 *     @type int          $offset                 Number of sites to offset the query. Used to build LIMIT clause.
   128 	 *     @type int             $offset                 Number of sites to offset the query. Used to build LIMIT clause.
   128 	 *                                                Default 0.
   129 	 *                                                   Default 0.
   129 	 *     @type bool         $no_found_rows          Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   130 	 *     @type bool            $no_found_rows          Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
   130 	 *     @type string|array $orderby                Site status or array of statuses. Accepts 'id', 'domain', 'path',
   131 	 *     @type string|array    $orderby                Site status or array of statuses. Accepts:
   131 	 *                                                'network_id', 'last_updated', 'registered', 'domain_length',
   132 	 *                                                   - 'id'
   132 	 *                                                'path_length', 'site__in' and 'network__in'. Also accepts false,
   133 	 *                                                   - 'domain'
   133 	 *                                                an empty array, or 'none' to disable `ORDER BY` clause.
   134 	 *                                                   - 'path'
   134 	 *                                                Default 'id'.
   135 	 *                                                   - 'network_id'
   135 	 *     @type string       $order                  How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
   136 	 *                                                   - 'last_updated'
   136 	 *     @type int          $network_id             Limit results to those affiliated with a given network ID. If 0,
   137 	 *                                                   - 'registered'
   137 	 *                                                include all networks. Default 0.
   138 	 *                                                   - 'domain_length'
   138 	 *     @type int[]        $network__in            Array of network IDs to include affiliated sites for. Default empty.
   139 	 *                                                   - 'path_length'
   139 	 *     @type int[]        $network__not_in        Array of network IDs to exclude affiliated sites for. Default empty.
   140 	 *                                                   - 'site__in'
   140 	 *     @type string       $domain                 Limit results to those affiliated with a given domain. Default empty.
   141 	 *                                                   - 'network__in'
   141 	 *     @type string[]     $domain__in             Array of domains to include affiliated sites for. Default empty.
   142 	 *                                                   - 'deleted'
   142 	 *     @type string[]     $domain__not_in         Array of domains to exclude affiliated sites for. Default empty.
   143 	 *                                                   - 'mature'
   143 	 *     @type string       $path                   Limit results to those affiliated with a given path. Default empty.
   144 	 *                                                   - 'spam'
   144 	 *     @type string[]     $path__in               Array of paths to include affiliated sites for. Default empty.
   145 	 *                                                   - 'archived'
   145 	 *     @type string[]     $path__not_in           Array of paths to exclude affiliated sites for. Default empty.
   146 	 *                                                   - 'public'
   146 	 *     @type int          $public                 Limit results to public sites. Accepts '1' or '0'. Default empty.
   147 	 *                                                   - false, an empty array, or 'none' to disable `ORDER BY` clause.
   147 	 *     @type int          $archived               Limit results to archived sites. Accepts '1' or '0'. Default empty.
   148 	 *                                                   Default 'id'.
   148 	 *     @type int          $mature                 Limit results to mature sites. Accepts '1' or '0'. Default empty.
   149 	 *     @type string          $order                  How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
   149 	 *     @type int          $spam                   Limit results to spam sites. Accepts '1' or '0'. Default empty.
   150 	 *     @type int             $network_id             Limit results to those affiliated with a given network ID. If 0,
   150 	 *     @type int          $deleted                Limit results to deleted sites. Accepts '1' or '0'. Default empty.
   151 	 *                                                   include all networks. Default 0.
   151 	 *     @type int          $lang_id                Limit results to a language ID. Default empty.
   152 	 *     @type int[]           $network__in            Array of network IDs to include affiliated sites for. Default empty.
   152 	 *     @type string[]     $lang__in               Array of language IDs to include affiliated sites for. Default empty.
   153 	 *     @type int[]           $network__not_in        Array of network IDs to exclude affiliated sites for. Default empty.
   153 	 *     @type string[]     $lang__not_in           Array of language IDs to exclude affiliated sites for. Default empty.
   154 	 *     @type string          $domain                 Limit results to those affiliated with a given domain. Default empty.
   154 	 *     @type string       $search                 Search term(s) to retrieve matching sites for. Default empty.
   155 	 *     @type string[]        $domain__in             Array of domains to include affiliated sites for. Default empty.
   155 	 *     @type string[]     $search_columns         Array of column names to be searched. Accepts 'domain' and 'path'.
   156 	 *     @type string[]        $domain__not_in         Array of domains to exclude affiliated sites for. Default empty.
   156 	 *                                                Default empty array.
   157 	 *     @type string          $path                   Limit results to those affiliated with a given path. Default empty.
   157 	 *     @type bool         $update_site_cache      Whether to prime the cache for found sites. Default true.
   158 	 *     @type string[]        $path__in               Array of paths to include affiliated sites for. Default empty.
   158 	 *     @type bool         $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
   159 	 *     @type string[]        $path__not_in           Array of paths to exclude affiliated sites for. Default empty.
   159 	 *     @type array        $meta_query             Meta query clauses to limit retrieved sites by. See `WP_Meta_Query`.
   160 	 *     @type int             $public                 Limit results to public sites. Accepts '1' or '0'. Default empty.
   160 	 *                                                Default empty.
   161 	 *     @type int             $archived               Limit results to archived sites. Accepts '1' or '0'. Default empty.
   161 	 *     @type string       $meta_key               Limit sites to those matching a specific metadata key.
   162 	 *     @type int             $mature                 Limit results to mature sites. Accepts '1' or '0'. Default empty.
   162 	 *                                                Can be used in conjunction with `$meta_value`. Default empty.
   163 	 *     @type int             $spam                   Limit results to spam sites. Accepts '1' or '0'. Default empty.
   163 	 *     @type string       $meta_value             Limit sites to those matching a specific metadata value.
   164 	 *     @type int             $deleted                Limit results to deleted sites. Accepts '1' or '0'. Default empty.
   164 	 *                                                Usually used in conjunction with `$meta_key`. Default empty.
   165 	 *     @type int             $lang_id                Limit results to a language ID. Default empty.
   165 	 *     @type string       $meta_type              Data type that the `$meta_value` column will be CAST to for
   166 	 *     @type string[]        $lang__in               Array of language IDs to include affiliated sites for. Default empty.
   166 	 *                                                comparisons. Default empty.
   167 	 *     @type string[]        $lang__not_in           Array of language IDs to exclude affiliated sites for. Default empty.
   167 	 *     @type string       $meta_compare           Comparison operator to test the `$meta_value`. Default empty.
   168 	 *     @type string          $search                 Search term(s) to retrieve matching sites for. Default empty.
       
   169 	 *     @type string[]        $search_columns         Array of column names to be searched. Accepts 'domain' and 'path'.
       
   170 	 *                                                   Default empty array.
       
   171 	 *     @type bool            $update_site_cache      Whether to prime the cache for found sites. Default true.
       
   172 	 *     @type bool            $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
       
   173 	 *     @type string|string[] $meta_key               Meta key or keys to filter by.
       
   174 	 *     @type string|string[] $meta_value             Meta value or values to filter by.
       
   175 	 *     @type string          $meta_compare           MySQL operator used for comparing the meta value.
       
   176 	 *                                                   See WP_Meta_Query::__construct for accepted values and default value.
       
   177 	 *     @type string          $meta_compare_key       MySQL operator used for comparing the meta key.
       
   178 	 *                                                   See WP_Meta_Query::__construct for accepted values and default value.
       
   179 	 *     @type string          $meta_type              MySQL data type that the meta_value column will be CAST to for comparisons.
       
   180 	 *                                                   See WP_Meta_Query::__construct for accepted values and default value.
       
   181 	 *     @type string          $meta_type_key          MySQL data type that the meta_key column will be CAST to for comparisons.
       
   182 	 *                                                   See WP_Meta_Query::__construct for accepted values and default value.
       
   183 	 *     @type array           $meta_query             An associative array of WP_Meta_Query arguments.
       
   184 	 *                                                   See WP_Meta_Query::__construct for accepted values.
   168 	 * }
   185 	 * }
   169 	 */
   186 	 */
   170 	public function __construct( $query = '' ) {
   187 	public function __construct( $query = '' ) {
   171 		$this->query_var_defaults = array(
   188 		$this->query_var_defaults = array(
   172 			'fields'                 => '',
   189 			'fields'                 => '',
   232 		/**
   249 		/**
   233 		 * Fires after the site query vars have been parsed.
   250 		 * Fires after the site query vars have been parsed.
   234 		 *
   251 		 *
   235 		 * @since 4.6.0
   252 		 * @since 4.6.0
   236 		 *
   253 		 *
   237 		 * @param WP_Site_Query $this The WP_Site_Query instance (passed by reference).
   254 		 * @param WP_Site_Query $query The WP_Site_Query instance (passed by reference).
   238 		 */
   255 		 */
   239 		do_action_ref_array( 'parse_site_query', array( &$this ) );
   256 		do_action_ref_array( 'parse_site_query', array( &$this ) );
   240 	}
   257 	}
   241 
   258 
   242 	/**
   259 	/**
   276 		/**
   293 		/**
   277 		 * Fires before sites are retrieved.
   294 		 * Fires before sites are retrieved.
   278 		 *
   295 		 *
   279 		 * @since 4.6.0
   296 		 * @since 4.6.0
   280 		 *
   297 		 *
   281 		 * @param WP_Site_Query $this Current instance of WP_Site_Query (passed by reference).
   298 		 * @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
   282 		 */
   299 		 */
   283 		do_action_ref_array( 'pre_get_sites', array( &$this ) );
   300 		do_action_ref_array( 'pre_get_sites', array( &$this ) );
   284 
   301 
   285 		// Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
   302 		// Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
   286 		$this->meta_query->parse_query_vars( $this->query_vars );
   303 		$this->meta_query->parse_query_vars( $this->query_vars );
   331 		}
   348 		}
   332 
   349 
   333 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   350 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
   334 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   351 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   335 
   352 
   336 		// Ignore the $fields argument as the queried result will be the same regardless.
   353 		// Ignore the $fields, $update_site_cache, $update_site_meta_cache argument as the queried result will be the same regardless.
   337 		unset( $_args['fields'] );
   354 		unset( $_args['fields'], $_args['update_site_cache'], $_args['update_site_meta_cache'] );
   338 
   355 
   339 		$key          = md5( serialize( $_args ) );
   356 		$key          = md5( serialize( $_args ) );
   340 		$last_changed = wp_cache_get_last_changed( 'sites' );
   357 		$last_changed = wp_cache_get_last_changed( 'sites' );
   341 
   358 
   342 		$cache_key   = "get_sites:$key:$last_changed";
   359 		$cache_key   = "get_sites:$key:$last_changed";
   627 			}
   644 			}
   628 		}
   645 		}
   629 
   646 
   630 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   647 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   631 
   648 
   632 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   649 		$clauses = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   633 
   650 
   634 		/**
   651 		/**
   635 		 * Filters the site query clauses.
   652 		 * Filters the site query clauses.
   636 		 *
   653 		 *
   637 		 * @since 4.6.0
   654 		 * @since 4.6.0
   638 		 *
   655 		 *
   639 		 * @param string[]      $pieces An associative array of site query clauses.
   656 		 * @param string[]      $clauses An associative array of site query clauses.
   640 		 * @param WP_Site_Query $query  Current instance of WP_Site_Query (passed by reference).
   657 		 * @param WP_Site_Query $query   Current instance of WP_Site_Query (passed by reference).
   641 		 */
   658 		 */
   642 		$clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
   659 		$clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $clauses ), &$this ) );
   643 
   660 
   644 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   661 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   645 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   662 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   646 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   663 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   647 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   664 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   669 		$this->sql_clauses['from']    = "FROM $wpdb->blogs $join";
   686 		$this->sql_clauses['from']    = "FROM $wpdb->blogs $join";
   670 		$this->sql_clauses['groupby'] = $groupby;
   687 		$this->sql_clauses['groupby'] = $groupby;
   671 		$this->sql_clauses['orderby'] = $orderby;
   688 		$this->sql_clauses['orderby'] = $orderby;
   672 		$this->sql_clauses['limits']  = $limits;
   689 		$this->sql_clauses['limits']  = $limits;
   673 
   690 
   674 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   691 		$this->request = "
       
   692 			{$this->sql_clauses['select']}
       
   693 			{$this->sql_clauses['from']}
       
   694 			{$where}
       
   695 			{$this->sql_clauses['groupby']}
       
   696 			{$this->sql_clauses['orderby']}
       
   697 			{$this->sql_clauses['limits']}
       
   698 		";
   675 
   699 
   676 		if ( $this->query_vars['count'] ) {
   700 		if ( $this->query_vars['count'] ) {
   677 			return (int) $wpdb->get_var( $this->request );
   701 			return (int) $wpdb->get_var( $this->request );
   678 		}
   702 		}
   679 
   703 
   713 	 *
   737 	 *
   714 	 * @since 4.6.0
   738 	 * @since 4.6.0
   715 	 *
   739 	 *
   716 	 * @global wpdb $wpdb WordPress database abstraction object.
   740 	 * @global wpdb $wpdb WordPress database abstraction object.
   717 	 *
   741 	 *
   718 	 * @param string   $string  Search string.
   742 	 * @param string   $search  Search string.
   719 	 * @param string[] $columns Array of columns to search.
   743 	 * @param string[] $columns Array of columns to search.
   720 	 * @return string Search SQL.
   744 	 * @return string Search SQL.
   721 	 */
   745 	 */
   722 	protected function get_search_sql( $string, $columns ) {
   746 	protected function get_search_sql( $search, $columns ) {
   723 		global $wpdb;
   747 		global $wpdb;
   724 
   748 
   725 		if ( false !== strpos( $string, '*' ) ) {
   749 		if ( false !== strpos( $search, '*' ) ) {
   726 			$like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
   750 			$like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $search ) ) ) . '%';
   727 		} else {
   751 		} else {
   728 			$like = '%' . $wpdb->esc_like( $string ) . '%';
   752 			$like = '%' . $wpdb->esc_like( $search ) . '%';
   729 		}
   753 		}
   730 
   754 
   731 		$searches = array();
   755 		$searches = array();
   732 		foreach ( $columns as $column ) {
   756 		foreach ( $columns as $column ) {
   733 			$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
   757 			$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
   762 				break;
   786 				break;
   763 			case 'domain':
   787 			case 'domain':
   764 			case 'last_updated':
   788 			case 'last_updated':
   765 			case 'path':
   789 			case 'path':
   766 			case 'registered':
   790 			case 'registered':
       
   791 			case 'deleted':
       
   792 			case 'spam':
       
   793 			case 'mature':
       
   794 			case 'archived':
       
   795 			case 'public':
   767 				$parsed = $orderby;
   796 				$parsed = $orderby;
   768 				break;
   797 				break;
   769 			case 'network_id':
   798 			case 'network_id':
   770 				$parsed = 'site_id';
   799 				$parsed = 'site_id';
   771 				break;
   800 				break;