From a9e83302e5033d1764f4fd955ad4af9c40d0aa58 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 27 Apr 2022 10:30:23 -0300 Subject: [PATCH 1/5] Add new parameters to ep_pc_skip_post_content_cleanup --- .../classes/Feature/ProtectedContent/ProtectedContent.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/ProtectedContent/ProtectedContent.php b/includes/classes/Feature/ProtectedContent/ProtectedContent.php index 72d044a41d..244c6875ee 100644 --- a/includes/classes/Feature/ProtectedContent/ProtectedContent.php +++ b/includes/classes/Feature/ProtectedContent/ProtectedContent.php @@ -247,11 +247,13 @@ public function remove_fields_from_password_protected( $post_args, $post_id ) { * Filter to skip the password protected content clean up. * * @hook ep_pc_skip_post_content_cleanup - * @since 4.0.0 - * @param {bool} $skip Whether the password protected content should have their content, and meta removed. + * @since 4.0.0, 4.2.0 added $post_args and $post_id + * @param {bool} $skip Whether the password protected content should have their content, and meta removed + * @param {array} $post_args Post arguments + * @param {int} $post_id Post ID * @return {bool} */ - if ( apply_filters( 'ep_pc_skip_post_content_cleanup', false ) ) { + if ( apply_filters( 'ep_pc_skip_post_content_cleanup', false, $post_args, $post_id ) ) { return $post_args; } From 587193947bb0bd67ce629227ab64a4d69b8363eb Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 27 Apr 2022 10:30:43 -0300 Subject: [PATCH 2/5] Prevent order fields from being removed --- .../Feature/WooCommerce/WooCommerce.php | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 91ffce7148..2e6ac64f0b 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -785,24 +785,26 @@ public function suggest_wc_add_post_type( $post_types ) { * @since 2.1 */ public function setup() { - if ( function_exists( 'WC' ) ) { - 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_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 ); - add_filter( 'ep_sync_taxonomies', [ $this, 'whitelist_taxonomies' ], 10, 2 ); - add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'add_order_items_search' ], 20, 2 ); - add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 ); - add_action( 'ep_wp_query_search_cached_posts', [ $this, 'disallow_duplicated_query' ], 10, 2 ); - add_action( 'parse_query', [ $this, 'maybe_hook_woocommerce_search_fields' ], 1 ); - add_action( 'parse_query', [ $this, 'search_order' ], 11 ); - add_filter( 'ep_term_suggest_post_type', [ $this, 'suggest_wc_add_post_type' ] ); - add_filter( 'ep_facet_include_taxonomies', [ $this, 'add_product_attributes' ] ); - add_filter( 'ep_weighting_fields_for_post_type', [ $this, 'add_product_attributes_to_weighting' ], 10, 2 ); - add_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_product_default_post_type_weights' ], 10, 2 ); + if ( ! function_exists( 'WC' ) ) { + return; } + 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_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 ); + add_filter( 'ep_sync_taxonomies', [ $this, 'whitelist_taxonomies' ], 10, 2 ); + add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'add_order_items_search' ], 20, 2 ); + add_filter( 'ep_pc_skip_post_content_cleanup', [ $this, 'keep_order_fields' ], 20, 2 ); + add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 ); + add_action( 'ep_wp_query_search_cached_posts', [ $this, 'disallow_duplicated_query' ], 10, 2 ); + add_action( 'parse_query', [ $this, 'maybe_hook_woocommerce_search_fields' ], 1 ); + add_action( 'parse_query', [ $this, 'search_order' ], 11 ); + add_filter( 'ep_term_suggest_post_type', [ $this, 'suggest_wc_add_post_type' ] ); + add_filter( 'ep_facet_include_taxonomies', [ $this, 'add_product_attributes' ] ); + add_filter( 'ep_weighting_fields_for_post_type', [ $this, 'add_product_attributes_to_weighting' ], 10, 2 ); + add_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_product_default_post_type_weights' ], 10, 2 ); } /** @@ -912,6 +914,26 @@ public function price_filter( $args, $query_args, $query ) { return $args; } + /** + * Prevent order fields from being removed. + * + * When Protected Content is enabled, all posts with password have their content removed. + * This can't happen for orders, as the order key is added in that field. + * + * @see https://github.com/10up/ElasticPress/issues/2726 + * + * @param bool $skip Whether the password protected content should have their content, and meta removed + * @param array $post_args Post arguments + * @return bool + */ + public function keep_order_fields( $skip, $post_args ) { + if ( 'shop_order' === $post_args['post_type'] ) { + return true; + } + + return $skip; + } + /** * Determines whether or not ES should be integrating with the provided query * From 126539758ab03069ab72ed37966751867fe0a4a1 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 27 Apr 2022 10:31:15 -0300 Subject: [PATCH 3/5] Adjust WC Orders test to use WC functions This way the orders are created with the correct parameters --- tests/php/features/TestWooCommerce.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index 35c8974b66..ad7a446085 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -197,26 +197,24 @@ public function testSearchShopOrderByMetaFieldAndId() { ElasticPress\Features::factory()->activate_feature( 'woocommerce' ); ElasticPress\Features::factory()->setup_features(); - $shop_order_id_1 = Functions\create_and_sync_post( - array( - 'post_type' => 'shop_order', - ) - ); + $this->assertTrue( class_exists( '\WC_Order' ) ); - Functions\create_and_sync_post( - array( - 'post_type' => 'shop_order', - ), - array( - '_billing_phone' => 'Phone number that matches an order ID: ' . $shop_order_id_1, - ) - ); + $shop_order_1 = new \WC_Order(); + $shop_order_1->save(); + $shop_order_id_1 = $shop_order_1->get_id(); + ElasticPress\Indexables::factory()->get( 'post' )->index( $shop_order_id_1, true ); + + $shop_order_2 = new \WC_Order(); + $shop_order_2->set_billing_phone( 'Phone number that matches an order ID: ' . $shop_order_id_1 ); + $shop_order_2->save(); + ElasticPress\Indexables::factory()->get( 'post' )->index( $shop_order_2->get_id(), true ); ElasticPress\Elasticsearch::factory()->refresh_indices(); $args = array( 's' => (string) $shop_order_id_1, 'post_type' => 'shop_order', + 'post_status' => 'any', ); $query = new \WP_Query( $args ); From 61ef63119973cdf0ba54c3f2d6c5f5485a2e2079 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 27 Apr 2022 10:36:58 -0300 Subject: [PATCH 4/5] Add `@since` tag --- includes/classes/Feature/WooCommerce/WooCommerce.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 2e6ac64f0b..be680dfeb3 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -922,6 +922,7 @@ public function price_filter( $args, $query_args, $query ) { * * @see https://github.com/10up/ElasticPress/issues/2726 * + * @since 4.2.0 * @param bool $skip Whether the password protected content should have their content, and meta removed * @param array $post_args Post arguments * @return bool From 52ae2357fc30e8a9ef1679853168276bd24214e9 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 27 Apr 2022 10:37:28 -0300 Subject: [PATCH 5/5] Code alignment --- tests/php/features/TestWooCommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index ad7a446085..5bf6f4060e 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -212,8 +212,8 @@ public function testSearchShopOrderByMetaFieldAndId() { ElasticPress\Elasticsearch::factory()->refresh_indices(); $args = array( - 's' => (string) $shop_order_id_1, - 'post_type' => 'shop_order', + 's' => (string) $shop_order_id_1, + 'post_type' => 'shop_order', 'post_status' => 'any', );