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; } diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 91ffce7148..be680dfeb3 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,27 @@ 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 + * + * @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 + */ + 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 * diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index 35c8974b66..5bf6f4060e 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', + 's' => (string) $shop_order_id_1, + 'post_type' => 'shop_order', + 'post_status' => 'any', ); $query = new \WP_Query( $args );