Files
wordpress-kratos-yang/functions.php
T
laoyang c125388ab7 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 组全抓到
2026-10-28 16:33:41 +08:00

113 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 模板函数
* @author Yongquan Yang <yangyq.net>
* @license MIT License
* @version 2020.03.17
*/
define('THEME_VERSION', wp_get_theme()->get('Version'));
// 主题配置
require get_template_directory() . '/inc/options-framework/autoload.php';
// 自定义代码(CSS / JS,存选项表而非主题目录)
require get_template_directory() . '/inc/theme-custom-code.php';
// 更新配置
require get_template_directory() . '/inc/update-checker/autoload.php';
// 核心配置
require get_template_directory() . '/inc/theme-core.php';
// 图片类选项的默认值(Logo/favicon/404/分享图… 空值时落到包内默认图)。
// 必须排在 theme-core.php 之后:它要用 ASSET_PATH 与 kratos_option。
require get_template_directory() . '/inc/theme-option-defaults.php';
// 站点配置
require get_template_directory() . '/inc/theme-setting.php';
// 纪念日自动哀悼(按日期判断,需在 theme-setting 之后,
// 因为它用到 kratos_option)
require get_template_directory() . '/inc/theme-mourning.php';
// 哀悼说明页(固定路径 /memory,由主题判定路由,不写数据库)
require get_template_directory() . '/inc/theme-mourning-page.php';
// 文章配置
require get_template_directory() . '/inc/theme-article.php';
// 代码高亮:highlight.js 自托管,接管正文与评论区的所有代码块。
// 必须排在 theme-core.php 之后:它要用 kratos_option 读三个开关。
// 必须**早于 theme-comments.php**:评论区渲染要调 yang_hl_render_block()。
// 挂在 the_content 优先级 9 —— 早于 wpautop(10),
// 因为裸 <pre>(站内实测 80 个,占 28.2%)一旦先被 wpautop 包进 <p>,
// 行首缩进就会被 white-space:normal 折叠掉,代码完全没法读。
require get_template_directory() . '/inc/theme-highlight.php';
// 评论区(统一渲染 + AJAX + 评论通知)。
// 必须在 theme-article.php 之后:后者不再定义评论函数,
// 但评论模板 comments.php 里引用的 comment_callbacks 由本文件提供。
// 也必须早于 theme-mail.php(评论通知要调 yang_mail_render)。
require get_template_directory() . '/inc/theme-comments.php';
// 小工具配置
require get_template_directory() . '/inc/theme-widgets.php';
// 文章增强
require get_template_directory() . '/inc/theme-shortcode.php';
// 文章目录([TOC] 短代码)
require get_template_directory() . '/inc/theme-toc.php';
// 归档式文章列表([Archives] 短代码):年/月归类 + 浏览/评论/点赞计数 + 四角年份切换。
// 必须排在 theme-article.php 之后:计数读的是它写的 post meta(views / love)。
// 也必须排在 theme-core.php 之后:它用 ASSET_PATH 与 THEME_VERSION 挂资源。
require get_template_directory() . '/inc/theme-archives.php';
// 添加导航目录
require get_template_directory() . '/inc/theme-navwalker.php';
// 对象存储配置
require get_template_directory() . '/inc/theme-dogecloud.php';
// CDN 加速(地址改写:CDN 地址 + 白名单 + 黑名单)。
// 必须排在 theme-core.php 之后:它要用 kratos_option 与 wp_parse_url。
// 挂在 template_redirect 上开输出缓冲,所以本文件晚加载不影响生效。
require get_template_directory() . '/inc/theme-cdn.php';
// 自动头像:为没有头像的访客按邮箱生成一张(纯 PHP,零外网请求)。
// 必须排在 theme-core.php 之后:它要用 kratos_option。
// 它挂在 get_avatar 过滤器上(优先级 20,晚于 theme-core.php 里
// 那个改 Gravatar 域名的过滤器),所以要保证 theme-core.php 先加载。
require get_template_directory() . '/inc/theme-avatar.php';
// 图片灯箱:正文图片点击在当前页放大、多图轮播、键盘切换。
// 必须排在 theme-core.php 之后:它要用 ASSET_PATH 与 THEME_VERSION。
// 必须排在 theme-article.php 之后:它挂在 the_content 优先级 20,
// 要去掉 imgnofollow()(优先级 10)给图片链接加的 target="_blank"。
require get_template_directory() . '/inc/theme-lightbox.php';
// 邮件模板:必须先于 theme-mail.php 加载,
// 因为后者在发信时会调用 yang_mail_render()
require get_template_directory() . '/inc/theme-mail-template.php';
// 邮件治理:统一接管发信通道、去重、关掉重复通知
require get_template_directory() . '/inc/theme-mail.php';
// SMTP 配置(连通性相关,实际接管在 theme-mail.php)
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';