Skip to content

Stats: avoid trying to access formatter for unavailable locale#43396

Merged
jeherve merged 5 commits intotrunkfrom
fix/php84-locale-error-stats
May 8, 2025
Merged

Stats: avoid trying to access formatter for unavailable locale#43396
jeherve merged 5 commits intotrunkfrom
fix/php84-locale-error-stats

Conversation

@jeherve
Copy link
Copy Markdown
Member

@jeherve jeherve commented May 7, 2025

Proposed changes:

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Some of the locale codes we pass to NumberFormatter are not always available. skr is a good example. I've detailed this problem in this issue: php/php-src#18515

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Internal reference: p1746629628168729-slack-CDLH4C1UZ

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

  • No

Testing instructions:

  • Switch your interface language to Saraiki.
  • Go to Posts > All Posts
  • Notice that posts in the Stats column are still displayed properly.
Image

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.
@jeherve jeherve requested review from Copilot and josephscott May 7, 2025 17:45
@jeherve jeherve self-assigned this May 7, 2025
@jeherve jeherve added Bug When a feature is broken and / or not performing as intended [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. [Status] Needs Review This PR is ready for review. [Pri] Normal labels May 7, 2025
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

This pull request prevents PHP fatal errors in the Stats column by ensuring that an invalid locale is replaced with a fallback locale ("en_US") when constructing the NumberFormatter.

  • Added a locale validity check with a fallback in the get_formatter method.
  • Included a changelog entry detailing the fix for the locale issue.

Reviewed Changes

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

File Description
projects/packages/stats-admin/src/class-admin-post-list-column.php Added a locale availability check and fallback logic in the get_formatter method.
projects/packages/stats-admin/changelog/fix-php84-locale-error-stats Added a changelog entry describing the PHP fatal error fix.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented May 7, 2025

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 or WordPress.com Site Helper), and enable the fix/php84-locale-error-stats branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/php84-locale-error-stats
bin/jetpack-downloader test jetpack-mu-wpcom-plugin fix/php84-locale-error-stats

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
Copy link
Copy Markdown
Contributor

github-actions bot commented May 7, 2025

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 a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ 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!

@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented May 7, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/stats-admin/src/class-admin-post-list-column.php 81/96 (84.38%) 1.23% 0 💚

Full summary · PHP report · JS report

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.
@jeherve jeherve requested a review from Copilot May 7, 2025 18:02
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

@kangzj kangzj left a comment

Choose a reason for hiding this comment

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

Works well and looks good! Thanks fixing this 👍

@jeherve jeherve merged commit bdb2551 into trunk May 8, 2025
66 checks passed
@jeherve jeherve deleted the fix/php84-locale-error-stats branch May 8, 2025 07:07
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label May 8, 2025
matticbot pushed a commit to Automattic/jetpack-stats-admin that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
matticbot pushed a commit to Automattic/jetpack-storybook that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
matticbot pushed a commit to Automattic/jetpack-mu-wpcom that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
matticbot pushed a commit to Automattic/jetpack-mu-wpcom-plugin that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
matticbot pushed a commit to Automattic/wpcom-site-helper that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
matticbot pushed a commit to Automattic/jetpack-production that referenced this pull request May 8, 2025
* Stats: avoid trying to access formatter for unavailable locale

This should avoid errors like:

Fatal error: Uncaught ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "skr" is invalid

Internal reference: p1746629628168729-slack-CDLH4C1UZ

Some of the locale codes we pass to NumberFormatter are not always available. `skr` is a good example.

* Extract locale check to speed up locale detection

See Automattic/jetpack#43396 (comment)

* Get locale with an underscore instead of dash

The dash is appropriate to display the language code in HTML, but is not valid ISO 639-3.

* Add test for new method

* Avoid static analysis error

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/14900742224

Upstream-Ref: Automattic/jetpack@bdb2551
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. [Package] Stats Admin [Pri] Normal [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants