Skip to content

fix(newsletter): send the signup request as JSON - #561

Open
enricobattocchi wants to merge 1 commit into
trunkfrom
fix-newsletter-signup
Open

enricobattocchi wants to merge 1 commit into
trunkfrom
fix-newsletter-signup

Conversation

@enricobattocchi

Copy link
Copy Markdown
Member

Context

  • Subscribing to the newsletter from the notice always showed "Something went wrong. Please try again later." The my.yoast.com subscribe endpoint only reads JSON, but the plugin sent the data as a regular form submission, so every request was rejected with a 400 error.

Summary

This PR can be summarized in the following changelog entry:

  • Fixes a bug where subscribing to the newsletter failed with an error message when the signup form was submitted.

Relevant technical choices:

  • The request body is encoded with wp_json_encode() and sent with a Content-Type: application/json header, matching how Yoast SEO calls the same endpoint.
  • The success check accepted only reply codes 201 to 299, so a 200 reply showed the error. It now accepts the full 200 to 299 range.

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  • Without this PR, go to the admin page where the Duplicate Post notice with the newsletter form appears.
  • Enter a valid email address and click "Subscribe".
  • See the message "Something went wrong. Please try again later."
  • With this PR, repeat the steps.
  • See the message "You have successfully subscribed to the newsletter. Please check your inbox." and check that the confirmation email arrives.
  • Enter an invalid email address, such as test@, and check that the form shows an error message.

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

  • Go to the admin page where the Duplicate Post notice with the newsletter form appears.
  • Enter a valid email address and click "Subscribe".
  • See the message "You have successfully subscribed to the newsletter. Please check your inbox." and check that the confirmation email arrives.

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

  • The newsletter signup form in the admin notice.

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label and noted the work hours.

Fixes #

🤖 Generated with Claude Code

The my.yoast.com subscribe endpoint only reads JSON bodies, so the
form-encoded request was always rejected with a 400. A 200 reply was
also treated as a failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new tests lack a required wp_unslash() stub and fail before exercising the subscription logic.

1 open finding
What changed in this PR

Fixes newsletter signup in the Duplicate Post admin notice by matching the endpoint’s JSON request format.

Changes:

  • Encodes subscription data as JSON with the appropriate content type.
  • Accepts all HTTP 200–299 responses as successful.
  • Adds regression tests for request formatting and response handling.
File Description
tests/​Unit/​UI/​Newsletter_Test.php Adds request and response regression tests.
src/​ui/​newsletter.php Sends JSON and accepts the full success range.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread tests/Unit/UI/Newsletter_Test.php Resolved
@coveralls-official

Copy link
Copy Markdown

Coverage Report for CI Build 37778855070

Coverage increased (+2.4%) to 65.24%

Details

  • Coverage increased (+2.4%) from the base build.
  • Patch coverage: 12 of 12 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2733
Covered Lines: 1783
Line Coverage: 65.24%
Coverage Strength: 9.7 hits per line

💛 - Coveralls

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.

2 participants