Skip to content

Commit

Permalink
PHP 8.1 compatibility fixes (#3357)
Browse files Browse the repository at this point in the history
* Fix connection array merge error

* Adding PHP 8.1 to the list of testable versions

* Changing PHPUnit version for PHP 8.1 (using 10.5.29)

* Changing PHPUnit version for PHP 8.1 (using 10.5.29)

* Revert test changes (will be moved to another PR)

* PHP 8.1 and PHPUnit 10.5 compatibility fixes

* Checking if the default options value is an array

* Changelog and readme entries

* Renaming class to test compatibility fix

* Renaming more classes to fix compatibility issues

* Renaming more classes to fix compatibility issues

* Migration configuration file for PHP 8.1

* Increasing wp-cli version

* Including missing PHPUnit 10.5 classes

* Bumping PHPUnit Polyfills version

* Disabling warnings and notices due removal

* Reverting changes with no effect

* Update test with expect error

* Including classes removed from PHPUnit 10.5 to maintain compatibility

* Renaming test classes following the new PHPUnit 10.5 format

* Fix deprecation warnings

* Removing unnecessary includes

* Revert class renaming due PHPUnit downgrade

* Reverting CI configuration changes

* Git ignore

* Fix deprecation issues

* Update tests removing deprecated methods

* Remove usage of short ternaries

* Fix lint issues

* Improving fix for the deprecation issue with get_option

* Abstracting the main stripe settings handling

* Fix merge issues

* Adding specific unit tests for the new Stripe settings methods

* Readme and changelog entries

* Fix lint issues

* Fix merge issues

* Fix merge issues

* Revert constants removal

* Including deprecated annotation

* Replacing Stripe settings retrieval method with a generic version support payment method settings as well

* Fix lint issues
  • Loading branch information
wjrosa authored Aug 19, 2024
1 parent c693101 commit f89dee1
Show file tree
Hide file tree
Showing 57 changed files with 294 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
woocommerce: [ 'beta' ]
wordpress: [ 'latest' ]
php: [ '7.4', '8.0' ]
php: [ '7.4', '8.0', '8.1' ]


name: Beta (PHP=${{ matrix.php }}, WP=${{ matrix.wordpress }}, WC=${{ matrix.woocommerce }})
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ docker/bin/jt
docker/data
docker/wordpress
docker/logs

# PHPUnit cache files
.phpunit.cache/
.phpunit.result.cache
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 8.7.0 - xxxx-xx-xx =
* Fix - Fix multiple compatibility issues and deprecation warnings when running the extension on PHP 8.1.
* Fix - Re-connect promotional surface blinking after disappearing for merchants that have already connected their Stripe account.
* Fix - Fix possible fatal errors when Stripe settings format is invalid during account connection.
* Fix - Clear webhook state after reconfiguring webhooks to remove outdated error and success statuses.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require-dev": {
"composer/installers": "1.9.0",
"phpunit/phpunit": "7.5.20",
"yoast/phpunit-polyfills": "^1.1",
"yoast/phpunit-polyfills": "^2.0",
"woocommerce/woocommerce-sniffs": "0.1.0",
"wp-cli/wp-cli-bundle": "2.5.0"
},
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function __construct() {
// Load the settings.
$this->init_settings();

$main_settings = get_option( 'woocommerce_stripe_settings' );
$main_settings = WC_Stripe_Helper::get_stripe_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
Expand Down
2 changes: 1 addition & 1 deletion includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public function get_stripe_return_url( $order = null, $id = null ) {
* @return array()
*/
public function generate_payment_request( $order, $prepared_payment_method ) {
$settings = get_option( 'woocommerce_stripe_settings', [] );
$settings = WC_Stripe_Helper::get_stripe_settings();
$is_short_statement_descriptor_enabled = ! empty( $settings['is_short_statement_descriptor_enabled'] ) && 'yes' === $settings['is_short_statement_descriptor_enabled'];
$capture = ! empty( $settings['capture'] ) && 'yes' === $settings['capture'] ? true : false;
$post_data = [];
Expand Down
15 changes: 10 additions & 5 deletions includes/admin/class-wc-rest-stripe-account-keys-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* @since 5.6.0
*/
class WC_REST_Stripe_Account_Keys_Controller extends WC_Stripe_REST_Base_Controller {
/**
* The option name for the Stripe gateway settings.
*
* @deprecated 8.7.0
*/
const STRIPE_GATEWAY_SETTINGS_OPTION_NAME = 'woocommerce_stripe_settings';

/**
Expand Down Expand Up @@ -154,7 +159,7 @@ public function register_routes() {
*/
public function get_account_keys() {
$allowed_params = [ 'publishable_key', 'secret_key', 'webhook_secret', 'test_publishable_key', 'test_secret_key', 'test_webhook_secret' ];
$stripe_settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
// Filter only the fields we want to return
$account_keys = array_intersect_key( $stripe_settings, array_flip( $allowed_params ) );

Expand Down Expand Up @@ -247,7 +252,7 @@ public function validate_test_secret_key( $param, $request, $key ) {
* @param WP_REST_Request $request Full data about the request.
*/
public function set_account_keys( WP_REST_Request $request ) {
$settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
$settings = WC_Stripe_Helper::get_stripe_settings();
$allowed_params = [ 'publishable_key', 'secret_key', 'webhook_secret', 'test_publishable_key', 'test_secret_key', 'test_webhook_secret' ];

$current_account_keys = array_intersect_key( $settings, array_flip( $allowed_params ) );
Expand Down Expand Up @@ -278,7 +283,7 @@ public function set_account_keys( WP_REST_Request $request ) {
// Before saving the settings, decommission any previously automatically configured webhook endpoint.
$settings = $this->decommission_configured_webhook_after_key_update( $settings, $current_account_keys );

update_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings );
WC_Stripe_Helper::update_main_stripe_settings( $settings );

// Disable all payment methods if all keys are different from the current ones
if ( $current_account_keys['publishable_key'] !== $settings['publishable_key']
Expand Down Expand Up @@ -324,7 +329,7 @@ public function test_account_keys( WP_REST_Request $request ) {
$publishable = wc_clean( wp_unslash( $request->get_param( 'publishable' ) ) );
$secret = wc_clean( wp_unslash( $request->get_param( 'secret' ) ) );

$settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
$settings = WC_Stripe_Helper::get_stripe_settings();

if ( $publishable === $this->mask_key_value( $publishable ) ) {
$publishable = $settings[ $live_mode ? 'publishable_key' : 'test_publishable_key' ];
Expand Down Expand Up @@ -384,7 +389,7 @@ public function configure_webhooks( WP_REST_Request $request ) {

WC_Rate_Limiter::set_rate_limit( $rate_limit_key, 60 );

$settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
$settings = WC_Stripe_Helper::get_stripe_settings();
$secret = wc_clean( wp_unslash( $request->get_param( 'secret' ) ) );
$saved_secret = $settings[ $live_mode ? 'secret_key' : 'test_secret_key' ];

Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-wc-rest-stripe-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ private function update_is_upe_enabled( WP_REST_Request $request ) {
return;
}

$settings = get_option( 'woocommerce_stripe_settings', [] );
$settings = WC_Stripe_Helper::get_stripe_settings();
$settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = $is_upe_enabled ? 'yes' : 'disabled';
update_option( 'woocommerce_stripe_settings', $settings );
WC_Stripe_Helper::update_main_stripe_settings( $settings );

// including the class again because otherwise it's not present.
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-wc-stripe-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function stripe_check_environment() {
$show_curl_notice = get_option( 'wc_stripe_show_curl_notice' );
$show_sca_notice = get_option( 'wc_stripe_show_sca_notice' );
$changed_keys_notice = get_option( 'wc_stripe_show_changed_keys_notice' );
$options = get_option( 'woocommerce_stripe_settings' );
$options = WC_Stripe_Helper::get_stripe_settings();
$testmode = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false;
$test_pub_key = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : '';
$test_secret_key = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : '';
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-wc-stripe-inbox-notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function should_show_marketing_note() {
}

// Make sure Apple Pay is enabled and setup is successful.
$stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$stripe_enabled = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled'];
$button_enabled = isset( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request'];
$verification_complete = isset( $stripe_settings['apple_pay_domain_set'] ) && 'yes' === $stripe_settings['apple_pay_domain_set'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WC_Stripe_Payment_Gateways_Controller {
*/
public function __construct() {
// If UPE is enabled and there are enabled payment methods, we need to load the disable Stripe confirmation modal.
$stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$enabled_upe_payment_methods = isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) ? $stripe_settings['upe_checkout_experience_accepted_payments'] : [];
$upe_payment_requests_enabled = 'yes' === $stripe_settings['payment_request'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public function set_flag( WP_REST_Request $request ) {
return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 );
}

$settings = get_option( 'woocommerce_stripe_settings', [] );
$settings = WC_Stripe_Helper::get_stripe_settings();
$settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = $is_upe_enabled ? 'yes' : 'disabled';

update_option( 'woocommerce_stripe_settings', $settings );
WC_Stripe_Helper::update_main_stripe_settings( $settings );

// including the class again because otherwise it's not present.
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-wc-stripe-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function admin_options( WC_Stripe_Payment_Gateway $gateway ) {
wc_back_link( __( 'Return to payments', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
echo '</h2>';

$settings = get_option( WC_Stripe_Connect::SETTINGS_OPTION, [] );
$settings = WC_Stripe_Helper::get_stripe_settings();

$account_data_exists = ( ! empty( $settings['publishable_key'] ) && ! empty( $settings['secret_key'] ) ) || ( ! empty( $settings['test_publishable_key'] ) && ! empty( $settings['test_secret_key'] ) );
echo $account_data_exists ? '<div id="wc-stripe-account-settings-container"></div>' : '<div id="wc-stripe-new-account-container"></div>';
Expand Down
6 changes: 3 additions & 3 deletions includes/class-wc-stripe-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function cache_account( $mode = null ) {
* @return string Transient key of test mode when testmode is enabled, otherwise returns the key of live mode.
*/
private function get_transient_key( $mode = null ) {
$settings_options = get_option( 'woocommerce_stripe_settings', [] );
$settings_options = WC_Stripe_Helper::get_stripe_settings();

// If the mode is not provided or is invalid, we'll check the current mode.
if ( is_null( $mode ) || ! in_array( $mode, [ 'test', 'live' ] ) ) {
Expand Down Expand Up @@ -309,7 +309,7 @@ public function configure_webhooks( $mode = 'live', $secret_key = '' ) {
WC_Stripe_API::set_secret_key( $previous_secret );
}

$settings = get_option( WC_Stripe::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
$settings = WC_Stripe_Helper::get_stripe_settings();

$webhook_secret_setting = 'live' === $mode ? 'webhook_secret' : 'test_webhook_secret';
$webhook_data_setting = 'live' === $mode ? 'webhook_data' : 'test_webhook_data';
Expand All @@ -322,7 +322,7 @@ public function configure_webhooks( $mode = 'live', $secret_key = '' ) {
'secret' => WC_Stripe_API::get_secret_key(),
];

update_option( WC_Stripe::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings );
WC_Stripe_Helper::update_main_stripe_settings( $settings );

// After reconfiguring webhooks, clear the webhook state.
WC_Stripe_Webhook_State::clear_state();
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-stripe-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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 = get_option( 'woocommerce_stripe_settings' );
$options = WC_Stripe_Helper::get_stripe_settings();
$secret_key = $options['secret_key'] ?? '';
$test_secret_key = $options['test_secret_key'] ?? '';

Expand Down Expand Up @@ -389,7 +389,7 @@ public static function detach_payment_method_from_customer( string $customer_id,
* @return bool True if the payment should be detached, false otherwise.
*/
public static function should_detach_payment_method_from_customer() {
$options = get_option( 'woocommerce_stripe_settings' );
$options = WC_Stripe_Helper::get_stripe_settings();
$test_mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'];

// If we are in test mode, we can always detach the payment method.
Expand Down
6 changes: 3 additions & 3 deletions includes/class-wc-stripe-apple-pay-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct() {
add_action( 'add_option_woocommerce_stripe_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 );
add_action( 'update_option_woocommerce_stripe_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 );

$this->stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
$this->stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$this->domain_name = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : str_replace( array( 'https://', 'http://' ), '', get_site_url() ); // @codingStandardsIgnoreLine
$this->apple_pay_domain_set = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' );
$this->apple_pay_verify_notice = '';
Expand Down Expand Up @@ -286,7 +286,7 @@ public function register_domain_with_apple( $secret_key ) {
$this->stripe_settings['apple_pay_domain_set'] = 'yes';
$this->apple_pay_domain_set = true;

update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
WC_Stripe_Helper::update_main_stripe_settings( $this->stripe_settings );

WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' );

Expand All @@ -297,7 +297,7 @@ public function register_domain_with_apple( $secret_key ) {
$this->stripe_settings['apple_pay_domain_set'] = 'no';
$this->apple_pay_domain_set = false;

update_option( 'woocommerce_stripe_settings', $this->stripe_settings );
WC_Stripe_Helper::update_main_stripe_settings( $this->stripe_settings );

WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-stripe-blocks-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct( $payment_request_configuration = null ) {
* Initializes the payment method type.
*/
public function initialize() {
$this->settings = get_option( 'woocommerce_stripe_settings', [] );
$this->settings = WC_Stripe_Helper::get_stripe_settings();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-stripe-feature-flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function is_upe_preview_enabled() {
* @return bool
*/
public static function is_upe_checkout_enabled() {
$stripe_settings = get_option( 'woocommerce_stripe_settings', null );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
return ! empty( $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] )
&& 'yes' === $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ];
}
Expand All @@ -33,7 +33,7 @@ public static function is_upe_checkout_enabled() {
* @return bool
*/
public static function did_merchant_disable_upe() {
$stripe_settings = get_option( 'woocommerce_stripe_settings', null );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
return ! empty( $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) && 'disabled' === $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ];
}
}
Loading

0 comments on commit f89dee1

Please sign in to comment.