Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/wp-includes/class-wp-site-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ class WP_Site_Query {
*/
public $date_query = false;

/**
* Taxonomy query container.
*
* @since x.x.x
* @var WP_Tax_Query A taxonomy query instance.
*/
public $tax_query = false;

/**
* Taxonomy query clauses.
*
* @since x.x.x
* @var array
*/
protected $tax_query_clauses;

/**
* Query vars set by the user.
*
Expand Down Expand Up @@ -112,6 +128,7 @@ class WP_Site_Query {
* @since 5.1.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
* 'meta_compare_key', 'meta_value', 'meta_type', and 'meta_compare' parameters.
* @since 5.3.0 Introduced the 'meta_type_key' parameter.
* @since x.x.x Introduced the 'tax_query' parameter.
*
* @param string|array $query {
* Optional. Array or query string of site query parameters. Default empty.
Expand Down Expand Up @@ -183,6 +200,8 @@ class WP_Site_Query {
* See WP_Meta_Query::__construct() for accepted values and default value.
* @type array $meta_query An associative array of WP_Meta_Query arguments.
* See WP_Meta_Query::__construct() for accepted values.
* @type array $tax_query An associative array of WP_Tax_Query arguments.
* See WP_Tax_Query::__construct() for accepted values.
* }
*/
public function __construct( $query = '' ) {
Expand Down Expand Up @@ -224,6 +243,7 @@ public function __construct( $query = '' ) {
'meta_value' => '',
'meta_type' => '',
'meta_compare' => '',
'tax_query' => '',
);

if ( ! empty( $query ) ) {
Expand Down Expand Up @@ -306,6 +326,21 @@ public function get_sites() {
$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
}

// Parse taxonomy query.
if ( ! empty( $this->query_vars['tax_query'] ) && is_array( $this->query_vars['tax_query'] ) ) {
$this->tax_query = new WP_Tax_Query( $this->query_vars['tax_query'] );
$this->tax_query_clauses = $this->tax_query->get_sql( $wpdb->blogs, 'blog_id' );
}

/**
* Fires after the site taxonomy query has been parsed.
*
* @since x.x.x
*
* @param WP_Site_Query $query The WP_Site_Query instance (passed by reference).
*/
do_action_ref_array( 'parse_site_tax_query', array( &$this ) );

$site_data = null;

/**
Expand Down Expand Up @@ -383,7 +418,10 @@ public function get_sites() {
// If querying for a count only, there's nothing more to do.
if ( $this->query_vars['count'] ) {
// $site_ids is actually a count in this case.
return (int) $site_ids;
$count = (int) $site_ids;
// Set found_sites for consistency with non-count queries.
$this->found_sites = $count;
return $count;
}

$site_ids = array_map( 'intval', $site_ids );
Expand Down Expand Up @@ -497,6 +535,9 @@ protected function get_site_ids() {

if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)';
if ( ! empty( $this->tax_query_clauses ) ) {
$fields = "COUNT(DISTINCT {$wpdb->blogs}.blog_id)";
}
} else {
$fields = "{$wpdb->blogs}.blog_id";
}
Expand Down Expand Up @@ -651,6 +692,17 @@ protected function get_site_ids() {
}
}

if ( ! empty( $this->tax_query_clauses ) ) {
$join .= $this->tax_query_clauses['join'];

// Strip leading 'AND'.
$this->sql_clauses['where']['tax_query'] = preg_replace( '/^\s*AND\s*/', '', $this->tax_query_clauses['where'] );

if ( ! $this->query_vars['count'] ) {
$groupby = "{$wpdb->blogs}.blog_id";
}
}

$where = implode( ' AND ', $this->sql_clauses['where'] );

$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
Expand Down
Loading
Loading