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
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
58 changes: 55 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,61 @@ on:
- '[0-9].[0-9x]*' # Version branches: 4.x.x, 4.1.x, 5.x

jobs:
phpunit:
name: PHP Unit
phpunit_single_site:
name: PHP Unit (Single Site)
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Start MySQL
run: sudo systemctl start mysql.service

- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144

- name: Setup Elasticsearch
uses: getong/elasticsearch-action@v1.2
with:
elasticsearch version: '7.5.0'

- name: Set standard 10up cache directories
run: |
composer config -g cache-dir "${{ env.COMPOSER_CACHE }}"

- name: Prepare composer cache
uses: actions/cache@v2
with:
path: ${{ env.COMPOSER_CACHE }}
key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
composer-${{ env.COMPOSER_VERSION }}-

- name: Set PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none

- name: Install dependencies
run: composer install

- name: Setup WP Tests
run: |
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1
sleep 10

- name: PHPUnit
run: |
composer run-script test-single-site

phpunit_multisite:
name: PHP Unit (Multisite)
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -69,4 +122,3 @@ jobs:
- name: PHPUnit
run: |
composer run-script test
composer run-script test-single-site
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 = (array) $query->query_vars['site__not_in'];
}

/**
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 = (array) $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 = (array) $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
3 changes: 3 additions & 0 deletions single-site.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<testsuites>
<testsuite name="elasticpress">
<directory prefix="Test" suffix=".php">./tests/php/</directory>
<exclude>./tests/php/indexables/TestCommentMultisite.php</exclude>
<exclude>./tests/php/indexables/TestPostMultisite.php</exclude>
<exclude>./tests/php/indexables/TestTermMultisite.php</exclude>
</testsuite>
</testsuites>
<filter>
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