Skip to content

Commit

Permalink
Allow WC Shipping to use larger migration state machine enums (#2804)
Browse files Browse the repository at this point in the history
* Allow WC Shipping to use larger migration state values

* Disable banners if the state machine is larger than WCS&T's responsibilities

---------

Co-authored-by: Gerhard <[email protected]>
  • Loading branch information
kallehauge and kloon authored Aug 29, 2024
1 parent bd37ded commit 92ce537
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 9 additions & 3 deletions classes/class-wc-connect-service-settings-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,20 @@ public function get_package_lookup() {
}

public function is_eligible_for_migration() {
$migration_state = intval( get_option( 'wcshipping_migration_state', 0 ) );

// If the migration state is greater than "COMPLETED", then we can assume that the next part of the migration
// state is being handled by WooCommerce Shipping.
if ( $migration_state > WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED ) {
return false;
}

$migration_dismissed = false;
if ( isset( $_COOKIE[ WC_Connect_Loader::MIGRATION_DISMISSAL_COOKIE_KEY ] ) && (int) $_COOKIE[ WC_Connect_Loader::MIGRATION_DISMISSAL_COOKIE_KEY ] === 1 ) {
$migration_dismissed = true;
}

$migration_state = get_option( 'wcshipping_migration_state' );

$migration_pending = ! $migration_state || WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED !== intval( $migration_state );
$migration_pending = ! $migration_state || WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED !== $migration_state;
$migration_enabled = $this->service_schemas_store->is_wcship_wctax_migration_enabled();

return $migration_pending && $migration_enabled && ! $migration_dismissed;
Expand Down
21 changes: 17 additions & 4 deletions classes/class-wc-rest-connect-migration-flag-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,31 @@ public function set_tracks( $tracks ) {
public function post( $request ) {
$params = $request->get_json_params();
$migration_state = intval( $params['migration_state'] );

// If the migration state is greater than "COMPLETED", then we can assume that the next part of the migration
// state is being handled by WooCommerce Shipping.
// This shouldn't be necessary since our migration shouldn't be displayed to begin with, but we're adding
// support for it, just in case.
if ( $migration_state > WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED ) {
return new WP_REST_Response(
array(
'result' => __( 'WooCommerce Shipping has taken over the migration process.', 'woocommerce-services' ),
),
200
);
}

if ( ! WC_Connect_WCST_To_WCShipping_Migration_State_Enum::is_valid_value( $migration_state ) ) {
$error = new WP_Error(
return new WP_Error(
'invalid_migration_state',
__( 'Invalid migration state. Can not update migration state.', 'woocommerce-services' ),
array( 'status' => 400 )
);
return $error;
}

$existing_migration_state = get_option( 'wcshipping_migration_state' );
if ( $existing_migration_state && intval( $existing_migration_state ) === $migration_state ) {
return new WP_REST_Response( array( 'result' => 'Migration flag is the same, no changes needed.' ), 304 );
return new WP_REST_Response( array( 'result' => __( 'Migration flag is the same, no changes needed.', 'woocommerce-services' ) ), 304 );
}

$result = update_option( 'wcshipping_migration_state', $migration_state );
Expand All @@ -61,7 +74,7 @@ public function post( $request ) {
)
);

return new WP_REST_Response( array( 'result' => 'Migration flag updated successfully.' ), 200 );
return new WP_REST_Response( array( 'result' => __( 'Migration flag updated successfully.', 'woocommerce-services' ) ), 200 );
}

$error = new WP_Error(
Expand Down

0 comments on commit 92ce537

Please sign in to comment.