From 2aaa84ade9633864be5e2a65540ff80d191cd875 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 5 Dec 2023 13:15:03 -0300 Subject: [PATCH 1/9] fix(recaptcha): skip captcha on "update totals" request --- includes/class-recaptcha.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/class-recaptcha.php b/includes/class-recaptcha.php index 8f3063b42d..a5a1090176 100644 --- a/includes/class-recaptcha.php +++ b/includes/class-recaptcha.php @@ -366,6 +366,10 @@ public static function verify_recaptcha_on_checkout() { if ( ! $should_verify_captcha ) { return; } + // Bail if validating form. + if ( isset( $_POST['woocommerce_checkout_update_totals'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing + return; + } $token = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing $check = self::verify_captcha( $token ); if ( \is_wp_error( $check ) ) { From 29ca89219c1fdeec12aef57142e6c40c63e9b8a0 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Thu, 23 Nov 2023 13:50:10 -0300 Subject: [PATCH 2/9] feat(donation): custom order button text --- includes/class-donations.php | 39 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/includes/class-donations.php b/includes/class-donations.php index 70d6ea0752..f3a4cb6488 100644 --- a/includes/class-donations.php +++ b/includes/class-donations.php @@ -65,17 +65,18 @@ class Donations { */ public static function init() { self::$donation_product_name = __( 'Donate', 'newspack' ); - if ( ! is_admin() ) { - add_action( 'wp_loaded', [ __CLASS__, 'process_donation_form' ], 99 ); - add_action( 'woocommerce_checkout_update_order_meta', [ __CLASS__, 'woocommerce_checkout_update_order_meta' ] ); - add_filter( 'woocommerce_billing_fields', [ __CLASS__, 'woocommerce_billing_fields' ] ); - add_filter( 'pre_option_woocommerce_enable_guest_checkout', [ __CLASS__, 'disable_guest_checkout' ] ); - add_action( 'woocommerce_check_cart_items', [ __CLASS__, 'handle_cart' ] ); - add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 ); - add_filter( 'newspack_blocks_donate_billing_fields_keys', [ __CLASS__, 'get_billing_fields' ] ); - add_action( 'woocommerce_checkout_create_order_line_item', [ __CLASS__, 'checkout_create_order_line_item' ], 10, 4 ); - add_action( 'woocommerce_coupons_enabled', [ __CLASS__, 'disable_coupons' ] ); - } + + add_action( 'wp_loaded', [ __CLASS__, 'process_donation_form' ], 99 ); + add_action( 'woocommerce_checkout_update_order_meta', [ __CLASS__, 'woocommerce_checkout_update_order_meta' ] ); + add_filter( 'woocommerce_billing_fields', [ __CLASS__, 'woocommerce_billing_fields' ] ); + add_filter( 'pre_option_woocommerce_enable_guest_checkout', [ __CLASS__, 'disable_guest_checkout' ] ); + add_action( 'woocommerce_check_cart_items', [ __CLASS__, 'handle_cart' ] ); + add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 ); + add_filter( 'newspack_blocks_donate_billing_fields_keys', [ __CLASS__, 'get_billing_fields' ] ); + add_action( 'woocommerce_checkout_create_order_line_item', [ __CLASS__, 'checkout_create_order_line_item' ], 10, 4 ); + add_filter( 'wcs_place_subscription_order_text', [ __CLASS__, 'order_button_text' ], 9 ); + add_filter( 'woocommerce_order_button_text', [ __CLASS__, 'order_button_text' ], 9 ); + add_filter( 'option_woocommerce_subscriptions_order_button_text', [ __CLASS__, 'order_button_text' ], 9 ); } /** @@ -614,6 +615,10 @@ public static function is_platform_other() { * Handle submission of the donation form. */ public static function process_donation_form() { + if ( is_admin() ) { + return; + } + $is_wc = self::is_platform_wc(); $donation_form_submitted = filter_input( INPUT_GET, 'newspack_donate', FILTER_SANITIZE_NUMBER_INT ); @@ -1055,5 +1060,17 @@ public static function disable_coupons( $enabled ) { } return false; } + + /** + * Set the "Place order" button text. + * + * @param string $text The button text. + */ + public static function order_button_text( $text ) { + if ( self::is_donation_cart() ) { + return __( 'Donate now', 'newspack-blocks' ); + } + return $text; + } } Donations::init(); From 9485c008fd37ea504bc3730293de9d4219fc9769 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 12 Dec 2023 11:37:07 -0300 Subject: [PATCH 3/9] fix: restore missing line --- includes/class-donations.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-donations.php b/includes/class-donations.php index f3a4cb6488..cc50a0ef60 100644 --- a/includes/class-donations.php +++ b/includes/class-donations.php @@ -74,6 +74,7 @@ public static function init() { add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 ); add_filter( 'newspack_blocks_donate_billing_fields_keys', [ __CLASS__, 'get_billing_fields' ] ); add_action( 'woocommerce_checkout_create_order_line_item', [ __CLASS__, 'checkout_create_order_line_item' ], 10, 4 ); + add_filter( 'woocommerce_coupons_enabled', [ __CLASS__, 'disable_coupons' ] ); add_filter( 'wcs_place_subscription_order_text', [ __CLASS__, 'order_button_text' ], 9 ); add_filter( 'woocommerce_order_button_text', [ __CLASS__, 'order_button_text' ], 9 ); add_filter( 'option_woocommerce_subscriptions_order_button_text', [ __CLASS__, 'order_button_text' ], 9 ); From 2d64b1558e617ac79a4091f1390eb22060b83404 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 19 Dec 2023 09:30:36 -0300 Subject: [PATCH 4/9] Update includes/class-donations.php Co-authored-by: Derrick Koo --- includes/class-donations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-donations.php b/includes/class-donations.php index cc50a0ef60..db1ea20baf 100644 --- a/includes/class-donations.php +++ b/includes/class-donations.php @@ -1069,7 +1069,7 @@ public static function disable_coupons( $enabled ) { */ public static function order_button_text( $text ) { if ( self::is_donation_cart() ) { - return __( 'Donate now', 'newspack-blocks' ); + return __( 'Donate now', 'newspack-plugin' ); } return $text; } From 6ff7dd04fba6ab130fba0bdf443bd03bf31e4219 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 19 Dec 2023 10:26:42 -0300 Subject: [PATCH 5/9] fix: support multiple price elements in the checkout ui --- .../class-woocommerce-cover-fees.php | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php index 1743794c9d..55da3fa5c6 100644 --- a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php +++ b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php @@ -13,9 +13,9 @@ * WooCommerce Order UTM class. */ class WooCommerce_Cover_Fees { - const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees'; - const PRICE_ELEMENT_ID = 'newspack-wc-price'; - const WC_ORDER_META_NAME = 'newspack_donor_covers_fees'; + const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees'; + const PRICE_ELEMENT_CLASS_NAME = 'newspack-wc-price'; + const WC_ORDER_META_NAME = 'newspack_donor_covers_fees'; /** * Initialize hooks. @@ -176,7 +176,7 @@ public static function amend_price_markup( $html, $price ) { if ( ! self::should_allow_covering_fees() ) { return $html; } - return str_replace( $price, '' . $price . '', $html ); + return str_replace( $price, '' . $price . '', $html ); } /** @@ -199,7 +199,10 @@ public static function print_checkout_helper_script() { // is not supported with coupons. $coupons_handling_script = 'setInterval(function(){ if(document.querySelector(".woocommerce-remove-coupon")){ - document.getElementById( "' . self::PRICE_ELEMENT_ID . '" ).textContent = "' . $original_price . '"; + var prices = document.querySelectorAll(".' . self::PRICE_ELEMENT_CLASS_NAME . '"); + for (var i = 0; i < prices.length; i++) { + prices[i].textContent = "' . $original_price . '"; + } } }, 1000);'; } @@ -208,15 +211,18 @@ public static function print_checkout_helper_script() { 'const form = document.querySelector(\'form[name="checkout"]\'); if ( form ) { form.addEventListener(\'change\', function( e ){ - const inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); - if( e.target.name === "payment_method" && e.target.value !== "stripe" && inputEl.checked ){ + var inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); + if( inputEl && e.target.name === "payment_method" && e.target.value !== "stripe" && inputEl.checked ){ inputEl.checked = false; newspackHandleCoverFees(inputEl); } }); } function newspackHandleCoverFees(inputEl) { - document.getElementById( "' . self::PRICE_ELEMENT_ID . '" ).textContent = inputEl.checked ? "' . $price_with_fee . '" : "' . $original_price . '"; + var prices = document.querySelectorAll(".' . self::PRICE_ELEMENT_CLASS_NAME . '"); + for (var i = 0; i < prices.length; i++) { + prices[i].textContent = inputEl.checked ? "' . $price_with_fee . '" : "' . $original_price . '"; + } };' . $coupons_handling_script ); } From d2b2f3bd2350ed04edf6a50e604c45cc4f5d26aa Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 19 Dec 2023 10:33:27 -0300 Subject: [PATCH 6/9] fix: use 'var' instead of 'const' --- .../reader-revenue/woocommerce/class-woocommerce-cover-fees.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php index 55da3fa5c6..3009f9267d 100644 --- a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php +++ b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php @@ -208,7 +208,7 @@ public static function print_checkout_helper_script() { } wp_add_inline_script( $handler, - 'const form = document.querySelector(\'form[name="checkout"]\'); + 'var form = document.querySelector(\'form[name="checkout"]\'); if ( form ) { form.addEventListener(\'change\', function( e ){ var inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); From 12e7c3913d7daa4f10d6957c0b6ee908599ebe51 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 19 Dec 2023 11:24:30 -0300 Subject: [PATCH 7/9] Revert "fix: use 'var' instead of 'const'" This reverts commit d2b2f3bd2350ed04edf6a50e604c45cc4f5d26aa. --- .../reader-revenue/woocommerce/class-woocommerce-cover-fees.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php index 3009f9267d..55da3fa5c6 100644 --- a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php +++ b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php @@ -208,7 +208,7 @@ public static function print_checkout_helper_script() { } wp_add_inline_script( $handler, - 'var form = document.querySelector(\'form[name="checkout"]\'); + 'const form = document.querySelector(\'form[name="checkout"]\'); if ( form ) { form.addEventListener(\'change\', function( e ){ var inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); From 1dcfd82d04755c5f781bc79195dfcd095f526ac9 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 19 Dec 2023 11:24:34 -0300 Subject: [PATCH 8/9] Revert "fix: support multiple price elements in the checkout ui" This reverts commit 6ff7dd04fba6ab130fba0bdf443bd03bf31e4219. --- .../class-woocommerce-cover-fees.php | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php index 55da3fa5c6..1743794c9d 100644 --- a/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php +++ b/includes/reader-revenue/woocommerce/class-woocommerce-cover-fees.php @@ -13,9 +13,9 @@ * WooCommerce Order UTM class. */ class WooCommerce_Cover_Fees { - const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees'; - const PRICE_ELEMENT_CLASS_NAME = 'newspack-wc-price'; - const WC_ORDER_META_NAME = 'newspack_donor_covers_fees'; + const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees'; + const PRICE_ELEMENT_ID = 'newspack-wc-price'; + const WC_ORDER_META_NAME = 'newspack_donor_covers_fees'; /** * Initialize hooks. @@ -176,7 +176,7 @@ public static function amend_price_markup( $html, $price ) { if ( ! self::should_allow_covering_fees() ) { return $html; } - return str_replace( $price, '' . $price . '', $html ); + return str_replace( $price, '' . $price . '', $html ); } /** @@ -199,10 +199,7 @@ public static function print_checkout_helper_script() { // is not supported with coupons. $coupons_handling_script = 'setInterval(function(){ if(document.querySelector(".woocommerce-remove-coupon")){ - var prices = document.querySelectorAll(".' . self::PRICE_ELEMENT_CLASS_NAME . '"); - for (var i = 0; i < prices.length; i++) { - prices[i].textContent = "' . $original_price . '"; - } + document.getElementById( "' . self::PRICE_ELEMENT_ID . '" ).textContent = "' . $original_price . '"; } }, 1000);'; } @@ -211,18 +208,15 @@ public static function print_checkout_helper_script() { 'const form = document.querySelector(\'form[name="checkout"]\'); if ( form ) { form.addEventListener(\'change\', function( e ){ - var inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); - if( inputEl && e.target.name === "payment_method" && e.target.value !== "stripe" && inputEl.checked ){ + const inputEl = document.getElementById( "' . self::CUSTOM_FIELD_NAME . '" ); + if( e.target.name === "payment_method" && e.target.value !== "stripe" && inputEl.checked ){ inputEl.checked = false; newspackHandleCoverFees(inputEl); } }); } function newspackHandleCoverFees(inputEl) { - var prices = document.querySelectorAll(".' . self::PRICE_ELEMENT_CLASS_NAME . '"); - for (var i = 0; i < prices.length; i++) { - prices[i].textContent = inputEl.checked ? "' . $price_with_fee . '" : "' . $original_price . '"; - } + document.getElementById( "' . self::PRICE_ELEMENT_ID . '" ).textContent = inputEl.checked ? "' . $price_with_fee . '" : "' . $original_price . '"; };' . $coupons_handling_script ); } From b0c4828c2869456a01d0a9f37468573a2c778ceb Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Wed, 20 Dec 2023 14:22:09 -0300 Subject: [PATCH 9/9] fix: prevent warning on coupon filter --- includes/class-donations.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/class-donations.php b/includes/class-donations.php index db1ea20baf..25a18e4fff 100644 --- a/includes/class-donations.php +++ b/includes/class-donations.php @@ -1052,6 +1052,9 @@ public static function update_billing_fields( $billing_fields ) { * @return bool */ public static function disable_coupons( $enabled ) { + if ( is_admin() ) { + return $enabled; + } $cart = WC()->cart; if ( ! $cart ) { return $enabled;