Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 3738 #3739

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
80 changes: 45 additions & 35 deletions includes/classes/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public function setup() {
/**
* Activate a feature
*
* @param string $slug Feature slug
* @since 2.2
* @param string $slug Feature slug
* @param string $target Whether to update a feature settings' draft or current
* @since 2.2, 5.0.0 added $target
*/
public function activate_feature( $slug ) {
$this->update_feature( $slug, array( 'active' => true ) );
public function activate_feature( $slug, $target = 'current' ) {
$this->update_feature( $slug, array( 'active' => true ), true, $target );
}

/**
Expand Down Expand Up @@ -92,7 +93,7 @@ public function get_registered_feature( $slug ) {
* @param string $slug Feature slug
* @param array $settings Array of settings
* @param bool $force Whether to force activate/deactivate
* @param string $target Whether to update a feature settings' draft or current
* @param string $target Whether to update a feature settings' draft or current. Changing current will also save the draft.
* @since 2.2, 5.0.0 added $target
* @return array|bool
*/
Expand Down Expand Up @@ -145,8 +146,10 @@ public function update_feature( $slug, $settings, $force = true, $target = 'curr
$new_settings = wp_parse_args( [ $slug => $new_feature_settings ], $saved_settings );
$new_settings = apply_filters( 'ep_sanitize_feature_settings', $new_settings, $feature );

Utils\update_option( 'ep_feature_settings_draft', $new_settings );

// This is as far as we go if saving just a draft
if ( 'draft' === $target ) {
Utils\update_option( 'ep_feature_settings_draft', $new_settings );
return true;
}

Expand Down Expand Up @@ -266,41 +269,48 @@ public function handle_feature_activation() {
* If a requirement status changes, we need to handle that by activating/deactivating/showing notification
*/

if ( ( $is_wp_cli || is_admin() ) && ! empty( $old_requirement_statuses ) ) {
foreach ( $new_requirement_statuses as $slug => $code ) {
$feature = $this->get_registered_feature( $slug );
if ( ( ! $is_wp_cli && ! is_admin() ) || empty( $old_requirement_statuses ) ) {
return;
}

foreach ( $new_requirement_statuses as $slug => $code ) {
$feature = $this->get_registered_feature( $slug );

// If a feature is forced inactive, do nothing
$feature_settings = $feature->get_settings();
if ( is_array( $feature_settings ) && ! empty( $feature_settings['force_inactive'] ) ) {
continue;
}
// If a feature is forced inactive, do nothing
$feature_settings = $feature->get_settings();
if ( is_array( $feature_settings ) && ! empty( $feature_settings['force_inactive'] ) ) {
continue;
}

// This is a new feature
if ( ! isset( $old_requirement_statuses[ $slug ] ) ) {
if ( 0 === $code ) {
$this->activate_feature( $slug );
// By default we will activate the feature in the current settings. If it requires a sync, we'll only update the draft
$activate_feature_target = 'current';

// This is a new feature
if ( ! isset( $old_requirement_statuses[ $slug ] ) ) {
if ( 0 === $code ) {
if ( $feature->requires_install_reindex ) {
$activate_feature_target = 'draft';
Utils\update_option( 'ep_feature_auto_activated_sync', sanitize_text_field( $slug ) );
}

$this->activate_feature( $slug, $activate_feature_target );
}
} else {
// This feature has a 0 "ok" code when it did not before
if ( $old_requirement_statuses[ $slug ] !== $code && ( 0 === $code || 2 === $code ) ) {
$active = ( 0 === $code );

if ( ! $feature->is_active() && $active ) {
// Need to activate and maybe set a sync notice
if ( $feature->requires_install_reindex ) {
$activate_feature_target = 'draft';
Utils\update_option( 'ep_feature_auto_activated_sync', sanitize_text_field( $slug ) );
}
}
} else {
// This feature has a 0 "ok" code when it did not before
if ( $old_requirement_statuses[ $slug ] !== $code && ( 0 === $code || 2 === $code ) ) {
$active = ( 0 === $code );

if ( ! $feature->is_active() && $active ) {
$this->activate_feature( $slug );

// Need to activate and maybe set a sync notice
if ( $feature->requires_install_reindex ) {
Utils\update_option( 'ep_feature_auto_activated_sync', sanitize_text_field( $slug ) );
}
} elseif ( $feature->is_active() && ! $active ) {
// Just deactivate, don't force
$this->deactivate_feature( $slug, false );
}

$this->activate_feature( $slug, $activate_feature_target );
} elseif ( $feature->is_active() && ! $active ) {
// Just deactivate, don't force
$this->deactivate_feature( $slug, false );
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions includes/classes/REST/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ public function update_settings( \WP_REST_Request $request ) {
}
}
}

FeaturesStore::factory()->update_feature( $slug, $new_settings[ $slug ], true, 'draft' );
}

foreach ( $settings_that_requires_features as $feature => $fields ) {
Expand All @@ -174,6 +172,10 @@ public function update_settings( \WP_REST_Request $request ) {
FeaturesStore::factory()->update_feature( $slug, $feature );
}

foreach ( $new_settings as $slug => $feature ) {
FeaturesStore::factory()->update_feature( $slug, $feature, true, 'draft' );
}

return [
'data' => $current_settings,
'success' => true,
Expand Down
14 changes: 8 additions & 6 deletions tests/php/TestFeatureActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,17 @@ public function testAutoDeactivateWithFeature() {
* test that when it's req status changes, it's enabled.
*
* @group feature-activation
* @since 2.2
* @since 5.0.0
*/
public function testAutoActivateWithFeature() {
public function test_auto_activate_with_feature() {
delete_site_option( 'ep_feature_requirement_statuses' );
delete_site_option( 'ep_feature_settings' );

ElasticPress\Features::factory()->register_feature(
new FeatureTest()
);
$feature = new FeatureTest();

$feature->requires_install_reindex = false;

ElasticPress\Features::factory()->register_feature( $feature );

update_site_option( 'ep_test_feature_on', 2 );

Expand Down Expand Up @@ -366,9 +368,9 @@ public function test_feature_setting_update() {
'active' => true,
'force_inactive' => false,
'field_1' => '1',
'field_4' => '1',
'field_2' => '1',
'field_3' => '1',
'field_4' => '1',
],
$draft_settings['test']
);
Expand Down
Loading