-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuper-static-cache-admin.php
304 lines (277 loc) · 11.4 KB
/
super-static-cache-admin.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
302
303
304
<?php
/*后台管理界面*/
/*最后更新 2017年5月*/
//展示菜单
function display_cache_menu(){
add_options_page('Super Static Cache', 'Super Static Cache', 'manage_options','Super-Static-Cache', 'show_cache_manage');
}
function show_cache_manage(){
do_update_actions();
require_once(dirname(__FILE__).'/options.php');
}
add_action('admin_menu', 'display_cache_menu');
//增加管理链接
function ssc_action_links($links,$pluginfile){
if($pluginfile == 'super-static-cache/super-static-cache.php'){
$link=array(
'<a href="'. get_admin_url(null, 'options-general.php?page=Super-Static-Cache') .'">'.__('Settings','super_static_cache').'</a>'
);
$links = array_merge($link, $links);
}
return $links;
}
add_filter('plugin_action_links', 'ssc_action_links',10,2);
//增加其它配置连接
function ssc_row_meta($links,$pluginfile){
if($pluginfile == 'super-static-cache/super-static-cache.php'){
$link=array(
'<a href="'. get_admin_url(null, 'options-general.php?page=Super-Static-Cache') .'">'.__('Settings','super_static_cache').'</a>',
'<a href="https://www.hitoy.org/super-static-cache-for-wordperss.html">'.__('Support','super_static_cache').'</a>',
'<a href="https://www.hitoy.org/super-static-cache-for-wordperss.html#Donations">'.__('Donate','super_static_cache').'</a>'
);
$links = array_merge($links,$link);
}
return $links;
}
add_filter('plugin_row_meta','ssc_row_meta',10,2);
//判断伪静态是否配置好
function is_rewrite_ok(){
global $wpssc;
if($wpssc->iscompress){
if(@fopen($wpssc->siteurl."/rewrite_ok.html","r")){
return true;
}
}else{
if(@fopen($wpssc->siteurl."/rewrite_ok.txt","r")){
return true;
}
}
return false;
}
//获取web服务器类型
function getwebserver(){
$software=strtolower($_SERVER["SERVER_SOFTWARE"]);
switch ($software){
case strstr($software,"nginx"):
return "nginx";
break;
case strstr($software,"apache"):
return "apache";
break;
case strstr($software,"iis"):
return "iis";
break;
default:
return "unknown";
}
}
//获取WP安装目录
function getwpinstallpath(){
global $wpssc;
return "/".substr($wpssc->wppath,strlen($wpssc->docroot));
}
//自动更新apache htaccess规则
function insert_htaccess($rule){
global $wpssc;
$raw_rules = file_get_contents($wpssc->wppath.".htaccess");
$rules = $rule."\n".$raw_rules;
if(!file_put_contents($wpssc->wppath.".htaccess",$rules,LOCK_EX)) return false;
return true;
}
//设置伪静态规则
function getrewriterule($escape=true){
if(is_rewrite_ok()) return false;
$cachemod=get_option("super_static_cache_mode");
$compress=get_option("super_static_cache_compress");
$webscr=getwebserver();
$rules=false;
$httpdcompressflushreule="\n<IfModule mod_headers.c>\n<FilesMatch \"\.(html|txt)\.gz$\">\nheader set Content-Encoding gzip\nheader set Content-Type text/html\n</FilesMatch>\n</Ifmodule>\n#End Super Static Cache\n";
$httpdrewriterule="#BEGIN Super Static Cache\n#Must the First Rewrite Rule\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase {wp_install_dir}\nRewriteRule ^super-static-cache/ - [L]\n\nRewriteCond %{REQUEST_METHOD} !POST\nRewriteCond %{QUERY_STRING} !.*=.*\nRewriteCond %{DOCUMENT_ROOT}{wp_install_dir}super-static-cache/$1{iscompressed} -f\nRewriteRule ^(.*)$ {wp_install_dir}super-static-cache/$1{iscompressed} [L]\n\nRewriteCond %{REQUEST_METHOD} !POST\nRewriteCond %{QUERY_STRING} !.*=.*\nRewriteCond %{DOCUMENT_ROOT}{wp_install_dir}super-static-cache/$1/index.html{iscompressed} -f\nRewriteRule ^(.*)$ {wp_install_dir}super-static-cache/$1/index.html{iscompressed} [L]\n</IfModule>\n";
$nginxcompressstatic = "#ngx_http_gzip_static_module and ngx_http_gunzip_module Must Be Added To Nginx\n gzip_static always;\n gunzip on;\n";
$nginxrewriterule='#BEGIN Super Static Cache
location {wp_install_dir} {
{wp_gzip_static_flush}
if (-f $request_filename) {
break;
}
if ($uri ~ {wp_install_dir}(.*)$){
set $wpuri $1;
set $sscfile $document_root{wp_install_dir}super-static-cache/$1;
}
set $ssc Y;
if ($query_string !~ .*=.*){
set $ssc "${ssc}Y";
}
if ($request_method != "POST"){
set $ssc "${ssc}Y";
}
if (-f $sscfile{iscompressed}){
set $ssc "${ssc}F";
}
if (-f $sscfile/index.html{iscompressed}){
set $ssc "${ssc}I";
}
if ($ssc = YYYF){
rewrite . {wp_install_dir}super-static-cache/$wpuri break;
}
if ($ssc = YYYI){
rewrite . {wp_install_dir}super-static-cache/$wpuri/index.html break;
}
if (!-e $request_filename){
rewrite . {wp_install_dir}index.php last;
}
}
#End Super Static Cache';
//Apache规则
if($cachemod == 'serverrewrite' && $webscr == 'apache' && $compress == true){
$httpdrewriterule=str_replace('{wp_install_dir}',getwpinstallpath(),$httpdrewriterule);
$httpdrewriterule=str_replace('{iscompressed}','.gz',$httpdrewriterule);
if($escape){
$rules=htmlentities($httpdrewriterule.$httpdcompressflushreule);
}else{
$rules=$httpdrewriterule.$httpdcompressflushreule;
}
}else if($cachemod == 'serverrewrite' && $webscr == 'apache' && $compress == false){
$httpdrewriterule=str_replace('{wp_install_dir}',getwpinstallpath(),$httpdrewriterule);
$httpdrewriterule=str_replace('{iscompressed}','',$httpdrewriterule);
if($escape){
$rules=htmlentities($httpdrewriterule.$httpdcompressflushreule);
}else{
$rules=$httpdrewriterule.$httpdcompressflushreule;
}
}
//Nginx规则
else if($cachemod == 'serverrewrite' && $webscr == 'nginx' && $compress == true){
$nginxrewriterule=str_replace('{wp_install_dir}',getwpinstallpath(),$nginxrewriterule);
$nginxrewriterule=str_replace('{iscompressed}','.gz',$nginxrewriterule);
$nginxrewriterule=str_replace('{wp_gzip_static_flush}',$nginxcompressstatic,$nginxrewriterule);
if($escape){
$rules=htmlentities($nginxrewriterule);
}else{
$rules=$nginxrewriterule;
}
}else if($cachemod == 'serverrewrite' && $webscr == 'nginx' && $compress == false){
$nginxrewriterule=str_replace('{wp_install_dir}',getwpinstallpath(),$nginxrewriterule);
$nginxrewriterule=str_replace('{iscompressed}','',$nginxrewriterule);
$nginxrewriterule=str_replace('{wp_gzip_static_flush}','',$nginxrewriterule);
if($escape){
$rules=htmlentities($nginxrewriterule);
}else{
$rules=$nginxrewriterule;
}
}else {
$rules = __('Your server type is not detected, Please visit https://www.hitoy.org/super-static-cache-for-wordperss.html for help.','super-static-cache');
}
return $rules;
}
//清除文章/单页缓存函数
//参数为id或者标题
function clear_post_cache($args){
global $wpssc,$wpdb;
if($args*1 > 0){
$link=get_permalink($args);
$wpssc->delete_cache($link);
}else if(strlen($args) > 2){
$postres=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_title like '%".$args."%' LIMIT 0,1 ");
$link = get_permalink($postres[0]->ID);
$wpssc->delete_cache($link);
}
}
//清除分类页面
function clear_category_cache(){
global $wpssc,$wpdb;
$sql="SELECT $wpdb->terms.term_id, name FROM $wpdb->terms LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id WHERE $wpdb->term_taxonomy.taxonomy = 'category'";
$cateobj = $wpdb->get_results($sql);
foreach($cateobj as $cate){
$link=get_category_link($cate->term_id);
$wpssc->delete_cache($link);
}
}
//清除tag页面
function clear_tag_cache(){
global $wpssc;
$tagobj=get_tags();
foreach($tagobj as $tag){
$link=get_tag_link($tag->term_id);
$wpssc->delete_cache($link);
}
}
//清除文章页/单页
function clear_post_page_cache($type){
global $wpssc,$wpdb;
$postres=$wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type='".$type."' and post_status='publish'");
foreach($postres as $post){
$link=get_permalink($post->ID);
$wpssc->delete_cache($link);
}
}
//清除首页缓存
function clear_home_cache(){
global $wpssc;
$wpssc->delete_cache($wpssc->siteurl."/index.html");
}
//更新配置
function do_update_actions(){
if(empty($_POST)) return;
if(!empty($_POST['super_static_cache_mode'])){
$super_static_cache_mode=trim($_POST['super_static_cache_mode']);
update_option('super_static_cache_mode',$super_static_cache_mode);
$super_static_cache_excet_arr=isset($_POST['super_static_cache_excet'])?$_POST['super_static_cache_excet']:array();
$super_static_cache_excet = implode(',',$super_static_cache_excet_arr);
update_option('super_static_cache_excet',$super_static_cache_excet);
$super_static_cache_strict=($_POST['super_static_cache_strict'] == "true")?true:false;
update_option('super_static_cache_strict',$super_static_cache_strict);
$super_static_cache_nocachesinglepage = $_POST['super_static_cache_nocachesinglepage'];
$super_static_cache_nocachesinglepage = str_replace("\r\n",",",$super_static_cache_nocachesinglepage);
update_option('super_static_cache_nocachesinglepage',$super_static_cache_nocachesinglepage);
//Apache 自动更新规则
if(getwebserver()=='apache' && !is_rewrite_ok()){
insert_htaccess(getrewriterule(false));
}
}
if(!empty($_POST['super_static_cache_compress'])){
$super_static_cache_compress = ($_POST['super_static_cache_compress'] == "true")?true:false;
update_option('super_static_cache_compress',$super_static_cache_compress);
}
if(!empty($_POST['update_cache_action_submit'])){
$update_cache_action_arr=isset($_POST['update_cache_action'])?$_POST['update_cache_action']:array();
$update_cache_action=implode(',',$update_cache_action_arr);
update_option('update_cache_action',$update_cache_action);
}
if(!empty($_POST['clearcache'])){
$del_type_arr=$_POST['clearcache'];
if(in_array('home',$del_type_arr)){
clear_home_cache();
}
if(in_array('single',$del_type_arr)){
clear_post_page_cache("post");
}
if(in_array('page',$del_type_arr)){
clear_post_page_cache("page");
}
if(in_array('category',$del_type_arr)){
clear_category_cache();
}
if(in_array('tag',$del_type_arr)){
clear_tag_cache();
}
if(in_array('all',$del_type_arr)){
global $wpssc;
if($wpssc->cachemod=="direct"){
clear_home_cache();
clear_category_cache();
clear_tag_cache();
clear_post_page_cache("post");
clear_post_page_cache("page");
}else if($wpssc->cachemod=="phprewrite" || $wpssc->cachemod=="serverrewrite"){
$wpssc->delete_cache($wpssc->siteurl."/");
}
}
}
if(!empty($_POST['clearpostpagecache'])){
$ids=explode(",",trim($_POST['clearpostpagecache']));
foreach($ids as $id){
clear_post_cache($id);
}
}
}