Skip to content

Commit aaee659

Browse files
committed
Fix: Use 'ep_' . ->slug . '_sync_kill' to prevent indexing the object
1 parent 4d38e92 commit aaee659

File tree

6 files changed

+110
-11
lines changed

6 files changed

+110
-11
lines changed

includes/classes/Feature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function requirements_status() {
193193
/**
194194
* Filter feature requirement status
195195
*
196-
* @hook ep_{indexable_slug}_index_kill
196+
* @hook ep_feature_requirements_status
197197
* @param {FeatureRequirementStatus} $status Current feature requirement status
198198
* @param {Feature} $feature Current feature
199199
* @since 2.2

includes/classes/Feature/WooCommerce/OrdersAutosuggest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function setup() {
8484
add_action( 'ep_woocommerce_shop_order_search_fields', [ $this, 'set_search_fields' ], 10, 2 );
8585
add_filter( 'ep_index_posts_args', [ $this, 'maybe_query_password_protected_posts' ] );
8686
add_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ], 10, 2 );
87+
add_filter( 'ep_pre_kill_sync_for_password_protected', [ $this, 'sync_password_protected_orders' ], 10, 3 );
8788
}
8889

8990
/**
@@ -104,6 +105,7 @@ public function tear_down() {
104105
remove_action( 'ep_woocommerce_shop_order_search_fields', [ $this, 'set_search_fields' ] );
105106
remove_filter( 'ep_index_posts_args', [ $this, 'maybe_query_password_protected_posts' ] );
106107
remove_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ] );
108+
remove_filter( 'ep_pre_kill_sync_for_password_protected', [ $this, 'sync_password_protected_orders' ] );
107109
}
108110

109111
/**
@@ -566,6 +568,22 @@ public function add_settings_schema( array $settings_schema ): array {
566568
return $settings_schema;
567569
}
568570

571+
/**
572+
* Short-circuit the sync for password protected orders.
573+
*
574+
* @param null|bool $new_skip Short-circuit flag
575+
* @param bool $skip Current value of $skip
576+
* @param int $object_id The object ID
577+
* @return null|bool
578+
*/
579+
public function sync_password_protected_orders( $new_skip, bool $skip, $object_id ) {
580+
if ( 'shop_order' === get_post_type( $object_id ) ) {
581+
return $skip;
582+
}
583+
584+
return $new_skip;
585+
}
586+
569587
/**
570588
* Return the help message for the setting schema field
571589
*

includes/classes/IndexHelper.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,24 +1178,46 @@ protected function should_skip_object_index( $indexable_object, $indexable ) {
11781178
* Filter whether to not sync specific item in dashboard or not
11791179
*
11801180
* @since 2.1
1181+
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
11811182
* @hook ep_item_sync_kill
11821183
* @param {boolean} $kill False means dont sync
11831184
* @param {array} $indexable_object Object to sync
11841185
* @return {Indexable} Indexable that object belongs to
11851186
*/
1186-
$ep_item_sync_kill = apply_filters( 'ep_item_sync_kill', false, $indexable_object, $indexable );
1187+
$ep_item_sync_kill = apply_filters_deprecated(
1188+
'ep_item_sync_kill',
1189+
[ false, $indexable_object, $indexable ],
1190+
'5.3.3',
1191+
'ep_' . $indexable->slug . '_sync_kill'
1192+
);
11871193

11881194
/**
1189-
* Conditionally kill indexing for a post
1195+
* Conditionally kill indexing for an object
11901196
*
1197+
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
11911198
* @hook ep_{indexable_slug}_index_kill
11921199
* @param {bool} $index True means dont index
11931200
* @param {int} $object_id Object ID
11941201
* @return {bool} New value
11951202
*/
1196-
$ep_indexable_sync_kill = apply_filters( 'ep_' . $indexable->slug . '_index_kill', false, $indexable_object->ID );
1203+
$ep_indexable_index_kill = apply_filters_deprecated(
1204+
'ep_' . $indexable->slug . '_index_kill',
1205+
[ false, $indexable_object->ID ],
1206+
'5.3.3',
1207+
'ep_' . $indexable->slug . '_sync_kill'
1208+
);
1209+
1210+
/**
1211+
* Conditionally kill indexing for an object
1212+
*
1213+
* @hook ep_{indexable_slug}_sync_kill
1214+
* @param {bool} $kill True means dont sync
1215+
* @param {int} $object_id Object ID
1216+
* @return {bool} New value
1217+
*/
1218+
$ep_indexable_sync_kill = apply_filters( 'ep_' . $indexable->slug . '_sync_kill', false, $indexable_object->ID );
11971219

1198-
return $ep_item_sync_kill || $ep_indexable_sync_kill;
1220+
return $ep_item_sync_kill || $ep_indexable_sync_kill || $ep_indexable_index_kill;
11991221
}
12001222

12011223
/**

includes/classes/Indexable.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,27 @@ public function index( $object_id, $blocking = false ) {
279279
/**
280280
* Conditionally kill indexing on a specific object
281281
*
282+
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
282283
* @hook ep_{indexable_slug}_index_kill
283284
* @param {bool} $kill True to not index
284285
* @param {int} $object_id Id of object to index
285286
* @since 3.0
286287
* @return {bool} New kill value
287288
*/
288-
if ( apply_filters( 'ep_' . $this->slug . '_index_kill', false, $object_id ) ) {
289+
if ( apply_filters_deprecated( 'ep_' . $this->slug . '_index_kill', [ false, $object_id ], '5.3.3', 'ep_' . $this->slug . '_sync_kill' ) ) {
290+
return false;
291+
}
292+
293+
/**
294+
* Conditionally kill indexing for an object.
295+
*
296+
* @hook ep_{$this->slug}_sync_kill
297+
* @param {bool} $kill True means dont sync
298+
* @param {int} $object_id Object ID
299+
* @return {bool} New value
300+
*/
301+
$ep_indexable_sync_kill = apply_filters( 'ep_' . $this->slug . '_sync_kill', false, $object_id );
302+
if ( $ep_indexable_sync_kill ) {
289303
return false;
290304
}
291305

tests/php/features/TestProtectedContent.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ public function testAdminCategories() {
224224
}
225225

226226
/**
227-
* Check if passwords on posts are synced when feature not active
227+
* Check if password protected post is not synced when feature is disabled
228228
*
229229
* @since 4.0.0
230230
* @group protected-content
231231
*/
232-
public function testNoSyncPasswordedPost() {
232+
public function test_password_protected_post_is_not_synced_when_feature_is_disabled() {
233233
add_filter( 'ep_post_sync_args', array( $this, 'filter_post_sync_args' ), 10, 1 );
234234

235235
$post_id = $this->ep_factory->post->create( array( 'post_password' => 'test' ) );
@@ -241,8 +241,7 @@ public function testNoSyncPasswordedPost() {
241241

242242
// Check if password was synced
243243
$post = ElasticPress\Indexables::factory()->get( 'post' )->get( $post_id );
244-
245-
$this->assertArrayNotHasKey( 'post_password', $post );
244+
$this->assertEmpty( $post );
246245
}
247246

248247
/**

tests/php/features/WooCommerce/TestWooCommerceOrdersAutosuggest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class TestWooCommerceOrdersAutosuggest extends BaseTestCase {
3636
*/
3737
public function set_up() {
3838
parent::set_up();
39-
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
4039
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
4140

4241
$this->woocommerce_feature = ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' );
@@ -46,6 +45,11 @@ public function set_up() {
4645

4746
ElasticPress\Features::factory()->setup_features();
4847

48+
ElasticPress\Elasticsearch::factory()->delete_all_indices();
49+
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
50+
51+
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();
52+
4953
$this->orders_autosuggest = $this->woocommerce_feature->orders_autosuggest;
5054
}
5155

@@ -438,4 +442,46 @@ public function test_get_setting_help_message_feature_not_available() {
438442
$new_settings_schema = $this->orders_autosuggest->add_settings_schema( [] );
439443
$this->assertStringContainsString( 'Due to the sensitive nature of orders', $new_settings_schema[0]['help'] );
440444
}
445+
446+
/**
447+
* Test shop_order with password is synced without Protected Content feature enabled.
448+
*
449+
* @since 5.3.3
450+
* @group woocommerce
451+
* @group woocommerce-orders-autosuggest
452+
*/
453+
public function test_order_with_password_is_synced() {
454+
add_filter( 'ep_woocommerce_orders_autosuggest_available', '__return_true' );
455+
456+
/**
457+
* Enable the orders autosuggest feature.
458+
*/
459+
$filter = function () {
460+
return [
461+
'woocommerce' => [
462+
'orders' => '1',
463+
],
464+
];
465+
};
466+
add_filter( 'pre_site_option_ep_feature_settings', $filter );
467+
add_filter( 'pre_option_ep_feature_settings', $filter );
468+
469+
$this->orders_autosuggest->setup();
470+
471+
$order = new \WC_Order();
472+
$order->set_order_key( '1234567890' ); // save in the post_password field
473+
$order->save();
474+
475+
$order_id = $order->get_id();
476+
477+
ElasticPress\Indexables::factory()->get( 'post' )->index( $order_id );
478+
ElasticPress\Elasticsearch::factory()->refresh_indices();
479+
480+
$order = ElasticPress\Indexables::factory()->get( 'post' )->get( $order_id );
481+
$this->assertNotEmpty( $order );
482+
483+
remove_filter( 'ep_woocommerce_orders_autosuggest_available', '__return_true' );
484+
remove_filter( 'pre_option_ep_feature_settings', $filter );
485+
remove_filter( 'pre_site_option_ep_feature_settings', $filter );
486+
}
441487
}

0 commit comments

Comments
 (0)