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
|
<?php /** * A parent class for those modules that offers a piece of code to * setup the server (gzip and caching) * * @package Hummingbird\Core */
namespace Hummingbird\Core;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Module_Server extends Module */ abstract class Module_Server extends Module {
/** * Module slug (used in transient). * * @var bool|string $transient_slug */ protected $transient_slug = false;
/** * Module status. * * @var array $status */ protected $status;
/** * Execute the module actions. It must be defined in subclasses. Executed when module is active. */ public function run() {}
/** * Initializes the module. Always executed even if the module is deactivated. * * Do not use __construct in subclasses, use init() instead */ public function init() {}
/** * Return the analyzed data for the module * * @param bool $force If set to true, cache will be cleared before getting the data. * @param bool $check_api If set to true, the api will be checked. * * @return mixed Analysis data */ public function get_analysis_data( $force = false, $check_api = false ) { if ( ! $this->transient_slug ) { return false; }
$transient = 'wphb-' . $this->transient_slug . '-data';
$this->status = get_site_option( $transient );
if ( $force || ! $this->status || $check_api ) { $this->clear_cache();
$this->status = $this->analyze_data( $check_api );
update_site_option( $transient, $this->status );
return $this->status; }
return $this->status; }
/** * Analyze the data * * @param bool $check_api If set to true, the api will be checked. * * @return mixed */ abstract protected function analyze_data( $check_api = false );
/** * Implement abstract parent method for clearing cache. */ public function clear_cache() { delete_site_option( 'wphb-' . $this->transient_slug . '-data' ); }
/** * Get the server code snippet * * @param string $server Server name (nginx,apache...). * @param array $expiry_times Type expiry times (javascript, css...). * * @return string */ public function get_server_code_snippet( $server, $expiry_times = array() ) { $method = 'get_' . str_replace( array( '-', ' ' ), '', strtolower( $server ) ) . '_code'; if ( ! method_exists( $this, $method ) ) { return ''; }
return call_user_func_array( array( $this, $method ), array( $expiry_times ) ); }
/** * Return the server type (Apache, NGINX...) * * @return string Server type */ public static function get_server_type() { global $is_apache, $is_IIS, $is_iis7, $is_nginx;
$type = '';
if ( $is_apache ) { // It's a common configuration to use nginx in front of Apache. // Let's make sure that this server is Apache. $response = wp_remote_get( home_url() );
if ( is_wp_error( $response ) ) { // Bad luck. $type = 'apache'; } else { $server = strtolower( wp_remote_retrieve_header( $response, 'server' ) ); // Could be LiteSpeed too. $type = strpos( $server, 'nginx' ) !== false ? 'nginx' : 'apache'; } } elseif ( $is_nginx ) { $type = 'nginx'; } elseif ( $is_IIS ) { $type = 'IIS'; } elseif ( $is_iis7 ) { $type = 'IIS 7'; }
return apply_filters( 'wp_hummingbird_is_active_module_cloudflare', $type ); }
/** * Get a list of server types * * @return array */ public static function get_servers() { return array( 'apache' => 'Apache', 'nginx' => 'NGINX', 'iis' => 'IIS', 'cloudflare' => 'Cloudflare', ); }
/** * Get code snippet for a module and server type * * @param string $module Module name. * @param string $server_type Server type (nginx, apache...). * @param array $expiry_times Type expiry times (javascript, css...). * * @return string Code snippet */ public static function get_code_snippet( $module, $server_type = '', $expiry_times = array() ) { $module = Utils::get_module( $module ); if ( ! $module ) { return ''; }
if ( ! $server_type ) { $server_type = self::get_server_type(); }
return apply_filters( 'wphb_code_snippet', $module->get_server_code_snippet( $server_type, $expiry_times ), $server_type, $module ); }
/** * Check if .htaccess is writable. * * @return bool */ public static function is_htaccess_writable() { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; }
$home_path = get_home_path(); return ( ! file_exists( $home_path . '.htaccess' ) && is_writable( $home_path ) ) || is_writable( $home_path . '.htaccess' ); }
/** * Check if .htaccess has Hummingbird caching or gzip rules in place. * * @param string $module Module slug. * * @return bool */ public static function is_htaccess_written( $module = '' ) { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; }
if ( ! function_exists( 'extract_from_markers' ) ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; }
$existing_rules = array_filter( extract_from_markers( get_home_path() . '.htaccess', 'WP-HUMMINGBIRD-' . strtoupper( $module ) ) ); return ! empty( $existing_rules ); }
/** * Add rules .htaccess file. * * @param string $module Gzip or caching module. * * @return bool */ public static function save_htaccess( $module ) { if ( self::is_htaccess_written( $module ) ) { return false; }
$htaccess_file = get_home_path() . '.htaccess';
if ( self::is_htaccess_writable() ) { $code = self::get_code_snippet( $module, 'apache' ); $code = explode( "\n", $code ); return insert_with_markers( $htaccess_file, 'WP-HUMMINGBIRD-' . strtoupper( $module ), $code ); }
return false; }
/** * Remove rules from .htaccess file. * * @param string $module Module. * * @return bool */ public static function unsave_htaccess( $module ) { if ( ! self::is_htaccess_written( $module ) ) { return false; }
$htaccess_file = get_home_path() . '.htaccess';
if ( self::is_htaccess_writable() ) { return insert_with_markers( $htaccess_file, 'WP-HUMMINGBIRD-' . strtoupper( $module ), '' ); }
return false; }
}
|