Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion includes/classes/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function requirements_status() {
/**
* Filter feature requirement status
*
* @hook ep_{indexable_slug}_index_kill
* @hook ep_feature_requirements_status
* @param {FeatureRequirementStatus} $status Current feature requirement status
* @param {Feature} $feature Current feature
* @since 2.2
Expand Down
18 changes: 18 additions & 0 deletions includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function setup() {
add_action( 'ep_woocommerce_shop_order_search_fields', [ $this, 'set_search_fields' ], 10, 2 );
add_filter( 'ep_index_posts_args', [ $this, 'maybe_query_password_protected_posts' ] );
add_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ], 10, 2 );
add_filter( 'ep_pre_kill_sync_for_password_protected', [ $this, 'sync_password_protected_orders' ], 10, 3 );
}

/**
Expand All @@ -104,6 +105,7 @@ public function tear_down() {
remove_action( 'ep_woocommerce_shop_order_search_fields', [ $this, 'set_search_fields' ] );
remove_filter( 'ep_index_posts_args', [ $this, 'maybe_query_password_protected_posts' ] );
remove_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ] );
remove_filter( 'ep_pre_kill_sync_for_password_protected', [ $this, 'sync_password_protected_orders' ] );
}

/**
Expand Down Expand Up @@ -566,6 +568,22 @@ public function add_settings_schema( array $settings_schema ): array {
return $settings_schema;
}

/**
* Short-circuit the sync for password protected orders.
*
* @param null|bool $new_skip Short-circuit flag
* @param bool $skip Current value of $skip
* @param int $object_id The object ID
* @return null|bool
*/
public function sync_password_protected_orders( $new_skip, bool $skip, $object_id ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burhandodhy let's avoid forcing types on methods hooked into actions and filters, as this has caused us some problems lately. We can certainly cast it to bool on return though.

if ( 'shop_order' === get_post_type( $object_id ) ) {
return $skip;
}

return $new_skip;
}

/**
* Return the help message for the setting schema field
*
Expand Down
30 changes: 26 additions & 4 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,24 +1178,46 @@ protected function should_skip_object_index( $indexable_object, $indexable ) {
* Filter whether to not sync specific item in dashboard or not
*
* @since 2.1
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
* @hook ep_item_sync_kill
* @param {boolean} $kill False means dont sync
* @param {array} $indexable_object Object to sync
* @return {Indexable} Indexable that object belongs to
*/
$ep_item_sync_kill = apply_filters( 'ep_item_sync_kill', false, $indexable_object, $indexable );
$ep_item_sync_kill = apply_filters_deprecated(
'ep_item_sync_kill',
[ false, $indexable_object, $indexable ],
'5.3.3',
'ep_' . $indexable->slug . '_sync_kill'
);

/**
* Conditionally kill indexing for a post
* Conditionally kill indexing for an object
*
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
* @hook ep_{indexable_slug}_index_kill
* @param {bool} $index True means dont index
* @param {int} $object_id Object ID
* @return {bool} New value
*/
$ep_indexable_sync_kill = apply_filters( 'ep_' . $indexable->slug . '_index_kill', false, $indexable_object->ID );
$ep_indexable_index_kill = apply_filters_deprecated(
'ep_' . $indexable->slug . '_index_kill',
[ false, $indexable_object->ID ],
'5.3.3',
'ep_' . $indexable->slug . '_sync_kill'
);

/**
* Conditionally kill indexing for an object
*
* @hook ep_{indexable_slug}_sync_kill
* @param {bool} $kill True means dont sync
* @param {int} $object_id Object ID
* @return {bool} New value
*/
$ep_indexable_sync_kill = apply_filters( 'ep_' . $indexable->slug . '_sync_kill', false, $indexable_object->ID );

return $ep_item_sync_kill || $ep_indexable_sync_kill;
return $ep_item_sync_kill || $ep_indexable_sync_kill || $ep_indexable_index_kill;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,27 @@ public function index( $object_id, $blocking = false ) {
/**
* Conditionally kill indexing on a specific object
*
* @deprecated 5.3.3 Use ep_{indexable_slug}_sync_kill instead
* @hook ep_{indexable_slug}_index_kill
* @param {bool} $kill True to not index
* @param {int} $object_id Id of object to index
* @since 3.0
* @return {bool} New kill value
*/
if ( apply_filters( 'ep_' . $this->slug . '_index_kill', false, $object_id ) ) {
if ( apply_filters_deprecated( 'ep_' . $this->slug . '_index_kill', [ false, $object_id ], '5.3.3', 'ep_' . $this->slug . '_sync_kill' ) ) {
return false;
}

/**
* Conditionally kill indexing for an object.
*
* @hook ep_{$this->slug}_sync_kill
* @param {bool} $kill True means dont sync
* @param {int} $object_id Object ID
* @return {bool} New value
*/
$ep_indexable_sync_kill = apply_filters( 'ep_' . $this->slug . '_sync_kill', false, $object_id );
if ( $ep_indexable_sync_kill ) {
return false;
}

Expand Down
7 changes: 3 additions & 4 deletions tests/php/features/TestProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ public function testAdminCategories() {
}

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

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

// Check if password was synced
$post = ElasticPress\Indexables::factory()->get( 'post' )->get( $post_id );

$this->assertArrayNotHasKey( 'post_password', $post );
$this->assertEmpty( $post );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class TestWooCommerceOrdersAutosuggest extends BaseTestCase {
*/
public function set_up() {
parent::set_up();
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );

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

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

ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->orders_autosuggest = $this->woocommerce_feature->orders_autosuggest;
}

Expand Down Expand Up @@ -438,4 +442,46 @@ public function test_get_setting_help_message_feature_not_available() {
$new_settings_schema = $this->orders_autosuggest->add_settings_schema( [] );
$this->assertStringContainsString( 'Due to the sensitive nature of orders', $new_settings_schema[0]['help'] );
}

/**
* Test shop_order with password is synced without Protected Content feature enabled.
*
* @since 5.3.3
* @group woocommerce
* @group woocommerce-orders-autosuggest
*/
public function test_order_with_password_is_synced() {
add_filter( 'ep_woocommerce_orders_autosuggest_available', '__return_true' );

/**
* Enable the orders autosuggest feature.
*/
$filter = function () {
return [
'woocommerce' => [
'orders' => '1',
],
];
};
add_filter( 'pre_site_option_ep_feature_settings', $filter );
add_filter( 'pre_option_ep_feature_settings', $filter );

$this->orders_autosuggest->setup();

$order = new \WC_Order();
$order->set_order_key( '1234567890' ); // save in the post_password field
$order->save();

$order_id = $order->get_id();

ElasticPress\Indexables::factory()->get( 'post' )->index( $order_id );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$order = ElasticPress\Indexables::factory()->get( 'post' )->get( $order_id );
$this->assertNotEmpty( $order );

remove_filter( 'ep_woocommerce_orders_autosuggest_available', '__return_true' );
remove_filter( 'pre_option_ep_feature_settings', $filter );
remove_filter( 'pre_site_option_ep_feature_settings', $filter );
}
}