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

Only integrate WC queries if main query, search, or has ep_integrate #3546

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Only integrate WC queries if main query, search, or has ep_integrate
  • Loading branch information
felipeelia committed Jul 19, 2023
commit 5b6c073eaea353e1d5cf917aff2bc730d7b7991b
12 changes: 11 additions & 1 deletion includes/classes/Feature/WooCommerce/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,22 @@ public function search_order( $wp ) {
}

/**
* Determines whether or not ES should be integrating with the provided query
* Determines whether or not ES should be integrating with the provided query.
*
* An order-related query will be integrated if:
* * Is the main query OR is a search OR has `ep_integrate` set as true
* * Is querying a supported post type like `order`
*
* @param \WP_Query $query Query we might integrate with
* @return bool
*/
public function should_integrate_with_query( \WP_Query $query ) : bool {
$has_ep_integrate = isset( $query->query_vars['ep_integrate'] ) && filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN );

if ( ! $query->is_main_query() && ! $query->is_search() && ! $has_ep_integrate ) {
return false;
}

/**
* Check the post type
*/
Expand Down
14 changes: 13 additions & 1 deletion includes/classes/Feature/WooCommerce/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,24 @@ public function translate_args( $query ) {
}

/**
* Determines whether or not ES should be integrating with the provided query
* Determines whether or not ES should be integrating with the provided query.
*
* A product-related query will be integrated if:
* * Is the main query OR is a search OR has `ep_integrate` set as true
* * Is querying a supported taxonomy like product attributes
* * Is querying a supported post type like `product`
*
* @param \WP_Query $query Query we might integrate with
* @return bool
*/
public function should_integrate_with_query( \WP_Query $query ) : bool {
$has_ep_integrate = isset( $query->query_vars['ep_integrate'] ) && filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN );
$is_search = '' !== $this->woocommerce->get_search_term( $query );

if ( ! $query->is_main_query() && ! $is_search && ! $has_ep_integrate ) {
return false;
}

/**
* Check for taxonomies
*/
Expand Down
44 changes: 42 additions & 2 deletions tests/php/features/WooCommerce/TestWooCommerceOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testShopOrderPostTypeQueryOn() {


/**
* Test Shop Order post type query does get integrated when the protected content feature is activated.
* Test Shop Order post type query does not get automatically integrated when the protected content feature is activated.
*
* @group woocommerce
* @group woocommerce-orders
Expand All @@ -126,7 +126,7 @@ public function testShopOrderPostTypeQueryWhenProtectedContentEnable() {
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertNull( $query->elasticsearch_success );
$this->assertEquals( 1, $query->post_count );
$this->assertEquals( 1, $query->found_posts );
}
Expand Down Expand Up @@ -160,6 +160,46 @@ public function testShopOrderPostTypeQueryWhenEPIntegrateSetFalse() {
$this->assertNull( $query->elasticsearch_success );
}

/**
* Test Shop Order post type query does get integrated when the protected content feature is activated and ep_integrate is set to true.
*
* @since 4.7.0
* @group woocommerce
* @group woocommerce-orders
*/
public function testShopOrderPostTypeQueryWhenEPIntegrateSetTrue() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$args = array(
'post_type' => 'shop_order',
'ep_integrate' => true,
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
}

/**
* Test Shop Order post type query does get integrated when the protected content feature is activated and it is the main query.
*
* @since 4.7.0
* @group woocommerce
* @group woocommerce-orders
*/
public function testShopOrderPostTypeQueryWhenMainQuery() {
global $wp_the_query;

ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$wp_the_query->query( [ 'post_type' => 'shop_order' ] );

$this->assertTrue( $wp_the_query->elasticsearch_success );
}

/**
* Test search for shop orders by order ID
*
Expand Down
69 changes: 62 additions & 7 deletions tests/php/features/WooCommerce/TestWooCommerceProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,15 @@ public function testProductsPostTypeQueryOn() {
}

/**
* Test products post type query does get integrated when querying WC product_cat taxonomy
* Test products post type query does not get automatically integrated when querying WC product_cat taxonomy
*
* @group woocommerce
* @group woocommerce-products
*/
public function testProductsPostTypeQueryProductCatTax() {
ElasticPress\Features::factory()->activate_feature( 'admin' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$this->ep_factory->post->create();

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

$args = array(
'tax_query' => array(
array(
Expand All @@ -89,15 +84,75 @@ public function testProductsPostTypeQueryProductCatTax() {

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertNull( $query->elasticsearch_success );

$args = [ 'product_cat' => 'cat' ];

$query = new \WP_Query( $args );

$this->assertNull( $query->elasticsearch_success );
}

/**
* Test WC product_cat taxonomy queries do get automatically integrated when ep_integrate is set to true
*
* @since 4.7.0
* @group woocommerce
* @group woocommerce-products
*/
public function testProductsPostTypeQueryProductCatTaxWhenEPIntegrateSetTrue() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$args = [
'product_cat' => 'cat',
'ep_integrate' => true,
];

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
}

/**
* Test WC product_cat taxonomy queries do get automatically integrated for the main query
*
* @since 4.7.0
* @group woocommerce
* @group woocommerce-products
*/
public function testProductsPostTypeQueryProductCatTaxWhenMainQuery() {
global $wp_the_query;

ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$args = [
'product_cat' => 'cat',
];

$wp_the_query->query( $args );

$this->assertTrue( $wp_the_query->elasticsearch_success );
}

/**
* Test products post type query does get automatically integrated for the main query
*
* @since 4.7.0
* @group woocommerce
* @group woocommerce-products
*/
public function testProductsPostTypeQueryProductWhenMainQuery() {
global $wp_the_query;

ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$wp_the_query->query( [ 'post_type' => 'product' ] );

$this->assertTrue( $wp_the_query->elasticsearch_success );
}

/**
* Test search integration is on in general for product searches
Expand Down