4.1.0 主题设置页加访问统计面板,接百度统计 API 直出后台 #33

新增 inc/theme-stat.php(约 1814 行),把百度统计的数据直接显示在
主题设置页,不用再跳去百度后台看。

主要设计决策:
- 凭据(API Key / Secret / refresh_token)存 wp-content/uploads/ 下
  独立文件(0600),不写 options 表、不在表单回显 —— 换主题/升级不丢
- 13 个指标全部实现,用户可在「面板显示哪些指标」里各别开关
  (每次 API 调用都占配额,只看几个数字的可以关掉其余)
- 面板走 AJAX 懒加载「先出壳、后取数」:同步渲染 8 个请求 ×5 秒超时
  会让设置页最坏卡 40 秒
- 缓存成功 1800 秒 / 失败 300 秒:接口挂了不至于每次刷新都等超时
- "--"(百度接口的"无数据")渲染成破折号,不转成 0 —— 否则
  "暂无数据"会被显示成"0 次访问"
- footer.php 的统计代码外面加埋点开关,关掉不停用代码本身

改动:
- inc/theme-stat.php   新建
- inc/theme-options.php 新增 8 个控件
- footer.php           统计代码套埋点开关
- functions.php        require 新模块
- style.css            版本号 4.0.0 → 4.1.0

验证:
- php -l 全量 98 文件 0 失败
- 正向判据 145/0,反证 19 组全抓到
- 真实接口连通性 10/0(OAuth 401 与 error_code 110 两种错误格式实测)
- 打包自检 82/0,其反证 15 组全抓到
This commit is contained in:
2026-10-28 16:33:41 +08:00
parent 05b4b56bf6
commit c125388ab7
5 changed files with 1938 additions and 3 deletions
+104
View File
@@ -825,6 +825,110 @@ function kratos_options()
'type' => 'textarea',
);
/* ══════════════════════════════════════════════════════════
* 访问统计面板(百度统计 API)
*
* 这一整段都是「附加功能」:不填任何东西时,本页其余部分
* 完全不受影响。面板本身由 inc/theme-stat.php 负责渲染,
* 这里只负责把控件摆出来。
* ══════════════════════════════════════════════════════════ */
$options[] = array(
'name' => __('启用统计埋点', 'kratos'),
'desc' => __('关掉后前台不再输出上面那段「统计代码」,但代码本身**不会被删除** —— '
. '想恢复时勾回来即可,不用再去百度后台找那串 hash。', 'kratos'),
'id' => 'stat_track_enable',
'std' => '1',
'type' => 'checkbox',
);
$options[] = array(
'name' => __('访问统计面板', 'kratos'),
'desc' => __('下面的面板会读百度统计的接口,把数据直接显示在这里。'
. '需要先在百度统计开通「数据导出服务」拿到 API Key。', 'kratos'),
'type' => 'info',
);
$options[] = array(
'name' => __('API Key', 'kratos'),
'desc' => __('百度统计 → 管理 → <a href="https://tongji.baidu.com/sc-web/home/dataapi" target="_blank" rel="noopener">数据导出服务</a>'
. ' 开通后可见,形如 <code>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</code>。<br>'
. '<b>留空表示不修改</b>已保存的值。', 'kratos'),
'id' => 'stat_api_key',
'type' => 'password',
'std' => '',
);
$options[] = array(
'name' => __('Secret Key', 'kratos'),
'desc' => __('与 API Key 在同一页面。'
. '<b>留空表示不修改</b>。这一项只存在服务器的独立文件里(权限 600),'
. '不会写进设置表,也不会在表单里回显。', 'kratos'),
'id' => 'stat_api_secret',
'type' => 'password',
'std' => '',
);
$options[] = array(
'name' => __('站点 ID', 'kratos'),
'desc' => __('百度统计「管理 → 网站列表」点进站点,地址栏里 <code>siteId=</code> 后面那串数字。'
. '直接粘整条地址也行,会自动抠出数字。', 'kratos'),
'id' => 'stat_site_id',
'type' => 'text',
);
$options[] = array(
'name' => __('授权码', 'kratos'),
'desc' => __('在面板里点「授权」拿到 code 之后,粘到这里并保存 —— '
. '主题会立刻用它换 refresh_token,之后自动续期,不用再管。<br>'
. '授权码只能用一次,保存后这一格会自动清空。', 'kratos'),
'id' => 'stat_auth_code',
'type' => 'text',
'std' => '',
);
$options[] = array(
'name' => __('refresh_token(高级)', 'kratos'),
'desc' => __('一般不需要手动填 —— 走上面的「授权码」流程会自动得到。'
. '只有在别处已经拿过 token、要迁移过来时才用。<br>'
. '<b>留空表示不修改</b>。', 'kratos'),
'id' => 'stat_refresh',
'type' => 'password',
'std' => '',
);
/* 指标多选:全做,但让用户决定显示哪些。
理由见 inc/theme-stat.php 的 yang_stat_metric_defs() ——
每个指标对应一次 API 调用,而配额是按次计的。 */
$yang_stat_metric_opts = array();
if (function_exists('yang_stat_metric_defs')) {
foreach (yang_stat_metric_defs() as $k => $def) {
$yang_stat_metric_opts[$k] = array('name' => $def['label']);
}
}
$yang_stat_metric_all = array();
foreach ($yang_stat_metric_opts as $k => $_) {
$yang_stat_metric_all[$k] = '1';
}
$options[] = array(
'name' => __('面板显示哪些指标', 'kratos'),
'desc' => __('全部取消勾选 = 面板只显示"已接通"不显示数字。'
. '勾得越多,每次刷新后台时调用的接口越多(配额按次计)。'
. '默认全选。', 'kratos'),
'id' => 'stat_metrics',
'std' => $yang_stat_metric_all,
'type' => 'multicheck',
'options' => $yang_stat_metric_opts,
);
$options[] = array(
'name' => __('数据概览', 'kratos'),
'desc' => function_exists('yang_stat_render_shell')
? yang_stat_render_shell()
: '<em>统计模块未加载</em>',
'type' => 'info',
);
$options[] = array(
'name' => __('robots.txt 内容', 'kratos'),
'desc' => __('- 需要 ', 'kratos') . $seoreading . __(' 是开启的状态,下面的配置才会生效', 'kratos'),