Skip to content

fix(recaptcha): skip script registration on TEC Community Events pages#4666

Open
wil-gerken wants to merge 4 commits intotrunkfrom
fix/prevent-two-captchas-tec-community-events
Open

fix(recaptcha): skip script registration on TEC Community Events pages#4666
wil-gerken wants to merge 4 commits intotrunkfrom
fix/prevent-two-captchas-tec-community-events

Conversation

@wil-gerken
Copy link
Copy Markdown
Contributor

@wil-gerken wil-gerken commented Apr 15, 2026

All Submissions:

Changes proposed in this Pull Request:

Newspack's reCAPTCHA loads Google's api.js on every front-end page. When The Events Calendar Community Events is active and the publisher has configured TEC's own reCAPTCHA key, the TEC Community Events submission page also loads api.js with a different site key. Two api.js loading on the same page with different keys break reCAPTCHA resulting in errors when trying to submit a community event.

Newspack's reCAPTCHA client only protects form[data-newspack-recaptcha], form#add_payment_method, and form.checkout, so it was never protecting TEC forms to begin with. This change adds an early return in Recaptcha::register_scripts() when we're on a TEC Community Events submission page, preventing the conflict while leaving TEC's own reCAPTCHA spam protection intact.

Closes NPPM-2743.

How to test the changes in this Pull Request:

Setup:

  1. Activate The Events Calendar and The Events Calendar Community Events.
  2. In Newspack wizard → Connections → reCAPTCHA, enable reCAPTCHA (v2 or v3)
  3. In WP Admin → Events → Settings → Addons, save a different reCAPTCHA site key (v2 or v3)
  4. Log out or open an incognito window as reCAPTCHA only renders for logged-out users on TEC forms.

Reproduce the problem (on trunk, before this PR):

  1. Visit /events/community/add/.
  2. Open DevTools → Network, filter by api.js. Confirm two separate https://www.google.com/recaptcha/api.js requests load.
  3. Fill in the community event form and submit. Confirm the form fails with an error.
download-1  

Verify the fix (with this PR):

  1. Reload /events/community/add/. Confirm only TEC's api.js loads, Newspack's is no longer present.
  2. Fill in and submit the community event form. Confirm it submits successfully.
  3. Load the homepage (or any non-TEC page). Confirm Newspack's api.js?render=<site_key> still loads (no regression).
  4. Switch Newspack reCAPTCHA to the other version (v2 if you used v3, or vice versa) and repeat steps 1 and 3 to confirm the guard works regardless of version.
  5. From repos/newspack-plugin/, run n test-php --filter Test_Recaptcha and confirm all 3 new tests pass.
Screenshot 2026-04-15 at 1 53 28 PM  

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?

Copilot AI review requested due to automatic review settings April 15, 2026 21:29
@wil-gerken wil-gerken requested a review from a team as a code owner April 15, 2026 21:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Prevents a reCAPTCHA breakage caused by double-loading Google’s api.js (with different site keys) when The Events Calendar Community Events submission page is used alongside Newspack reCAPTCHA.

Changes:

  • Add an early return in Recaptcha::register_scripts() on TEC Community Events submission/edit pages to avoid registering/enqueueing Newspack’s reCAPTCHA scripts there.
  • Add unit tests covering script registration behavior on normal pages, TEC community pages, and when reCAPTCHA is disabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
includes/class-recaptcha.php Adds TEC Community Events page detection and bails early to avoid conflicting api.js loads.
tests/unit-tests/recaptcha.php Adds unit tests validating script registration/enqueue behavior across page contexts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread includes/class-recaptcha.php Outdated
@wil-gerken wil-gerken marked this pull request as draft April 15, 2026 21:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/unit-tests/recaptcha.php Outdated
Comment thread tests/unit-tests/recaptcha.php
Comment thread tests/unit-tests/recaptcha.php Outdated
Comment thread tests/unit-tests/recaptcha.php
Comment thread tests/unit-tests/recaptcha.php Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wil-gerken wil-gerken marked this pull request as ready for review April 15, 2026 23:58
@wil-gerken wil-gerken added the [Status] Needs Review The issue or pull request needs to be reviewed label Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] Needs Review The issue or pull request needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants