Skip to content

POC - Refactoring settings (2) #4412

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions includes/admin/class-wc-stripe-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public function admin_notices() {
*/
public static function display_legacy_deprecation_notice( $plugin_file ) {
global $wp_list_table;
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$stripe_settings = WC_Stripe_Settings::get_instance();

// If Stripe is not enabled, don't show the legacy deprecation notice.
if ( ! isset( $stripe_settings['enabled'] ) || 'no' === $stripe_settings['enabled'] ) {
if ( ! $stripe_settings->get_enabled() || 'no' === $stripe_settings->get_enabled() ) {
return;
}

Expand Down
6 changes: 2 additions & 4 deletions includes/class-wc-stripe-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ public static function get_secret_key() {
* @param string|null $mode Optional. The mode to set the secret key for. 'live' or 'test'. Default will set the secret for the currently active mode.
*/
public static function set_secret_key_for_mode( $mode = null ) {
$options = WC_Stripe_Helper::get_stripe_settings();
$secret_key = $options['secret_key'] ?? '';
$test_secret_key = $options['test_secret_key'] ?? '';
$stripe_settings = WC_Stripe_Settings::get_instance();

if ( ! in_array( $mode, [ 'test', 'live' ], true ) ) {
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
}

self::set_secret_key( 'test' === $mode ? $test_secret_key : $secret_key );
self::set_secret_key( 'test' === $mode ? $stripe_settings->get_test_secret_key() : $stripe_settings->get_secret_key() );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions includes/class-wc-stripe-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
* @since 4.0.0
*/
class WC_Stripe_Helper {
/**
* Option name for storing Stripe settings.
*
* @deprecated 9.6.0 Use WC_Stripe_Settings::SETTINGS_OPTION instead.
*/
const SETTINGS_OPTION = 'woocommerce_stripe_settings';
const LEGACY_META_NAME_FEE = 'Stripe Fee';
const LEGACY_META_NAME_NET = 'Net Revenue From Stripe';
Expand All @@ -32,6 +37,8 @@ class WC_Stripe_Helper {
*
* @param string $method (Optional) The payment method to get the settings from.
* @return array $settings The Stripe settings.
*
* @deprecated 9.6.0 Use WC_Stripe_Settings specific getters instead.
*/
public static function get_stripe_settings( $method = null ) {
$settings = null === $method ? get_option( self::SETTINGS_OPTION, [] ) : get_option( 'woocommerce_stripe_' . $method . '_settings', [] );
Expand Down
4 changes: 1 addition & 3 deletions includes/class-wc-stripe-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public static function can_log(): bool {
return false;
}

$settings = WC_Stripe_Helper::get_stripe_settings();

if ( empty( $settings ) || ( isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) ) {
if ( 'yes' !== WC_Stripe_Settings::get_instance()->get_logging() ) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions includes/class-wc-stripe-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class WC_Stripe_Mode {
* @return bool Whether the plugin is in live mode.
*/
public static function is_live() {
$settings = WC_Stripe_Helper::get_stripe_settings();
return 'yes' !== ( $settings['testmode'] ?? 'no' );
return 'yes' !== ( WC_Stripe_Settings::get_instance()->get_test_mode() ?? 'no' );
}

/**
Expand All @@ -23,7 +22,6 @@ public static function is_live() {
* @return bool Whether the plugin is in test mode.
*/
public static function is_test() {
$settings = WC_Stripe_Helper::get_stripe_settings();
return 'yes' === ( $settings['testmode'] ?? 'no' );
return 'yes' === ( WC_Stripe_Settings::get_instance()->get_test_mode() ?? 'no' );
}
}
40 changes: 13 additions & 27 deletions includes/class-wc-stripe-payment-method-configurations.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ public static function get_upe_available_payment_method_ids() {
public static function get_upe_enabled_payment_method_ids( $force_refresh = false ) {
// If the payment method configurations API is not enabled, we fallback to the enabled payment methods stored in the DB.
if ( ! self::is_enabled() ) {
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
return isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) && ! empty( $stripe_settings['upe_checkout_experience_accepted_payments'] )
? $stripe_settings['upe_checkout_experience_accepted_payments']
: [ WC_Stripe_Payment_Methods::CARD ];
return WC_Stripe_Settings::get_instance()->get_upe_checkout_experience_accepted_payments();
}

// Migrate payment methods from DB to Stripe PMC if needed
Expand Down Expand Up @@ -303,17 +300,17 @@ function ( $id ) use ( $is_test_mode ) {
* @return bool
*/
public static function is_enabled() {
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$connection_type_key = WC_Stripe_Mode::is_test() ? 'test_connection_type' : 'connection_type';
$stripe_settings = WC_Stripe_Settings::get_instance();
$connection_type = WC_Stripe_Mode::is_test() ? $stripe_settings->get_test_connection_type() : $stripe_settings->get_connection_type();

// If the account is not a Connect OAuth account, we can't use the payment method configurations API.
if ( ! isset( $stripe_settings[ $connection_type_key ] ) || 'connect' !== $stripe_settings[ $connection_type_key ] ) {
if ( 'connect' !== $connection_type ) {
return false;
}

// If we have the pmc_enabled flag, and it is set to no, we should not use the payment method configurations API.
// We only disable the PMC if the flag is set to no explicitly, an empty value means the migration has not been attempted yet.
if ( isset( $stripe_settings['pmc_enabled'] ) && 'no' === $stripe_settings['pmc_enabled'] ) {
if ( 'no' === $stripe_settings->get_pmc_enabled() ) {
return false;
}

Expand All @@ -326,15 +323,15 @@ public static function is_enabled() {
* @param bool $force_migration Whether to force the migration.
*/
public static function maybe_migrate_payment_methods_from_db_to_pmc( $force_migration = false ) {
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$stripe_settings = WC_Stripe_Settings::get_instance();

// Skip if PMC is not enabled.
if ( ! self::is_enabled() ) {
return;
}

// Skip if migration already done (pmc_enabled is set) and we are not forcing the migration.
if ( ! empty( $stripe_settings['pmc_enabled'] ) && ! $force_migration ) {
if ( $stripe_settings->get_pmc_enabled() && ! $force_migration ) {
return;
}

Expand All @@ -344,18 +341,10 @@ public static function maybe_migrate_payment_methods_from_db_to_pmc( $force_migr
return;
}

$enabled_payment_methods = [];

if ( isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) &&
! empty( $stripe_settings['upe_checkout_experience_accepted_payments'] ) ) {
$enabled_payment_methods = array_merge(
$enabled_payment_methods,
$stripe_settings['upe_checkout_experience_accepted_payments']
);
}
$enabled_payment_methods = $stripe_settings->get_upe_checkout_experience_accepted_payments();

// Add Google Pay and Apple Pay to the list if payment_request is enabled
if ( ! empty( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request'] ) {
if ( 'yes' === $stripe_settings->get_payment_request() ) {
$enabled_payment_methods = array_merge(
$enabled_payment_methods,
[ WC_Stripe_Payment_Methods::GOOGLE_PAY, WC_Stripe_Payment_Methods::APPLE_PAY ]
Expand All @@ -381,22 +370,19 @@ public static function maybe_migrate_payment_methods_from_db_to_pmc( $force_migr
}

// If there is no payment method order defined, set it to the default order
if ( empty( $stripe_settings['stripe_upe_payment_method_order'] ) ) {
$stripe_settings['stripe_upe_payment_method_order'] = array_keys( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS );
if ( empty( $stripe_settings->get_stripe_upe_payment_method_order() ) ) {
$stripe_settings->set_stripe_upe_payment_method_order( array_keys( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS ) );
}

// Mark migration as complete in stripe settings
$stripe_settings['pmc_enabled'] = 'yes';
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
$stripe_settings->set_pmc_enabled( 'yes' );
}

/**
* Disables the payment method configuration sync by setting pmc_enabled to 'no' in the Stripe settings.
* This is called when no Payment Method Configuration is found that inherits from the WooCommerce Platform.
*/
private static function disable_payment_method_configuration_sync() {
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$stripe_settings['pmc_enabled'] = 'no';
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
WC_Stripe_Settings::get_instance()->set_pmc_enabled( 'no' );
}
}
Loading
Loading