Skip to content

内建统计函数number_format() 导致wp出现报错以及致命错误 #1340

@Deepseaon

Description

@Deepseaon

问题出处:

内建统计

实际行为描述:

造成wp报错,启用主题后编辑文字会报告json错误返回首页可能出现致命站点错误,切换其他主题就恢复正常,根据报错位置审计代码后判断可能是函数接收的数据类型错误导致报错

预期的行为:

解决冲突,正常运行

复现步骤:

使用内建统计功能

配置与环境:

已修复无法提供复现链接,3.0.4版本

截图(若有)

wp报错致命站点错误:
Fatal error: Uncaught TypeError: number_format(): Argument #1 ($num) must be of type float, string given in
/wp-content/themes/Sakurairo/functions.php:764 Stack trace: #0
/wp-content/themes/Sakurairo/functions.php(764): number_format() #1
/wp-content/themes/Sakurairo/functions.php(809): restyle_text() #2
/wp-content/themes/Sakurairo/functions.php(3156): get_post_views() #3
/wp-content/themes/Sakurairo/functions.php(3740): get_archive_info() #4
/wp-content/themes/Sakurairo/exhibition.php(25): get_site_stats() #5
/wp-includes/template.php(812): require('...') #6
/wp-includes/template.php(745): load_template() #7
/wp-includes/general-template.php(206): locate_template() #8
/wp-content/themes/Sakurairo/index.php(33): get_template_part() #9
/wp-includes/template-loader.php(106): include('...') #10
/wp-blog-header.php(19): require_once('...') #11
/index.php(17): require('...') #12 {main} thrown in
/wp-content/themes/Sakurairo/functions.php on line 764

补充信息:

764行相关函数:
function restyle_text($number)
{
switch (iro_opt('statistics_format')) {
case "type_2": //23,333 次访问
return number_format($number);
case "type_3": //23 333 次访问
return number_format($number, 0, '.', ' ');
case "type_4": //23k 次访问
if ($number >= 1000) {
return round($number / 1000, 2) . 'k';
}
return $number;
default:
return $number;
}
}
在 restyle_text 函数中,当使用 type_2 和 type_3 格式时,number_format() 函数接收的 $number 参数是字符串类型而不是数字类型
进行修正后不再报错
function restyle_text($number)
{
// 确保 $number 是数字类型
$number = is_numeric($number) ? (float)$number : 0;

switch (iro_opt('statistics_format')) {
    case "type_2": //23,333 次访问
        return number_format($number);
    case "type_3": //23 333 次访问
        return number_format($number, 0, '.', ' ');
    case "type_4": //23k 次访问
        if ($number >= 1000) {
            return round($number / 1000, 2) . 'k';
        }
        return $number;
    default:
        return $number;
}

}

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions