// 在插件初始化时添加日志,检查cron是否正常触发
public function plugin_init(): void
{
// 现有代码...
// 添加调试日志
error_log("AI Post Debug: 插件初始化 - 系统环境: " . PHP_OS);
// 检查WordPress Cron状态
if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
error_log("AI Post Debug: WP_CRON已禁用,任务可能无法自动执行");
}
// 现有代码...
}
private function check_system_environment() {
$issues = [];
// 检查操作系统
$os = PHP_OS;
error_log("AI Post Debug: 操作系统 - " . $os);
// 检查PHP版本
$php_version = phpversion();
error_log("AI Post Debug: PHP版本 - " . $php_version);
// 检查文件权限
$plugin_dir = plugin_dir_path(dirname(__FILE__));
$is_writable = is_writable($plugin_dir);
error_log("AI Post Debug: 插件目录可写权限 - " . ($is_writable ? '是' : '否'));
// 检查curl扩展
$curl_enabled = function_exists('curl_version');
error_log("AI Post Debug: CURL扩展 - " . ($curl_enabled ? '已启用' : '未启用'));
if (!$curl_enabled) {
$issues[] = "CURL扩展未启用,这可能导致API请求失败";
}
// 检查内存限制
$memory_limit = ini_get('memory_limit');
error_log("AI Post Debug: 内存限制 - " . $memory_limit);
// 检查最大执行时间
$max_execution_time = ini_get('max_execution_time');
error_log("AI Post Debug: 最大执行时间 - " . $max_execution_time);
return [
'has_issues' => !empty($issues),
'issues' => $issues,
'environment' => [
'os' => $os,
'php_version' => $php_version,
'is_writable' => $is_writable,
'curl_enabled' => $curl_enabled,
'memory_limit' => $memory_limit,
'max_execution_time' => $max_execution_time
]
];
}
public function ai_post_cron($key)
{
// 添加环境检测日志
$env_check = $this->check_system_environment();
if ($env_check['has_issues']) {
error_log("AI Post Debug: 系统环境检测发现问题: " . implode(", ", $env_check['issues']));
}
// 设置更长的执行时间限制
set_time_limit(300); // 5分钟
// 现有代码...
}
// 在构造函数中添加
public function __construct()
{
// 现有代码...
// 检测Linux环境并应用特定修复
if (stripos(PHP_OS, 'linux') !== false) {
// 增加内存限制
ini_set('memory_limit', '256M');
// 禁用输出缓冲 ,防止长时间运行的脚本被中断
if (ob_get_level()) ob_end_clean();
error_log("AI Post Debug: 检测到Linux环境,已应用特定优化");
}
// 现有代码...
}
private function test_api_connection() {
$api_url = "https://api.openai.com"; // 或其他您使用的API
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
error_log("AI Post Debug: API连接测试 - HTTP状态码: " . $http_code . ($error ? ", 错误: " . $error : ""));
return [
'success' => ($http_code >= 200 && $http_code < 300),
'http_code' => $http_code,
'error' => $error
];
}
define('DISABLE_WP_CRON', true);
# 编辑crontab
crontab -e
# 添加以下行(每5分钟执行一次)
*/5 * * * * wget -q -O /dev/null https://您的网站地址/wp-cron.php?doing_wp_cron >/dev/null 2>&1
tail -f /var/log/php/error.log
# 或
tail -f /var/log/apache2/error.log
# 或
tail -f /var/log/nginx/error.log
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
访问: https://您的网站地址/wp-cron.php?doing_wp_cron