Skip to content

Commit 330a931

Browse files
frossoCopilot
andauthored
refactor: ECE buttons and settings consistency (#11182)
Co-authored-by: Copilot <[email protected]>
1 parent 0177531 commit 330a931

21 files changed

+729
-129
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: update
3+
4+
refactor: Google Pay/Apple Pay settings storage consistency

includes/admin/class-wc-rest-payments-settings-controller.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function get_settings(): WP_REST_Response {
509509
'account_branding_primary_color' => $this->wcpay_gateway->get_option( 'account_branding_primary_color' ),
510510
'account_branding_secondary_color' => $this->wcpay_gateway->get_option( 'account_branding_secondary_color' ),
511511
'account_domestic_currency' => $this->wcpay_gateway->get_option( 'account_domestic_currency' ),
512-
'is_payment_request_enabled' => 'yes' === $this->wcpay_gateway->get_option( 'payment_request' ),
512+
'is_payment_request_enabled' => $this->wcpay_gateway->is_payment_request_enabled(),
513513
'is_apple_google_pay_in_payment_methods_options_enabled' => 'yes' === $this->wcpay_gateway->get_option( 'apple_google_pay_in_payment_methods_options' ),
514514
'is_debug_log_enabled' => 'yes' === $this->wcpay_gateway->get_option( 'enable_logging' ),
515515
'payment_request_enabled_locations' => $this->wcpay_gateway->get_option( 'payment_request_button_locations' ),
@@ -870,7 +870,24 @@ private function update_is_payment_request_enabled( WP_REST_Request $request ) {
870870

871871
$is_payment_request_enabled = $request->get_param( 'is_payment_request_enabled' );
872872

873-
$this->wcpay_gateway->update_option( 'payment_request', $is_payment_request_enabled ? 'yes' : 'no' );
873+
// Update Google Pay and Apple Pay enabled settings to keep them in sync.
874+
$google_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\GooglePayDefinition::get_id() );
875+
$apple_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\ApplePayDefinition::get_id() );
876+
if ( $is_payment_request_enabled ) {
877+
if ( $google_pay_gateway ) {
878+
$google_pay_gateway->enable();
879+
}
880+
if ( $apple_pay_gateway ) {
881+
$apple_pay_gateway->enable();
882+
}
883+
} else {
884+
if ( $google_pay_gateway ) {
885+
$google_pay_gateway->disable();
886+
}
887+
if ( $apple_pay_gateway ) {
888+
$apple_pay_gateway->disable();
889+
}
890+
}
874891
}
875892

876893
/**

includes/class-duplicates-detection-service.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ private function search_for_payment_request_buttons() {
141141
if ( strpos( $gateway->id, $keyword ) !== false ) {
142142
$this->gateways_qualified_by_duplicates_detector[ $prb_payment_method ][] = $gateway->id;
143143
break;
144-
} elseif ( 'yes' === $gateway->get_option( 'payment_request' ) && in_array( $gateway->id, [ 'woocommerce_payments', 'stripe' ], true ) ) {
144+
} elseif ( 'woocommerce_payments' === $gateway->id && method_exists( $gateway, 'is_payment_request_enabled' ) && $gateway->is_payment_request_enabled() ) {
145+
$this->gateways_qualified_by_duplicates_detector[ $prb_payment_method ][] = $gateway->id;
146+
break;
147+
} elseif ( 'stripe' === $gateway->id && 'yes' === $gateway->get_option( 'payment_request' ) ) {
145148
$this->gateways_qualified_by_duplicates_detector[ $prb_payment_method ][] = $gateway->id;
146149
break;
147150
} elseif ( 'yes' === $gateway->get_option( 'express_checkout_enabled' ) ) {

includes/class-wc-payment-gateway-wcpay.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -441,20 +441,6 @@ public function get_form_fields() {
441441
'type' => 'title',
442442
'description' => '',
443443
],
444-
'payment_request' => [
445-
'title' => __( 'Enable/disable', 'woocommerce-payments' ),
446-
'label' => sprintf(
447-
/* translators: 1) br tag 2) Stripe anchor tag 3) Apple anchor tag */
448-
__( 'Enable payment request buttons (Apple Pay, Google Pay, and more). %1$sBy using Apple Pay, you agree to %2$s and %3$s\'s Terms of Service.', 'woocommerce-payments' ),
449-
'<br />',
450-
'<a href="https://stripe.com/apple-pay/legal" target="_blank">Stripe</a>',
451-
'<a href="https://developer.apple.com/apple-pay/acceptable-use-guidelines-for-websites/" target="_blank">Apple</a>'
452-
),
453-
'type' => 'checkbox',
454-
'description' => __( 'If enabled, users will be able to pay using Apple Pay, Google Pay or the Payment Request API if supported by the browser.', 'woocommerce-payments' ),
455-
'default' => empty( get_option( 'woocommerce_woocommerce_payments_settings' ) ) ? 'yes' : 'no', // Enable by default for new installations only.
456-
'desc_tip' => true,
457-
],
458444
'payment_request_button_type' => [
459445
'title' => __( 'Button type', 'woocommerce-payments' ),
460446
'type' => 'select',
@@ -959,7 +945,18 @@ public function is_saved_cards_enabled() {
959945
* @return bool Whether the setting to show the payment request buttons is enabled or not.
960946
*/
961947
public function is_payment_request_enabled() {
962-
return 'yes' === $this->get_option( 'payment_request' );
948+
$google_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\GooglePayDefinition::get_id() );
949+
if ( $google_pay_gateway && $google_pay_gateway->is_enabled() ) {
950+
return true;
951+
}
952+
953+
// Fallback, just in case.
954+
$apple_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\ApplePayDefinition::get_id() );
955+
if ( $apple_pay_gateway && $apple_pay_gateway->is_enabled() ) {
956+
return true;
957+
}
958+
959+
return false;
963960
}
964961

965962
/**

includes/class-wc-payments-account.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2726,7 +2726,7 @@ private function get_store_setup_details(): array {
27262726
'debug_log_enabled' => 'yes' === $gateway->get_option( 'enable_logging' ),
27272727

27282728
'payment_request' => [
2729-
'enabled' => 'yes' === $gateway->get_option( 'payment_request' ),
2729+
'enabled' => $gateway->is_payment_request_enabled(),
27302730
'enabled_locations' => $gateway->get_option( 'payment_request_button_locations' ),
27312731
'button_type' => $gateway->get_option( 'payment_request_button_type' ),
27322732
'button_size' => $gateway->get_option( 'payment_request_button_size' ),

includes/class-wc-payments-apple-pay-registration.php

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class WC_Payments_Apple_Pay_Registration {
4949
private $domain_name;
5050

5151
/**
52-
* Stores Apple Pay domain verification issues.
52+
* Option name for storing Apple Pay domain verification errors.
5353
*
5454
* @var string
5555
*/
56-
private $apple_pay_verify_notice;
56+
const APPLE_PAY_DOMAIN_ERROR_OPTION = 'wcpay_apple_pay_domain_error';
5757

5858
/**
5959
* Initialize class actions.
@@ -63,11 +63,10 @@ class WC_Payments_Apple_Pay_Registration {
6363
* @param WC_Payment_Gateway_WCPay $gateway WooCommerce Payments gateway.
6464
*/
6565
public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payments_Account $account, WC_Payment_Gateway_WCPay $gateway ) {
66-
$this->domain_name = wp_parse_url( get_site_url(), PHP_URL_HOST );
67-
$this->apple_pay_verify_notice = '';
68-
$this->payments_api_client = $payments_api_client;
69-
$this->account = $account;
70-
$this->gateway = $gateway;
66+
$this->domain_name = wp_parse_url( get_site_url(), PHP_URL_HOST );
67+
$this->payments_api_client = $payments_api_client;
68+
$this->account = $account;
69+
$this->gateway = $gateway;
7170
}
7271

7372
/**
@@ -88,30 +87,47 @@ public function init() {
8887
add_action( 'admin_init', [ $this, 'verify_domain_on_domain_name_change' ] );
8988

9089
add_action( 'woocommerce_woocommerce_payments_admin_notices', [ $this, 'display_error_notice' ] );
91-
add_action( 'add_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 );
92-
add_action( 'update_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 );
90+
91+
// Listen to Apple Pay gateway settings changes for domain verification.
92+
add_action( 'add_option_woocommerce_woocommerce_payments_apple_pay_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 );
93+
add_action( 'update_option_woocommerce_woocommerce_payments_apple_pay_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 );
94+
95+
// Also listen to main gateway settings changes, since it's a prerequisite for Apple Pay.
96+
add_action( 'update_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_updated_main_gateway_settings' ], 10, 2 );
9397
}
9498

9599
/**
96-
* Whether the gateway and Express Checkout Buttons (prerequisites for Apple Pay) are enabled.
100+
* Whether Apple Pay is enabled.
101+
*
102+
* Checks both the main gateway and the Apple Pay gateway are enabled.
97103
*
98-
* @return bool Whether Apple Pay required settings are enabled.
104+
* @return bool Whether Apple Pay is enabled.
99105
*/
100106
private function is_enabled() {
101-
return $this->gateway->is_enabled() && 'yes' === $this->gateway->get_option( 'payment_request' );
107+
// Check if the main gateway is enabled.
108+
if ( ! $this->gateway->is_enabled() ) {
109+
return false;
110+
}
111+
112+
// Check if the Apple Pay gateway is enabled.
113+
$apple_pay_gateway = WC_Payments::get_payment_gateway_by_id( 'apple_pay' );
114+
115+
if ( ! $apple_pay_gateway ) {
116+
return false;
117+
}
118+
119+
return $apple_pay_gateway->is_enabled();
102120
}
103121

104122
/**
105-
* Whether the gateway and Express Checkout Buttons were enabled in previous settings.
123+
* Whether Apple Pay was enabled in previous settings.
106124
*
107-
* @param array|null $prev_settings Gateway settings.
125+
* @param array|null $prev_settings Apple Pay gateway settings.
108126
*
109-
* @return bool Whether Apple Pay required settings are enabled.
127+
* @return bool Whether Apple Pay was enabled.
110128
*/
111129
private function was_enabled( $prev_settings ) {
112-
$gateway_enabled = 'yes' === ( $prev_settings['enabled'] ?? 'no' );
113-
$payment_request_enabled = 'yes' === ( $prev_settings['payment_request'] ?? 'no' );
114-
return $gateway_enabled && $payment_request_enabled;
130+
return 'yes' === ( $prev_settings['enabled'] ?? 'no' );
115131
}
116132

117133
/**
@@ -153,6 +169,7 @@ public function register_domain() {
153169
if ( isset( $registration_response['id'] ) && ( isset( $registration_response['apple_pay']['status'] ) && 'active' === $registration_response['apple_pay']['status'] ) ) {
154170
$this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name );
155171
$this->gateway->update_option( 'apple_pay_domain_set', 'yes' );
172+
delete_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION );
156173

157174
Logger::log( __( 'Your domain has been verified with Apple Pay!', 'woocommerce-payments' ) );
158175
Tracker::track_admin(
@@ -170,11 +187,10 @@ public function register_domain() {
170187
} catch ( API_Exception $e ) {
171188
$error = $e->getMessage();
172189
}
173-
// Display error message in notice.
174-
$this->apple_pay_verify_notice = $error;
175190

176191
$this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name );
177192
$this->gateway->update_option( 'apple_pay_domain_set', 'no' );
193+
update_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION, $error );
178194

179195
Logger::log( 'Error registering domain with Apple: ' . $error );
180196
Tracker::track_admin(
@@ -192,7 +208,7 @@ public function register_domain() {
192208
*/
193209
public function verify_domain_if_configured() {
194210
// If Express Checkout Buttons are not enabled,
195-
// do not attempt to register domain.
211+
// do not attempt to register the domain.
196212
if ( ! $this->is_enabled() ) {
197213
return;
198214
}
@@ -218,12 +234,27 @@ public function verify_domain_on_new_settings( $option, $settings ) {
218234
* @param array $settings Settings after update.
219235
*/
220236
public function verify_domain_on_updated_settings( $prev_settings, $settings ) {
221-
// If Gateway or Express Checkout Buttons weren't enabled, then might need to verify now.
237+
// If Apple Pay wasn't enabled, then might need to verify now.
222238
if ( ! $this->was_enabled( $prev_settings ) ) {
223239
$this->verify_domain_if_configured();
224240
}
225241
}
226242

243+
/**
244+
* Conditionally process the Apple Pay domain verification after main gateway settings are updated.
245+
*
246+
* @param array $prev_settings Settings before update.
247+
* @param array $settings Settings after update.
248+
*/
249+
public function verify_domain_on_updated_main_gateway_settings( $prev_settings, $settings ) {
250+
$was_main_gateway_enabled = 'yes' === ( $prev_settings['enabled'] ?? 'no' );
251+
252+
// If main gateway wasn't enabled before, might need to verify now.
253+
if ( ! $was_main_gateway_enabled ) {
254+
$this->verify_domain_if_configured();
255+
}
256+
}
257+
227258
/**
228259
* Display Apple Pay registration errors.
229260
*/
@@ -232,29 +263,36 @@ public function display_error_notice() {
232263
return;
233264
}
234265

235-
$empty_notice = empty( $this->apple_pay_verify_notice );
236266
$domain_set = $this->gateway->get_option( 'apple_pay_domain_set' );
267+
$error_notice = get_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION, '' );
268+
$empty_notice = empty( $error_notice );
269+
237270
// Don't display error notice if verification notice is empty and
238271
// apple_pay_domain_set option equals to '' or 'yes'.
239272
if ( $empty_notice && 'no' !== $domain_set ) {
240273
return;
241274
}
242275

276+
// Clear the error after retrieving it so it only displays once.
277+
if ( ! $empty_notice ) {
278+
delete_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION );
279+
}
280+
243281
/**
244282
* Apple pay is enabled by default and domain verification initializes
245283
* when setting screen is displayed. So if domain verification is not set,
246284
* something went wrong so lets notify user.
247285
*/
248-
$allowed_html = [
286+
$allowed_html = [
249287
'a' => [
250288
'href' => [],
251289
'title' => [],
252290
],
253291
];
254-
$payment_request_button_text = __( 'Express checkouts:', 'woocommerce-payments' );
255-
$verification_failed_without_error = __( 'Apple Pay domain verification failed.', 'woocommerce-payments' );
256-
$verification_failed_with_error = __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-payments' );
257-
$check_log_text = WC_Payments_Utils::esc_interpolated_html(
292+
$verification_failed = $empty_notice
293+
? __( 'Apple Pay domain verification failed.', 'woocommerce-payments' )
294+
: __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-payments' );
295+
$check_log_text = WC_Payments_Utils::esc_interpolated_html(
258296
/* translators: a: Link to the logs page */
259297
__( 'Please check the <a>logs</a> for more details on this issue. Debug log must be enabled under <strong>Advanced settings</strong> to see recorded logs.', 'woocommerce-payments' ),
260298
[
@@ -271,20 +309,14 @@ public function display_error_notice() {
271309

272310
?>
273311
<div class="notice notice-error apple-pay-message">
274-
<?php if ( $empty_notice ) : ?>
275-
<p>
276-
<strong><?php echo esc_html( $payment_request_button_text ); ?></strong>
277-
<?php echo esc_html( $verification_failed_without_error ); ?>
278-
<?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?>
279-
</p>
280-
<?php else : ?>
281-
<p>
282-
<strong><?php echo esc_html( $payment_request_button_text ); ?></strong>
283-
<?php echo esc_html( $verification_failed_with_error ); ?>
284-
<?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?>
285-
</p>
286-
<p><i><?php echo wp_kses( make_clickable( esc_html( $this->apple_pay_verify_notice ) ), $allowed_html ); ?></i></p>
287-
<?php endif; ?>
312+
<p>
313+
<strong><?php esc_html_e( 'Express checkouts:', 'woocommerce-payments' ); ?></strong>
314+
<?php echo esc_html( $verification_failed ); ?>
315+
<?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?>
316+
</p>
317+
<?php if ( ! $empty_notice ) : ?>
318+
<p><i><?php echo wp_kses( make_clickable( esc_html( $error_notice ) ), $allowed_html ); ?></i></p>
319+
<?php endif; ?>
288320
<p><?php echo $check_log_text; /* @codingStandardsIgnoreLine */ ?></p>
289321
</div>
290322
<?php

includes/class-wc-payments-blocks-payment-method.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function is_active() {
5252
* @return string[] A list of script handles.
5353
*/
5454
public function get_payment_method_script_handles() {
55-
5655
if ( ( is_cart() || is_checkout() || is_product() || has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || is_admin() ) ) {
5756
WC_Payments_Utils::enqueue_style(
5857
'wc-blocks-checkout-style',

includes/class-wc-payments-onboarding-service.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,11 +1439,23 @@ public function update_enabled_payment_methods_ids( $gateway, $capabilities = []
14391439
$gateway->update_is_woopay_enabled( false );
14401440
}
14411441

1442-
// Update gateway option with the Apple/Google Pay capability.
1442+
// Update Apple/Google Pay gateway enabled state.
1443+
$google_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\GooglePayDefinition::get_id() );
1444+
$apple_pay_gateway = WC_Payments::get_payment_gateway_by_id( \WCPay\PaymentMethods\Configs\Definitions\ApplePayDefinition::get_id() );
14431445
if ( ! empty( $capabilities['apple_google'] ) || ( ! empty( $capabilities['apple_pay'] ) || ! empty( $capabilities['google_pay'] ) ) ) {
1444-
$gateway->update_option( 'payment_request', 'yes' );
1446+
if ( $apple_pay_gateway ) {
1447+
$apple_pay_gateway->enable();
1448+
}
1449+
if ( $google_pay_gateway ) {
1450+
$google_pay_gateway->enable();
1451+
}
14451452
} else {
1446-
$gateway->update_option( 'payment_request', 'no' );
1453+
if ( $apple_pay_gateway ) {
1454+
$apple_pay_gateway->disable();
1455+
}
1456+
if ( $google_pay_gateway ) {
1457+
$google_pay_gateway->disable();
1458+
}
14471459
}
14481460
}
14491461

includes/class-wc-payments-status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function render_status_report_section() {
255255
<td class="help"><?php echo wc_help_tip( esc_html__( 'Whether the store has Payment Request enabled or not.', 'woocommerce-payments' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.OutputNotEscaped */ ?></td>
256256
<td>
257257
<?php
258-
$payment_request_enabled = 'yes' === $this->gateway->get_option( 'payment_request' );
258+
$payment_request_enabled = $this->gateway->is_payment_request_enabled();
259259
$payment_request_enabled_locations = $this->gateway->get_option( 'payment_request_button_locations', [] );
260260
$payment_request_enabled_locations = empty( $payment_request_enabled_locations ) ? 'no locations enabled' : implode( ',', $payment_request_enabled_locations );
261261
echo esc_html( $payment_request_enabled ? __( 'Enabled', 'woocommerce-payments' ) . ' (' . $payment_request_enabled_locations . ')' : __( 'Disabled', 'woocommerce-payments' ) );

includes/class-wc-payments.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ function () {
702702
require_once __DIR__ . '/migrations/class-erase-bnpl-announcement-meta.php';
703703
require_once __DIR__ . '/migrations/class-erase-deprecated-flags-and-options.php';
704704
require_once __DIR__ . '/migrations/class-manual-capture-payment-method-settings-update.php';
705+
require_once __DIR__ . '/migrations/class-migrate-payment-request-to-express-checkout-enabled.php';
705706
add_action( 'woocommerce_woocommerce_payments_updated', [ new Allowed_Payment_Request_Button_Types_Update( self::get_gateway() ), 'maybe_migrate' ] );
706707
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Allowed_Payment_Request_Button_Sizes_Update( self::get_gateway() ), 'maybe_migrate' ] );
707708
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Update_Service_Data_From_Server( self::get_account_service() ), 'maybe_migrate' ] );
@@ -714,6 +715,7 @@ function () {
714715
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Erase_Bnpl_Announcement_Meta(), 'maybe_migrate' ] );
715716
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Erase_Deprecated_Flags_And_Options(), 'maybe_migrate' ] );
716717
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Manual_Capture_Payment_Method_Settings_Update( self::get_gateway(), self::get_payment_gateway_map() ), 'maybe_migrate' ] );
718+
add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Migrate_Payment_Request_To_Express_Checkout_Enabled(), 'maybe_migrate' ] );
717719

718720
include_once WCPAY_ABSPATH . '/includes/class-wc-payments-explicit-price-formatter.php';
719721
WC_Payments_Explicit_Price_Formatter::init();

0 commit comments

Comments
 (0)