Skip to content

Commit 022fac2

Browse files
authored
Add Compatibility With Woocommerce 6.6 (#2362)
* Fix minor comment typo: 'Processses' to 'Processes' * Add 'short_statement_descriptor' form field setting * Update 'title' & 'title_upe' fields' type * Make form field settings compatible with WC v6.6 * Update changelog: 'Fix - Add compatibility with Woocommerce 6.6.' * Fix typo: 'Woocommerce' to 'WooCommerce' in changelog * Move $form_fields from 'update_settings()' to 'validate_field()' Improves code readability but trades-off a bit of performance. * Ensure to only use 'safe_text' for WC version 6.6 and greater * Retroactively apply validation on plugin settings Before, validation would be applied only on settings that would be saved after this commit has been merged. Now, validation will be applied on settings regardless of whether they're saved, after this commit has been merged.
1 parent a04ddb5 commit 022fac2

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
= 6.5.0 - 2022-xx-xx =
44
* Fix - Fix terminal location creation if site title is missing.
5+
* Fix - Add compatibility with WooCommerce 6.6.
56

67
= 6.4.1 - 2022-06-01 =
78
* Fix - Ensure proper URL formatting.

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,28 +240,28 @@ public function get_settings() {
240240
/* Settings > General */
241241
'is_stripe_enabled' => $this->gateway->is_enabled(),
242242
'is_test_mode_enabled' => $this->gateway->is_in_test_mode(),
243-
'title' => $this->gateway->get_option( 'title' ),
244-
'title_upe' => $this->gateway->get_option( 'title_upe' ),
245-
'description' => $this->gateway->get_option( 'description' ),
243+
'title' => $this->gateway->get_validated_option( 'title' ),
244+
'title_upe' => $this->gateway->get_validated_option( 'title_upe' ),
245+
'description' => $this->gateway->get_validated_option( 'description' ),
246246

247247
/* Settings > Payments accepted on checkout */
248248
'enabled_payment_method_ids' => $this->gateway->get_upe_enabled_payment_method_ids(),
249249
'available_payment_method_ids' => $this->gateway->get_upe_available_payment_methods(),
250250

251251
/* Settings > Express checkouts */
252252
'is_payment_request_enabled' => 'yes' === $this->gateway->get_option( 'payment_request' ),
253-
'payment_request_button_type' => $this->gateway->get_option( 'payment_request_button_type' ),
254-
'payment_request_button_theme' => $this->gateway->get_option( 'payment_request_button_theme' ),
255-
'payment_request_button_size' => $this->gateway->get_option( 'payment_request_button_size' ),
256-
'payment_request_button_locations' => $this->gateway->get_option( 'payment_request_button_locations' ),
253+
'payment_request_button_type' => $this->gateway->get_validated_option( 'payment_request_button_type' ),
254+
'payment_request_button_theme' => $this->gateway->get_validated_option( 'payment_request_button_theme' ),
255+
'payment_request_button_size' => $this->gateway->get_validated_option( 'payment_request_button_size' ),
256+
'payment_request_button_locations' => $this->gateway->get_validated_option( 'payment_request_button_locations' ),
257257

258258
/* Settings > Payments & transactions */
259259
'is_manual_capture_enabled' => ! $this->gateway->is_automatic_capture_enabled(),
260260
'is_saved_cards_enabled' => 'yes' === $this->gateway->get_option( 'saved_cards' ),
261261
'is_separate_card_form_enabled' => 'no' === $this->gateway->get_option( 'inline_cc_form' ),
262-
'statement_descriptor' => $this->gateway->get_option( 'statement_descriptor' ),
262+
'statement_descriptor' => $this->gateway->get_validated_option( 'statement_descriptor' ),
263263
'is_short_statement_descriptor_enabled' => 'yes' === $this->gateway->get_option( 'is_short_statement_descriptor_enabled' ),
264-
'short_statement_descriptor' => $this->gateway->get_option( 'short_statement_descriptor' ),
264+
'short_statement_descriptor' => $this->gateway->get_validated_option( 'short_statement_descriptor' ),
265265

266266
/* Settings > Advanced settings */
267267
'is_debug_log_enabled' => 'yes' === $this->gateway->get_option( 'logging' ),
@@ -336,7 +336,7 @@ private function update_title( WP_REST_Request $request ) {
336336
return;
337337
}
338338

339-
$this->gateway->update_option( 'title', $title );
339+
$this->gateway->update_validated_option( 'title', $title );
340340
}
341341

342342
/**
@@ -351,7 +351,7 @@ private function update_title_upe( WP_REST_Request $request ) {
351351
return;
352352
}
353353

354-
$this->gateway->update_option( 'title_upe', $title_upe );
354+
$this->gateway->update_validated_option( 'title_upe', $title_upe );
355355
}
356356

357357
/**
@@ -366,7 +366,7 @@ private function update_description( WP_REST_Request $request ) {
366366
return;
367367
}
368368

369-
$this->gateway->update_option( 'description', $description );
369+
$this->gateway->update_validated_option( 'description', $description );
370370
}
371371

372372
/**
@@ -456,7 +456,7 @@ private function update_account_statement_descriptor( WP_REST_Request $request )
456456
return;
457457
}
458458

459-
$this->gateway->update_option( 'statement_descriptor', $account_statement_descriptor );
459+
$this->gateway->update_validated_option( 'statement_descriptor', $account_statement_descriptor );
460460
}
461461

462462
/**
@@ -492,7 +492,7 @@ private function update_short_account_statement_descriptor( WP_REST_Request $req
492492
return;
493493
}
494494

495-
$this->gateway->update_option( 'short_statement_descriptor', $short_account_statement_descriptor );
495+
$this->gateway->update_validated_option( 'short_statement_descriptor', $short_account_statement_descriptor );
496496
}
497497

498498
/**
@@ -553,7 +553,7 @@ private function update_payment_request_settings( WP_REST_Request $request ) {
553553
}
554554

555555
$value = $request->get_param( $request_key );
556-
$this->gateway->update_option( $attribute, $value );
556+
$this->gateway->update_validated_option( $attribute, $value );
557557
}
558558
}
559559

includes/admin/stripe-settings.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
exit;
44
}
55

6+
$is_gte_wc6_6 = defined( WC_VERSION ) && version_compare( WC_VERSION, '6.6', '>=' );
7+
68
$stripe_settings = apply_filters(
79
'wc_stripe_settings',
810
[
@@ -15,14 +17,14 @@
1517
],
1618
'title' => [
1719
'title' => __( 'Title', 'woocommerce-gateway-stripe' ),
18-
'type' => 'text',
20+
'type' => $is_gte_wc6_6 ? 'safe_text' : 'text',
1921
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-gateway-stripe' ),
2022
'default' => __( 'Credit Card (Stripe)', 'woocommerce-gateway-stripe' ),
2123
'desc_tip' => true,
2224
],
2325
'title_upe' => [
2426
'title' => __( 'Title', 'woocommerce-gateway-stripe' ),
25-
'type' => 'text',
27+
'type' => $is_gte_wc6_6 ? 'safe_text' : 'text',
2628
'description' => __( 'This controls the title which the user sees during checkout when multiple payment methods are enabled.', 'woocommerce-gateway-stripe' ),
2729
'default' => __( 'Popular payment methods', 'woocommerce-gateway-stripe' ),
2830
'desc_tip' => true,
@@ -107,6 +109,13 @@
107109
'default' => '',
108110
'desc_tip' => true,
109111
],
112+
'short_statement_descriptor' => [
113+
'title' => __( 'Short Statement Descriptor', 'woocommerce-gateway-stripe' ),
114+
'type' => 'text',
115+
'description' => __( 'Shortened version of the statement descriptor in combination with the customer order number.', 'woocommerce-gateway-stripe' ),
116+
'default' => '',
117+
'desc_tip' => true,
118+
],
110119
'capture' => [
111120
'title' => __( 'Capture', 'woocommerce-gateway-stripe' ),
112121
'label' => __( 'Capture charge immediately', 'woocommerce-gateway-stripe' ),

includes/class-wc-gateway-stripe.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ public function __construct() {
9797
$this->maybe_init_pre_orders();
9898

9999
// Get setting values.
100-
$this->title = $this->get_option( 'title' );
101-
$this->description = $this->get_option( 'description' );
100+
$this->title = $this->get_validated_option( 'title' );
101+
$this->description = $this->get_validated_option( 'description' );
102102
$this->enabled = $this->get_option( 'enabled' );
103103
$this->testmode = 'yes' === $this->get_option( 'testmode' );
104104
$this->inline_cc_form = 'yes' === $this->get_option( 'inline_cc_form' );
105105
$this->capture = 'yes' === $this->get_option( 'capture', 'yes' );
106-
$this->statement_descriptor = WC_Stripe_Helper::clean_statement_descriptor( $this->get_option( 'statement_descriptor' ) );
106+
$this->statement_descriptor = WC_Stripe_Helper::clean_statement_descriptor( $this->get_validated_option( 'statement_descriptor' ) );
107107
$this->saved_cards = 'yes' === $this->get_option( 'saved_cards' );
108-
$this->secret_key = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' );
109-
$this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' );
108+
$this->secret_key = $this->testmode ? $this->get_validated_option( 'test_secret_key' ) : $this->get_validated_option( 'secret_key' );
109+
$this->publishable_key = $this->testmode ? $this->get_validated_option( 'test_publishable_key' ) : $this->get_validated_option( 'publishable_key' );
110110
$this->payment_request = 'yes' === $this->get_option( 'payment_request', 'yes' );
111111

112112
WC_Stripe_API::set_secret_key( $this->secret_key );
@@ -1247,4 +1247,57 @@ public function update_onboarding_settings( $settings ) {
12471247

12481248
return $settings;
12491249
}
1250+
1251+
/**
1252+
* Validates a field value before updating.
1253+
*
1254+
* @param string $field_key the form field key.
1255+
* @param string $field_value the form field value.
1256+
*
1257+
* @return bool True if the value was updated, false otherwise.
1258+
*/
1259+
public function update_validated_option( $field_key, $field_value ) {
1260+
$validated_field_value = $this->validate_field( $field_key, $field_value );
1261+
return $this->update_option( $field_key, $validated_field_value );
1262+
}
1263+
1264+
/**
1265+
* Retrieves validated field value.
1266+
*
1267+
* @param string $field_key the form field key.
1268+
* @param mixed $empty_value fallback value.
1269+
*
1270+
* @return string validated field value.
1271+
*/
1272+
public function get_validated_option( $field_key, $empty_value = null ) {
1273+
$value = parent::get_option( $field_key, $empty_value );
1274+
return $this->validate_field( $field_key, $value );
1275+
}
1276+
1277+
/**
1278+
* Ensures validated field values.
1279+
*
1280+
* @param string $field_key the form field key.
1281+
* @param string $field_value the form field value.
1282+
*
1283+
* @return string validated field value.
1284+
*/
1285+
private function validate_field( $field_key, $field_value ) {
1286+
if ( is_callable( [ $this, 'validate_' . $field_key . '_field' ] ) ) {
1287+
return $this->{'validate_' . $field_key . '_field'}( $field_key, $field_value );
1288+
}
1289+
1290+
if ( empty( $this->form_fields ) ) {
1291+
$this->init_form_fields();
1292+
}
1293+
if ( key_exists( $field_key, $this->form_fields ) ) {
1294+
$field_type = $this->form_fields[ $field_key ]['type'];
1295+
1296+
if ( is_callable( [ $this, 'validate_' . $field_type . '_field' ] ) ) {
1297+
return $this->{'validate_' . $field_type . '_field'}( $field_key, $field_value );
1298+
}
1299+
}
1300+
1301+
return $this->validate_text_field( $field_key, $field_value );
1302+
}
12501303
}

includes/class-wc-stripe-order-handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er
191191
}
192192

193193
/**
194-
* Processses the orders that are redirected.
194+
* Processes the orders that are redirected.
195195
*
196196
* @since 4.0.0
197197
* @version 4.0.0

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
130130

131131
= 6.5.0 - 2022-xx-xx =
132132
* Fix - Fix terminal location creation if site title is missing.
133+
* Fix - Add compatibility with WooCommerce 6.6.
133134

134135
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

0 commit comments

Comments
 (0)