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

Add Support for site__in and site__not_in parameters #2991

Merged
merged 10 commits into from
Oct 11, 2022
41 changes: 36 additions & 5 deletions includes/classes/Indexable/Comment/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,24 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) {
$formatted_args = $this->indexable->format_args( $query->query_vars );

$scope = 'current';

$site__in = [];
$site__not_in = [];

if ( ! empty( $query->query_vars['sites'] ) ) {
$scope = $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__not_in'] ) ) {
$site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in'];
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -138,16 +154,31 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) {

if ( 'all' === $scope ) {
$this->index = $this->indexable->get_network_alias();
} elseif ( is_numeric( $scope ) ) {
$this->index = $this->indexable->get_index_name( (int) $scope );
} elseif ( is_array( $scope ) ) {
} elseif ( ! empty( $site__in ) ) {
$this->index = [];

foreach ( $scope as $site_id ) {
foreach ( $site__in as $site_id ) {
$this->index[] = $this->indexable->get_index_name( $site_id );
}

$this->index = implode( ',', $this->index );
} elseif ( ! empty( $site__not_in ) ) {

$sites = get_sites(
array(
'fields' => 'ids',
'site__not_in' => $site__not_in,
)
);
foreach ( $sites as $site_id ) {
if ( ! Utils\is_site_indexable( $site_id ) ) {
continue;
}
$index[] = Indexables::factory()->get( 'comment' )->get_index_name( $site_id );
}

$this->index = implode( ',', $index );

}

$ep_query = $this->indexable->query_es( $formatted_args, $query->query_vars, $this->index, $query );
Expand Down
41 changes: 36 additions & 5 deletions includes/classes/Indexable/Post/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,25 @@ public function get_es_posts( $posts, $query ) {
if ( count( $new_posts ) < 1 ) {

$scope = 'current';

$site__in = '';
$site__not_in = '';

if ( ! empty( $query_vars['sites'] ) ) {
$scope = $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'] ) ) {

$site__in = (array) $query_vars['site__in'];
$scope = 'all' === $query_vars['site__in'] ? 'all' : $site__in;
}

if ( ! empty( $query_vars['site__not_in'] ) ) {
$site__not_in = ! is_array( $query_vars['site__not_in'] ) ? array( $query_vars['site__not_in'] ) : $query_vars['site__not_in'];
}

$formatted_args = Indexables::factory()->get( 'post' )->format_args( $query_vars, $query );
Expand All @@ -301,12 +318,26 @@ public function get_es_posts( $posts, $query ) {

if ( 'all' === $scope ) {
$index = Indexables::factory()->get( 'post' )->get_network_alias();
} elseif ( is_numeric( $scope ) ) {
$index = Indexables::factory()->get( 'post' )->get_index_name( (int) $scope );
} elseif ( is_array( $scope ) ) {
} elseif ( ! empty( $site__in ) ) {
$index = [];

foreach ( $scope as $site_id ) {
foreach ( $site__in as $site_id ) {
$index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id );
}

$index = implode( ',', $index );
} elseif ( ! empty( $site__not_in ) ) {

$sites = get_sites(
array(
'fields' => 'ids',
'site__not_in' => $site__not_in,
)
);
foreach ( $sites as $site_id ) {
if ( ! Utils\is_site_indexable( $site_id ) ) {
continue;
}
$index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id );
}

Expand Down
39 changes: 34 additions & 5 deletions includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,24 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {
$formatted_args = $indexable->format_args( $query->query_vars );

$scope = 'current';

$site__in = [];
$site__not_in = [];

if ( ! empty( $query->query_vars['sites'] ) ) {
$scope = $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__not_in'] ) ) {
$site__not_in = ! is_array( $query->query_vars['site__not_in'] ) ? array( $query->query_vars['site__not_in'] ) : $query->query_vars['site__not_in'];
}

/**
Expand All @@ -107,15 +123,28 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {

if ( 'all' === $scope ) {
$index = $indexable->get_network_alias();
} elseif ( is_numeric( $scope ) ) {
$index = $indexable->get_index_name( (int) $scope );
} elseif ( is_array( $scope ) ) {
} elseif ( ! empty( $site__in ) ) {
$index = [];

foreach ( $scope as $site_id ) {
foreach ( $site__in as $site_id ) {
$index[] = $indexable->get_index_name( $site_id );
}

$index = implode( ',', $index );
} elseif ( ! empty( $site__not_in ) ) {

$sites = get_sites(
array(
'fields' => 'ids',
'site__not_in' => $site__not_in,
)
);
foreach ( $sites as $site_id ) {
if ( ! Utils\is_site_indexable( $site_id ) ) {
continue;
}
$index[] = Indexables::factory()->get( 'term' )->get_index_name( $site_id );
}
$index = implode( ',', $index );
}

Expand Down
2 changes: 2 additions & 0 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,7 @@ function skip_translations_api() {

require_once $_tests_dir . '/includes/bootstrap.php';

require_once __DIR__ . '/includes/classes/factory/CategoryFactory.php';
require_once __DIR__ . '/includes/classes/factory/CommentFactory.php';
require_once __DIR__ . '/includes/classes/BaseTestCase.php';
require_once __DIR__ . '/includes/classes/FeatureTest.php';
22 changes: 22 additions & 0 deletions tests/php/includes/classes/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ class BaseTestCase extends WP_UnitTestCase {
*/
protected $applied_filters = array();

/**
* Holds the factory object
*
* @var obj
* @since 4.4.0
*/
protected $ep_factory;

/**
* Set up the test case.
*
* @var obj
* @since 4.4.0
*/
public function setup() {

$this->ep_factory = new \stdClass();
$this->ep_factory->category = new CategoryFactory();
$this->ep_factory->comment = new CommentFactory();
parent::setup();
}

/**
* Helper function to test whether a post sync has happened
*
Expand Down
34 changes: 34 additions & 0 deletions tests/php/includes/classes/factory/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Class for category factory.
*/

namespace ElasticPressTest;

use ElasticPress;

/**
* Unit test factory for the category.
*
* @since 4.4.0
*/
class CategoryFactory extends \WP_UnitTest_Factory_For_Term {

/**
* Inserts a comment and "sync" it to Elasticsearch
*
* @param array $args The category details.
*
* @return int|false The category's ID on success, false on failure.
*/
public function create_object( $args ) {

$args['taxonomy'] = 'category';
$id = parent::create_object( $args );

ElasticPress\Indexables::factory()->get( 'term' )->index( $id, true );

return $id;
}

}
32 changes: 32 additions & 0 deletions tests/php/includes/classes/factory/CommentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Class for comment factory.
*/

namespace ElasticPressTest;

use ElasticPress;

/**
* Unit test factory for the comment.
*
* @since 4.4.0
*/
class CommentFactory extends \WP_UnitTest_Factory_For_Comment {

/**
* Inserts a comment.
*
* @param array $args The comment details.
*
* @return int|false The comment's ID on success, false on failure.
*/
public function create_object( $args ) {
$id = wp_insert_comment( $this->addslashes_deep( $args ) );

ElasticPress\Indexables::factory()->get( 'comment' )->index( $id );

return $id;
}

}
Loading