PluginProbe
WPJAM Basic / trunk
WPJAM Basic vtrunk
wpjam-basic / components / wpjam-basic.php

wpjam-basic.php in WPJAM Basic trunk, at components/wpjam-basic.php

440 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Name: 优化设置
4 URI: https://mp.weixin.qq.com/s/zkA0Nx4u81PCZWByQq3iiA
5 Description: 优化设置通过屏蔽和增强功能来加快 WordPress 的加载
6 Version: 2.0
7 */
8 class WPJAM_Basic{
9 public static function __callStatic($method, $args){
10 return wpjam_call_option(static::class, $method, ...$args);
11 }
12
13 public static function get_sections(){
14 $options = array_column(get_taxonomies(['public'=>true, 'hierarchical'=>true], 'objects'), 'label', 'name');
15 $for_field = count($options) <= 1 ? ['type'=>'hidden', 'value'=>'category'] : ['before'=>'分类模式:', 'options'=>$options];
16
17 return [
18 'disabled' => ['title'=>'功能屏蔽', 'fields'=>[
19 'basic' =>['title'=>'常规功能', 'fields'=>[
20 'disable_revisions' =>['label'=>'屏蔽文章修订功能,精简文章表数据。', 'value'=>1],
21 'disable_trackbacks' =>['label'=>'彻底�
22 �闭Trackback,防止垃圾留言。', 'value'=>1],
23 'disable_xml_rpc' =>['label'=>'�
24 �闭XML-RPC功能,只在后台发布文章。', 'value'=>1],
25 'disable_batch_rest' =>['label'=>'�
26 �闭REST API批量处理端点(<code>wp-json/batch</code>)'],
27 'disable_auto_update' =>['label'=>'�
28 �闭自动更新功能,手动或SSH方式更新。'],
29 'disable_feed' =>['label'=>'屏蔽站点Feed,防止文章被快速被采集。'],
30 'disable_admin_email_check' =>['label'=>'屏蔽站点管理员邮箱定期验证功能。'],
31 ]],
32 'convert' =>['title'=>'转换功能', 'fields'=>[
33 'disable_emoji' =>['label'=>'屏蔽Emoji转换成图片功能,直接使用Emoji。', 'value'=>1],
34 'disable_texturize' =>['label'=>'屏蔽字符转换成格式化的HTML实体功能。', 'value'=>1],
35 'disable_capital_P_dangit' =>['label'=>'屏蔽WordPress大小写修正,自行决定如何书写。', 'value'=>1],
36 ]],
37 'backend' =>['title'=>'后台功能', 'fields'=>[
38 'disable_privacy' =>['label'=>'移除为欧洲通用数据保护条例生成的页面。', 'value'=>1],
39 'disable_dashboard_primary' =>['label'=>'移除仪表盘的「WordPress 活动及新闻」。'],
40 'disable_backend' =>['before'=>'移除后台界面右上角:', 'sep'=>'&emsp;', 'type'=>'fields', 'fields'=>[
41 'disable_help_tabs' =>['label'=>'帮助'],
42 'disable_screen_options' =>['label'=>'选项',],
43 ]]
44 ]],
45 'page' =>['title'=>'页面功能', 'fields'=>[
46 'disable_head_links' =>['label'=>'移除页面头部版本号和服务发现标签代码。'],
47 'disable_admin_bar' =>['label'=>'移除工�
48 �栏和后台个人资料中工�
49 �栏相�
50 �选项。']
51 ]],
52 'embed' =>['title'=>'嵌�
53 �功能', 'fields'=>[
54 'disable_autoembed' =>['label'=>'禁用Auto Embeds功能,加快页面解析速度。'],
55 'disable_embed' =>['label'=>'屏蔽嵌�
56 ��
57 �他WordPress文章的Embed功能。'],
58 ]],
59 'gutenberg' =>['title'=>'古�
60 �堡编辑器', 'fields'=>[
61 'disable_block_editor' =>['label'=>'屏蔽Gutenberg编辑器,换回经�
62 �编辑器。'],
63 'disable_widgets_block_editor' =>['label'=>'屏蔽小工�
64 �区块编辑器模式,切换回经�
65 �模式。']
66 ]],
67 ]],
68 'enhance' => ['title'=>'增强优化', 'fields'=>[
69 'static_cdn' => ['title'=>'前端�
70 ��
71 �库', 'options'=>wpjam_fill(self::get_items('static_cdn'), fn($v)=> parse_url($v, PHP_URL_HOST))],
72 'gravatar' => ['title'=>'Gravatar加速', 'type'=>'fieldset', 'fields'=>[
73 'gravatar'=>['after'=>'加速服务', 'options'=>wpjam_options(self::get_items('gravatar'), ['type'=>'select'])+['custom'=>[
74 'title' => '自定义',
75 'fields' => ['gravatar_custom'=>['placeholder'=>'请输�
76 � Gravatar 加速服务地址']]
77 ]]]
78 ]],
79 'google_fonts' => ['title'=>'Google字体加速', 'type'=>'fieldset', 'fields'=>[
80 'google_fonts'=>['type'=>'select', 'after'=>'加速服务', 'options'=>wpjam_options(self::get_items('google_font'), ['type'=>'select'])+['custom'=>[
81 'title' => '自定义',
82 'fields' => self::google_font()
83 ]]]
84 ]],
85 'x-frame-options' =>['title'=>'Frame嵌�
86 �', 'options'=>[''=>'所有网页', 'SAMEORIGIN'=>'只�
87 �许同域名网页', 'DENY'=>'不�
88 �许任何网页']],
89 'no_category_base' =>['title'=>'分类链接简化', 'fields'=>[
90 'no_category_base'=>['label'=>'去掉分类目录链接中的 category。', 'fields'=>['no_category_base_for'=>$for_field]
91 ]]],
92 'timestamp_file_name' =>['title'=>'图片时间戳', 'label'=>'给上传的图片加上时间戳,防止大量的SQL查询。'],
93 'optimized_by_wpjam' =>['title'=>'WPJAM Basic', 'label'=>'在网站底部显示:Optimized by WPJAM Basic。']
94 ]]
95 ];
96 }
97
98 public static function static_cdn($src=null){
99 $hosts = self::get_items('static_cdn');
100 $host = self::get_setting('static_cdn');
101 $host = $host && in_array($host, $hosts) ? $host : $hosts[0];
102
103 return isset($src) ? ($src && !str_starts_with($src, $host) ? str_replace(array_diff($hosts, [$host]), $host, $src) : $src) : $host;
104 }
105
106 public static function google_font($html=null){
107 $map = [
108 'googleapis_fonts' => '//fonts.googleapis.com',
109 'googleapis_ajax' => '//ajax.googleapis.com',
110 'googleusercontent_themes' => '//themes.googleusercontent.com',
111 'gstatic_fonts' => '//fonts.gstatic.com'
112 ];
113
114 if(isset($html)){
115 if($name = self::get_setting('google_fonts')){
116 return str_replace(array_values($map), ($name == 'custom'
117 ? wpjam_map($map, fn($v, $k)=> str_replace(['http://','https://'], '//', self::get_setting($k) ?: $v))
118 : self::get_item($name.'[replace]', 'google_font')
119 ), $html);
120 }
121
122 return $html;
123 }
124
125 return wpjam_map($map, fn($v)=> ['placeholder'=>'请输�
126 �'.str_replace('//', '', $v).'加速服务地址']);
127 }
128
129 public static function avatar_data($id_or_email, $args){
130 if(is_numeric($id_or_email)){
131 $user_id = $id_or_email;
132 }elseif(is_string($id_or_email)){
133 $email = $id_or_email;
134 }elseif(is_object($id_or_email)){
135 if(isset($id_or_email->comment_ID)){
136 $comment = get_comment($id_or_email);
137 $user_id = $comment->user_id;
138 $email = $comment->comment_author_email;
139 $avatarurl = get_comment_meta($comment->comment_ID, 'avatarurl', true);
140 }elseif($id_or_email instanceof WP_User){
141 $user_id = $id_or_email->ID;
142 }elseif($id_or_email instanceof WP_Post){
143 $user_id = $id_or_email->post_author;
144 }
145 }
146
147 $user_id ??= 0;
148 $email ??= '';
149 $avatarurl = !empty($avatarurl) ? $avatarurl : ($user_id ? get_user_meta($user_id, 'avatarurl', true) : '');
150
151 if($avatarurl){
152 return ['found_avatar'=>true, 'url'=>wpjam_get_thumbnail($avatarurl, $args)];
153 }
154
155 $name = self::get_setting('gravatar');
156 $value = $name == 'custom' ? self::get_setting('gravatar_custom') : ($name ? self::get_item($name.'[url]', 'gravatar') : '');
157
158 $value && add_filter('get_avatar_url', fn($url)=> str_replace(array_map(fn($v)=>$v.'gravatar.com/avatar/', ['https://secure.', 'http://0.', 'http://1.', 'http://2.']), $value, $url));
159
160 return ['user_id'=>$user_id, 'email'=>$email];
161 }
162
163 public static function disabled($feature, ...$args){
164 return self::get_setting('disable_'.$feature, ...$args);
165 }
166
167 public static function init(){
168 wpjam_map(['trackbacks'=>'tb', 'embed'=>'embed'], fn($v, $k)=> self::disabled($k) && $GLOBALS['wp']->remove_query_var($v));
169
170 wpjam_map(['trackbacks', 'revisions'], fn($v)=> self::disabled($v, 1) && wpjam_map(['post', 'page'], fn($pt)=> remove_post_type_support($pt, $v)));
171 }
172
173 public static function add_hooks(){
174 add_action('wp_loaded', fn()=> ob_start(fn($html)=> apply_filters('wpjam_html', static::google_font($html))));
175
176 self::add_items([
177 'https://cdnjs.cloudflare.com/ajax/libs',
178 'https://s4.zstatic.net/ajax/libs',
179 'https://cdnjs.snrat.com/ajax/libs',
180 'https://lib.baomitu.com',
181 'https://cdnjs.loli.net/ajax/libs',
182 'https://use.sevencdn.com/ajax/libs',
183 ], 'static_cdn');
184
185 self::add_items([
186 'geekzu' => ['title'=>'极客族', 'url'=>'https://sdn.geekzu.org/avatar/'],
187 'loli' => ['title'=>'loli', 'url'=>'https://gravatar.loli.net/avatar/'],
188 'sep_cc' => ['title'=>'sep.cc', 'url'=>'https://cdn.sep.cc/avatar/'],
189 '7ed' => ['title'=>'7ED', 'url'=>'https://use.sevencdn.com/avatar/'],
190 'cravatar' => ['title'=>'Cravatar', 'url'=>'https://cravatar.cn/avatar/'],
191 ], 'gravatar');
192
193 self::add_items([
194 'geekzu' => ['title'=>'极客族', 'replace'=>['//fonts.geekzu.org', '//gapis.geekzu.org/ajax', '//gapis.geekzu.org/g-themes', '//gapis.geekzu.org/g-fonts']],
195 'loli' => ['title'=> 'loli', 'replace'=>['//fonts.loli.net', '//ajax.loli.net', '//themes.loli.net', '//gstatic.loli.net']],
196 'ustc' => ['title'=>'中科大', 'replace'=>['//fonts.lug.ustc.edu.cn', '//ajax.lug.ustc.edu.cn', '//google-themes.lug.ustc.edu.cn', '//fonts-gstatic.lug.ustc.edu.cn']]
197 ], 'google_font');
198
199 // 屏蔽站点 Feed
200 if(self::disabled('feed')){
201 add_action('template_redirect', fn()=> is_feed() && wp_die('Feed已经�
202 �闭, 请访问<a href="'.get_bloginfo('url').'">网站首页</a>!', 'Feed�
203 �闭', 200));
204 }
205
206 // 移除 WP_Head 版本号和服务发现标签代码
207 if(self::disabled('head_links')){
208 wpjam_hooks('-', [
209 ['wp_head', ['rsd_link', 'wlwmanifest_link', 'feed_links_extra', 'index_rel_link', 'parent_post_rel_link', 'start_post_rel_link', 'adjacent_posts_rel_link_wp_head','wp_shortlink_wp_head', 'rest_output_link_wp_head']],
210 ['template_redirect', ['wp_shortlink_header', 'rest_output_link_header']]
211 ]);
212
213 wpjam_hooks([
214 ['the_generator', ''],
215 ['style_loader_src, script_loader_src', fn($src)=> preg_replace('/[\&\?]ver='.preg_quote($GLOBALS['wp_version']).'(&|$)/', '', $src ?: '')]
216 ]);
217 }
218
219 // 屏蔽WordPress大小写修正
220 if(self::disabled('capital_P_dangit', 1)){
221 wpjam_hooks('-', 'the_content, the_title, wp_title, document_title, comment_text, widget_text_content', 'capital_P_dangit');
222 }
223
224 // 屏蔽字符转码
225 if(self::disabled('texturize', 1)){
226 wpjam_hook('run_wptexturize', false);
227 }
228
229 //移除 admin bar
230 if(self::disabled('admin_bar')){
231 wpjam_hook('show_admin_bar', false);
232 }
233
234 //禁用 XML-RPC 接口
235 if(self::disabled('xml_rpc', 1)){
236 wpjam_hooks([
237 ['xmlrpc_enabled', false],
238 ['xmlrpc_methods', []],
239 ['-', 'xmlrpc_rsd_apis', 'rest_output_rsd']
240 ]);
241 }
242
243 //禁用 REST API 批量处理端点
244 if(self::disabled('batch_rest')){
245 wpjam_hook('rest_pre_dispatch', 'maybe', fn($server, $request)=> str_starts_with(strtolower(untrailingslashit($request->get_route())), '/batch/') ? new WP_Error('rest_batch_disabled', '批量 API 已被禁用', ['status'=>401]) : null, 0, 3);
246 }
247
248 // 屏蔽小工�
249 �区块编辑器模式
250 if(self::disabled('widgets_block_editor')){
251 wpjam_hooks('gutenberg_use_widgets_block_editor, use_widgets_block_editor', false);
252 }
253
254 // 屏蔽站点管理员邮箱验证功能
255 if(self::disabled('admin_email_check')){
256 wpjam_hook('admin_email_check_interval', 0);
257 }
258
259 // 屏蔽 Emoji
260 if(self::disabled('emoji', 1)){
261 add_action('admin_init', fn()=> wpjam_hooks('-', [
262 ['admin_print_scripts', 'print_emoji_detection_script'],
263 ['admin_print_styles', 'print_emoji_styles']
264 ]));
265
266 wpjam_hooks('-', [
267 ['wp_head, embed_head', 'print_emoji_detection_script'],
268 ['wp_print_styles', 'print_emoji_styles'],
269 ['wp_mail', 'wp_staticize_emoji_for_email'],
270
271 ['the_content_feed, comment_text_rss', 'wp_staticize_emoji'],
272
273 ]);
274
275 wpjam_hooks([
276 ['emoji_svg_url', ''],
277 ['tiny_mce_plugins', '-', ['wpemoji']]
278 ]);
279 }
280
281 //禁用文章修订功能
282 if(self::disabled('revisions', 1)){
283 defined('WP_POST_REVISIONS') || define('WP_POST_REVISIONS', false);
284
285 wpjam_hook('-', 'pre_post_update', 'wp_save_post_revision');
286 }
287
288 // 屏蔽Trackbacks
289 if(self::disabled('trackbacks', 1)){
290 if(self::disabled('xml_rpc', 1)){ //彻底�
291 �闭 pingback
292 wpjam_hook('xmlrpc_methods', '-', ['pingback.ping', 'pingback.extensions.getPingbacks']);
293 }
294
295 wpjam_hooks('-', [
296 ['do_pings', 'do_all_pings'], //禁用 pingbacks, enclosures, trackbacks
297 ['publish_post', '_publish_post_hook'] //去掉 _encloseme 和 do_ping 操作。
298 ]);
299 }
300
301 //禁用 Auto OEmbed
302 if(self::disabled('autoembed')){
303 wpjam_hooks('-', [
304 ['edit_form_advanced, edit_page_form', [$GLOBALS['wp_embed'], 'maybe_run_ajax_cache']],
305 ['the_content, widget_text_content, widget_block_content', [$GLOBALS['wp_embed'], 'autoembed']]
306 ]);
307 }
308
309 // 屏蔽文章Embed
310 if(self::disabled('embed')){
311 wpjam_hooks('-', 'wp_head', ['wp_oembed_add_discovery_links', 'wp_oembed_add_host_js']);
312 }
313
314 // 屏蔽自动更新和更新检查作业
315 if(self::disabled('auto_update')){
316 wpjam_hook('automatic_updater_disabled', true);
317
318 wpjam_hooks('-', [
319 ['init', 'wp_schedule_update_checks'],
320 ...array_map(fn($v)=> [$v, $v], ['wp_version_check', 'wp_update_plugins', 'wp_update_themes'])
321 ]);
322 }
323
324 // 屏蔽后台隐私
325 if(self::disabled('privacy', 1)){
326 wpjam_hooks('-', [
327 ['user_request_action_confirmed', ['_wp_privacy_account_request_confirmed', '_wp_privacy_send_request_confirmation_notification']],
328 ['wp_privacy_personal_data_exporters', ['wp_register_comment_personal_data_exporter', 'wp_register_media_personal_data_exporter', 'wp_register_user_personal_data_exporter']],
329 ['wp_privacy_personal_data_erasers', 'wp_register_comment_personal_data_eraser'],
330 ['init', 'wp_schedule_delete_old_privacy_export_files'],
331 ['wp_privacy_delete_old_export_files', 'wp_privacy_delete_old_export_files']
332 ]);
333
334 wpjam_hook('option_wp_page_for_privacy_policy', 0);
335 }
336
337 wpjam_hooks('style_loader_src, script_loader_src', [static::class, 'static_cdn']);
338
339 wpjam_hook('pre_get_avatar_data', '+', [self::class, 'avatar_data'], 10, 2);
340
341 if($options = self::get_setting('x-frame-options')){
342 wpjam_hook('send_headers', fn()=> header('X-Frame-Options: '.$options));
343 }
344
345 // 防止重名造成大量的 SQL
346 if($ts_name = self::get_setting('timestamp_file_name')){
347 wpjam_hooks('wp_handle_sideload_prefilter, wp_handle_upload_prefilter', '+', fn($file)=> empty($file['md5_filename']) ? ['name'=> time().'-'.$file['name']] : []);
348 }
349
350 if(self::get_setting('no_category_base') && ($no_base = self::get_setting('no_category_base_for', 'category'))){
351 wpjam_hook('register_taxonomy_args', '+', fn($name)=> $name == $no_base ? ['permastruct'=>'%'.$name.'%'] : [], 8, 2);
352
353 if($no_base == 'category' && try_prefix($_SERVER['REQUEST_URI'], '-', '/category/')){
354 wpjam_hook('template_redirect', fn()=> wp_redirect(site_url($_SERVER['REQUEST_URI']), 301));
355 }
356 }
357
358 if(is_admin()){
359 if(self::disabled('auto_update')){
360 wpjam_hooks('-', 'admin_init', ['_maybe_update_core', '_maybe_update_plugins', '_maybe_update_themes']);
361 }
362
363 if(self::disabled('block_editor')){
364 wpjam_hook('use_block_editor_for_post_type', false);
365 }
366
367 if(self::disabled('help_tabs')){
368 add_action('in_admin_header', fn()=> $GLOBALS['current_screen']->remove_help_tabs());
369 }
370
371 if(self::disabled('screen_options')){
372 wpjam_hooks([
373 ['screen_options_show_screen', false],
374 ['hidden_columns', []]
375 ]);
376 }
377
378 if(self::disabled('privacy', 1)){
379 add_action('admin_menu', fn()=> wpjam_call_multiple('remove_submenu_page', [
380 ['options-general.php', 'options-privacy.php'],
381 ['tools.php', 'export-personal-data.php'],
382 ['tools.php', 'erase-personal-data.php']
383 ]), 11);
384
385 add_action('admin_init', fn()=> wpjam_hooks('-', [
386 ['admin_init', ['WP_Privacy_Policy_Content', 'text_change_check']],
387 ['edit_form_after_title', ['WP_Privacy_Policy_Content', 'notice']],
388 ['admin_init', ['WP_Privacy_Policy_Content', 'add_suggested_content']],
389 ['post_updated', ['WP_Privacy_Policy_Content', '_policy_page_updated']],
390 ['list_pages', '_wp_privacy_settings_filter_draft_page_titles'],
391 ]), 1);
392 }
393
394 if(self::disabled('dashboard_primary')){
395 add_action('do_meta_boxes', fn($screen, $context)=> remove_meta_box('dashboard_primary', $screen, $context), 10, 2);
396 }
397 }
398 }
399 }
400
401 wpjam_register_option('wpjam-basic', [
402 'title' => '优化设置',
403 'model' => 'WPJAM_Basic',
404 'summary' => __FILE__,
405 'site_default' => true,
406 'menu_page' => ['menu_title'=>'WPJAM', 'sub_title'=>'优化设置', 'icon'=>'ri-rocket-fill', 'position'=>'58.99']
407 ]);
408
409 function wpjam_basic_get_setting($name, ...$args){
410 return WPJAM_Basic::get_setting($name, ...$args);
411 }
412
413 function wpjam_basic_update_setting($name, $value){
414 return WPJAM_Basic::update_setting($name, $value);
415 }
416
417 function wpjam_basic_delete_setting($name){
418 return WPJAM_Basic::delete_setting($name);
419 }
420
421 function wpjam_add_basic_sub_page($sub_slug, $args=[]){
422 wpjam_add_menu_page($sub_slug, ['parent'=>'wpjam-basic']+$args);
423 }
424
425 function wpjam_register_gravatar($name, $args){
426 return WPJAM_Basic::add_item($name, $args, 'gravatar');
427 }
428
429 function wpjam_register_google_font($name, $args){
430 return WPJAM_Basic::add_item($name, $args, 'google_font');
431 }
432
433 function wpjam_add_static_cdn($host){
434 return WPJAM_Basic::add_item(null, $host, 'static_cdn');
435 }
436
437 function wpjam_get_static_cdn(){
438 return WPJAM_Basic::static_cdn();
439 }
440