Skip to content

Newsletters: fix subscription options translations and persistent - #50909

Open
dlind1 wants to merge 8 commits into
trunkfrom
fix/newsletter-subscription-options-persistence
Open

Newsletters: fix subscription options translations and persistent#50909
dlind1 wants to merge 8 commits into
trunkfrom
fix/newsletter-subscription-options-persistence

Conversation

@dlind1

@dlind1 dlind1 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Fixes I18N-289

Proposed changes

  • Switch to the user locale before retrieving subscription options, so that the defaults would be translated.
  • Compare the subscription options with the translated defaults on save, to avoid saving a value in the database if no changes were made.

Does this pull request change what data or activity we track or use?

Testing instructions

  • Create two test sites, switch your user locale to a popular not-English one, like Spanish, French, German or Japanese.
  • On the first test site, go to /wp-admin/admin.php?page=jetpack-newsletter&p=%2F%3Ftab%3Dsettings, confirm that "Welcome email message" appears in English.
  • Make the 'Save' button appear by cutting the text from the textarea, then pasting it back, save the message.
  • Checkout the PR and sandbox public-api.
  • On site two confirm that the Welcome message appears translated, try saving it in the same way as for the first site.
  • Change your user locale to another non-English one.
  • Refresh the pages and confirm that the message appears in English on the first site and in the new locale on the second site.

@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/newsletter-subscription-options-persistence branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/newsletter-subscription-options-persistence

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions Bot added [Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels Jul 29, 2026
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 29, 2026
@jp-launch-control

jp-launch-control Bot commented Jul 29, 2026

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php 436/900 (48.44%) 1.44% 4 💔

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

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.

Pull request overview

This PR updates Jetpack’s WP.com JSON API site settings endpoint behavior for subscription_options so defaults are translated in the current user’s locale and unchanged defaults are not persisted (preventing locale “freezing” in the DB).

Changes:

  • Read subscription_options under the current user locale when building the settings response.
  • On POST, compare incoming subscription_options against the current value/defaults and persist only truly changed sub-keys.
  • Add/adjust PHPUnit coverage for the new persistence/no-op behavior and falsy hide_free_tier handling.

Reviewed changes

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

File Description
projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php Adds POST regression tests ensuring unchanged defaults aren’t persisted and only edited sub-keys are stored.
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php Switches read path to user-locale retrieval and changes POST logic to ignore unchanged default values and store only diffs.
projects/plugins/jetpack/changelog/fix-newsletter-subscription-options-persistence Adds a Jetpack changelog entry describing the user-facing bugfix.
Comments suppressed due to low confidence (2)

projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php:286

  • This test installs a default_option_subscription_options filter via an anonymous closure but never removes it, which can affect later tests in the same run. Clean it up in a finally block to avoid cross-test contamination.
	public function test_post_subscription_options_ignores_unchanged_defaults() {
		$defaults = $this->register_subscription_options_default();

		$setting  = wp_json_encode( array( 'subscription_options' => $defaults ), JSON_UNESCAPED_SLASHES );
		$response = $this->make_post_request( $setting );

projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php:303

  • Like the other new subscription_options tests, this leaves the default_option_subscription_options test filter installed after the test completes (and it can stack if called repeatedly). Add cleanup in a finally block so later tests aren’t influenced by the stubbed defaults.
	public function test_post_subscription_options_persists_only_changed_sub_keys() {
		$defaults = $this->register_subscription_options_default();

		$setting  = wp_json_encode(
			array( 'subscription_options' => array_merge( $defaults, array( 'welcome' => 'My own welcome' ) ) ),
			JSON_UNESCAPED_SLASHES
		);
		$response = $this->make_post_request( $setting );

@dlind1

dlind1 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: Atomic / self-hosted sites still get empty defaults

This fix only covers the WPCOM JSON API endpoint (/rest/v1.4/sites/{id}/settings). Atomic and self-hosted sites use the Jetpack v4 endpoint (/wp-json/jetpack/v4/settings) instead, which is unchanged here.

On v4, subscription_options is read via get_option( 'subscription_options', $default ) with a hardcoded empty-string default from the schema. Passing a default makes subscription_options_fallback() short-circuit (if ( $passed_default ) return $default;), so it returns empty strings for invitation/welcome/comment_follow rather than the real (translatable) defaults — worse than untranslated.

Knock-on effect on save: the settings UI rebuilds and posts the whole bag from those empty GET values, and v4's array_merge (with no change-detection) persists the empty strings over the real defaults.

So the "translate the defaults / don't persist unchanged defaults" goal isn't met on Atomic. Suggest extending the same read-in-user-locale + change-detection handling to the v4 endpoint (likely by dropping the empty-string schema default so the fallback fires).

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.

Pull request overview

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

Comments suppressed due to low confidence (2)

projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php:248

  • register_subscription_options_default() adds a global default_option_subscription_options filter but never removes it. Because filters are global in the PHP process, this can leak into later tests and change get_option( 'subscription_options' ) behavior unexpectedly. Consider storing the callback so it can be removed in tear_down() (or removing it at the end of each test).
		add_filter(
			'default_option_subscription_options',
			static function ( $default_value, $option, $passed_default ) use ( $defaults ) {
				return $passed_default ? $default_value : $defaults;
			},
			10,
			3
		);

projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php:665

  • get_subscription_options_in_user_locale() repeats the defaults+stored merge block twice (including calling the default_option_subscription_options filter and get_option() twice). This is redundant work and can introduce unexpected side effects if the filter is not idempotent; it should only run once.
		$subscription_options         = array_merge( $default_subscription_options, $stored_subscription_options );

		if ( $switched_locale ) {
			restore_previous_locale();
		}

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

Labels

[Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants