diff --git a/includes/admin/class-wc-stripe-admin-notices.php b/includes/admin/class-wc-stripe-admin-notices.php index 525a4cadc7..7802add23b 100644 --- a/includes/admin/class-wc-stripe-admin-notices.php +++ b/includes/admin/class-wc-stripe-admin-notices.php @@ -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; } diff --git a/includes/class-wc-stripe-api.php b/includes/class-wc-stripe-api.php index 3f4fd67ab1..05b8d16206 100644 --- a/includes/class-wc-stripe-api.php +++ b/includes/class-wc-stripe-api.php @@ -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() ); } /** diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index f3cf3b77d6..d506da520f 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -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'; @@ -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', [] ); diff --git a/includes/class-wc-stripe-logger.php b/includes/class-wc-stripe-logger.php index ba47fb064f..09e849aa91 100644 --- a/includes/class-wc-stripe-logger.php +++ b/includes/class-wc-stripe-logger.php @@ -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; } diff --git a/includes/class-wc-stripe-mode.php b/includes/class-wc-stripe-mode.php index c9d826b292..2ce6a10fcc 100644 --- a/includes/class-wc-stripe-mode.php +++ b/includes/class-wc-stripe-mode.php @@ -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' ); } /** @@ -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' ); } } diff --git a/includes/class-wc-stripe-payment-method-configurations.php b/includes/class-wc-stripe-payment-method-configurations.php index 5fd99aa465..6b257aa14a 100644 --- a/includes/class-wc-stripe-payment-method-configurations.php +++ b/includes/class-wc-stripe-payment-method-configurations.php @@ -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 @@ -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; } @@ -326,7 +323,7 @@ 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() ) { @@ -334,7 +331,7 @@ public static function maybe_migrate_payment_methods_from_db_to_pmc( $force_migr } // 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; } @@ -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 ] @@ -381,13 +370,12 @@ 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' ); } /** @@ -395,8 +383,6 @@ public static function maybe_migrate_payment_methods_from_db_to_pmc( $force_migr * 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' ); } } diff --git a/includes/class-wc-stripe-settings.php b/includes/class-wc-stripe-settings.php new file mode 100644 index 0000000000..6d7a441ead --- /dev/null +++ b/includes/class-wc-stripe-settings.php @@ -0,0 +1,308 @@ +settings = $settings; + } + + /** + * Returns the *Singleton* instance of this class. + * + * @return WC_Stripe_Settings The *Singleton* instance. + */ + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Get the value of the `enabled` setting. + * + * @return string + */ + public function get_enabled() { + return $this->settings['enabled'] ?? ''; + } + + /** + * Get the value of the `logging` setting. + * + * @return string + */ + public function get_logging() { + return $this->settings['logging'] ?? ''; + } + + /** + * Get the publishable key. + * + * @return string + */ + public function get_publishable_key() { + return $this->settings['publishable_key'] ?? ''; + } + + /** + * Get the secret key. + * + * @return string + */ + public function get_secret_key() { + return $this->settings['secret_key'] ?? ''; + } + + /** + * Get the statement descriptor. + * + * @return string + */ + public function get_statement_descriptor() { + return $this->settings['statement_descriptor'] ?? ''; + } + + /** + * Get the test publishable key. + * + * @return string + */ + public function get_test_publishable_key() { + return $this->settings['test_publishable_key'] ?? ''; + } + + /** + * Get the test secret key. + * + * @return string + */ + public function get_test_secret_key() { + return $this->settings['test_secret_key'] ?? ''; + } + + /** + * Get the express checkout button type. + * + * @return string + */ + public function get_express_checkout_button_type() { + return $this->settings['payment_request_button_type'] ?? 'default'; + } + + /** + * Get the express checkout button theme. + * + * @return string + */ + public function get_express_checkout_button_theme() { + return $this->settings['payment_request_button_theme'] ?? 'dark'; + } + + /** + * Gets the button height. + * + * @return string + */ + public function get_express_checkout_button_height() { + $height = $this->settings['payment_request_button_size'] ?? 'default'; + if ( 'small' === $height ) { + return '40'; + } + + if ( 'large' === $height ) { + return '56'; + } + + return '48'; + } + + /** + * Gets the button radius. + * + * @return string + */ + public function get_express_checkout_button_radius() { + $height = $this->settings['payment_request_button_size'] ?? 'default'; + if ( 'small' === $height ) { + return '2'; + } + + if ( 'large' === $height ) { + return '6'; + } + + return '4'; + } + + /** + * Pages where the express checkout buttons should be displayed. + * + * @return array + */ + public function get_express_checkout_button_locations() { + // If the locations have not been set return the default setting. + if ( ! isset( $this->settings['payment_request_button_locations'] ) ) { + return [ 'product', 'cart' ]; + } + + // If all locations are removed through the settings UI the location config will be set to + // an empty string "". If that's the case (and if the settings are not an array for any + // other reason) we should return an empty array. + if ( ! is_array( $this->settings['payment_request_button_locations'] ) ) { + return []; + } + + return $this->settings['payment_request_button_locations']; + } + + /** + * Set the payment request button locations. + * + * @param array $value The value to set. + * @return void + */ + public function set_payment_request_button_locations( $value ) { + $this->settings['payment_request_button_locations'] = $value; + update_option( self::SETTINGS_OPTION, $this->settings ); + } + + /** + * Get the test mode setting. + * + * @return string + */ + public function get_test_mode() { + return $this->settings['test_mode'] ?? ''; + } + + /** + * Get the value of the `upe_checkout_experience_accepted_payments` setting. + * + * @return array + */ + public function get_upe_checkout_experience_accepted_payments() { + return ! empty( $this->settings['upe_checkout_experience_accepted_payments'] ) + ? $this->settings['upe_checkout_experience_accepted_payments'] + : [ WC_Stripe_Payment_Methods::CARD ]; + } + + /** + * Get the value of the `webhook_secret` setting. + * + * @return string + */ + public function get_webhook_secret() { + return $this->settings['webhook_secret'] ?? ''; + } + + /** + * Get the value of the `test_webhook_secret` setting. + * + * @return string + */ + public function get_test_webhook_secret() { + return $this->settings['test_webhook_secret'] ?? ''; + } + + /** + * Get the value of the `connection_type` setting. + * + * @return string + */ + public function get_connection_type() { + return $this->settings['connection_type'] ?? ''; + } + + /** + * Get the value of the `test_connection_type` setting. + * + * @return string + */ + public function get_test_connection_type() { + return $this->settings['test_connection_type'] ?? ''; + } + + /** + * Get the value of the `pmc_enabled` setting. + * + * @return string + */ + public function get_pmc_enabled() { + return $this->settings['pmc_enabled'] ?? ''; + } + + /** + * Set the `pmc_enabled` setting to true. + * + * @param string $value The value to set. + * @return void + */ + public function set_pmc_enabled( $value ) { + $this->settings['pmc_enabled'] = $value; + update_option( self::SETTINGS_OPTION, $this->settings ); + } + + /** + * Get the value of the `payment_request` setting. + * + * @return string + */ + public function get_payment_request() { + return $this->settings['payment_request'] ?? ''; + } + + /** + * Get the value of the `stripe_upe_payment_method_order` setting. + * + * @return array + */ + public function get_stripe_upe_payment_method_order() { + return $this->settings['stripe_upe_payment_method_order'] ?? []; + } + + /** + * Set the `stripe_upe_payment_method_order` setting. + * + * @param array $value The value to set. + * @return void + */ + public function set_stripe_upe_payment_method_order( $value ) { + $this->settings['stripe_upe_payment_method_order'] = $value; + update_option( self::SETTINGS_OPTION, $this->settings ); + } +} diff --git a/includes/class-wc-stripe-status.php b/includes/class-wc-stripe-status.php index f585aca4f7..a27124ce07 100644 --- a/includes/class-wc-stripe-status.php +++ b/includes/class-wc-stripe-status.php @@ -152,7 +152,7 @@ public function render_status_report_section() {