Skip to content

Commit

Permalink
Merge pull request #2728 from 10up/fix/issue-2726
Browse files Browse the repository at this point in the history
Prevent order fields from being removed
  • Loading branch information
felipeelia authored Apr 27, 2022
2 parents bce3903 + 52ae235 commit 5e544fd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
57 changes: 40 additions & 17 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
28 changes: 13 additions & 15 deletions tests/php/features/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 5e544fd

Please sign in to comment.