-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-debug-testing-maintenance.html
More file actions
202 lines (176 loc) · 6.66 KB
/
20-debug-testing-maintenance.html
File metadata and controls
202 lines (176 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>20 调试、测试与维护</title>
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<main class="page">
<section class="hero">
<div class="hero-card">
<h1>20 调试、测试与维护</h1>
<p>整理 WP_DEBUG、日志、Query Monitor 思路、错误处理、计划任务和维护清单。</p>
</div>
<img src="assets/20-debug-testing-maintenance.svg" alt="20 调试、测试与维护">
</section>
<nav class="nav">
<a href="index.html">首页</a>
<a href="01-hooks-functions.html">01</a>
<a href="02-theme-setup-assets.html">02</a>
<a href="03-template-loop-conditions.html">03</a>
<a href="04-cpt-taxonomy.html">04</a>
<a href="05-media-images.html">05</a>
<a href="06-menus-widgets-sidebars.html">06</a>
<a href="07-admin-ui-settings.html">07</a>
<a href="08-plugin-architecture.html">08</a>
<a href="09-shortcodes-content.html">09</a>
<a href="10-gutenberg-blocks.html">10</a>
<a href="11-elementor-integration.html">11</a>
<a href="12-customizer-settings-api.html">12</a>
<a href="13-forms-email-ajax.html">13</a>
<a href="14-security-permissions.html">14</a>
<a href="15-users-roles-capabilities.html">15</a>
<a href="16-rest-api-ajax.html">16</a>
<a href="17-seo-schema-head.html">17</a>
<a href="18-performance-cache.html">18</a>
<a href="19-migration-config.html">19</a>
<a href="20-debug-testing-maintenance.html">20</a>
</nav>
<section class="card">
<h2>本页关键词</h2>
<div class="tag-list">
<span>WP_DEBUG</span>
<span>error_log</span>
<span>WP_Error</span>
<span>cron</span>
<span>maintenance</span>
</div>
</section>
<section class="card">
<h2>学习目标</h2>
<ul class="checklist">
<li>会开启调试日志</li>
<li>会写 error_log</li>
<li>会处理 WP_Error</li>
<li>会注册计划任务</li>
<li>知道维护检查清单</li>
</ul>
</section>
<section class="card">
<h2>代码使用提醒</h2>
<p>本页代码适合用于学习和研究。复制到正式网站前,请先备份,并优先在测试环境验证。</p>
<p>涉及用户输入、后台保存、接口请求、删除操作和邮件发送时,要同时考虑权限、nonce、sanitize、validate 和 escape。</p>
</section>
<section class="code-grid">
<article class="code-card">
<div class="code-title">
<h3>1. wp-config 调试配置</h3>
<span class="badge">调试</span>
</div>
<div class="code"><?php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );</div>
<div class="code-note">正式站不建议把错误直接显示给访客。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>2. 写入调试日志</h3>
<span class="badge">调试</span>
</div>
<div class="code"><?php
error_log( 'Contact form handler started.' );
$data = array( 'name' => 'Test', 'status' => 'ok' );
error_log( print_r( $data, true ) );</div>
<div class="code-note">临时排查可用 error_log,修复后应清理过多日志。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>3. 处理 WP_Error</h3>
<span class="badge">稳定</span>
</div>
<div class="code"><?php
$response = wp_remote_get( 'https://example.com/api' );
if ( is_wp_error( $response ) ) {
error_log( $response->get_error_message() );
return;
}
$body = wp_remote_retrieve_body( $response );</div>
<div class="code-note">调用接口、插入数据、上传文件时都可能返回 WP_Error。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>4. 注册计划任务</h3>
<span class="badge">维护</span>
</div>
<div class="code"><?php
function mysite_schedule_daily_task() {
if ( ! wp_next_scheduled( 'mysite_daily_cleanup' ) ) {
wp_schedule_event( time(), 'daily', 'mysite_daily_cleanup' );
}
}
add_action( 'wp', 'mysite_schedule_daily_task' );
function mysite_daily_cleanup_callback() {
// 每日清理逻辑。
}
add_action( 'mysite_daily_cleanup', 'mysite_daily_cleanup_callback' );</div>
<div class="code-note">WP-Cron 依赖访问触发,不等同于系统 cron。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>5. 停用插件时清理计划任务</h3>
<span class="badge">维护</span>
</div>
<div class="code"><?php
function mysite_clear_scheduled_tasks() {
$timestamp = wp_next_scheduled( 'mysite_daily_cleanup' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'mysite_daily_cleanup' );
}
}
register_deactivation_hook( __FILE__, 'mysite_clear_scheduled_tasks' );</div>
<div class="code-note">注册了计划任务,就要考虑停用时清理。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>6. 维护模式短路输出</h3>
<span class="badge">维护</span>
</div>
<div class="code"><?php
function mysite_simple_maintenance_mode() {
if ( current_user_can( 'manage_options' ) ) {
return;
}
if ( get_option( 'mysite_maintenance_mode' ) ) {
wp_die( '网站维护中,请稍后访问。', 'Maintenance', array( 'response' => 503 ) );
}
}
add_action( 'template_redirect', 'mysite_simple_maintenance_mode' );</div>
<div class="code-note">维护模式要允许管理员访问,且返回合适状态码。</div>
</article>
<article class="code-card">
<div class="code-title">
<h3>7. 基础维护检查清单代码注释</h3>
<span class="badge">维护</span>
</div>
<div class="code"><?php
/**
* 每次更新前检查:
* 1. 备份数据库和文件
* 2. 记录当前 WordPress / 主题 / 插件版本
* 3. 在测试环境更新
* 4. 测试首页、表单、购物车、登录、搜索
* 5. 查看 debug.log 是否有新错误
*/</div>
<div class="code-note">把维护流程写进项目文档,比只靠记忆更可靠。</div>
</article>
</section>
<section class="summary-box">
<h2>本页总结</h2>
<p>调试维护的重点是日志、错误处理、计划任务和复测流程。任何修复都要以备份和可回滚为前提。</p>
</section>
</main>
</body>
</html>