Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 'sites' => 'current' doesn't work as expected #3243

Merged
merged 3 commits into from
Jan 4, 2023
Merged
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
14 changes: 8 additions & 6 deletions includes/classes/Indexable/Comment/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) {
$site__not_in = [];

if ( ! empty( $query->query_vars['sites'] ) ) {

_deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) );
$site__in = (array) $query->query_vars['sites'];
$scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in;
}

if ( ! empty( $query->query_vars['site__in'] ) ) {
$site__in = (array) $query->query_vars['site__in'];
$scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in;
if ( ! empty( $query->query_vars['site__in'] ) || ! empty( $query->query_vars['sites'] ) ) {
$site__in = ! empty( $query->query_vars['site__in'] ) ? (array) $query->query_vars['site__in'] : (array) $query->query_vars['sites'];

if ( in_array( 'all', $site__in, true ) ) {
$scope = 'all';
} elseif ( in_array( 'current', $site__in, true ) ) {
$site__in = (array) get_current_blog_id();
}
}

if ( ! empty( $query->query_vars['site__not_in'] ) ) {
Expand Down
13 changes: 7 additions & 6 deletions includes/classes/Indexable/Post/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,17 @@ public function get_es_posts( $posts, $query ) {
$site__not_in = '';

if ( ! empty( $query_vars['sites'] ) ) {

_deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) );
$site__in = (array) $query_vars['sites'];
$scope = 'all' === $query_vars['sites'] ? 'all' : $site__in;
}

if ( ! empty( $query_vars['site__in'] ) ) {
if ( ! empty( $query_vars['site__in'] ) || ! empty( $query_vars['sites'] ) ) {
$site__in = ! empty( $query_vars['site__in'] ) ? (array) $query_vars['site__in'] : (array) $query_vars['sites'];

$site__in = (array) $query_vars['site__in'];
$scope = 'all' === $query_vars['site__in'] ? 'all' : $site__in;
if ( in_array( 'all', $site__in, true ) ) {
$scope = 'all';
} elseif ( in_array( 'current', $site__in, true ) ) {
$site__in = (array) get_current_blog_id();
}
}

if ( ! empty( $query_vars['site__not_in'] ) ) {
Expand Down
14 changes: 8 additions & 6 deletions includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {
$site__not_in = [];

if ( ! empty( $query->query_vars['sites'] ) ) {

_deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) );
$site__in = (array) $query->query_vars['sites'];
$scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in;
}

if ( ! empty( $query->query_vars['site__in'] ) ) {
$site__in = (array) $query->query_vars['site__in'];
$scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in;
if ( ! empty( $query->query_vars['site__in'] ) || ! empty( $query->query_vars['sites'] ) ) {
$site__in = ! empty( $query->query_vars['site__in'] ) ? (array) $query->query_vars['site__in'] : (array) $query->query_vars['sites'];

if ( in_array( 'all', $site__in, true ) ) {
$scope = 'all';
} elseif ( in_array( 'current', $site__in, true ) ) {
$site__in = (array) get_current_blog_id();
}
}

if ( ! empty( $query->query_vars['site__not_in'] ) ) {
Expand Down
82 changes: 82 additions & 0 deletions tests/php/indexables/TestCommentMultisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,86 @@ public function testCommentQueryWithDeprecatedSitesParam() {
$this->assertEquals( 9, count( $query->get_comments() ) );
}

/**
* Test Comment Query with the deprecated `sites` param and with value `current`
*
* @since 4.4.1
* @expectedDeprecated maybe_filter_query
*/
public function testCommentQueryWithDeprecatedSitesParamWithValueCurrent() {

$sites = ElasticPress\Utils\get_sites();
if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$post_id = $this->ep_factory->post->create();
$this->ep_factory->comment->create_many( 3, array( 'comment_post_ID' => $post_id ) );

ElasticPress\Elasticsearch::factory()->refresh_indices();
restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

$query = new \WP_Comment_Query(
array(
'ep_integrate' => true,
'sites' => 'current',
)
);

$comments = $query->get_comments();
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 3, count( $comments ) );

foreach ( $comments as $comment ) {
$this->assertEquals( $sites[1]['blog_id'], $comment->site_id );
}
}

/**
* Test Comment Query with the `site__in` param and with value `current`
*
* @since 4.4.1
*/
public function testCommentQueryWithSiteInParamWithValueCurrent() {

$sites = ElasticPress\Utils\get_sites();
if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$post_id = $this->ep_factory->post->create();
$this->ep_factory->comment->create_many( 3, array( 'comment_post_ID' => $post_id ) );

ElasticPress\Elasticsearch::factory()->refresh_indices();
restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

$query = new \WP_Comment_Query(
array(
'ep_integrate' => true,
'site__in' => 'current',
)
);

$comments = $query->get_comments();
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 3, count( $comments ) );

foreach ( $comments as $comment ) {
$this->assertEquals( $sites[1]['blog_id'], $comment->site_id );
}
}
}
96 changes: 96 additions & 0 deletions tests/php/indexables/TestPostMultisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,102 @@ public function testWPQuerySearchContentWithDeprecatedSitesParam() {
$this->cleanUpSites( $sites );
}


/**
* Test a simple post content search with deprecated `sites` parameter and with value `current`
*
* @since 4.4.1
* @expectedDeprecated get_es_posts
* @group testMultipleTests
*/
public function testWPQuerySearchContentWithDeprecatedSitesParamWithValueCurrent() {

$sites = ElasticPress\Utils\get_sites();

if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$this->ep_factory->post->create_many( 2, array( 'post_content' => 'findme' ) );
$this->ep_factory->post->create();

ElasticPress\Elasticsearch::factory()->refresh_indices();

restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

$args = array(
's' => 'findme',
'sites' => 'current',
);

$query = new \WP_Query( $args );
$posts = $query->posts;

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $query->post_count, 2 );
$this->assertEquals( $query->found_posts, 2 );

foreach ( $posts as $post ) {
$this->assertEquals( $post->site_id, $sites[1]['blog_id'] );
}

$this->cleanUpSites( $sites );
}

/**
* Test a simple post content search with `site__in` parameter and with value `current`.
*
* @since 4.4.1
* @group testMultipleTests
*/
public function testWPQuerySearchContentWithDeprecatedSiteInParamWithValueCurrent() {

$sites = ElasticPress\Utils\get_sites();

if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$this->ep_factory->post->create_many( 2, array( 'post_content' => 'findme' ) );
$this->ep_factory->post->create();

ElasticPress\Elasticsearch::factory()->refresh_indices();

restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

$args = array(
's' => 'findme',
'site__in' => 'current',
);

$query = new \WP_Query( $args );
$posts = $query->posts;

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $query->post_count, 2 );
$this->assertEquals( $query->found_posts, 2 );

foreach ( $posts as $post ) {
$this->assertEquals( $post->site_id, $sites[1]['blog_id'] );
}

$this->cleanUpSites( $sites );
}

/**
* Tests WP Query returns the data from all sites except one.
*
Expand Down
87 changes: 87 additions & 0 deletions tests/php/indexables/TestTermMultisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,91 @@ public function testTermQuerySearchWithDeprecatedSitesParam() {
$this->assertEquals( 4, count( $query->get_terms() ) );

}

/**
* Test WP Term Query with the deprecated `sites` param and with value `current`
*
* @since 4.4.1
* @expectedDeprecated maybe_filter_query
*/
public function testTermQuerySearchWithDeprecatedSitesParamAndValueCurrent() {

$sites = ElasticPress\Utils\get_sites();

if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$this->ep_factory->category->create_many( 4 );

ElasticPress\Elasticsearch::factory()->refresh_indices();
restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

// test for only current site.
$args = array(
'sites' => 'current',
'taxonomy' => 'category',
'hide_empty' => false,
'ep_integrate' => true,
);
$query = new \WP_Term_Query( $args );
$terms = $query->get_terms();

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 4, count( $terms ) );

foreach ( $terms as $term ) {
$this->assertEquals( $sites[1]['blog_id'], $term->site_id );
}
}

/**
* Test WP Term Query with the `site__in` param and with value `current`
*
* @since 4.4.1
*/
public function testTermQuerySearchWithSiteInParamAndValueCurrent() {

$sites = ElasticPress\Utils\get_sites();

if ( ! is_multisite() ) {
$this->assertEmpty( $sites );
return;
}

foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );

$this->ep_factory->category->create_many( 4 );

ElasticPress\Elasticsearch::factory()->refresh_indices();
restore_current_blog();
}

switch_to_blog( $sites[1]['blog_id'] );

// test for only current site.
$args = array(
'site__in' => 'current',
'taxonomy' => 'category',
'hide_empty' => false,
'ep_integrate' => true,
);
$query = new \WP_Term_Query( $args );
$terms = $query->get_terms();

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 4, count( $terms ) );

foreach ( $terms as $term ) {
$this->assertEquals( $sites[1]['blog_id'], $term->site_id );
}
}
}