Skip to content
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

fix: remove reCAPTCHA for Woo from modal checkout #1984

Merged
merged 11 commits into from
Dec 4, 2024

Conversation

laurelfulford
Copy link
Contributor

@laurelfulford laurelfulford commented Dec 3, 2024

All Submissions:

Changes proposed in this Pull Request:

This PR removes and dequeues code from the reCAPTCHA for WooCommerce plugin from our modal checkout.

See 1207817176293825-as-1208873550136264

Note: the way I went about removing the scripts might be terrible; it seemed kind of duplicative, but I couldn't think of a better way that didn't require spelling out each allowed script.

How to test the changes in this Pull Request:

  1. Install reCAPTCHA for WooCommerce and enable it on your site.
  2. Try running through a modal checkout; note you get an error.
  3. Apply this PR.
  4. Confirm that you can complete the modal checkout.
  5. View source in the modal checkout and confirm you don't have anything referencing recaptcha from the plugin (a g-recaptcha div, plus the script, rcfwc.js).
  6. Confirm that those elements still exist in the regular checkout and that the reCAPTCHA there doesn't throw errors.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

includes/class-modal-checkout.php Outdated Show resolved Hide resolved
includes/class-modal-checkout.php Outdated Show resolved Hide resolved
/**
* Remove hooks related to reCAPTCHA for WooCommerce
*/
public static function remove_recaptcha_hooks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: How common is this extension? It feels strange to have to account for specific 3rd party plugin hooks like this within our own plugin. It also doesn't seem very sustainable for other cases that might be impacting checkout down the line.

Is it possible to do this more dynamically using an approach similar to how we handle allowed scripts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How common is this extension?

It's active on 28 sites now (and installed but inactive on others) -- definitely more than I would have expected!

Is it possible to do this more dynamically using an approach similar to how we handle allowed scripts?

Like pass a filterable array of hooks to remove? I can dig into it!

@laurelfulford
Copy link
Contributor Author

laurelfulford commented Dec 4, 2024

Ok I think I have everything sorted, @chickenn00dle!

As an extra precaution, I did some error logging for the scripts/styles getting removed before and after this change, and re-added a couple -- one (the woocommerce styles one) was causing issues. I didn't see any issues caused by selectWoo, but better safe than sorry! (It's Woo's accessible fork of select2, which was also being enqueued [I think] as a fallback).

For the remove_action part, I added an array with the hook and callback that can be filtered using newspack_blocks_modal_checkout_remove_hooks.

Just let me know if anything needs changing -- thanks!

Copy link
Contributor

@chickenn00dle chickenn00dle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I tested with the changes but am still seeing the recaptcha error on my end:
Screenshot 2024-12-04 at 10 58 59

Not sure if its the same extension but I am testing with this one: https://woocommerce.com/products/recaptcha-for-woocommerce/

I also left two small comments that are non-blocking (sort of). Let me know what you think!

],
[
'hook' => 'woocommerce_review_order_before_submit',
'callback' => 'rcfwc_field_checkout',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another (non-blocking question) I had a look through the code and am seeing we have this array of fields to filter out during validation in the modal checkout js:

const removeFromValidation = [
'save_user_in_woopay',
];

For this hook, which I'm assuming is adding a checkout field, is this something that can be removed using the same strategy?

We'd still need to remove hooks associated with any backend logic of course, but if we can stick to the same strategy for bypassing field validation that'd be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this hook, which I'm assuming is adding a checkout field, is this something that can be removed using the same strategy?

I'm not sure of the ins and outs of how this works, but it's adding a div, like:

<div class="g-recaptcha" data-sitekey="6LfrhJEqAAAAAPuv8JoMdAqEM6G-TKQxc2Es_37A"></div>

I double-checked, and it doesn't look like anything related to reCAPTCHA is included in $form.serializeArray(), which is what we're checking removeFromValidation for.

*
* @param string[] $remove_list Array of hooks to remove.
*/
$remove_list = apply_filters( 'newspack_blocks_modal_checkout_remove_hooks', $remove_list );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another non-blocking comment. This is definitely better. But in the end we are still needing to specify the rcfwc_ callbacks here. This is fine if this is the only one, but I can't imagine we don't run into similar issues in the future with other extensions.

I haven't dug too deep into it, but it's possible to view all hooks associated with a specific action via the $wp_filter global. Is it possible to do something like create an allow list of plugins, and check the added filters for the checkout processing hook(s), then remove if not in the allow list?

I'm happy to go with this quick fix specific to recaptcha for woocommerce, but really feel this should be improved so we don't have to keep accounting for one-off plugins like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now (and in the interest of getting this fixed and rolled out during alphas), my vote would be to keep it simple and revisit when we need to add more plugins 😅 I know something more abstracted would be ideal but I'm worried about cutting this too close to the wire.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to spin up a separate issue to capture this and see what I can do with it! 🙂

@laurelfulford
Copy link
Contributor Author

I've updated the code to actually work with the correct ReCaptcha for WooCommerce plugin 😆

Anything related to the plugin should be dequeued/removed in the modal checkout, but still be present in the regular checkout. The plugin uses the prefix i13 for scripts and elements it inserts.

Copy link
Contributor

@chickenn00dle chickenn00dle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks @laurelfulford

$remove_list = [];

// reCaptcha for WooCommerce.
if ( method_exists( 'I13_Woo_Recpatcha', '__construct' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit: we can just do a check for the class with class_exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Raz! Fixed in ad577d6.

@laurelfulford laurelfulford merged commit 8e250eb into trunk Dec 4, 2024
8 checks passed
@laurelfulford laurelfulford deleted the fix/remove-recaptcha-for-woo-from-modal branch December 4, 2024 20:04
Copy link

github-actions bot commented Dec 4, 2024

Hey @laurelfulford, good job getting this PR merged! 🎉

Now, the needs-changelog label has been added to it.

Please check if this PR needs to be included in the "Upcoming Changes" and "Release Notes" doc. If it doesn't, simply remove the label.

If it does, please add an entry to our shared document, with screenshots and testing instructions if applicable, then remove the label.

Thank you! ❤️

matticbot pushed a commit that referenced this pull request Dec 4, 2024
# [4.5.0-alpha.2](v4.5.0-alpha.1...v4.5.0-alpha.2) (2024-12-04)

### Bug Fixes

* remove reCaptcha for WooCommere code from modal checkout ([#1984](#1984)) ([8e250eb](8e250eb))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 4.5.0-alpha.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Dec 9, 2024
# [4.5.0](v4.4.0...v4.5.0) (2024-12-09)

### Bug Fixes

* also search for coauthor posts by term slug ([#1954](#1954)) ([49357ff](49357ff))
* **modal-checkout:** allow all gateway assets ([#1988](#1988)) ([e371e30](e371e30))
* **modal-checkout:** handle paypal ([#1985](#1985)) ([9bb2b8c](9bb2b8c))
* **ras-acc:** correct spacing issue around saved credit cards ([#1980](#1980)) ([52a5c57](52a5c57))
* **ras-acc:** fix display issues with Additional Fields ([#1979](#1979)) ([b9390ef](b9390ef))
* **ras-acc:** remove space caused by empty divs ([#1978](#1978)) ([8cb6ead](8cb6ead))
* remove reCaptcha for WooCommere code from modal checkout ([#1984](#1984)) ([8e250eb](8e250eb))

### Features

* add Bluesky support to the Author Profile, List blocks ([#1969](#1969)) ([d26a7e4](d26a7e4))
* add CSS class to variation buttons for tracking ([#1989](#1989)) ([910e6b1](910e6b1))
* add support for Newspack Guest Contributor in HPP blocks ([#1934](#1934)) ([c16849e](c16849e))
* merge RAS-ACC work into trunk ([#1977](#1977)) ([2eeaa89](2eeaa89))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants