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
+7 -1
View File
@@ -55,7 +55,13 @@
if (kratos_option('s_gov')) {
echo '<p><a href="' . kratos_option('s_gov_link', '#') . '" target="_blank" rel="nofollow" ><i class="police-ico"></i>' . kratos_option('s_gov') . '</a></p>';
}
if (kratos_option('seo_statistical')) {echo '<p>' . kratos_option('seo_statistical') . '</p>';}
/* 统计脚本:内容由用户在「收录与统计 → 统计代码」里填,
开关由「埋点开关」控制(见 inc/theme-stat.php)。
开关与代码分离,是为了让"临时停掉统计"不必删代码 ——
删了之后想恢复就得重新去百度后台找那串 hash。 */
if (yang_stat_tracking_enabled() && kratos_option('seo_statistical')) {
echo '<p>' . kratos_option('seo_statistical') . '</p>';
}
/* 国旗:勾了「是否添加国旗」才显示。以前 src 直接取选项,
用户没上传地址就得到 <img src="">,页面上是一个破图图标。
逻辑抽到 yang_flag_block() 里,测试能直接测同一个函数,
+10
View File
@@ -100,3 +100,13 @@ require get_template_directory() . '/inc/theme-smtp.php';
// 写作增强:发布时自动生成别名 / 标签 / 分类 / 摘要(四引擎各自独立)
require get_template_directory() . '/inc/theme-writing.php';
// 访问统计面板:百度统计 API 的数据直出到设置页 + 前台埋点开关。
// 加载时序注意:本文件(functions.php)在 wp-settings 阶段就执行,
// 而 inc/theme-options.php 要等到 init:20 才被 autoload.php require
// (见 inc/options-framework/autoload.php:40/59)。所以本模块**先于**
// theme-options.php 加载 —— 它定义的面板函数到构建选项数组时已经就绪,
// 那边可以直接调用,不需要 function_exists 兜底。
// 但也因为先加载:本文件里不得调用 kratos_option(),
// 那个函数由 autoload.php 在更晚才定义(目前只在函数体内调用,安全)。
require get_template_directory() . '/inc/theme-stat.php';
+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'),
+1815
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,7 +1,7 @@
/*
Theme Name: Kratos_Yang
Text Domain: kratos_Yang
Version: 4.0.0
Version: 4.1.0
Requires PHP: 7.0
Description: 这是一个Kratos主题的分支版本,用来增加一些定制化功能。
Tags: 博客, 双栏, 响应式, 自定义背景, 自定义颜色, 自定义图标, 自定义菜单, 特色图像, 收录优化, 邮件中心, 主题选项, 小工具, 文章置顶, 无障碍友好