Files
wordpress-kratos-yang/functions.php
T
laoyang 0a5486e195 3.2.57 新增「写作增强」:发布时自动生成别名/标签/分类/摘要 #25
需求(工单 #25):写文章时自动生成别名(翻译)、自动生成标签、
自动选择分类、自动生成摘要,问主题能不能做、是否必须用插件。
结论:主题自己就能做,不依赖任何插件。

新增 inc/theme-writing.php(四个独立引擎 + 设置读写 + 密钥处理):

- 触发点:transition_post_status,且要求
  new_status === publish && old_status !== publish
  ⇒ 只在「第一次点发布」时动手,自动保存不触发,已发布的文章一概不碰。
- 幂等锁 _yang_writing_done:同一篇只做一次,避免反复覆写。

四道「不覆盖」守卫(用户逐条确认的决策):
- 摘要:只在 post_excerpt 为空时才写。
- 分类:只在文章还挂默认分类(未分类)时才动。
- 别名:只在「空」或「不是纯 ASCII」时才生成。判据不是"空"而是
  "不是纯 ASCII" —— 中文标题下 WP 生成的是 %e4%bf%a1... 这种编码串,
  它非空但不可用。已有英文别名的 966 篇一概不动(别名=URL,改了
  就是成批 404)。
- 标签:wp_set_post_tags(..., true) 追加,绝不删除手工标签。

修一个真实产品 bug:3.2.56 及以前 apply() 里写的是
list($suggest, $_) = yang_writing_make_tags(...),$new 被整个丢掉;
接着又拿 $suggest 去 get_term_by 二次分类 —— 而 $suggest 里每个词
都来自词典、必然全部命中 ⇒「新建」分支永远进不去 ⇒ 这条功能在真实
发布流程里一次都没生效过。改为 list($exist_hits, $new_cands),
两段分别复核后合并。

标签两级策略(新博客也能用):先匹配已有标签池,匹配不到才新建,
每篇新增数量受 w_tag_new_limit 约束(默认 3,设 0 = 永不新建)。

中文分词(服务器无 scws/jieba):先按虚词与标点切短语,再在短语
内部取 2~6 字 n-gram。不切句直接取窗口会产出「星等换算与赤」
这类跨词碎片,且字面上"出现 ≥2 次"能通过所有频次闸门 —— 这是
n-gram 的固有缺陷,只能靠切分规避,调阈值无效。
虚词表统一由 yang_writing_seps() 提供(闸门④ 复用同一函数)。

翻译:默认 MyMemory(免注册),可选百度/有道。任何失败都返回空串
跳过该项,绝不阻塞发布。

密钥:存独立选项 yang_writing_secrets,不进 kratos;password 类型 +
std 空串;页面只显示末 4 位;留空提交 = 不修改。

设置页:新增「写作增强」独立 heading 标签页,四个开关默认全关
(升级不改变现有行为)。framework 里 'requires' 这个键根本没被读取,
条件显示改用 options-custom.js 显式逻辑;info 类型不生成 .section
包裹层,补一条 .section-info 规则才能藏住服务商申请说明。

验证(发布前):
- 写作端到端 71 条、后台渲染 21 条、密钥路径 9 条,合计 101 条全过
- 反证 22 组(W1~W16 端到端 / A1~A6 后台)× R1~R14 发布包注入,
  全部让失败数从 0 增加
- 测试自身不污染:每节差分回滚 + 总闸门,标签库稳定 703 / 967 篇
- 发布走 theme-update/,主题目录仍是 3.2.56,未被覆盖
2026-10-27 16:05:43 +08:00

102 lines
4.6 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';