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: Prevent using the ES for coupon search queries #3222

Merged
merged 2 commits into from
Jan 20, 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
8 changes: 6 additions & 2 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,11 @@ public function get_admin_searchable_post_types() {
* @return bool
*/
public function blacklist_coupons( $enabled, $query ) {
if ( method_exists( $query, 'get' ) && 'shop_coupon' === $query->get( 'post_type' ) ) {
if ( is_admin() ) {
return $enabled;
}

if ( 'shop_coupon' === $query->get( 'post_type' ) && empty( $query->query_vars['ep_integrate'] ) ) {
return false;
}

Expand Down Expand Up @@ -860,7 +864,7 @@ public function setup() {
}
add_action( 'ep_formatted_args', [ $this, 'price_filter' ], 10, 3 );
add_filter( 'ep_sync_insert_permissions_bypass', [ $this, 'bypass_order_permissions_check' ], 10, 2 );
add_filter( 'ep_elasticpress_enabled', [ $this, 'blacklist_coupons' ], 10, 2 );
add_filter( 'ep_integrate_search_queries', [ $this, 'blacklist_coupons' ], 10, 2 );
add_filter( 'ep_prepare_meta_allowed_protected_keys', [ $this, 'whitelist_meta_keys' ], 10, 2 );
add_filter( 'woocommerce_layered_nav_query_post_ids', [ $this, 'convert_post_object_to_id' ], 10, 4 );
add_filter( 'woocommerce_unfiltered_product_ids', [ $this, 'convert_post_object_to_id' ], 10, 4 );
Expand Down
114 changes: 111 additions & 3 deletions tests/php/features/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,17 @@ public function testAddVariationsSkusMeta() {
$this->assertTrue( class_exists( '\WC_Product_Variation' ) );

$main_product = new \WC_Product_Variable();
$main_product->set_sku('main-product_sku');
$main_product->set_sku( 'main-product_sku' );
$main_product_id = $main_product->save();

$variation_1 = new \WC_Product_Variation();
$variation_1->set_parent_id( $main_product_id );
$variation_1->set_sku('child-sku-1');
$variation_1->set_sku( 'child-sku-1' );
$variation_1->save();

$variation_2 = new \WC_Product_Variation();
$variation_2->set_parent_id( $main_product_id );
$variation_2->set_sku('child-sku-2');
$variation_2->set_sku( 'child-sku-2' );
$variation_2->save();

$main_product_as_post = get_post( $main_product_id );
Expand Down Expand Up @@ -346,4 +346,112 @@ public function testEPWoocommerceAdminProductsListSearchFields() {
[ 'post_title', 'post_content' ]
);
}

/**
* Tests the search query for a shop_coupon.
*
* @since 4.4.1
*/
public function testSearchQueryForCoupon() {

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

// ensures that the search query doesn't use Elasticsearch.
$query = new \WP_Query(
[
'post_type' => 'shop_coupon',
's' => 'test-coupon',
]
);
$this->assertNull( $query->elasticsearch_success );

// ensures that the search query doesn't use Elasticsearch when ep_integrate set to false.
$query = new \WP_Query(
[
'post_type' => 'shop_coupon',
's' => 'test-coupon',
'ep_integrate' => false,
]
);
$this->assertNull( $query->elasticsearch_success );

// ensures that the search query use Elasticsearch when ep_integrate set to true.
$query = new \WP_Query(
[
'post_type' => 'shop_coupon',
's' => 'test-coupon',
'ep_integrate' => true,
]
);
$this->assertTrue( $query->elasticsearch_success );
}

/**
* Tests the search query for a shop_coupon in admin use Elasticsearch when protected content is enabled.
*
* @since 4.4.1
*/
public function testSearchQueryForCouponWhenProtectedContentIsEnable() {

set_current_screen( 'dashboard' );
$this->assertTrue( is_admin() );

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

$this->ep_factory->post->create(
array(
'post_content' => 'test-coupon',
'post_type' => 'shop_coupon',
)
);

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

$query = new \WP_Query(
[
'post_type' => 'shop_coupon',
's' => 'test-coupon',
]
);

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 1, $query->post_count );
}

/**
* Tests the search query for a shop_coupon in admin does not use Elasticsearch when protected content is not enabled.
*
* @since 4.4.1
*/
public function testSearchQueryForCouponWhenProtectedContentIsNotEnable() {

set_current_screen( 'dashboard' );
$this->assertTrue( is_admin() );

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

$this->ep_factory->post->create(
array(
'post_content' => 'test-coupon',
'post_type' => 'shop_coupon',
)
);

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

$query = new \WP_Query(
[
'post_type' => 'shop_coupon',
's' => 'test-coupon',
'ep_integrate' => true,
]
);

$this->assertNull( $query->elasticsearch_success );
$this->assertEquals( 1, $query->post_count );
}
}