评论邮件重复的根因是发信源不止一个,分两层处理:一层管「谁发」,一层管「发几封」。 只做前者的话邮件照样重复,只是都从同一个 SMTP 出去。 邮件治理(新增 inc/theme-mail.php) - phpmailer_init 优先级 9999 接管通道,保证主题配置优先于其它 SMTP 插件; 接管前先重置可能被别的插件设过的字段,避免残留半套配置。 - From 强制等于 SMTP 登录账号。QQ / 163 会以「发件人不符」拒收。 - wp_mail 优先级 99 统一 From 与 Content-Type;调用方自带的 From 与 Content-Type 被剥离后重建,自定义 header 保留。 - wp_mail 优先级 100 做短期去重:同一收件人 + 同主题 + 同正文,90 秒内只发一次。 用 transient 是因为评论通知跨请求(wpDiscuz 走 AJAX、审核通知走后台请求), 静态变量跨不过去。90 秒足够宽(一次提交引发的通知都在同一秒)也足够窄 (手动重发通常隔几分钟,不该被吞)。 - 被去重的那次把 to 置空并由 pre_wp_mail 返回 true,而不是 return null: 返回 null 会改变 wp_mail 返回值语义,有的插件据此判定失败并重试,反而更多封。 - 用 filter 关掉核心的 notify_postauthor / notify_moderator,不改进数据库里的 comments_notify / moderation_notify。改用户设置属于越权,而且用户下次进后台 一勾又回来了;filter 是可逆的。只拦通知,不影响评论审核流程。 - 线上站点 m_smtp=1 但 m_host 为空时判定为「未就绪」并完全放手。只勾开关不填 主机时若强行接管,所有邮件会静默失败,比回退到 mail() 更糟。 邮件模板(新增 inc/theme-mail-template.php) - 表格布局 + 全内联样式:Outlook 2007-2019 用 Word 引擎,不认 flex / grid; Gmail 会剥掉 <head> 里的 style 块(含 media query)。 - 不用外部图片、不用 CSS 变量、不用 ::before。按钮的 padding 放在 td 上, 因为 Outlook 忽略 a 的 padding。 - 宽度用混合写法:HTML 属性 width=600 给 Outlook,CSS 的 width:100% + max-width:600px 让手机窄屏自动收窄。只写死 600px 会让手机横向滚动。 - badge 背景用预算的实色而非 rgba。Outlook 不支持 rgba,会当成非法值丢掉, 半透明背景变成全透明,badge 直接消失。 - 主色下沿那条 3px 深色带必须 height 同时写成 HTML 属性和 CSS, 否则 Outlook 把它撑成一行文字高,比例就毁了。 - 用 WCAG 相对亮度判断主色深浅来决定叠在上面的文字用白还是深色。 简单取平均会把 #00ff00 判成中等亮度(人眼对绿最敏感、蓝最不敏感)。 - 次要文字统一调到 #5f6b78。原来的浅灰在白底上只有 1.84:1,看着"高级"但读不出来; 现在最低 4.62:1,全部过 AA。 theme-smtp.php - 删掉 comment_notify / comment_approved 两个钩子及其硬编码模板。这两个钩子与 wpDiscuz 的 notifyCommentSubscribers 完全重复,是重复收信的直接来源。 纪念日自动哀悼(新增 inc/theme-mourning.php) - 法定纪念日三个默认勾选:09-03 抗战胜利纪念日、09-30 烈士纪念日、 12-13 南京大屠杀死难者国家公祭日。依据是 2014 年全国人大常委会两次决定, 已在代码注释里记下来源。 - 民间通行做法(05-12、09-18 等)默认不勾,由用户自己决定。替用户做价值判断不合适。 - 一次性全国哀悼日(2020-04-04 等)支持 YYYY-MM-DD 写法,只在当年生效。 不能当周年循环,否则明年 4 月 4 日又自动变黑白。 - 按站点时区判断(current_time),不按服务器时区。服务器在 UTC 时会晚 8 小时生效。 - 只挂 wp_head 而不限定 is_home():老代码限定首页,详情页不黑白,那是 bug。 - 打印时取消灰度,否则打印出来是灰的。 - 保留旧的 g_rip 手动常开开关。不能因为升级把原本黑白中的站点变回彩色。 options-framework - 补 multicheck 的渲染分支与 of_sanitize_multicheck。框架原来只在保存侧处理了 这个类型,没有渲染分支,所以这个类型此前根本不可用。 - 值存成 array(键 => 1|0) 而不是「勾选了的键组成的列表」:列表无法区分 「用户明确全部取消」和「从未配置」,会导致取消勾选后静默回退到默认值。 - 读取侧用 array_key_exists 判断是否保存过,而不是判断值是否为空。 - 配套补上 of-multicheck 样式,没有样式时 9 个选项会挤成一行流水文字。 去掉广告配置 - 删除主题选项里的广告配置段,以及 single.php 里上下两处广告位输出。 验证 - 全主题 68 个 PHP 文件语法检查通过。 - 行为测试 82 条、multicheck 闭环 25 条、邮件几何 15 条、设计审计 23 条。 - 反向验证 16 个注入 bug 全部被断言抓住(去重失效、不写标记、to 未清空、 From 未剥离、亮度算法退化、时区改用 date()、全不勾回退默认等)。 - 邮件最低文字对比度 4.62:1;320px 视口下卡片自动收窄到 296px 不溢出。
129 lines
3.9 KiB
PHP
129 lines
3.9 KiB
PHP
<?php
|
|
/**
|
|
* 站点相关函数
|
|
* @author Yongquan Yang <yangyq.net>
|
|
* @license MIT License
|
|
* @version 2020.08.04
|
|
*/
|
|
|
|
// 标题配置
|
|
function title($title, $sep)
|
|
{
|
|
global $paged, $page;
|
|
if (is_feed()) {
|
|
return $title;
|
|
}
|
|
$title .= get_bloginfo('name');
|
|
$site_description = get_bloginfo('description', 'display');
|
|
if ($site_description && (is_home() || is_front_page())) {
|
|
$title = "{$title} {$sep} {$site_description}";
|
|
}
|
|
if ($paged >= 2 || $page >= 2) {
|
|
$title = "{$title} {$sep} " . sprintf(__('第 %s 页', 'kratos'), max($paged, $page));
|
|
}
|
|
return $title;
|
|
}
|
|
add_filter('wp_title', 'title', 10, 2);
|
|
|
|
// Keywords 配置
|
|
function keywords()
|
|
{
|
|
global $post;
|
|
$keywords = '';
|
|
if (is_home()) {
|
|
$keywords = kratos_option('seo_keywords');
|
|
} elseif (is_single()) {
|
|
$keywords = get_post_meta($post->ID, "seo_keywords_value", true);
|
|
if ($keywords == '') {
|
|
$tags = wp_get_post_tags($post->ID);
|
|
foreach ($tags as $tag) {
|
|
$keywords = $keywords . $tag->name . ",";
|
|
}
|
|
$keywords = rtrim($keywords, ',');
|
|
}
|
|
} elseif (is_page()) {
|
|
$keywords = get_post_meta($post->ID, "seo_keywords_value", true);
|
|
if ($keywords == '') {
|
|
$keywords = kratos_option('seo_keywords');
|
|
}
|
|
} else {
|
|
$keywords = single_tag_title('', false);
|
|
}
|
|
return trim(strip_tags($keywords));
|
|
}
|
|
|
|
// Description 配置
|
|
function description()
|
|
{
|
|
global $post;
|
|
$description = '';
|
|
if (is_home()) {
|
|
$description = kratos_option('seo_description');
|
|
} elseif (is_single()) {
|
|
$description = get_post_meta($post->ID, "seo_description_value", true);
|
|
if ($description == '') {
|
|
$description = get_the_excerpt();
|
|
}
|
|
if ($description == '') {
|
|
$description = str_replace("\n", "", mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
|
|
}
|
|
} elseif (is_category()) {
|
|
$description = category_description();
|
|
} elseif (is_tag()) {
|
|
$description = tag_description();
|
|
} elseif (is_page()) {
|
|
$description = get_post_meta($post->ID, "seo_description_value", true);
|
|
if ($description == '') {
|
|
$description = kratos_option('seo_description');
|
|
}
|
|
}
|
|
return trim(strip_tags($description));
|
|
}
|
|
|
|
// robots.txt 配置
|
|
add_filter('robots_txt', function ($output, $public) {
|
|
if ('0' == $public) {
|
|
return "User-agent: *\nDisallow: /\n";
|
|
} else {
|
|
if (!empty(kratos_option('seo_robots'))) {
|
|
$output = esc_attr(strip_tags(kratos_option('seo_robots')));
|
|
}
|
|
return $output;
|
|
}
|
|
}, 10, 2);
|
|
|
|
// 哀悼黑白站点:已迁移到 inc/theme-mourning.php
|
|
//
|
|
// 旧的 mourning() 只判 is_home() 且只能手动常开,无法按纪念日自动生效。
|
|
// 新实现挂在 wp_head(优先级 1),支持按日期自动判断,并保持向后兼容
|
|
// (旧的 g_rip 开关仍表示「一直黑白」)。
|
|
|
|
// 抓取图片链接(搜索引擎或者社交工具分享时抓取图片的链接)
|
|
/*
|
|
function share_thumbnail_url()
|
|
{
|
|
global $post;
|
|
if (has_post_thumbnail($post->ID)) {
|
|
$post_thumbnail_id = get_post_thumbnail_id($post);
|
|
$img = wp_get_attachment_image_src($post_thumbnail_id, 'full');
|
|
$img = $img[0];
|
|
} else {
|
|
$content = $post->post_content;
|
|
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?); ?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
|
|
if (!empty($strResult[1])) {
|
|
$img = $strResult[1][0];
|
|
} else {
|
|
$img = kratos_option('seo_shareimg', ASSET_PATH . '/assets/img/default.jpg');
|
|
}
|
|
}
|
|
return $img;
|
|
}
|
|
*/
|
|
// 支持上传 svg
|
|
add_filter('upload_mimes', 'upload_svg');
|
|
function upload_svg($existing_mimes = array())
|
|
{
|
|
$existing_mimes['svg'] = 'image/svg+xml';
|
|
return $existing_mimes;
|
|
}
|