Skip to content

Conversation

Copy link

Copilot AI commented Sep 24, 2025

This PR fixes a Fatal error that occurs when the post views functionality encounters empty or invalid data in PHP 8+. The issue manifests as:

Fatal error: Uncaught TypeError: number_format(): Argument #1 ($num) must be of type int|float, string given in /wp-content/themes/Sakurairo/functions.php:764

Problem

The get_post_views() function calls restyle_text() which directly passes values to PHP's number_format() function. When post view metadata contains empty strings, null values, or other non-numeric data, number_format() throws a TypeError in PHP 8+ due to stricter type checking.

This causes:

  • Fatal errors when creating/updating posts
  • Blank homepage display
  • "此响应不是合法的 JSON 响应" errors in WordPress admin

Solution

Enhanced restyle_text() function:

  • Added is_numeric() check to validate input before formatting
  • Non-numeric values (empty strings, null, invalid data) are converted to 0
  • Prevents TypeError while maintaining expected behavior

Improved get_post_views() function:

  • Added input validation and type casting for post IDs
  • Added protection against invalid views metadata
  • Changed error return from inconsistent string to integer 0
  • Used strict comparison operators for better type safety

Testing

The fix has been thoroughly tested with various edge cases:

  • Empty strings: '''0'
  • Null values: null'0'
  • Invalid data: 'invalid''0'
  • Valid numbers: 1234'1,234' (formatted correctly)

All formatting types (type_1 through type_4) work correctly with the fix.

Fixes #1326.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Sep 24, 2025

@KotoriK 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] 【TypeError】number_format() 收到空字符串导致 Fatal error Fix TypeError in number_format() when handling empty string post views Sep 24, 2025
Copilot AI requested a review from KotoriK September 24, 2025 10:47
@KotoriK KotoriK changed the base branch from main to preview October 12, 2025 10:58
@github-actions
Copy link

这个 PR 已经 45 天没有任何活动了,将被标记为过时 stale 。 删除 stale 的标签或评论,否则将在 10 天内关闭。

@github-actions github-actions bot added the Stale label Nov 27, 2025
@github-actions
Copy link

github-actions bot commented Dec 7, 2025

此 PR 由于被标记为 stale 且超过 10 天没有活动,已自动关闭。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

【TypeError】number_format() 收到空字符串导致 Fatal error

3 participants