-
Notifications
You must be signed in to change notification settings - Fork 68
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
Old WC versions: try loading WCPay if the account is connected #3010
Conversation
includes/class-wc-payments.php
Outdated
$account_data = get_option( 'wcpay_account_data' ); | ||
if ( ! isset( $account_data['account'] ) ) { | ||
return; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, could we move the initialization of self::$api_client
and self::$account
before the call to check_plugin_dependencies
so that here we could just do something like self::$api_client->is_stripe_connected()
?
There might not always be an account cached from the previous version at this point, so it would be good to still try to issue a request to check it in the server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, ideally we still use self::$api_client->is_stripe_connected()
.
I wonder, could we move the initialization of
self::$api_client
andself::$account
before the call tocheck_plugin_dependencies
[...]
I will explore this path and see if that's possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, could we move the initialization of self::$api_client and self::$account before the call to check_plugin_dependencies [...]
The flow of self::$api_client->is_stripe_connected()
looks like the following:
- WC_Payments_Account->is_stripe_connected()
- WC_Payments_Account->try_is_stripe_connected()
- WC_Payments_Account->get_cached_account_data()
- WC_Payments_API_Client->get_account_data()
- WC_Payments::get_gateway()->is_in_dev_mode()
For the last call above, it's tricky as it requires WC_Payments::$card_gateway assigned value, which is only available very late:
woocommerce-payments/includes/class-wc-payments.php
Lines 267 to 269 in 2d76e58
self::$card_gateway = new $upe_class( self::$api_client, self::$account, self::$customer_service, self::$token_service, self::$action_scheduler_service, $payment_methods, self::$failed_transaction_rate_limiter ); | |
} else { | |
self::$card_gateway = new $card_class( self::$api_client, self::$account, self::$customer_service, self::$token_service, self::$action_scheduler_service, self::$failed_transaction_rate_limiter ); |
If we move this gateway code before the call to check_plugin_dependencies
, it will create a fatal error for the case WooCommerce does not exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked my current code, and found out that it will yell fatal errors in this case:
- WooCommerce is not active.
- WCPay cache account exists.
I tried to change this behavior around the check for old WC versions but could not find a proper way due to the dual purposes of the current check_plugin_dependencies
code. May you have some other ideas?
That does not mean this change is not possible but it requires more thought and careful testing.
All in all, we may not change anything here now as it might create more issues with this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for digging into this. In general, it feels like the test/dev mode checks shouldn't belong in the payment gateway settings anymore because our plugin is no longer just a payment gateway. The dual purpose of check_plugin_dependencies
is also something that has been causing slight headaches for a while. But refactoring either of those is way out of scope of this PR.
I also tested and it looks like we manage to populate the account before deactivating the plugin, so maybe auto-update will have the same effect: it would populate the cache and then update, so things can keep working.
So how about this:
- Move the whole
get_option
andisset
checks into their own function that returns a boolean- I'd really like that function to be a part of
WC_Payments_Account
but requiring it might cause some issues down the line so probably fine to keep it in this file - Just
isset( $account_data['account'] )
might not be sufficient, as the account can be a string. So we need to change it toisset( $account_data['account'] ) && is_array( $account_data['account'] )
- I'd really like that function to be a part of
So something like:
private static function has_cached_account_connection(): bool {
$account_data = get_option( 'wcpay_account_data' );
return isset( $account_data['account'] ) && is_array( $account_data['account'] );
}
- Call that function inside
check_plugin_dependencies
rather than after it - Inside
check_plugin_dependencies
if any of the dependencies is not there at all always return false. - If the dependency is present but outdated, instead of returning false, return the value of
has_cached_account_connection()
Edit: but that would not render the notices for people who are connected but out of date 🤔 Maybe it is time to split the check_plugin_dependencies
into two - one for returning true/false and one for rendering the notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, it feels like the test/dev mode checks shouldn't belong in the payment gateway settings anymore because our plugin is no longer just a payment gateway.
Agreed here!
I also tested and it looks like we manage to populate the account before deactivating the plugin, so maybe auto-update will have the same effect: it would populate the cache and then update, so things can keep working.
I will double-check this too.
Edit: but that would not render the notices for people who are connected but out of date 🤔 Maybe it is time to split the
check_plugin_dependencies
into two - one for returning true/false and one for rendering the notice.
What's about the following idea?
check_plugin_dependencies()
stops using param$silent
, and returns an array including two keys:passed
with a boolean value indicating whether or not andmessage
.- When calling
check_plugin_dependencies()
, we display an admin error notice formessage
, and continue loading the whole plugin or not based onpassed
. Something like this to replace those lines:
list( $message, $passed ) = self::check_plugin_dependencies();
if ( null !== $message ) {
self::display_admin_error( $message );
}
if ( false === $passed ) {
return;
}
Update: With this, we do not need to change logic much inside check_plugin_dependencies()
and it's still possible to decouple the decision loading the whole plugin and displaying any notice message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's a better approach than having a function that does two things. Let's try that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marcinbot. I've implemented my idea and your suggestion:
# Conflicts: # changelog.txt # readme.txt
…dependency status flag.
# Conflicts: # changelog.txt # readme.txt
Following the test instructions give the expected behaviour. The refactoring of the version check makes sense and the messaging is clear. I was worried that a site experiencing a temporary connection issue wouldn't attempt to reconnect, but on testing that cache value is also an array - so it's all good. I was thinking of the "no account" value of If you could resolve the merge conflict and re-run the CI tests this looks ready to merge. |
# Conflicts: # changelog.txt
Awesome, thanks James! Fixed the changelog. |
Context: paJDYF-2Dr-p2#comment-8197
Changes proposed in this Pull Request
Handle the case new WCPay versions do not support current WC core versions:
WC_Payments_Account::is_stripe_connected
. Instead, I use option_namewcpay_account_data
(can not use this constant too due to the same loading issue).Testing instructions
5.8
,6.0
, etc.woocommerce-payments/woocommerce-payments.php
Line 11 in 87c34e4
Before
screenshot above, AND no WCPay menu items is loading.After
screenshot above, AND WCPay menu items are still loading.Post merge