From f058fa634c5d6d51b27cc00faf6d17775a052b5e Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Thu, 15 Jan 2026 16:02:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=8E=A7=E5=88=B6=E5=8F=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 管理员可在前台看到调试按钮,点击打开控制台 - 拦截 console.error 并显示红色通知 - 普通用户遇到错误时提示联系管理员 - 管理员可屏蔽特定错误,屏蔽后不再向用户显示 - 捕获全局 JS 错误和 Promise 错误 - 设置页面可管理已屏蔽的错误(查看/取消屏蔽/批量清空) - 生产环境禁用 console.log 和 console.warn --- functions.php | 293 ++++++++++++++++++++++++++++++++++++++++++++++++++ settings.php | 116 ++++++++++++++++++++ 2 files changed, 409 insertions(+) diff --git a/functions.php b/functions.php index a404563..4d36b3e 100644 --- a/functions.php +++ b/functions.php @@ -395,6 +395,299 @@ add_action('wp_head', 'argon_hot_reload_auto_refresh_script', 1); // 初始化热更新检测 add_action('init', 'argon_hot_reload_init'); +// ==================== 前端调试控制台功能 ==================== + +// 获取已屏蔽的错误列表 +function argon_get_muted_errors() { + return get_option('argon_muted_errors', array()); +} + +// 屏蔽错误 +function argon_mute_error() { + check_ajax_referer('argon_debug_console', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error(__('权限不足', 'argon')); + } + + $error_hash = sanitize_text_field($_POST['error_hash']); + $error_message = sanitize_text_field($_POST['error_message']); + $error_source = sanitize_text_field($_POST['error_source']); + + $muted_errors = argon_get_muted_errors(); + $muted_errors[$error_hash] = array( + 'message' => $error_message, + 'source' => $error_source, + 'muted_at' => current_time('timestamp'), + 'muted_by' => wp_get_current_user()->display_name + ); + + update_option('argon_muted_errors', $muted_errors); + wp_send_json_success(); +} +add_action('wp_ajax_argon_mute_error', 'argon_mute_error'); + +// 取消屏蔽错误 +function argon_unmute_error() { + check_ajax_referer('argon_debug_console', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error(__('权限不足', 'argon')); + } + + $error_hash = sanitize_text_field($_POST['error_hash']); + $muted_errors = argon_get_muted_errors(); + + if (isset($muted_errors[$error_hash])) { + unset($muted_errors[$error_hash]); + update_option('argon_muted_errors', $muted_errors); + } + + wp_send_json_success(); +} +add_action('wp_ajax_argon_unmute_error', 'argon_unmute_error'); + +// 批量删除屏蔽的错误 +function argon_clear_muted_errors() { + check_ajax_referer('argon_debug_console', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error(__('权限不足', 'argon')); + } + + update_option('argon_muted_errors', array()); + wp_send_json_success(); +} +add_action('wp_ajax_argon_clear_muted_errors', 'argon_clear_muted_errors'); + +// 输出调试控制台脚本 +function argon_debug_console_script() { + if (get_option('argon_enable_debug_console', 'false') != 'true') { + return; + } + + $is_admin = current_user_can('manage_options'); + $muted_errors = argon_get_muted_errors(); + $muted_hashes = array_keys($muted_errors); + ?> + + + + + +