
本身wordpress是有cf的官方插件支持的,可以在网站有评论文章更新的时候自动刷新缓存
但是问题是我是saas接入的,这个插件无法在对应的zone给我刷新缓存
之前我是通过魔改这个官方插件实现刷新的,但是这次手贱更新了插件,干脆自己搓一个新的方案吧
代码:
/**
* Cloudflare SaaS 自动清理缓存
*/
// ================= 配置环境变量 =================
define('CF_API_TOKEN', 'JDQp-m123456789123456789123456789'); //cf的API Token 请给被接入的域名的缓存权限
define('CF_ZONE_ID', 'a1234567891234567891234567899999'); //在cf官网托管的也就是被接入的域名的区域ID
define('SAAS_DOMAIN', 'cangshui.net'); //你接入的saas域名
define('CUSTOM_LOG_PATH', '/www/wwwlog/test.log'); //日志路径,关闭就改成/dev/null
// ===============================================
function cloudflare_saas_purge_with_log($post_id, $trigger_event) {
if (!$post_id || wp_is_post_revision($post_id) || get_post_status($post_id) == 'auto-draft') {
return;
}
$post_path = str_replace(home_url(), '', get_permalink($post_id));
$urls = [
"https://" . SAAS_DOMAIN . "/", // 首页
"https://" . SAAS_DOMAIN . $post_path, // 文章页
"https://" . SAAS_DOMAIN . "/?_pjax=%23container", // 首页 PJAX
"https://" . SAAS_DOMAIN . $post_path . "?_pjax=%23container", // 文章页 PJAX
];
$urls = array_values(array_unique($urls));
$endpoint = "https://api.cloudflare.com/client/v4/zones/" . CF_ZONE_ID . "/purge_cache";
$args = [
'method' => 'POST',
'blocking' => true,
'timeout' => 12,
'headers' => [
'Authorization' => 'Bearer ' . trim(CF_API_TOKEN),
'Content-Type' => 'application/json',
],
'body' => json_encode(['files' => $urls]),
];
$response = wp_remote_post($endpoint, $args);
$time = date('[Y-m-d H:i:s]');
$status_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$log_entry = "{$time} [事件: {$trigger_event}] [文章ID: {$post_id}]\n";
$log_entry .= "目标 URL: " . implode(', ', $urls) . "\n";
if (is_wp_error($response)) {
$log_entry .= "结果: 失败 (WP_Error: " . $response->get_error_message() . ")\n";
} else {
$log_entry .= "结果: HTTP {$status_code} | 返回数据: {$body}\n";
}
$log_entry .= "-----------------------------------------------------------\n";
@file_put_contents(CUSTOM_LOG_PATH, $log_entry, FILE_APPEND);
}
/**
* 挂载钩子
*/
// 1. 文章操作:发布、更新、状态变更
add_action('wp_insert_post', function($post_id) {
cloudflare_saas_purge_with_log($post_id, 'Post_Update/Create');
}, 10, 1);
// 2. 评论操作:处理所有状态转换(如:后台点击“批准”、“垃圾”、“回收站”等)
add_action('transition_comment_status', function($new_status, $old_status, $comment) {
// 只要状态发生了改变(例如从未批准变为批准,或从批准变为回收站),就刷新缓存
if ($new_status !== $old_status) {
cloudflare_saas_purge_with_log($comment->comment_post_ID, "Comment_Status_Change({$old_status}_to_{$new_status})");
}
}, 10, 3);
// 3. 评论操作:直接在前台提交并自动批准的评论
add_action('comment_post', function($comment_id, $comment_approved) {
if ($comment_approved === 1) {
$comment = get_comment($comment_id);
cloudflare_saas_purge_with_log($comment->comment_post_ID, 'New_Comment_Auto_Approved');
}
}, 10, 2);
// 4. 评论操作:编辑现有评论内容后保存
add_action('edit_comment', function($comment_id) {
$comment = get_comment($comment_id);
cloudflare_saas_purge_with_log($comment->comment_post_ID, 'Comment_Edited');
}, 10, 1);
直接把代码复制粘贴到functions.php最后面就行,自己配置下变量
目前只刷新首页 文章页 PJAX加载的首页 PJAX加载的文章页 四个url 你可以自己自定义更多的url
会触发刷新的场景在php代码中已经注释了 你也可以自己添加更多的场景