-
Notifications
You must be signed in to change notification settings - Fork 4
/
wp-admin-boost.php
301 lines (273 loc) · 12.1 KB
/
wp-admin-boost.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Plugin Name: WP Admin Boost
* Description: 使用jsdelivr加速WordPress的后台核心小文件与插件小文件,大幅提高后台访问速度。
* Author: 潘羿
* Author URI:https://www.idleleo.com/
* Version: 1.0.3
* Network: True
* License: GPLv3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
(new WP_ADMIN_BOOST)->init();
class WP_ADMIN_BOOST
{
private $page_url;
public function __construct()
{
$this->page_url = network_admin_url(is_multisite() ? 'settings.php?page=wp-admin-boost' : 'options-general.php?page=wp-admin-boost');
}
public function init()
{
if (is_admin() && !(defined('DOING_AJAX') && DOING_AJAX)) {
add_filter(sprintf('%splugin_action_links_%s', is_multisite() ? 'network_admin_' : '', plugin_basename(__FILE__)), function ($links) {
return array_merge(
[sprintf('<a href="%s">%s</a>', $this->page_url, '设置')],
$links
);
});
update_option("wpab_admin", get_option('wpab_admin') ?: '2');
update_option("wpab_admin_googlefonts", get_option('wpab_admin_googlefonts') ?: '2');
update_option("wpab_admin_googleajax", get_option('wpab_admin_googleajax') ?: '2');
update_option("wpab_admin_plugin", get_option('wpab_admin_plugin') ?: '2');
update_option("wpab_block_activate_plugin", get_option('wpab_block_activate_plugin') ?: '');
register_deactivation_hook(__FILE__, function () {
delete_option("wpab_admin");
delete_option("wpab_admin_googlefonts");
delete_option("wpab_admin_googleajax");
delete_option("wpab_admin_plugin");
delete_option("wpab_block_activate_plugin");
});
add_action(is_multisite() ? 'network_admin_menu' : 'admin_menu', function () {
add_submenu_page(
is_multisite() ? 'settings.php' : 'options-general.php',
'WP Admin Boost',
'WP Admin Boost',
is_multisite() ? 'manage_network_options' : 'manage_options',
'wp-admin-boost',
[$this, 'options_page_html']
);
});
if (get_option('wpab_admin') != 2 && !stristr($GLOBALS['wp_version'], 'alpha') && !stristr($GLOBALS['wp_version'], 'beta')) {
add_action('init', function () {
ob_start(function ($buffer) {
$buffer = preg_replace('~' . home_url('/') . '(wp-admin|wp-includes)/(css|js)/~', sprintf('//cdn.jsdelivr.net/gh/WordPress/WordPress@%s/$1/$2/', $GLOBALS['wp_version']), $buffer);
return $buffer;
});
});
};
if (!(defined('DOING_AJAX') && DOING_AJAX)) {
if (get_option('wpab_admin_googlefonts') != 2) {
$this->page_str_replace('str_replace', ['fonts.googleapis.com', 'fonts.font.im'], get_option('wpab_admin_googlefonts'));
}
if (get_option('wpab_admin_googleajax') != 2) {
$this->page_str_replace('str_replace', ['ajax.googleapis.com', 'ajax.proxy.ustclug.org'], get_option('wpab_admin_googleajax'));
}
}
if (get_option('wpab_admin_plugin') == 1) {
add_action('init', 'panyi_admin_speedup');
function panyi_admin_speedup()
{
ob_start(function ($buffer) {
$apl = get_option('active_plugins');
$plugins = get_plugins();
if (get_option('wpab_block_activate_plugin')) {
$block_speedup = explode(",", get_option('wpab_block_activate_plugin'));
}
foreach ($apl as $p) {
if (isset($plugins[$p])) {
$path = explode("/", $p);
if (in_array($path[0], $block_speedup)) {
continue;
}
$buffer = preg_replace('~' . home_url('/') . 'wp-content/plugins/' . $path[0] . '/(.*).(css|js|woff|woff2|jpg|png|gif|svg|webp)~', sprintf('//cdn.jsdelivr.net/wp/plugins/' . $path[0] . '/tags/%s/$1.$2', $plugins[$p]['Version']), $buffer);
}
}
return $buffer;
});
}
}
}
if (is_admin()) {
add_action('admin_init', function () {
register_setting('wpab', 'wpab_admin');
register_setting('wpab', 'wpab_admin_googlefonts');
register_setting('wpab', 'wpab_admin_googleajax');
register_setting('wpab', 'wpab_admin_plugin');
register_setting('wpab', 'wpab_block_activate_plugin');
add_settings_section(
'wpab_section_main',
'设置',
[$this, 'field_section_main'],
'wpab'
);
add_settings_field(
'wpab_field_select_admin',
'加速管理后台',
[$this, 'field_admin'],
'wpab',
'wpab_section_main'
);
add_settings_field(
'wpab_field_select_googlefonts',
'加速谷歌字体',
[$this, 'field_admin_googlefonts'],
'wpab',
'wpab_section_main'
);
add_settings_field(
'wpab_field_select_googleajax',
'加速谷歌前端公共库',
[$this, 'field_admin_googleajax'],
'wpab',
'wpab_section_main'
);
add_settings_field(
'wpab_field_select_admin_plugin',
'加速后台插件',
[$this, 'field_admin_plugin'],
'wpab',
'wpab_section_main'
);
add_settings_field(
'wpab_field_select_block_plugin',
'禁用加速插件',
[$this, 'field_select_block_plugin'],
'wpab',
'wpab_section_main'
);
});
}
}
public function field_section_main()
{
?>
<p class="description">使用jsdelivr提供的CDN加速WordPress后台,包括核心小文件与插件小文件</p>
<?php
}
public function field_admin()
{
$this->field_cb('wpab_admin', '将WordPress核心所依赖的静态文件切换为公共资源,此选项极大的加快管理后台访问速度', true);
}
public function field_admin_googlefonts()
{
$this->field_cb('wpab_admin_googlefonts', '将加速谷歌字体,使用谷歌在中国的服务,<b>未包含谷歌字体没必要使用</b>', true);
}
public function field_admin_googleajax()
{
$this->field_cb('wpab_admin_googleajax', '将加速谷歌前端公共库,使用中科大的服务,<b>未包含谷歌前端公共库没必要使用</b>', true);
}
public function field_admin_plugin()
{
$this->field_cb('wpab_admin_plugin', '将所有激活的插件小文件切换为公共资源,进一步加快管理后台访问速度,<b>注意!请仔细检查是否兼容</b>', true);
}
public function field_select_block_plugin()
{
$ntap = explode(",", get_option('wpab_block_activate_plugin'));
$apl = get_option('active_plugins');
$plugins = get_plugins();
?>
<p class="description">
请选择需要<b>禁用</b>加速的插件:
</p>
<table>
<thead>
<tr>
<th>
插件名
</th>
<th>
版本号
</th>
<th>
禁用
</th>
</tr>
</thead>
<tbody>
<?php
foreach ($apl as $p) {
$path = explode("/", $p);
if (isset($plugins[$p])) {
?>
<tr>
<td><?php echo $plugins[$p]['Name']; ?></td>
<td><?php echo $plugins[$p]['Version']; ?></td>
<td><label><input type="checkbox" value="<?php echo $path[0]; ?>" name="block_activate_plugin[]" <?php if (in_array($path[0], $ntap)) {
echo 'checked="checked"';
} ?>></label></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php
}
private function field_cb($option_name, $description, $is_global = false)
{
$option_value = get_option($option_name);
?>
<label>
<input type="radio" value="1" name="<?php echo $option_name; ?>" <?php checked($option_value, '1'); ?>><?php echo $is_global ? '启用  ' : '全局启用  ' ?>
</label>
<label>
<input type="radio" value="2" name="<?php echo $option_name; ?>" <?php checked($option_value, '2'); ?>>禁用
</label>
<p class="description">
<?php echo $description; ?>
</p>
<?php
}
/**
* @param $replace_func string 要调用的字符串关键字替换函数
* @param $param array 传递给字符串替换函数的参数
* @param $level int 替换级别:1.全局替换 3.前台替换 4.后台替换
*/
private function page_str_replace($replace_func, $param, $level)
{
if ($level == 3 && is_admin()) {
return;
} elseif ($level == 4 && !is_admin()) {
return;
}
add_action('init', function () use ($replace_func, $param) {
ob_start(function ($buffer) use ($replace_func, $param) {
$param[] = $buffer;
return call_user_func_array($replace_func, $param);
});
});
}
public function options_page_html()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
update_option("wpab_admin", sanitize_text_field($_POST['wpab_admin']));
update_option("wpab_admin_googlefonts", sanitize_text_field($_POST['wpab_admin_googlefonts']));
update_option("wpab_admin_googleajax", sanitize_text_field($_POST['wpab_admin_googleajax']));
update_option("wpab_admin_plugin", sanitize_text_field($_POST['wpab_admin_plugin']));
update_option("wpab_block_activate_plugin", sanitize_text_field(implode(',', $_POST['block_activate_plugin'])));
echo '<div class="notice notice-success settings-error is-dismissible"><p><strong>设置已保存</strong></p></div>';
}
if (!current_user_can('manage_options')) {
return;
}
settings_errors('wpab_messages');
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="<?php echo $this->page_url; ?>" method="post">
<?php
settings_fields('wpab');
do_settings_sections('wpab');
submit_button('保存配置');
?>
</form>
</div>
<p style="text-align:right;">
欢迎访问<a href="https://www.idleleo.com/" target="_blank">无主界</a>!获取更多的WordPress技巧。<br />
感谢<a href="https://wp-china.org" target="_blank">WP中国本土化社区</a>的源代码。
</p>
<?php
}
}