@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Critical CSS settings AJAX logic. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | { |
13 | 13 | // fetch all options at once and populate them individually explicitely as globals. |
14 | 14 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
15 | - foreach ( $all_options as $_option => $_value ) { |
|
15 | + foreach ($all_options as $_option => $_value) { |
|
16 | 16 | global ${$_option}; |
17 | 17 | ${$_option} = $_value; |
18 | 18 | } |
@@ -21,47 +21,47 @@ discard block |
||
21 | 21 | |
22 | 22 | public function run() { |
23 | 23 | // add filters. |
24 | - add_action( 'wp_ajax_fetch_critcss', array( $this, 'critcss_fetch_callback' ) ); |
|
25 | - add_action( 'wp_ajax_save_critcss', array( $this, 'critcss_save_callback' ) ); |
|
26 | - add_action( 'wp_ajax_rm_critcss', array( $this, 'critcss_rm_callback' ) ); |
|
27 | - add_action( 'wp_ajax_rm_critcss_all', array( $this, 'critcss_rm_all_callback' ) ); |
|
28 | - add_action( 'wp_ajax_ao_ccss_export', array( $this, 'ao_ccss_export_callback' ) ); |
|
29 | - add_action( 'wp_ajax_ao_ccss_import', array( $this, 'ao_ccss_import_callback' ) ); |
|
24 | + add_action('wp_ajax_fetch_critcss', array($this, 'critcss_fetch_callback')); |
|
25 | + add_action('wp_ajax_save_critcss', array($this, 'critcss_save_callback')); |
|
26 | + add_action('wp_ajax_rm_critcss', array($this, 'critcss_rm_callback')); |
|
27 | + add_action('wp_ajax_rm_critcss_all', array($this, 'critcss_rm_all_callback')); |
|
28 | + add_action('wp_ajax_ao_ccss_export', array($this, 'ao_ccss_export_callback')); |
|
29 | + add_action('wp_ajax_ao_ccss_import', array($this, 'ao_ccss_import_callback')); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | public function critcss_fetch_callback() { |
33 | 33 | // Ajax handler to obtain a critical CSS file from the filesystem. |
34 | 34 | // Check referer. |
35 | - check_ajax_referer( 'fetch_critcss_nonce', 'critcss_fetch_nonce' ); |
|
35 | + check_ajax_referer('fetch_critcss_nonce', 'critcss_fetch_nonce'); |
|
36 | 36 | |
37 | 37 | // Initialize error flag. |
38 | 38 | $error = true; |
39 | 39 | |
40 | 40 | // Allow no content for MANUAL rules (as they may not exist just yet). |
41 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
41 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
42 | 42 | $content = ''; |
43 | 43 | $error = false; |
44 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
44 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
45 | 45 | // Or check user permissios and filename. |
46 | 46 | // Set file path and obtain its content. |
47 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
48 | - if ( file_exists( $critcssfile ) ) { |
|
49 | - $content = file_get_contents( $critcssfile ); |
|
47 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
48 | + if (file_exists($critcssfile)) { |
|
49 | + $content = file_get_contents($critcssfile); |
|
50 | 50 | $error = false; |
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
54 | 54 | // Prepare response. |
55 | - if ( $error ) { |
|
55 | + if ($error) { |
|
56 | 56 | $response['code'] = '500'; |
57 | - $response['string'] = 'Error reading file ' . $critcssfile . '.'; |
|
57 | + $response['string'] = 'Error reading file '.$critcssfile.'.'; |
|
58 | 58 | } else { |
59 | 59 | $response['code'] = '200'; |
60 | 60 | $response['string'] = $content; |
61 | 61 | } |
62 | 62 | |
63 | 63 | // Dispatch respose. |
64 | - echo json_encode( $response ); |
|
64 | + echo json_encode($response); |
|
65 | 65 | |
66 | 66 | // Close ajax request. |
67 | 67 | wp_die(); |
@@ -74,22 +74,22 @@ discard block |
||
74 | 74 | |
75 | 75 | // Ajax handler to write a critical CSS to the filesystem |
76 | 76 | // Check referer. |
77 | - check_ajax_referer( 'save_critcss_nonce', 'critcss_save_nonce' ); |
|
77 | + check_ajax_referer('save_critcss_nonce', 'critcss_save_nonce'); |
|
78 | 78 | |
79 | 79 | // Allow empty contents for MANUAL rules (as they are fetched later). |
80 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
80 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
81 | 81 | $critcssfile = false; |
82 | 82 | $status = true; |
83 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
83 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
84 | 84 | // Or check user permissios and filename |
85 | 85 | // Set critical CSS content. |
86 | - $critcsscontents = stripslashes( $_POST['critcsscontents'] ); |
|
86 | + $critcsscontents = stripslashes($_POST['critcsscontents']); |
|
87 | 87 | |
88 | 88 | // If there is content and it's valid, write the file. |
89 | - if ( $critcsscontents && autoptimizeCriticalCSSCore::ao_ccss_check_contents( $critcsscontents ) ) { |
|
89 | + if ($critcsscontents && autoptimizeCriticalCSSCore::ao_ccss_check_contents($critcsscontents)) { |
|
90 | 90 | // Set file path and status. |
91 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
92 | - $status = file_put_contents( $critcssfile, $critcsscontents, LOCK_EX ); |
|
91 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
92 | + $status = file_put_contents($critcssfile, $critcsscontents, LOCK_EX); |
|
93 | 93 | // Or set as error. |
94 | 94 | } else { |
95 | 95 | $error = true; |
@@ -100,20 +100,20 @@ discard block |
||
100 | 100 | } |
101 | 101 | |
102 | 102 | // Prepare response. |
103 | - if ( ! $status || $error ) { |
|
103 | + if (!$status || $error) { |
|
104 | 104 | $response['code'] = '500'; |
105 | - $response['string'] = 'Error saving file ' . $critcssfile . '.'; |
|
105 | + $response['string'] = 'Error saving file '.$critcssfile.'.'; |
|
106 | 106 | } else { |
107 | 107 | $response['code'] = '200'; |
108 | - if ( $critcssfile ) { |
|
109 | - $response['string'] = 'File ' . $critcssfile . ' saved.'; |
|
108 | + if ($critcssfile) { |
|
109 | + $response['string'] = 'File '.$critcssfile.' saved.'; |
|
110 | 110 | } else { |
111 | 111 | $response['string'] = 'Empty content do not need to be saved.'; |
112 | 112 | } |
113 | 113 | } |
114 | 114 | |
115 | 115 | // Dispatch respose. |
116 | - echo json_encode( $response ); |
|
116 | + echo json_encode($response); |
|
117 | 117 | |
118 | 118 | // Close ajax request. |
119 | 119 | wp_die(); |
@@ -123,40 +123,40 @@ discard block |
||
123 | 123 | public function critcss_rm_callback() { |
124 | 124 | // Ajax handler to delete a critical CSS from the filesystem |
125 | 125 | // Check referer. |
126 | - check_ajax_referer( 'rm_critcss_nonce', 'critcss_rm_nonce' ); |
|
126 | + check_ajax_referer('rm_critcss_nonce', 'critcss_rm_nonce'); |
|
127 | 127 | |
128 | 128 | // Initialize error and status flags. |
129 | 129 | $error = true; |
130 | 130 | $status = false; |
131 | 131 | |
132 | 132 | // Allow no file for MANUAL rules (as they may not exist just yet). |
133 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
133 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
134 | 134 | $error = false; |
135 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
135 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
136 | 136 | // Or check user permissios and filename |
137 | 137 | // Set file path and delete it. |
138 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
139 | - if ( file_exists( $critcssfile ) ) { |
|
140 | - $status = unlink( $critcssfile ); |
|
138 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
139 | + if (file_exists($critcssfile)) { |
|
140 | + $status = unlink($critcssfile); |
|
141 | 141 | $error = false; |
142 | 142 | } |
143 | 143 | } |
144 | 144 | |
145 | 145 | // Prepare response. |
146 | - if ( $error ) { |
|
146 | + if ($error) { |
|
147 | 147 | $response['code'] = '500'; |
148 | - $response['string'] = 'Error removing file ' . $critcssfile . '.'; |
|
148 | + $response['string'] = 'Error removing file '.$critcssfile.'.'; |
|
149 | 149 | } else { |
150 | 150 | $response['code'] = '200'; |
151 | - if ( $status ) { |
|
152 | - $response['string'] = 'File ' . $critcssfile . ' removed.'; |
|
151 | + if ($status) { |
|
152 | + $response['string'] = 'File '.$critcssfile.' removed.'; |
|
153 | 153 | } else { |
154 | 154 | $response['string'] = 'No file to be removed.'; |
155 | 155 | } |
156 | 156 | } |
157 | 157 | |
158 | 158 | // Dispatch respose. |
159 | - echo json_encode( $response ); |
|
159 | + echo json_encode($response); |
|
160 | 160 | |
161 | 161 | // Close ajax request. |
162 | 162 | wp_die(); |
@@ -165,28 +165,28 @@ discard block |
||
165 | 165 | public function critcss_rm_all_callback() { |
166 | 166 | // Ajax handler to delete a critical CSS from the filesystem |
167 | 167 | // Check referer. |
168 | - check_ajax_referer( 'rm_critcss_all_nonce', 'critcss_rm_all_nonce' ); |
|
168 | + check_ajax_referer('rm_critcss_all_nonce', 'critcss_rm_all_nonce'); |
|
169 | 169 | |
170 | 170 | // Initialize error and status flags. |
171 | 171 | $error = true; |
172 | 172 | $status = false; |
173 | 173 | |
174 | 174 | // Remove all ccss files on filesystem. |
175 | - if ( current_user_can( 'manage_options' ) ) { |
|
176 | - if ( file_exists( AO_CCSS_DIR ) && is_dir( AO_CCSS_DIR ) ) { |
|
177 | - array_map( 'unlink', glob( AO_CCSS_DIR . 'ccss_*.css', GLOB_BRACE ) ); |
|
175 | + if (current_user_can('manage_options')) { |
|
176 | + if (file_exists(AO_CCSS_DIR) && is_dir(AO_CCSS_DIR)) { |
|
177 | + array_map('unlink', glob(AO_CCSS_DIR.'ccss_*.css', GLOB_BRACE)); |
|
178 | 178 | $error = false; |
179 | 179 | $status = true; |
180 | 180 | } |
181 | 181 | } |
182 | 182 | |
183 | 183 | // Prepare response. |
184 | - if ( $error ) { |
|
184 | + if ($error) { |
|
185 | 185 | $response['code'] = '500'; |
186 | 186 | $response['string'] = 'Error removing all critical CSS files.'; |
187 | 187 | } else { |
188 | 188 | $response['code'] = '200'; |
189 | - if ( $status ) { |
|
189 | + if ($status) { |
|
190 | 190 | $response['string'] = 'Critical CSS Files removed.'; |
191 | 191 | } else { |
192 | 192 | $response['string'] = 'No file removed.'; |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | } |
195 | 195 | |
196 | 196 | // Dispatch respose. |
197 | - echo json_encode( $response ); |
|
197 | + echo json_encode($response); |
|
198 | 198 | |
199 | 199 | // Close ajax request. |
200 | 200 | wp_die(); |
@@ -203,70 +203,70 @@ discard block |
||
203 | 203 | public function ao_ccss_export_callback() { |
204 | 204 | // Ajax handler export settings |
205 | 205 | // Check referer. |
206 | - check_ajax_referer( 'ao_ccss_export_nonce', 'ao_ccss_export_nonce' ); |
|
206 | + check_ajax_referer('ao_ccss_export_nonce', 'ao_ccss_export_nonce'); |
|
207 | 207 | |
208 | - if ( ! class_exists( 'ZipArchive' ) ) { |
|
208 | + if (!class_exists('ZipArchive')) { |
|
209 | 209 | $response['code'] = '500'; |
210 | 210 | $response['msg'] = 'PHP ZipArchive not present, cannot create zipfile'; |
211 | - echo json_encode( $response ); |
|
211 | + echo json_encode($response); |
|
212 | 212 | wp_die(); |
213 | 213 | } |
214 | 214 | |
215 | 215 | // Init array, get options and prepare the raw object. |
216 | 216 | $settings = array(); |
217 | - $settings['rules'] = get_option( 'autoptimize_ccss_rules' ); |
|
218 | - $settings['additional'] = get_option( 'autoptimize_ccss_additional' ); |
|
219 | - $settings['viewport'] = get_option( 'autoptimize_ccss_viewport' ); |
|
220 | - $settings['finclude'] = get_option( 'autoptimize_ccss_finclude' ); |
|
221 | - $settings['rtimelimit'] = get_option( 'autoptimize_ccss_rtimelimit' ); |
|
222 | - $settings['noptimize'] = get_option( 'autoptimize_ccss_noptimize' ); |
|
223 | - $settings['debug'] = get_option( 'autoptimize_ccss_debug' ); |
|
224 | - $settings['key'] = get_option( 'autoptimize_ccss_key' ); |
|
217 | + $settings['rules'] = get_option('autoptimize_ccss_rules'); |
|
218 | + $settings['additional'] = get_option('autoptimize_ccss_additional'); |
|
219 | + $settings['viewport'] = get_option('autoptimize_ccss_viewport'); |
|
220 | + $settings['finclude'] = get_option('autoptimize_ccss_finclude'); |
|
221 | + $settings['rtimelimit'] = get_option('autoptimize_ccss_rtimelimit'); |
|
222 | + $settings['noptimize'] = get_option('autoptimize_ccss_noptimize'); |
|
223 | + $settings['debug'] = get_option('autoptimize_ccss_debug'); |
|
224 | + $settings['key'] = get_option('autoptimize_ccss_key'); |
|
225 | 225 | |
226 | 226 | // Initialize error flag. |
227 | 227 | $error = true; |
228 | 228 | |
229 | 229 | // Check user permissions. |
230 | - if ( current_user_can( 'manage_options' ) ) { |
|
230 | + if (current_user_can('manage_options')) { |
|
231 | 231 | // Prepare settings file path and content. |
232 | - $exportfile = AO_CCSS_DIR . 'settings.json'; |
|
233 | - $contents = json_encode( $settings ); |
|
234 | - $status = file_put_contents( $exportfile, $contents, LOCK_EX ); |
|
232 | + $exportfile = AO_CCSS_DIR.'settings.json'; |
|
233 | + $contents = json_encode($settings); |
|
234 | + $status = file_put_contents($exportfile, $contents, LOCK_EX); |
|
235 | 235 | $error = false; |
236 | 236 | } |
237 | 237 | |
238 | 238 | // Prepare archive. |
239 | - $zipfile = AO_CCSS_DIR . date( 'Ymd-H\hi' ) . '_ao_ccss_settings.zip'; |
|
240 | - $file = pathinfo( $zipfile, PATHINFO_BASENAME ); |
|
239 | + $zipfile = AO_CCSS_DIR.date('Ymd-H\hi').'_ao_ccss_settings.zip'; |
|
240 | + $file = pathinfo($zipfile, PATHINFO_BASENAME); |
|
241 | 241 | $zip = new ZipArchive(); |
242 | - $ret = $zip->open( $zipfile, ZipArchive::CREATE ); |
|
243 | - if ( true !== $ret ) { |
|
242 | + $ret = $zip->open($zipfile, ZipArchive::CREATE); |
|
243 | + if (true !== $ret) { |
|
244 | 244 | $error = true; |
245 | 245 | } else { |
246 | - $zip->addFile( AO_CCSS_DIR . 'settings.json', 'settings.json' ); |
|
247 | - if ( file_exists( AO_CCSS_DIR . 'queue.json' ) ) { |
|
248 | - $zip->addFile( AO_CCSS_DIR . 'queue.json', 'queue.json' ); |
|
246 | + $zip->addFile(AO_CCSS_DIR.'settings.json', 'settings.json'); |
|
247 | + if (file_exists(AO_CCSS_DIR.'queue.json')) { |
|
248 | + $zip->addFile(AO_CCSS_DIR.'queue.json', 'queue.json'); |
|
249 | 249 | } |
250 | 250 | $options = array( |
251 | 251 | 'add_path' => './', |
252 | 252 | 'remove_all_path' => true, |
253 | 253 | ); |
254 | - $zip->addGlob( AO_CCSS_DIR . '*.css', 0, $options ); |
|
254 | + $zip->addGlob(AO_CCSS_DIR.'*.css', 0, $options); |
|
255 | 255 | $zip->close(); |
256 | 256 | } |
257 | 257 | |
258 | 258 | // Prepare response. |
259 | - if ( ! $status || $error ) { |
|
259 | + if (!$status || $error) { |
|
260 | 260 | $response['code'] = '500'; |
261 | - $response['msg'] = 'Error saving file ' . $file . ', code: ' . $ret; |
|
261 | + $response['msg'] = 'Error saving file '.$file.', code: '.$ret; |
|
262 | 262 | } else { |
263 | 263 | $response['code'] = '200'; |
264 | - $response['msg'] = 'File ' . $file . ' saved.'; |
|
264 | + $response['msg'] = 'File '.$file.' saved.'; |
|
265 | 265 | $response['file'] = $file; |
266 | 266 | } |
267 | 267 | |
268 | 268 | // Dispatch respose. |
269 | - echo json_encode( $response ); |
|
269 | + echo json_encode($response); |
|
270 | 270 | |
271 | 271 | // Close ajax request. |
272 | 272 | wp_die(); |
@@ -275,30 +275,30 @@ discard block |
||
275 | 275 | public function ao_ccss_import_callback() { |
276 | 276 | // Ajax handler import settings |
277 | 277 | // Check referer. |
278 | - check_ajax_referer( 'ao_ccss_import_nonce', 'ao_ccss_import_nonce' ); |
|
278 | + check_ajax_referer('ao_ccss_import_nonce', 'ao_ccss_import_nonce'); |
|
279 | 279 | |
280 | 280 | // Initialize error flag. |
281 | 281 | $error = false; |
282 | 282 | |
283 | 283 | // Process an uploaded file with no errors. |
284 | - if ( current_user_can( 'manage_options' ) && ! $_FILES['file']['error'] && $_FILES['file']['size'] < 500001 && strpos( $_FILES['file']['name'], '.zip' ) === strlen( $_FILES['file']['name'] ) - 4 ) { |
|
284 | + if (current_user_can('manage_options') && !$_FILES['file']['error'] && $_FILES['file']['size'] < 500001 && strpos($_FILES['file']['name'], '.zip') === strlen($_FILES['file']['name']) - 4) { |
|
285 | 285 | // create tmp dir with hard guess name in AO_CCSS_DIR. |
286 | - $_secret_dir = wp_hash( uniqid( md5( AUTOPTIMIZE_CACHE_URL ), true ) ); |
|
287 | - $_import_tmp_dir = trailingslashit( AO_CCSS_DIR . $_secret_dir ); |
|
288 | - mkdir( $_import_tmp_dir ); |
|
286 | + $_secret_dir = wp_hash(uniqid(md5(AUTOPTIMIZE_CACHE_URL), true)); |
|
287 | + $_import_tmp_dir = trailingslashit(AO_CCSS_DIR.$_secret_dir); |
|
288 | + mkdir($_import_tmp_dir); |
|
289 | 289 | |
290 | 290 | // Save file to that tmp directory but give it our own name to prevent directory traversal risks when using original name. |
291 | - $zipfile = $_import_tmp_dir . uniqid( 'import_settings-', true ) . '.zip'; |
|
292 | - move_uploaded_file( $_FILES['file']['tmp_name'], $zipfile ); |
|
291 | + $zipfile = $_import_tmp_dir.uniqid('import_settings-', true).'.zip'; |
|
292 | + move_uploaded_file($_FILES['file']['tmp_name'], $zipfile); |
|
293 | 293 | |
294 | 294 | // Extract archive in the tmp directory. |
295 | 295 | $zip = new ZipArchive; |
296 | - if ( $zip->open( $zipfile ) === true ) { |
|
296 | + if ($zip->open($zipfile) === true) { |
|
297 | 297 | // loop through all files in the zipfile. |
298 | 298 | for ($i = 0; $i < $zip->numFiles; $i++) { |
299 | 299 | // but only extract known good files. |
300 | - if ( preg_match('/^settings\.json$|^ccss_[a-z0-9]{32}\.css$/', $zip->getNameIndex( $i ) ) > 0 ) { |
|
301 | - $zip->extractTo( AO_CCSS_DIR, $zip->getNameIndex( $i ) ); |
|
300 | + if (preg_match('/^settings\.json$|^ccss_[a-z0-9]{32}\.css$/', $zip->getNameIndex($i)) > 0) { |
|
301 | + $zip->extractTo(AO_CCSS_DIR, $zip->getNameIndex($i)); |
|
302 | 302 | } |
303 | 303 | } |
304 | 304 | $zip->close(); |
@@ -307,26 +307,26 @@ discard block |
||
307 | 307 | } |
308 | 308 | |
309 | 309 | // and remove temp. dir with all contents (the import-zipfile). |
310 | - $this->rrmdir( $_import_tmp_dir ); |
|
310 | + $this->rrmdir($_import_tmp_dir); |
|
311 | 311 | |
312 | - if ( ! $error ) { |
|
312 | + if (!$error) { |
|
313 | 313 | // Archive extraction ok, continue importing settings from AO_CCSS_DIR. |
314 | 314 | // Settings file. |
315 | - $importfile = AO_CCSS_DIR . 'settings.json'; |
|
315 | + $importfile = AO_CCSS_DIR.'settings.json'; |
|
316 | 316 | |
317 | - if ( file_exists( $importfile ) ) { |
|
317 | + if (file_exists($importfile)) { |
|
318 | 318 | // Get settings and turn them into an object. |
319 | - $settings = json_decode( file_get_contents( $importfile ), true ); |
|
319 | + $settings = json_decode(file_get_contents($importfile), true); |
|
320 | 320 | |
321 | 321 | // Update options. |
322 | - update_option( 'autoptimize_ccss_rules', $settings['rules'] ); |
|
323 | - update_option( 'autoptimize_ccss_additional', $settings['additional'] ); |
|
324 | - update_option( 'autoptimize_ccss_viewport', $settings['viewport'] ); |
|
325 | - update_option( 'autoptimize_ccss_finclude', $settings['finclude'] ); |
|
326 | - update_option( 'autoptimize_ccss_rtimelimit', $settings['rtimelimit'] ); |
|
327 | - update_option( 'autoptimize_ccss_noptimize', $settings['noptimize'] ); |
|
328 | - update_option( 'autoptimize_ccss_debug', $settings['debug'] ); |
|
329 | - update_option( 'autoptimize_ccss_key', $settings['key'] ); |
|
322 | + update_option('autoptimize_ccss_rules', $settings['rules']); |
|
323 | + update_option('autoptimize_ccss_additional', $settings['additional']); |
|
324 | + update_option('autoptimize_ccss_viewport', $settings['viewport']); |
|
325 | + update_option('autoptimize_ccss_finclude', $settings['finclude']); |
|
326 | + update_option('autoptimize_ccss_rtimelimit', $settings['rtimelimit']); |
|
327 | + update_option('autoptimize_ccss_noptimize', $settings['noptimize']); |
|
328 | + update_option('autoptimize_ccss_debug', $settings['debug']); |
|
329 | + update_option('autoptimize_ccss_key', $settings['key']); |
|
330 | 330 | } else { |
331 | 331 | // Settings file doesn't exist, update error flag. |
332 | 332 | $error = 'settings file does not exist'; |
@@ -337,28 +337,28 @@ discard block |
||
337 | 337 | } |
338 | 338 | |
339 | 339 | // Prepare response. |
340 | - if ( $error ) { |
|
340 | + if ($error) { |
|
341 | 341 | $response['code'] = '500'; |
342 | - $response['msg'] = 'Error importing settings: ' . $error; |
|
342 | + $response['msg'] = 'Error importing settings: '.$error; |
|
343 | 343 | } else { |
344 | 344 | $response['code'] = '200'; |
345 | 345 | $response['msg'] = 'Settings imported successfully'; |
346 | 346 | } |
347 | 347 | |
348 | 348 | // Dispatch respose. |
349 | - echo json_encode( $response ); |
|
349 | + echo json_encode($response); |
|
350 | 350 | |
351 | 351 | // Close ajax request. |
352 | 352 | wp_die(); |
353 | 353 | } |
354 | 354 | |
355 | - public function critcss_check_filename( $filename ) { |
|
355 | + public function critcss_check_filename($filename) { |
|
356 | 356 | // Try to avoid directory traversal when reading/writing/deleting critical CSS files. |
357 | - if ( strpos( $filename, 'ccss_' ) !== 0 ) { |
|
357 | + if (strpos($filename, 'ccss_') !== 0) { |
|
358 | 358 | return false; |
359 | - } elseif ( substr( $filename, -4, 4 ) !== '.css' ) { |
|
359 | + } elseif (substr($filename, -4, 4) !== '.css') { |
|
360 | 360 | return false; |
361 | - } elseif ( sanitize_file_name( $filename ) !== $filename ) { |
|
361 | + } elseif (sanitize_file_name($filename) !== $filename) { |
|
362 | 362 | // Use WordPress core's sanitize_file_name to see if anything fishy is going on. |
363 | 363 | return false; |
364 | 364 | } else { |
@@ -366,14 +366,14 @@ discard block |
||
366 | 366 | } |
367 | 367 | } |
368 | 368 | |
369 | - public function rrmdir( $path ) { |
|
369 | + public function rrmdir($path) { |
|
370 | 370 | // recursively remove a directory as found on |
371 | 371 | // https://andy-carter.com/blog/recursively-remove-a-directory-in-php. |
372 | - $files = glob($path . '/*'); |
|
373 | - foreach ( $files as $file ) { |
|
374 | - is_dir( $file ) ? $this->rrmdir( $file ) : unlink( $file ); |
|
372 | + $files = glob($path.'/*'); |
|
373 | + foreach ($files as $file) { |
|
374 | + is_dir($file) ? $this->rrmdir($file) : unlink($file); |
|
375 | 375 | } |
376 | - rmdir( $path ); |
|
376 | + rmdir($path); |
|
377 | 377 | |
378 | 378 | return; |
379 | 379 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles disk-cache-related operations. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -38,22 +38,22 @@ discard block |
||
38 | 38 | * @param string $md5 Hash. |
39 | 39 | * @param string $ext Extension. |
40 | 40 | */ |
41 | - public function __construct( $md5, $ext = 'php' ) |
|
41 | + public function __construct($md5, $ext = 'php') |
|
42 | 42 | { |
43 | 43 | $_min_ext = ''; |
44 | - if ( apply_filters( 'autoptimize_filter_cache_url_add_min_ext', false ) ) { |
|
44 | + if (apply_filters('autoptimize_filter_cache_url_add_min_ext', false)) { |
|
45 | 45 | $_min_ext = '.min'; |
46 | 46 | } |
47 | 47 | |
48 | 48 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
49 | 49 | $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
50 | - if ( ! $this->nogzip ) { |
|
51 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.php'; |
|
50 | + if (!$this->nogzip) { |
|
51 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.php'; |
|
52 | 52 | } else { |
53 | - if ( in_array( $ext, array( 'js', 'css' ) ) ) { |
|
54 | - $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
53 | + if (in_array($ext, array('js', 'css'))) { |
|
54 | + $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
55 | 55 | } else { |
56 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
56 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function check() |
67 | 67 | { |
68 | - return file_exists( $this->cachedir . $this->filename ); |
|
68 | + return file_exists($this->cachedir.$this->filename); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,11 +75,11 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function retrieve() |
77 | 77 | { |
78 | - if ( $this->check() ) { |
|
79 | - if ( false == $this->nogzip ) { |
|
80 | - return file_get_contents( $this->cachedir . $this->filename . '.none' ); |
|
78 | + if ($this->check()) { |
|
79 | + if (false == $this->nogzip) { |
|
80 | + return file_get_contents($this->cachedir.$this->filename.'.none'); |
|
81 | 81 | } else { |
82 | - return file_get_contents( $this->cachedir . $this->filename ); |
|
82 | + return file_get_contents($this->cachedir.$this->filename); |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | return false; |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @return void |
95 | 95 | */ |
96 | - public function cache( $data, $mime ) |
|
96 | + public function cache($data, $mime) |
|
97 | 97 | { |
98 | 98 | // readonly FS explicitly OK'ed by developer, so just pretend all is OK. |
99 | - if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) { |
|
99 | + if (defined('AUTOPTIMIZE_CACHE_READONLY')) { |
|
100 | 100 | return true; |
101 | 101 | } |
102 | 102 | |
@@ -104,44 +104,44 @@ discard block |
||
104 | 104 | // |
105 | 105 | // to be activated for users that experience these ugly errors; |
106 | 106 | // PHP Warning: file_put_contents failed to open stream: No such file or directory. |
107 | - if ( apply_filters( 'autoptimize_filter_cache_checkdirs_on_write', false ) ) { |
|
107 | + if (apply_filters('autoptimize_filter_cache_checkdirs_on_write', false)) { |
|
108 | 108 | $this->check_and_create_dirs(); |
109 | 109 | } |
110 | 110 | |
111 | - if ( false === $this->nogzip ) { |
|
111 | + if (false === $this->nogzip) { |
|
112 | 112 | // We handle gzipping ourselves. |
113 | 113 | $file = 'default.php'; |
114 | - $phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file ); |
|
115 | - $phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode ); |
|
114 | + $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$file); |
|
115 | + $phpcode = str_replace(array('%%CONTENT%%', 'exit;'), array($mime, ''), $phpcode); |
|
116 | 116 | |
117 | - file_put_contents( $this->cachedir . $this->filename, $phpcode ); |
|
118 | - file_put_contents( $this->cachedir . $this->filename . '.none', $data ); |
|
117 | + file_put_contents($this->cachedir.$this->filename, $phpcode); |
|
118 | + file_put_contents($this->cachedir.$this->filename.'.none', $data); |
|
119 | 119 | } else { |
120 | 120 | // Write code to cache without doing anything else. |
121 | - file_put_contents( $this->cachedir . $this->filename, $data ); |
|
121 | + file_put_contents($this->cachedir.$this->filename, $data); |
|
122 | 122 | |
123 | 123 | // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single. |
124 | - if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) { |
|
125 | - $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION ); |
|
126 | - $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension; |
|
127 | - if ( ( 'css' === $_extension || 'js' === $_extension ) && ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) { |
|
128 | - file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data ); |
|
124 | + if (self::do_fallback() && strpos($this->filename, '_snippet_') === false && strpos($this->filename, '_single_') === false) { |
|
125 | + $_extension = pathinfo($this->filename, PATHINFO_EXTENSION); |
|
126 | + $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX.'fallback.'.$_extension; |
|
127 | + if (('css' === $_extension || 'js' === $_extension) && !file_exists($this->cachedir.$_extension.'/'.$_fallback_file)) { |
|
128 | + file_put_contents($this->cachedir.$_extension.'/'.$_fallback_file, $data); |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | - if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) { |
|
132 | + if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) { |
|
133 | 133 | // Create an additional cached gzip file. |
134 | - file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) ); |
|
134 | + file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($data, 9, FORCE_GZIP)); |
|
135 | 135 | // If PHP Brotli extension is installed, create an additional cached Brotli file. |
136 | - if ( function_exists( 'brotli_compress' ) ) { |
|
137 | - file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) ); |
|
136 | + if (function_exists('brotli_compress')) { |
|
137 | + file_put_contents($this->cachedir.$this->filename.'.br', brotli_compress($data, 11, BROTLI_GENERIC)); |
|
138 | 138 | } |
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | 142 | // Provide 3rd party action hook for every cache file that is created. |
143 | 143 | // This hook can for example be used to inject a copy of the created cache file to a other domain. |
144 | - do_action( 'autoptimize_action_cache_file_created', $this->cachedir . $this->filename ); |
|
144 | + do_action('autoptimize_action_cache_file_created', $this->cachedir.$this->filename); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | // The original idea here was to provide 3rd party code a hook so that |
157 | 157 | // it can "listen" to all the complete autoptimized-urls that the page |
158 | 158 | // will emit... Or something to that effect I think? |
159 | - apply_filters( 'autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL . $this->filename ); |
|
159 | + apply_filters('autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL.$this->filename); |
|
160 | 160 | |
161 | 161 | return $this->filename; |
162 | 162 | } |
@@ -169,11 +169,11 @@ discard block |
||
169 | 169 | * @param string $file Filename. |
170 | 170 | * @return bool |
171 | 171 | */ |
172 | - protected static function is_valid_cache_file( $dir, $file ) |
|
172 | + protected static function is_valid_cache_file($dir, $file) |
|
173 | 173 | { |
174 | - if ( '.' !== $file && '..' !== $file && |
|
175 | - false !== strpos( $file, AUTOPTIMIZE_CACHEFILE_PREFIX ) && |
|
176 | - is_file( $dir . $file ) ) { |
|
174 | + if ('.' !== $file && '..' !== $file && |
|
175 | + false !== strpos($file, AUTOPTIMIZE_CACHEFILE_PREFIX) && |
|
176 | + is_file($dir.$file)) { |
|
177 | 177 | |
178 | 178 | // It's a valid file! |
179 | 179 | return true; |
@@ -191,16 +191,16 @@ discard block |
||
191 | 191 | protected static function clear_cache_classic() |
192 | 192 | { |
193 | 193 | $contents = self::get_cache_contents(); |
194 | - foreach ( $contents as $name => $files ) { |
|
195 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
196 | - foreach ( $files as $file ) { |
|
197 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
198 | - @unlink( $dir . $file ); // @codingStandardsIgnoreLine |
|
194 | + foreach ($contents as $name => $files) { |
|
195 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
196 | + foreach ($files as $file) { |
|
197 | + if (self::is_valid_cache_file($dir, $file)) { |
|
198 | + @unlink($dir.$file); // @codingStandardsIgnoreLine |
|
199 | 199 | } |
200 | 200 | } |
201 | 201 | } |
202 | 202 | |
203 | - @unlink( AUTOPTIMIZE_CACHE_DIR . '/.htaccess' ); // @codingStandardsIgnoreLine |
|
203 | + @unlink(AUTOPTIMIZE_CACHE_DIR.'/.htaccess'); // @codingStandardsIgnoreLine |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
@@ -211,19 +211,19 @@ discard block |
||
211 | 211 | * |
212 | 212 | * @return bool |
213 | 213 | */ |
214 | - protected static function rmdir( $pathname ) |
|
214 | + protected static function rmdir($pathname) |
|
215 | 215 | { |
216 | - $files = self::get_dir_contents( $pathname ); |
|
217 | - foreach ( $files as $file ) { |
|
218 | - $path = $pathname . '/' . $file; |
|
219 | - if ( is_dir( $path ) ) { |
|
220 | - self::rmdir( $path ); |
|
216 | + $files = self::get_dir_contents($pathname); |
|
217 | + foreach ($files as $file) { |
|
218 | + $path = $pathname.'/'.$file; |
|
219 | + if (is_dir($path)) { |
|
220 | + self::rmdir($path); |
|
221 | 221 | } else { |
222 | - unlink( $path ); |
|
222 | + unlink($path); |
|
223 | 223 | } |
224 | 224 | } |
225 | 225 | |
226 | - return rmdir( $pathname ); |
|
226 | + return rmdir($pathname); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
@@ -244,12 +244,12 @@ discard block |
||
244 | 244 | $new_name = self::get_unique_name(); |
245 | 245 | |
246 | 246 | // Makes sure the new pathname is on the same level... |
247 | - $new_pathname = dirname( $dir ) . '/' . $new_name; |
|
248 | - $renamed = @rename( $dir, $new_pathname ); // @codingStandardsIgnoreLine |
|
247 | + $new_pathname = dirname($dir).'/'.$new_name; |
|
248 | + $renamed = @rename($dir, $new_pathname); // @codingStandardsIgnoreLine |
|
249 | 249 | |
250 | 250 | // When renamed, re-create the default cache directory back so it's |
251 | 251 | // available again... |
252 | - if ( $renamed ) { |
|
252 | + if ($renamed) { |
|
253 | 253 | $ok = self::cacheavail(); |
254 | 254 | } |
255 | 255 | |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public static function advanced_cache_clear_enabled() |
265 | 265 | { |
266 | - return apply_filters( 'autoptimize_filter_cache_clear_advanced', false ); |
|
266 | + return apply_filters('autoptimize_filter_cache_clear_advanced', false); |
|
267 | 267 | } |
268 | 268 | |
269 | 269 | /** |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | protected static function get_unique_name() |
275 | 275 | { |
276 | 276 | $prefix = self::get_advanced_cache_clear_prefix(); |
277 | - $new_name = uniqid( $prefix, true ); |
|
277 | + $new_name = uniqid($prefix, true); |
|
278 | 278 | |
279 | 279 | return $new_name; |
280 | 280 | } |
@@ -287,8 +287,8 @@ discard block |
||
287 | 287 | protected static function get_advanced_cache_clear_prefix() |
288 | 288 | { |
289 | 289 | $pathname = self::get_pathname_base(); |
290 | - $basename = basename( $pathname ); |
|
291 | - $prefix = $basename . '-artifact-'; |
|
290 | + $basename = basename($pathname); |
|
291 | + $prefix = $basename.'-artifact-'; |
|
292 | 292 | |
293 | 293 | return $prefix; |
294 | 294 | } |
@@ -301,9 +301,9 @@ discard block |
||
301 | 301 | * |
302 | 302 | * @return array |
303 | 303 | */ |
304 | - protected static function get_dir_contents( $pathname ) |
|
304 | + protected static function get_dir_contents($pathname) |
|
305 | 305 | { |
306 | - return array_slice( scandir( $pathname ), 2 ); |
|
306 | + return array_slice(scandir($pathname), 2); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | /** |
@@ -316,24 +316,24 @@ discard block |
||
316 | 316 | public static function delete_advanced_cache_clear_artifacts() |
317 | 317 | { |
318 | 318 | // Don't go through these motions (called from the cachechecker) if advanced cache clear isn't even active. |
319 | - if ( ! self::advanced_cache_clear_enabled() ) { |
|
319 | + if (!self::advanced_cache_clear_enabled()) { |
|
320 | 320 | return false; |
321 | 321 | } |
322 | 322 | |
323 | 323 | $dir = self::get_pathname_base(); |
324 | 324 | $prefix = self::get_advanced_cache_clear_prefix(); |
325 | - $parent = dirname( $dir ); |
|
325 | + $parent = dirname($dir); |
|
326 | 326 | $ok = false; |
327 | 327 | |
328 | 328 | // Returns the list of files without '.' and '..' elements. |
329 | - $files = self::get_dir_contents( $parent ); |
|
330 | - if ( is_array( $files ) && ! empty( $files ) ) { |
|
331 | - foreach ( $files as $file ) { |
|
332 | - $path = $parent . '/' . $file; |
|
333 | - $prefixed = ( false !== strpos( $path, $prefix ) ); |
|
329 | + $files = self::get_dir_contents($parent); |
|
330 | + if (is_array($files) && !empty($files)) { |
|
331 | + foreach ($files as $file) { |
|
332 | + $path = $parent.'/'.$file; |
|
333 | + $prefixed = (false !== strpos($path, $prefix)); |
|
334 | 334 | // Removing only our own (prefixed) directories... |
335 | - if ( is_dir( $path ) && $prefixed ) { |
|
336 | - $ok = self::rmdir( $path ); |
|
335 | + if (is_dir($path) && $prefixed) { |
|
336 | + $ok = self::rmdir($path); |
|
337 | 337 | } |
338 | 338 | } |
339 | 339 | } |
@@ -353,9 +353,9 @@ discard block |
||
353 | 353 | { |
354 | 354 | $pathname = self::get_pathname_base(); |
355 | 355 | |
356 | - if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
|
356 | + if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) { |
|
357 | 357 | $blog_id = get_current_blog_id(); |
358 | - $pathname .= $blog_id . '/'; |
|
358 | + $pathname .= $blog_id.'/'; |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | return $pathname; |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | */ |
369 | 369 | protected static function get_pathname_base() |
370 | 370 | { |
371 | - $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
371 | + $pathname = WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
372 | 372 | |
373 | 373 | return $pathname; |
374 | 374 | } |
@@ -380,46 +380,46 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return bool |
382 | 382 | */ |
383 | - public static function clearall( $propagate = true ) |
|
383 | + public static function clearall($propagate = true) |
|
384 | 384 | { |
385 | - if ( ! self::cacheavail() ) { |
|
385 | + if (!self::cacheavail()) { |
|
386 | 386 | return false; |
387 | 387 | } |
388 | 388 | |
389 | 389 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
390 | - if ( self::advanced_cache_clear_enabled() ) { |
|
390 | + if (self::advanced_cache_clear_enabled()) { |
|
391 | 391 | self::clear_cache_via_rename(); |
392 | 392 | } else { |
393 | 393 | self::clear_cache_classic(); |
394 | 394 | } |
395 | 395 | |
396 | 396 | // Remove 404 handler if required. |
397 | - if ( self::do_fallback() ) { |
|
398 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php'; |
|
399 | - @unlink( $_fallback_php ); // @codingStandardsIgnoreLine |
|
397 | + if (self::do_fallback()) { |
|
398 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).'autoptimize_404_handler.php'; |
|
399 | + @unlink($_fallback_php); // @codingStandardsIgnoreLine |
|
400 | 400 | } |
401 | 401 | |
402 | 402 | // Remove the transient so it gets regenerated... |
403 | - delete_transient( 'autoptimize_stats' ); |
|
403 | + delete_transient('autoptimize_stats'); |
|
404 | 404 | |
405 | 405 | // Cache was just purged, clear page cache and allow others to hook into our purging... |
406 | - if ( true === $propagate ) { |
|
407 | - if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { |
|
406 | + if (true === $propagate) { |
|
407 | + if (!function_exists('autoptimize_do_cachepurged_action')) { |
|
408 | 408 | function autoptimize_do_cachepurged_action() { |
409 | - do_action( 'autoptimize_action_cachepurged' ); |
|
409 | + do_action('autoptimize_action_cachepurged'); |
|
410 | 410 | } |
411 | 411 | } |
412 | - add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); |
|
413 | - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); |
|
412 | + add_action('shutdown', 'autoptimize_do_cachepurged_action', 11); |
|
413 | + add_action('autoptimize_action_cachepurged', array('autoptimizeCache', 'flushPageCache'), 10, 0); |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | // Warm cache (part of speedupper)! |
417 | - if ( apply_filters( 'autoptimize_filter_speedupper', true ) && false == get_transient( 'autoptimize_cache_warmer_protector' ) ) { |
|
418 | - set_transient( 'autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60 * 10 ); |
|
419 | - $url = site_url() . '/?ao_speedup_cachebuster=' . rand( 1, 100000 ); |
|
420 | - $url = apply_filters( 'autoptimize_filter_cache_warmer_url', $url ); |
|
421 | - $cache = @wp_remote_get( $url ); // @codingStandardsIgnoreLine |
|
422 | - unset( $cache ); |
|
417 | + if (apply_filters('autoptimize_filter_speedupper', true) && false == get_transient('autoptimize_cache_warmer_protector')) { |
|
418 | + set_transient('autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60*10); |
|
419 | + $url = site_url().'/?ao_speedup_cachebuster='.rand(1, 100000); |
|
420 | + $url = apply_filters('autoptimize_filter_cache_warmer_url', $url); |
|
421 | + $cache = @wp_remote_get($url); // @codingStandardsIgnoreLine |
|
422 | + unset($cache); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | return true; |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | */ |
435 | 435 | public static function clearall_actionless() |
436 | 436 | { |
437 | - return self::clearall( false ); |
|
437 | + return self::clearall(false); |
|
438 | 438 | } |
439 | 439 | |
440 | 440 | /** |
@@ -446,8 +446,8 @@ discard block |
||
446 | 446 | { |
447 | 447 | $contents = array(); |
448 | 448 | |
449 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
450 | - $contents[ $dir ] = scandir( AUTOPTIMIZE_CACHE_DIR . $dir ); |
|
449 | + foreach (array('', 'js', 'css') as $dir) { |
|
450 | + $contents[$dir] = scandir(AUTOPTIMIZE_CACHE_DIR.$dir); |
|
451 | 451 | } |
452 | 452 | |
453 | 453 | return $contents; |
@@ -460,21 +460,21 @@ discard block |
||
460 | 460 | */ |
461 | 461 | public static function stats() |
462 | 462 | { |
463 | - $stats = get_transient( 'autoptimize_stats' ); |
|
463 | + $stats = get_transient('autoptimize_stats'); |
|
464 | 464 | |
465 | 465 | // If no transient, do the actual scan! |
466 | - if ( ! is_array( $stats ) ) { |
|
467 | - if ( ! self::cacheavail() ) { |
|
466 | + if (!is_array($stats)) { |
|
467 | + if (!self::cacheavail()) { |
|
468 | 468 | return 0; |
469 | 469 | } |
470 | 470 | $stats = self::stats_scan(); |
471 | 471 | $count = $stats[0]; |
472 | - if ( $count > 100 ) { |
|
472 | + if ($count > 100) { |
|
473 | 473 | // Store results in transient. |
474 | 474 | set_transient( |
475 | 475 | 'autoptimize_stats', |
476 | 476 | $stats, |
477 | - apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) |
|
477 | + apply_filters('autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS) |
|
478 | 478 | ); |
479 | 479 | } |
480 | 480 | } |
@@ -497,30 +497,30 @@ discard block |
||
497 | 497 | $size = 0; |
498 | 498 | |
499 | 499 | // Scan everything in our cache directories. |
500 | - foreach ( self::get_cache_contents() as $name => $files ) { |
|
501 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
502 | - foreach ( $files as $file ) { |
|
503 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
504 | - if ( AUTOPTIMIZE_CACHE_NOGZIP && |
|
500 | + foreach (self::get_cache_contents() as $name => $files) { |
|
501 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
502 | + foreach ($files as $file) { |
|
503 | + if (self::is_valid_cache_file($dir, $file)) { |
|
504 | + if (AUTOPTIMIZE_CACHE_NOGZIP && |
|
505 | 505 | ( |
506 | - false !== strpos( $file, '.js' ) || |
|
507 | - false !== strpos( $file, '.css' ) || |
|
508 | - false !== strpos( $file, '.img' ) || |
|
509 | - false !== strpos( $file, '.txt' ) |
|
506 | + false !== strpos($file, '.js') || |
|
507 | + false !== strpos($file, '.css') || |
|
508 | + false !== strpos($file, '.img') || |
|
509 | + false !== strpos($file, '.txt') |
|
510 | 510 | ) |
511 | 511 | ) { |
512 | 512 | // Web server is gzipping, we count .js|.css|.img|.txt files. |
513 | 513 | $count++; |
514 | - } elseif ( ! AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos( $file, '.none' ) ) { |
|
514 | + } elseif (!AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos($file, '.none')) { |
|
515 | 515 | // We are gzipping ourselves via php, counting only .none files. |
516 | 516 | $count++; |
517 | 517 | } |
518 | - $size += filesize( $dir . $file ); |
|
518 | + $size += filesize($dir.$file); |
|
519 | 519 | } |
520 | 520 | } |
521 | 521 | } |
522 | 522 | |
523 | - $stats = array( $count, $size, time() ); |
|
523 | + $stats = array($count, $size, time()); |
|
524 | 524 | |
525 | 525 | return $stats; |
526 | 526 | } |
@@ -535,26 +535,26 @@ discard block |
||
535 | 535 | public static function cacheavail() |
536 | 536 | { |
537 | 537 | // readonly FS explicitly OK'ed by dev, let's assume the cache dirs are there! |
538 | - if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) { |
|
538 | + if (defined('AUTOPTIMIZE_CACHE_READONLY')) { |
|
539 | 539 | return true; |
540 | 540 | } |
541 | 541 | |
542 | - if ( false === autoptimizeCache::check_and_create_dirs() ) { |
|
542 | + if (false === autoptimizeCache::check_and_create_dirs()) { |
|
543 | 543 | return false; |
544 | 544 | } |
545 | 545 | |
546 | 546 | // Using .htaccess inside our cache folder to overrule wp-super-cache. |
547 | - $htaccess = AUTOPTIMIZE_CACHE_DIR . '/.htaccess'; |
|
548 | - if ( ! is_file( $htaccess ) ) { |
|
547 | + $htaccess = AUTOPTIMIZE_CACHE_DIR.'/.htaccess'; |
|
548 | + if (!is_file($htaccess)) { |
|
549 | 549 | /** |
550 | 550 | * Create `wp-content/AO_htaccess_tmpl` file with |
551 | 551 | * whatever htaccess rules you might need |
552 | 552 | * if you want to override default AO htaccess |
553 | 553 | */ |
554 | - $htaccess_tmpl = WP_CONTENT_DIR . '/AO_htaccess_tmpl'; |
|
555 | - if ( is_file( $htaccess_tmpl ) ) { |
|
556 | - $content = file_get_contents( $htaccess_tmpl ); |
|
557 | - } elseif ( is_multisite() || ! AUTOPTIMIZE_CACHE_NOGZIP ) { |
|
554 | + $htaccess_tmpl = WP_CONTENT_DIR.'/AO_htaccess_tmpl'; |
|
555 | + if (is_file($htaccess_tmpl)) { |
|
556 | + $content = file_get_contents($htaccess_tmpl); |
|
557 | + } elseif (is_multisite() || !AUTOPTIMIZE_CACHE_NOGZIP) { |
|
558 | 558 | $content = '<IfModule mod_expires.c> |
559 | 559 | ExpiresActive On |
560 | 560 | ExpiresByType text/css A30672000 |
@@ -608,13 +608,13 @@ discard block |
||
608 | 608 | </IfModule>'; |
609 | 609 | } |
610 | 610 | |
611 | - if ( self::do_fallback() === true ) { |
|
612 | - $content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php'; |
|
611 | + if (self::do_fallback() === true) { |
|
612 | + $content .= "\nErrorDocument 404 ".trailingslashit(parse_url(content_url(), PHP_URL_PATH)).'autoptimize_404_handler.php'; |
|
613 | 613 | } |
614 | - @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine |
|
614 | + @file_put_contents($htaccess, $content); // @codingStandardsIgnoreLine |
|
615 | 615 | } |
616 | 616 | |
617 | - if ( self::do_fallback() ) { |
|
617 | + if (self::do_fallback()) { |
|
618 | 618 | self::check_fallback_php(); |
619 | 619 | } |
620 | 620 | |
@@ -629,17 +629,17 @@ discard block |
||
629 | 629 | */ |
630 | 630 | public static function check_fallback_php() { |
631 | 631 | $_fallback_filename = 'autoptimize_404_handler.php'; |
632 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename; |
|
632 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).$_fallback_filename; |
|
633 | 633 | $_fallback_status = true; |
634 | 634 | |
635 | - if ( ! file_exists( $_fallback_php ) && is_writable( WP_CONTENT_DIR ) ) { |
|
636 | - $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename ); |
|
637 | - $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents ); |
|
638 | - $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents ); |
|
639 | - if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) { |
|
640 | - $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents ); |
|
635 | + if (!file_exists($_fallback_php) && is_writable(WP_CONTENT_DIR)) { |
|
636 | + $_fallback_php_contents = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$_fallback_filename); |
|
637 | + $_fallback_php_contents = str_replace('<?php exit;', '<?php', $_fallback_php_contents); |
|
638 | + $_fallback_php_contents = str_replace('<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents); |
|
639 | + if (apply_filters('autoptimize_filter_cache_fallback_log_errors', false)) { |
|
640 | + $_fallback_php_contents = str_replace('// error_log', 'error_log', $_fallback_php_contents); |
|
641 | 641 | } |
642 | - $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents ); |
|
642 | + $_fallback_status = file_put_contents($_fallback_php, $_fallback_php_contents); |
|
643 | 643 | } |
644 | 644 | |
645 | 645 | return $_fallback_status; |
@@ -656,8 +656,8 @@ discard block |
||
656 | 656 | public static function do_fallback() { |
657 | 657 | static $_do_fallback = null; |
658 | 658 | |
659 | - if ( null === $_do_fallback ) { |
|
660 | - $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '1' ) ); |
|
659 | + if (null === $_do_fallback) { |
|
660 | + $_do_fallback = (bool) apply_filters('autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option('autoptimize_cache_fallback', '1')); |
|
661 | 661 | } |
662 | 662 | |
663 | 663 | return $_do_fallback; |
@@ -669,31 +669,31 @@ discard block |
||
669 | 669 | * and 410'ing ("Gone") if fallback not available. |
670 | 670 | */ |
671 | 671 | public static function wordpress_notfound_fallback() { |
672 | - $original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
673 | - if ( strpos( $original_request, wp_basename( WP_CONTENT_DIR ) . AUTOPTIMIZE_CACHE_CHILD_DIR ) !== false && is_404() ) { |
|
672 | + $original_request = strtok($_SERVER['REQUEST_URI'], '?'); |
|
673 | + if (strpos($original_request, wp_basename(WP_CONTENT_DIR).AUTOPTIMIZE_CACHE_CHILD_DIR) !== false && is_404()) { |
|
674 | 674 | // make sure this is not considered a 404. |
675 | 675 | global $wp_query; |
676 | 676 | $wp_query->is_404 = false; |
677 | 677 | |
678 | 678 | // set fallback path. |
679 | - $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
|
680 | - $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css; |
|
679 | + $js_or_css = pathinfo($original_request, PATHINFO_EXTENSION); |
|
680 | + $fallback_path = AUTOPTIMIZE_CACHE_DIR.$js_or_css.'/autoptimize_fallback.'.$js_or_css; |
|
681 | 681 | |
682 | 682 | // prepare for Shakeeb's Unused CSS files to be 404-handled as well. |
683 | - if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) { |
|
684 | - $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request ); |
|
683 | + if (strpos($original_request, 'uucss/uucss-') !== false) { |
|
684 | + $original_request = preg_replace('/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request); |
|
685 | 685 | } |
686 | 686 | |
687 | 687 | // set fallback URL. |
688 | - $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
|
688 | + $fallback_target = preg_replace('/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request); |
|
689 | 689 | |
690 | 690 | // redirect to fallback if possible. |
691 | - if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { |
|
691 | + if ($original_request !== $fallback_target && file_exists($fallback_path)) { |
|
692 | 692 | // redirect to fallback. |
693 | - wp_redirect( $fallback_target, 302 ); |
|
693 | + wp_redirect($fallback_target, 302); |
|
694 | 694 | } else { |
695 | 695 | // return HTTP 410 (gone) reponse. |
696 | - status_header( 410 ); |
|
696 | + status_header(410); |
|
697 | 697 | } |
698 | 698 | } |
699 | 699 | } |
@@ -705,13 +705,13 @@ discard block |
||
705 | 705 | * @return bool |
706 | 706 | */ |
707 | 707 | public static function check_and_create_dirs() { |
708 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) { |
|
708 | + if (!defined('AUTOPTIMIZE_CACHE_DIR')) { |
|
709 | 709 | // We didn't set a cache. |
710 | 710 | return false; |
711 | 711 | } |
712 | 712 | |
713 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
714 | - if ( ! self::check_cache_dir( AUTOPTIMIZE_CACHE_DIR . $dir ) ) { |
|
713 | + foreach (array('', 'js', 'css') as $dir) { |
|
714 | + if (!self::check_cache_dir(AUTOPTIMIZE_CACHE_DIR.$dir)) { |
|
715 | 715 | return false; |
716 | 716 | } |
717 | 717 | } |
@@ -726,25 +726,25 @@ discard block |
||
726 | 726 | * |
727 | 727 | * @return bool |
728 | 728 | */ |
729 | - protected static function check_cache_dir( $dir ) |
|
729 | + protected static function check_cache_dir($dir) |
|
730 | 730 | { |
731 | 731 | // Try creating the dir if it doesn't exist. |
732 | - if ( ! file_exists( $dir ) ) { |
|
733 | - @mkdir( $dir, 0775, true ); // @codingStandardsIgnoreLine |
|
734 | - if ( ! file_exists( $dir ) ) { |
|
732 | + if (!file_exists($dir)) { |
|
733 | + @mkdir($dir, 0775, true); // @codingStandardsIgnoreLine |
|
734 | + if (!file_exists($dir)) { |
|
735 | 735 | return false; |
736 | 736 | } |
737 | 737 | } |
738 | 738 | |
739 | 739 | // If we still cannot write, bail. |
740 | - if ( ! is_writable( $dir ) ) { |
|
740 | + if (!is_writable($dir)) { |
|
741 | 741 | return false; |
742 | 742 | } |
743 | 743 | |
744 | 744 | // Create an index.html in there to avoid prying eyes! |
745 | - $idx_file = rtrim( $dir, '/\\' ) . '/index.html'; |
|
746 | - if ( ! is_file( $idx_file ) ) { |
|
747 | - @file_put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); // @codingStandardsIgnoreLine |
|
745 | + $idx_file = rtrim($dir, '/\\').'/index.html'; |
|
746 | + if (!is_file($idx_file)) { |
|
747 | + @file_put_contents($idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>'); // @codingStandardsIgnoreLine |
|
748 | 748 | } |
749 | 749 | |
750 | 750 | return true; |
@@ -758,54 +758,54 @@ discard block |
||
758 | 758 | // @codingStandardsIgnoreStart |
759 | 759 | public static function flushPageCache() |
760 | 760 | { |
761 | - if ( function_exists( 'wp_cache_clear_cache' ) ) { |
|
762 | - if ( is_multisite() ) { |
|
761 | + if (function_exists('wp_cache_clear_cache')) { |
|
762 | + if (is_multisite()) { |
|
763 | 763 | $blog_id = get_current_blog_id(); |
764 | - wp_cache_clear_cache( $blog_id ); |
|
764 | + wp_cache_clear_cache($blog_id); |
|
765 | 765 | } else { |
766 | 766 | wp_cache_clear_cache(); |
767 | 767 | } |
768 | - } elseif ( has_action( 'cachify_flush_cache' ) ) { |
|
769 | - do_action( 'cachify_flush_cache' ); |
|
770 | - } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) { |
|
768 | + } elseif (has_action('cachify_flush_cache')) { |
|
769 | + do_action('cachify_flush_cache'); |
|
770 | + } elseif (function_exists('w3tc_pgcache_flush')) { |
|
771 | 771 | w3tc_pgcache_flush(); |
772 | - } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) { |
|
772 | + } elseif (function_exists('wp_fast_cache_bulk_delete_all')) { |
|
773 | 773 | wp_fast_cache_bulk_delete_all(); |
774 | - } elseif ( function_exists( 'rapidcache_clear_cache' ) ) { |
|
774 | + } elseif (function_exists('rapidcache_clear_cache')) { |
|
775 | 775 | rapidcache_clear_cache(); |
776 | - } elseif ( class_exists( 'Swift_Performance_Cache' ) ) { |
|
776 | + } elseif (class_exists('Swift_Performance_Cache')) { |
|
777 | 777 | Swift_Performance_Cache::clear_all_cache(); |
778 | - } elseif ( class_exists( 'WpFastestCache' ) ) { |
|
778 | + } elseif (class_exists('WpFastestCache')) { |
|
779 | 779 | $wpfc = new WpFastestCache(); |
780 | 780 | $wpfc->deleteCache(); |
781 | - } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) { |
|
781 | + } elseif (class_exists('c_ws_plugin__qcache_purging_routines')) { |
|
782 | 782 | c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache |
783 | - } elseif ( class_exists( 'zencache' ) ) { |
|
783 | + } elseif (class_exists('zencache')) { |
|
784 | 784 | zencache::clear(); |
785 | - } elseif ( class_exists( 'comet_cache' ) ) { |
|
785 | + } elseif (class_exists('comet_cache')) { |
|
786 | 786 | comet_cache::clear(); |
787 | - } elseif ( class_exists( 'WpeCommon' ) ) { |
|
787 | + } elseif (class_exists('WpeCommon')) { |
|
788 | 788 | // WPEngine cache purge/flush methods to call by default |
789 | 789 | $wpe_methods = array( |
790 | 790 | 'purge_varnish_cache', |
791 | 791 | ); |
792 | 792 | |
793 | 793 | // More agressive clear/flush/purge behind a filter |
794 | - if ( apply_filters( 'autoptimize_flush_wpengine_aggressive', false ) ) { |
|
795 | - $wpe_methods = array_merge( $wpe_methods, array( 'purge_memcached', 'clear_maxcdn_cache' ) ); |
|
794 | + if (apply_filters('autoptimize_flush_wpengine_aggressive', false)) { |
|
795 | + $wpe_methods = array_merge($wpe_methods, array('purge_memcached', 'clear_maxcdn_cache')); |
|
796 | 796 | } |
797 | 797 | |
798 | 798 | // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing) |
799 | - $wpe_methods = apply_filters( 'autoptimize_flush_wpengine_methods', $wpe_methods ); |
|
799 | + $wpe_methods = apply_filters('autoptimize_flush_wpengine_methods', $wpe_methods); |
|
800 | 800 | |
801 | - foreach ( $wpe_methods as $wpe_method ) { |
|
802 | - if ( method_exists( 'WpeCommon', $wpe_method ) ) { |
|
801 | + foreach ($wpe_methods as $wpe_method) { |
|
802 | + if (method_exists('WpeCommon', $wpe_method)) { |
|
803 | 803 | WpeCommon::$wpe_method(); |
804 | 804 | } |
805 | 805 | } |
806 | - } elseif ( function_exists( 'sg_cachepress_purge_cache' ) ) { |
|
806 | + } elseif (function_exists('sg_cachepress_purge_cache')) { |
|
807 | 807 | sg_cachepress_purge_cache(); |
808 | - } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) { |
|
808 | + } elseif (array_key_exists('KINSTA_CACHE_ZONE', $_SERVER)) { |
|
809 | 809 | $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all'; |
810 | 810 | $_kinsta_response = wp_remote_get( |
811 | 811 | $_kinsta_clear_cache_url, |
@@ -814,18 +814,18 @@ discard block |
||
814 | 814 | 'timeout' => 5, |
815 | 815 | ) |
816 | 816 | ); |
817 | - } elseif ( defined('NGINX_HELPER_BASENAME') ) { |
|
818 | - do_action( 'rt_nginx_helper_purge_all' ); |
|
819 | - } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) { |
|
817 | + } elseif (defined('NGINX_HELPER_BASENAME')) { |
|
818 | + do_action('rt_nginx_helper_purge_all'); |
|
819 | + } elseif (file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')) { |
|
820 | 820 | // fallback for WP-Super-Cache |
821 | 821 | global $cache_path; |
822 | - if ( is_multisite() ) { |
|
822 | + if (is_multisite()) { |
|
823 | 823 | $blog_id = get_current_blog_id(); |
824 | - prune_super_cache( get_supercache_dir( $blog_id ), true ); |
|
825 | - prune_super_cache( $cache_path . 'blogs/', true ); |
|
824 | + prune_super_cache(get_supercache_dir($blog_id), true); |
|
825 | + prune_super_cache($cache_path.'blogs/', true); |
|
826 | 826 | } else { |
827 | - prune_super_cache( $cache_path . 'supercache/', true ); |
|
828 | - prune_super_cache( $cache_path, true ); |
|
827 | + prune_super_cache($cache_path.'supercache/', true); |
|
828 | + prune_super_cache($cache_path, true); |
|
829 | 829 | } |
830 | 830 | } |
831 | 831 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * processes the queue, submitting jobs to criticalcss.com and retrieving generated CSS from criticalcss.com and saving rules. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,23 +13,23 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
20 | 20 | |
21 | 21 | // Add queue control to a registered event. |
22 | - add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) ); |
|
22 | + add_action('ao_ccss_queue', array($this, 'ao_ccss_queue_control')); |
|
23 | 23 | // Add cleaning job to a registered event. |
24 | - add_action( 'ao_ccss_maintenance', array( $this, 'ao_ccss_cleaning' ) ); |
|
24 | + add_action('ao_ccss_maintenance', array($this, 'ao_ccss_cleaning')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function ao_ccss_queue_control() { |
28 | 28 | // The queue execution backend. |
29 | 29 | global $ao_ccss_key; |
30 | - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
|
30 | + if (!isset($ao_ccss_key) || empty($ao_ccss_key)) { |
|
31 | 31 | // no key set, not processing the queue! |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log('No key set, so not processing queue.', 3); |
|
33 | 33 | return; |
34 | 34 | } |
35 | 35 | |
@@ -50,35 +50,35 @@ discard block |
||
50 | 50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
51 | 51 | */ |
52 | 52 | $queue_debug = false; |
53 | - if ( file_exists( AO_CCSS_DEBUG ) ) { |
|
54 | - $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
|
55 | - $qdobj = json_decode( $qdobj_raw, true ); |
|
56 | - if ( $qdobj ) { |
|
57 | - if ( 1 === $qdobj['enable'] ) { |
|
53 | + if (file_exists(AO_CCSS_DEBUG)) { |
|
54 | + $qdobj_raw = file_get_contents(AO_CCSS_DEBUG); |
|
55 | + $qdobj = json_decode($qdobj_raw, true); |
|
56 | + if ($qdobj) { |
|
57 | + if (1 === $qdobj['enable']) { |
|
58 | 58 | $queue_debug = true; |
59 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
|
59 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue operating in debug mode with the following settings: <'.$qdobj_raw.'>', 3); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // Set some default values for $qdobj to avoid function call warnings. |
65 | - if ( ! $queue_debug ) { |
|
65 | + if (!$queue_debug) { |
|
66 | 66 | $qdobj['htcode'] = false; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Check if queue is already running. |
70 | 70 | $queue_lock = false; |
71 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
71 | + if (file_exists(AO_CCSS_LOCK)) { |
|
72 | 72 | $queue_lock = true; |
73 | 73 | } |
74 | 74 | |
75 | 75 | // Proceed with the queue if it's not already running. |
76 | - if ( ! $queue_lock ) { |
|
76 | + if (!$queue_lock) { |
|
77 | 77 | |
78 | 78 | // Log queue start and create the lock file. |
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
|
80 | - if ( touch( AO_CCSS_LOCK ) ) { |
|
81 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control started', 3); |
|
80 | + if (touch(AO_CCSS_LOCK)) { |
|
81 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control locked', 3); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Attach required variables. |
@@ -86,45 +86,45 @@ discard block |
||
86 | 86 | global $ao_ccss_rtimelimit; |
87 | 87 | |
88 | 88 | // Initialize counters. |
89 | - if ( $ao_ccss_rtimelimit == 0 ) { |
|
89 | + if ($ao_ccss_rtimelimit == 0) { |
|
90 | 90 | // no time limit set, let's go with 1000 seconds. |
91 | 91 | $ao_ccss_rtimelimit = 1000; |
92 | 92 | } |
93 | 93 | $mt = time() + $ao_ccss_rtimelimit; // maxtime queue processing can run. |
94 | 94 | $jc = 1; // job count number. |
95 | 95 | $jr = 1; // jobs requests number. |
96 | - $jt = count( $ao_ccss_queue ); // number of jobs in queue. |
|
96 | + $jt = count($ao_ccss_queue); // number of jobs in queue. |
|
97 | 97 | |
98 | 98 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
99 | - array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
|
99 | + array_multisort(array_column($ao_ccss_queue, 'jqstat'), $ao_ccss_queue); // @codingStandardsIgnoreLine |
|
100 | 100 | |
101 | 101 | // Iterates over the entire queue. |
102 | - foreach ( $ao_ccss_queue as $path => $jprops ) { |
|
102 | + foreach ($ao_ccss_queue as $path => $jprops) { |
|
103 | 103 | // Prepare flags and target rule. |
104 | 104 | $update = false; |
105 | 105 | $deljob = false; |
106 | 106 | $rule_update = false; |
107 | 107 | $oldccssfile = false; |
108 | - $trule = explode( '|', $jprops['rtarget'] ); |
|
108 | + $trule = explode('|', $jprops['rtarget']); |
|
109 | 109 | |
110 | 110 | // Log job count. |
111 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
|
111 | + autoptimizeCriticalCSSCore::ao_ccss_log('Processing job '.$jc.' of '.$jt.' with id <'.$jprops['ljid'].'> and status <'.$jprops['jqstat'].'>', 3); |
|
112 | 112 | |
113 | 113 | // Process NEW jobs. |
114 | - if ( 'NEW' == $jprops['jqstat'] ) { |
|
114 | + if ('NEW' == $jprops['jqstat']) { |
|
115 | 115 | |
116 | 116 | // Log the new job. |
117 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
|
117 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found NEW job with local ID <'.$jprops['ljid'].'>, starting its queue processing', 3); |
|
118 | 118 | |
119 | 119 | // Compare job and rule hashes (if any). |
120 | - $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
|
120 | + $hash = $this->ao_ccss_diff_hashes($jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget']); |
|
121 | 121 | |
122 | 122 | // If job hash is new or different of a previous one. |
123 | - if ( $hash ) { |
|
124 | - if ( $jr > 2 ) { |
|
123 | + if ($hash) { |
|
124 | + if ($jr > 2) { |
|
125 | 125 | // we already posted 2 jobs to criticalcss.com, don't post more this run |
126 | 126 | // but we can keep on processing the queue to keep it tidy. |
127 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Holding off on generating request for job with local ID <' . $jprops['ljid'] . '>, maximum number of POSTS reached.', 3 ); |
|
127 | + autoptimizeCriticalCSSCore::ao_ccss_log('Holding off on generating request for job with local ID <'.$jprops['ljid'].'>, maximum number of POSTS reached.', 3); |
|
128 | 128 | continue; |
129 | 129 | } |
130 | 130 | |
@@ -132,182 +132,182 @@ discard block |
||
132 | 132 | $jprops['hash'] = $hash; |
133 | 133 | |
134 | 134 | // Dispatch the job generate request and increment request count. |
135 | - $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
|
135 | + $apireq = $this->ao_ccss_api_generate($path, $queue_debug, $qdobj['htcode']); |
|
136 | 136 | $jr++; |
137 | 137 | |
138 | 138 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
139 | - if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
|
139 | + if ('JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status']) { |
|
140 | 140 | // SUCCESS: request has a valid result. |
141 | 141 | // Update job properties. |
142 | 142 | $jprops['jid'] = $apireq['job']['id']; |
143 | 143 | $jprops['jqstat'] = $apireq['job']['status']; |
144 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
145 | - } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
144 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request successful, remote id <'.$jprops['jid'].'>, status now is <'.$jprops['jqstat'].'>', 3); |
|
145 | + } elseif ('STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
146 | 146 | // ERROR: concurrent requests |
147 | 147 | // Update job properties. |
148 | 148 | $jprops['jid'] = $apireq['job']['id']; |
149 | 149 | $jprops['jqstat'] = $apireq['job']['status']; |
150 | - if ( $apireq['job']['error'] ) { |
|
150 | + if ($apireq['job']['error']) { |
|
151 | 151 | $jprops['jrstat'] = $apireq['job']['error']; |
152 | 152 | } else { |
153 | 153 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
154 | 154 | } |
155 | 155 | $jprops['jvstat'] = 'NONE'; |
156 | - $jprops['jftime'] = microtime( true ); |
|
157 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
158 | - } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
|
156 | + $jprops['jftime'] = microtime(true); |
|
157 | + autoptimizeCriticalCSSCore::ao_ccss_log('Concurrent requests when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
158 | + } elseif ('INVALID_JWT_TOKEN' == $apireq['errorCode']) { |
|
159 | 159 | // ERROR: key validation |
160 | 160 | // Update job properties. |
161 | 161 | $jprops['jqstat'] = $apireq['errorCode']; |
162 | 162 | $jprops['jrstat'] = $apireq['error']; |
163 | 163 | $jprops['jvstat'] = 'NONE'; |
164 | - $jprops['jftime'] = microtime( true ); |
|
165 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
166 | - } elseif ( empty( $apireq ) ) { |
|
164 | + $jprops['jftime'] = microtime(true); |
|
165 | + autoptimizeCriticalCSSCore::ao_ccss_log('API key validation error when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
166 | + } elseif (empty($apireq)) { |
|
167 | 167 | // ERROR: no response |
168 | 168 | // Update job properties. |
169 | 169 | $jprops['jqstat'] = 'NO_RESPONSE'; |
170 | 170 | $jprops['jrstat'] = 'NONE'; |
171 | 171 | $jprops['jvstat'] = 'NONE'; |
172 | - $jprops['jftime'] = microtime( true ); |
|
173 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
172 | + $jprops['jftime'] = microtime(true); |
|
173 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
174 | 174 | } else { |
175 | 175 | // UNKNOWN: unhandled generate exception |
176 | 176 | // Update job properties. |
177 | 177 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
178 | 178 | $jprops['jrstat'] = 'NONE'; |
179 | 179 | $jprops['jvstat'] = 'NONE'; |
180 | - $jprops['jftime'] = microtime( true ); |
|
181 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
182 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
180 | + $jprops['jftime'] = microtime(true); |
|
181 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
182 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
183 | 183 | } |
184 | 184 | } else { |
185 | 185 | // SUCCESS: Job hash is equal to a previous one, so it's done |
186 | 186 | // Update job status and finish time. |
187 | 187 | $jprops['jqstat'] = 'JOB_DONE'; |
188 | - $jprops['jftime'] = microtime( true ); |
|
189 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
188 | + $jprops['jftime'] = microtime(true); |
|
189 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> requires no further processing, status now is <'.$jprops['jqstat'].'>', 3); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | // Set queue update flag. |
193 | 193 | $update = true; |
194 | 194 | |
195 | - } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
|
195 | + } elseif ('JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat']) { |
|
196 | 196 | // Process QUEUED and ONGOING jobs |
197 | 197 | // Log the pending job. |
198 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
|
198 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found PENDING job with local ID <'.$jprops['ljid'].'>, continuing its queue processing', 3); |
|
199 | 199 | |
200 | 200 | // Dispatch the job result request and increment request count. |
201 | - $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
|
201 | + $apireq = $this->ao_ccss_api_results($jprops['jid'], $queue_debug, $qdobj['htcode']); |
|
202 | 202 | |
203 | 203 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
204 | 204 | // Replace API response values if queue debugging is enabled and some value is set. |
205 | - if ( $queue_debug ) { |
|
206 | - if ( $qdobj['status'] ) { |
|
205 | + if ($queue_debug) { |
|
206 | + if ($qdobj['status']) { |
|
207 | 207 | $apireq['status'] = $qdobj['status']; |
208 | 208 | } |
209 | - if ( $qdobj['resultStatus'] ) { |
|
209 | + if ($qdobj['resultStatus']) { |
|
210 | 210 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
211 | 211 | } |
212 | - if ( $qdobj['validationStatus'] ) { |
|
212 | + if ($qdobj['validationStatus']) { |
|
213 | 213 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | - if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
|
217 | + if ('JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status']) { |
|
218 | 218 | // SUCCESS: request has a valid result |
219 | 219 | // Process a PENDING job |
220 | 220 | // Update job properties. |
221 | 221 | $jprops['jqstat'] = $apireq['status']; |
222 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
|
223 | - } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
|
222 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'> unchanged', 3); |
|
223 | + } elseif ('JOB_DONE' == $apireq['status']) { |
|
224 | 224 | // Process a DONE job |
225 | 225 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
226 | - if ( 'HTML_404' == $apireq['resultStatus'] ) { |
|
226 | + if ('HTML_404' == $apireq['resultStatus']) { |
|
227 | 227 | $apireq['resultStatus'] = 'GOOD'; |
228 | 228 | } |
229 | 229 | |
230 | - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
|
230 | + if ('GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus']) { |
|
231 | 231 | // SUCCESS: GOOD job with GOOD validation |
232 | 232 | // Update job properties. |
233 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
|
233 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, false); |
|
234 | 234 | $jprops['jqstat'] = $apireq['status']; |
235 | 235 | $jprops['jrstat'] = $apireq['resultStatus']; |
236 | 236 | $jprops['jvstat'] = $apireq['validationStatus']; |
237 | - $jprops['jftime'] = microtime( true ); |
|
237 | + $jprops['jftime'] = microtime(true); |
|
238 | 238 | $rule_update = true; |
239 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
|
240 | - } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
|
239 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'>, file saved <'.$jprops['file'].'>', 3); |
|
240 | + } elseif ('GOOD' == $apireq['resultStatus'] && ('WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'])) { |
|
241 | 241 | // SUCCESS: GOOD job with WARN or BAD validation |
242 | 242 | // Update job properties. |
243 | 243 | $jprops['jqstat'] = $apireq['status']; |
244 | 244 | $jprops['jrstat'] = $apireq['resultStatus']; |
245 | 245 | $jprops['jvstat'] = $apireq['validationStatus']; |
246 | - $jprops['jftime'] = microtime( true ); |
|
247 | - if ( apply_filters( 'autoptimize_filter_ccss_save_review_rules', true ) ) { |
|
248 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
|
246 | + $jprops['jftime'] = microtime(true); |
|
247 | + if (apply_filters('autoptimize_filter_ccss_save_review_rules', true)) { |
|
248 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, true); |
|
249 | 249 | $rule_update = true; |
250 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
|
250 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but requires REVIEW', 3); |
|
251 | 251 | } else { |
252 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but required REVIEW so not saved.', 3 ); |
|
252 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but required REVIEW so not saved.', 3); |
|
253 | 253 | } |
254 | - } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
|
254 | + } elseif ('GOOD' != $apireq['resultStatus'] && ('GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'])) { |
|
255 | 255 | // ERROR: no GOOD, WARN or BAD results |
256 | 256 | // Update job properties. |
257 | 257 | $jprops['jqstat'] = $apireq['status']; |
258 | 258 | $jprops['jrstat'] = $apireq['resultStatus']; |
259 | 259 | $jprops['jvstat'] = $apireq['validationStatus']; |
260 | - $jprops['jftime'] = microtime( true ); |
|
261 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
260 | + $jprops['jftime'] = microtime(true); |
|
261 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
262 | 262 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
263 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
263 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
264 | 264 | } else { |
265 | 265 | // UNKNOWN: unhandled JOB_DONE exception |
266 | 266 | // Update job properties. |
267 | 267 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
268 | 268 | $jprops['jrstat'] = $apireq['resultStatus']; |
269 | 269 | $jprops['jvstat'] = $apireq['validationStatus']; |
270 | - $jprops['jftime'] = microtime( true ); |
|
271 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
|
270 | + $jprops['jftime'] = microtime(true); |
|
271 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job is UNKNOWN, status now is <'.$jprops['jqstat'].'>', 2); |
|
272 | 272 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
273 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
273 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
274 | 274 | } |
275 | - } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
275 | + } elseif ('JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
276 | 276 | // ERROR: failed job |
277 | 277 | // Update job properties. |
278 | 278 | $jprops['jqstat'] = $apireq['job']['status']; |
279 | - if ( $apireq['job']['error'] ) { |
|
279 | + if ($apireq['job']['error']) { |
|
280 | 280 | $jprops['jrstat'] = $apireq['job']['error']; |
281 | 281 | } else { |
282 | 282 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
283 | 283 | } |
284 | 284 | $jprops['jvstat'] = 'NONE'; |
285 | - $jprops['jftime'] = microtime( true ); |
|
286 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
287 | - } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
285 | + $jprops['jftime'] = microtime(true); |
|
286 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
287 | + } elseif ('This css no longer exists. Please re-generate it.' == $apireq['error']) { |
|
288 | 288 | // ERROR: CSS doesn't exist |
289 | 289 | // Update job properties. |
290 | 290 | $jprops['jqstat'] = 'NO_CSS'; |
291 | 291 | $jprops['jrstat'] = $apireq['error']; |
292 | 292 | $jprops['jvstat'] = 'NONE'; |
293 | - $jprops['jftime'] = microtime( true ); |
|
294 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
295 | - } elseif ( empty( $apireq ) ) { |
|
293 | + $jprops['jftime'] = microtime(true); |
|
294 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
295 | + } elseif (empty($apireq)) { |
|
296 | 296 | // ERROR: no response |
297 | 297 | // Update job properties. |
298 | 298 | $jprops['jqstat'] = 'NO_RESPONSE'; |
299 | 299 | $jprops['jrstat'] = 'NONE'; |
300 | 300 | $jprops['jvstat'] = 'NONE'; |
301 | - $jprops['jftime'] = microtime( true ); |
|
302 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
301 | + $jprops['jftime'] = microtime(true); |
|
302 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
303 | 303 | } else { |
304 | 304 | // UNKNOWN: unhandled results exception |
305 | 305 | // Update job properties. |
306 | 306 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
307 | 307 | $jprops['jrstat'] = 'NONE'; |
308 | 308 | $jprops['jvstat'] = 'NONE'; |
309 | - $jprops['jftime'] = microtime( true ); |
|
310 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
309 | + $jprops['jftime'] = microtime(true); |
|
310 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | // Set queue update flag. |
@@ -315,40 +315,40 @@ discard block |
||
315 | 315 | } |
316 | 316 | |
317 | 317 | // Mark DONE jobs for removal. |
318 | - if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
|
318 | + if ('JOB_DONE' == $jprops['jqstat']) { |
|
319 | 319 | $update = true; |
320 | 320 | $deljob = true; |
321 | 321 | } |
322 | 322 | |
323 | 323 | // Persist updated queue object. |
324 | - if ( $update ) { |
|
325 | - if ( ! $deljob ) { |
|
324 | + if ($update) { |
|
325 | + if (!$deljob) { |
|
326 | 326 | // Update properties of a NEW or PENDING job... |
327 | - $ao_ccss_queue[ $path ] = $jprops; |
|
327 | + $ao_ccss_queue[$path] = $jprops; |
|
328 | 328 | } else { |
329 | 329 | // ...or remove the DONE job. |
330 | - unset( $ao_ccss_queue[ $path ] ); |
|
331 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
|
330 | + unset($ao_ccss_queue[$path]); |
|
331 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> is DONE and was removed from the queue', 3); |
|
332 | 332 | } |
333 | 333 | |
334 | 334 | // Update queue object. |
335 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
336 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
337 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
|
335 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
336 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
337 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue updated by job id <'.$jprops['ljid'].'>', 3); |
|
338 | 338 | |
339 | 339 | // Update target rule. |
340 | - if ( $rule_update ) { |
|
341 | - $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
|
342 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
|
340 | + if ($rule_update) { |
|
341 | + $this->ao_ccss_rule_update($jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash']); |
|
342 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> updated the target rule <'.$jprops['rtarget'].'>', 3); |
|
343 | 343 | } |
344 | 344 | } else { |
345 | 345 | // Or log no queue action. |
346 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
|
346 | + autoptimizeCriticalCSSCore::ao_ccss_log('Nothing to do on this job', 3); |
|
347 | 347 | } |
348 | 348 | |
349 | 349 | // Break the loop if request time limit is (almost exceeded). |
350 | - if ( time() > $mt ) { |
|
351 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'The time limit of ' . $ao_ccss_rtimelimit . ' seconds was exceeded, queue control must finish now', 3 ); |
|
350 | + if (time() > $mt) { |
|
351 | + autoptimizeCriticalCSSCore::ao_ccss_log('The time limit of '.$ao_ccss_rtimelimit.' seconds was exceeded, queue control must finish now', 3); |
|
352 | 352 | break; |
353 | 353 | } |
354 | 354 | |
@@ -357,46 +357,46 @@ discard block |
||
357 | 357 | } |
358 | 358 | |
359 | 359 | // Remove the lock file and log the queue end. |
360 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
361 | - unlink( AO_CCSS_LOCK ); |
|
362 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
|
360 | + if (file_exists(AO_CCSS_LOCK)) { |
|
361 | + unlink(AO_CCSS_LOCK); |
|
362 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control unlocked', 3); |
|
363 | 363 | } |
364 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
|
364 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control finished', 3); |
|
365 | 365 | |
366 | 366 | // Log that queue is locked. |
367 | 367 | } else { |
368 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
|
368 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue is already running, skipping the attempt to run it again', 3); |
|
369 | 369 | } |
370 | 370 | } |
371 | 371 | |
372 | - public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) { |
|
372 | + public function ao_ccss_diff_hashes($ljid, $hash, $hashes, $rule) { |
|
373 | 373 | // Compare job hashes |
374 | 374 | // STEP 1: update job hashes. |
375 | - if ( 1 == count( $hashes ) ) { |
|
375 | + if (1 == count($hashes)) { |
|
376 | 376 | // Job with a single hash |
377 | 377 | // Set job hash. |
378 | 378 | $hash = $hashes[0]; |
379 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 ); |
|
379 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with SINGLE hash <'.$hash.'>', 3); |
|
380 | 380 | } else { |
381 | 381 | // Job with multiple hashes |
382 | 382 | // Loop through hashes to concatenate them. |
383 | 383 | $nhash = ''; |
384 | - foreach ( $hashes as $shash ) { |
|
384 | + foreach ($hashes as $shash) { |
|
385 | 385 | $nhash .= $shash; |
386 | 386 | } |
387 | 387 | |
388 | 388 | // Set job hash. |
389 | - $hash = md5( $nhash ); |
|
390 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 ); |
|
389 | + $hash = md5($nhash); |
|
390 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with a COMPOSITE hash <'.$hash.'>', 3); |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | // STEP 2: compare job to existing jobs to prevent double submission for same type+hash. |
394 | 394 | global $ao_ccss_queue; |
395 | 395 | |
396 | - foreach ( $ao_ccss_queue as $queue_item ) { |
|
397 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 ); |
|
398 | - if ( $queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array( $queue_item['jqstat'], array( 'JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE' ) ) ) { |
|
399 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 ); |
|
396 | + foreach ($ao_ccss_queue as $queue_item) { |
|
397 | + autoptimizeCriticalCSSCore::ao_ccss_log('Comparing <'.$rule.$hash.'> with <'.$queue_item['rtarget'].$queue_item['hash'].'>', 3); |
|
398 | + if ($queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array($queue_item['jqstat'], array('JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE'))) { |
|
399 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the already pending job <'.$queue_item['ljid'].'>', 3); |
|
400 | 400 | return false; |
401 | 401 | } |
402 | 402 | } |
@@ -406,32 +406,32 @@ discard block |
||
406 | 406 | global $ao_ccss_rules; |
407 | 407 | |
408 | 408 | // Prepare rule variables. |
409 | - $trule = explode( '|', $rule ); |
|
410 | - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
409 | + $trule = explode('|', $rule); |
|
410 | + $srule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
411 | 411 | |
412 | 412 | // Check if a MANUAL rule exist and return false. |
413 | - if ( ! empty( $srule ) && ( 0 == $srule['hash'] && 0 != $srule['file'] ) ) { |
|
414 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
413 | + if (!empty($srule) && (0 == $srule['hash'] && 0 != $srule['file'])) { |
|
414 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the MANUAL rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
415 | 415 | return false; |
416 | - } elseif ( ! empty( $srule ) ) { |
|
416 | + } elseif (!empty($srule)) { |
|
417 | 417 | // Check if an AUTO rule exist. |
418 | - if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) { |
|
418 | + if ($hash === $srule['hash'] && is_file(AO_CCSS_DIR.$srule['file']) && 0 != filesize(AO_CCSS_DIR.$srule['file'])) { |
|
419 | 419 | // Check if job hash matches rule, if the CCSS file exists said file is not empty and return FALSE is so. |
420 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
420 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> MATCH the one in rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
421 | 421 | return false; |
422 | 422 | } else { |
423 | 423 | // Or return the new hash if they differ. |
424 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> DOES NOT MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '> or rule\'s CCSS file was invalid.', 3 ); |
|
424 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> DOES NOT MATCH the one in rule <'.$trule[0].'|'.$trule[1].'> or rule\'s CCSS file was invalid.', 3); |
|
425 | 425 | return $hash; |
426 | 426 | } |
427 | 427 | } else { |
428 | 428 | // Or just return the hash if no rule exist yet. |
429 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 ); |
|
429 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> has no rule yet', 3); |
|
430 | 430 | return $hash; |
431 | 431 | } |
432 | 432 | } |
433 | 433 | |
434 | - public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
|
434 | + public function ao_ccss_api_generate($path, $debug, $dcode) { |
|
435 | 435 | // POST jobs to criticalcss.com and return responses |
436 | 436 | // Get key and key status. |
437 | 437 | global $ao_ccss_key; |
@@ -443,35 +443,35 @@ discard block |
||
443 | 443 | global $ao_ccss_noptimize; |
444 | 444 | |
445 | 445 | $site_host = get_site_url(); |
446 | - $site_path = parse_url( $site_host, PHP_URL_PATH ); |
|
446 | + $site_path = parse_url($site_host, PHP_URL_PATH); |
|
447 | 447 | |
448 | - if ( ! empty( $site_path ) ) { |
|
449 | - $site_host = str_replace( $site_path, '', $site_host ); |
|
448 | + if (!empty($site_path)) { |
|
449 | + $site_host = str_replace($site_path, '', $site_host); |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | // Logic to bind to one domain to avoid site clones of sites would |
453 | 453 | // automatically begin spawning requests to criticalcss.com which has |
454 | 454 | // a per domain cost. |
455 | 455 | global $ao_ccss_domain; |
456 | - if ( empty( $ao_ccss_domain ) ) { |
|
456 | + if (empty($ao_ccss_domain)) { |
|
457 | 457 | // first request being done, update option to allow future requests are only allowed if from same domain. |
458 | - update_option( 'autoptimize_ccss_domain', str_rot13( $site_host ) ); |
|
459 | - } elseif ( trim( $ao_ccss_domain, '\'"' ) !== 'none' && parse_url( $site_host, PHP_URL_HOST ) !== parse_url( $ao_ccss_domain, PHP_URL_HOST ) && apply_filters( 'autoptimize_filter_ccss_bind_domain', true ) ) { |
|
458 | + update_option('autoptimize_ccss_domain', str_rot13($site_host)); |
|
459 | + } elseif (trim($ao_ccss_domain, '\'"') !== 'none' && parse_url($site_host, PHP_URL_HOST) !== parse_url($ao_ccss_domain, PHP_URL_HOST) && apply_filters('autoptimize_filter_ccss_bind_domain', true)) { |
|
460 | 460 | // not the same domain, log as error and return without posting to criticalcss.com. |
461 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $ao_ccss_domain . ' so not proceeding.', 2 ); |
|
461 | + autoptimizeCriticalCSSCore::ao_ccss_log('Request for domain '.$site_host.' does not match bound domain '.$ao_ccss_domain.' so not proceeding.', 2); |
|
462 | 462 | return false; |
463 | 463 | } |
464 | 464 | |
465 | - $src_url = $site_host . $path; |
|
465 | + $src_url = $site_host.$path; |
|
466 | 466 | |
467 | 467 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
468 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
468 | + if (!empty($ao_ccss_noptimize)) { |
|
469 | 469 | $src_url .= '?ao_noptirocket=1'; |
470 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
470 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
471 | 471 | $src_url .= '?ao_nolazy=1'; |
472 | 472 | } |
473 | 473 | |
474 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
474 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
475 | 475 | |
476 | 476 | // Initialize request body. |
477 | 477 | $body = array(); |
@@ -481,88 +481,88 @@ discard block |
||
481 | 481 | |
482 | 482 | // Prepare and add viewport size to the body if available. |
483 | 483 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
484 | - if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) { |
|
484 | + if (!empty($viewport['w']) && !empty($viewport['h'])) { |
|
485 | 485 | $body['width'] = $viewport['w']; |
486 | 486 | $body['height'] = $viewport['h']; |
487 | 487 | } |
488 | 488 | |
489 | 489 | // Prepare and add forceInclude to the body if available. |
490 | 490 | global $ao_ccss_finclude; |
491 | - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude ); |
|
492 | - if ( ! empty( $finclude ) ) { |
|
491 | + $finclude = $this->ao_ccss_finclude($ao_ccss_finclude); |
|
492 | + if (!empty($finclude)) { |
|
493 | 493 | $body['forceInclude'] = $finclude; |
494 | 494 | } |
495 | 495 | |
496 | 496 | // Add filter to allow the body array to be altered (e.g. to add customPageHeaders). |
497 | - $body = apply_filters( 'autoptimize_ccss_cron_api_generate_body', $body ); |
|
497 | + $body = apply_filters('autoptimize_ccss_cron_api_generate_body', $body); |
|
498 | 498 | |
499 | 499 | // Body must be json and log it. |
500 | - $body = json_encode( $body ); |
|
501 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 ); |
|
500 | + $body = json_encode($body); |
|
501 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request body is '.$body, 3); |
|
502 | 502 | |
503 | 503 | // Prepare the request. |
504 | - $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER ); |
|
504 | + $url = esc_url_raw(AO_CCSS_API.'generate?aover='.AO_CCSS_VER); |
|
505 | 505 | $args = array( |
506 | 506 | 'headers' => array( |
507 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
507 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
508 | 508 | 'Content-type' => 'application/json; charset=utf-8', |
509 | - 'Authorization' => 'JWT ' . $key, |
|
509 | + 'Authorization' => 'JWT '.$key, |
|
510 | 510 | 'Connection' => 'close', |
511 | 511 | ), |
512 | 512 | 'body' => $body, |
513 | 513 | ); |
514 | 514 | |
515 | 515 | // Dispatch the request and store its response code. |
516 | - $req = wp_safe_remote_post( $url, $args ); |
|
517 | - $code = wp_remote_retrieve_response_code( $req ); |
|
518 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
516 | + $req = wp_safe_remote_post($url, $args); |
|
517 | + $code = wp_remote_retrieve_response_code($req); |
|
518 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
519 | 519 | |
520 | - if ( $debug && $dcode ) { |
|
520 | + if ($debug && $dcode) { |
|
521 | 521 | // If queue debug is active, change response code. |
522 | 522 | $code = $dcode; |
523 | 523 | } |
524 | 524 | |
525 | - if ( 200 == $code ) { |
|
525 | + if (200 == $code) { |
|
526 | 526 | // Response code is OK. |
527 | 527 | // Workaround criticalcss.com non-RESTful reponses. |
528 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) { |
|
528 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status']) { |
|
529 | 529 | // Log successful and return encoded request body. |
530 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 ); |
|
530 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied successfully', 3); |
|
531 | 531 | |
532 | 532 | // This code also means the key is valid, so cache key status for 24h if not already cached. |
533 | - if ( ( ! $key_status || 2 != $key_status ) && $key ) { |
|
534 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
535 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
533 | + if ((!$key_status || 2 != $key_status) && $key) { |
|
534 | + update_option('autoptimize_ccss_keyst', 2); |
|
535 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
536 | 536 | } |
537 | 537 | |
538 | 538 | // Return the request body. |
539 | 539 | return $body; |
540 | 540 | } else { |
541 | 541 | // Log successful requests with invalid reponses. |
542 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
543 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
542 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
543 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
544 | 544 | return $body; |
545 | 545 | } |
546 | 546 | } else { |
547 | 547 | // Response code is anything else. |
548 | 548 | // Log failed request with a valid response code and return body. |
549 | - if ( $code ) { |
|
550 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
551 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
549 | + if ($code) { |
|
550 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with error code <'.$code.'>, body follows...', 2); |
|
551 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
552 | 552 | |
553 | - if ( 401 == $code ) { |
|
553 | + if (401 == $code) { |
|
554 | 554 | // If request is unauthorized, also clear key status. |
555 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
556 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
555 | + update_option('autoptimize_ccss_keyst', 1); |
|
556 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
557 | 557 | } |
558 | 558 | |
559 | 559 | // Return the request body. |
560 | 560 | return $body; |
561 | 561 | } else { |
562 | 562 | // Log failed request with no response and return false. |
563 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 ); |
|
564 | - if ( is_wp_error( $req ) ) { |
|
565 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
563 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> has no response, this could be a service timeout', 2); |
|
564 | + if (is_wp_error($req)) { |
|
565 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
566 | 566 | } |
567 | 567 | |
568 | 568 | return false; |
@@ -570,76 +570,76 @@ discard block |
||
570 | 570 | } |
571 | 571 | } |
572 | 572 | |
573 | - public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
|
573 | + public function ao_ccss_api_results($jobid, $debug, $dcode) { |
|
574 | 574 | // GET jobs from criticalcss.com and return responses |
575 | 575 | // Get key. |
576 | 576 | global $ao_ccss_key; |
577 | 577 | $key = $ao_ccss_key; |
578 | 578 | |
579 | 579 | // Prepare the request. |
580 | - $url = AO_CCSS_API . 'results?resultId=' . $jobid; |
|
580 | + $url = AO_CCSS_API.'results?resultId='.$jobid; |
|
581 | 581 | $args = array( |
582 | 582 | 'headers' => array( |
583 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
584 | - 'Authorization' => 'JWT ' . $key, |
|
583 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
584 | + 'Authorization' => 'JWT '.$key, |
|
585 | 585 | 'Connection' => 'close', |
586 | 586 | ), |
587 | 587 | ); |
588 | 588 | |
589 | 589 | // Dispatch the request and store its response code. |
590 | - $req = wp_safe_remote_get( $url, $args ); |
|
591 | - $code = wp_remote_retrieve_response_code( $req ); |
|
592 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
590 | + $req = wp_safe_remote_get($url, $args); |
|
591 | + $code = wp_remote_retrieve_response_code($req); |
|
592 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
593 | 593 | |
594 | - if ( $debug && $dcode ) { |
|
594 | + if ($debug && $dcode) { |
|
595 | 595 | // If queue debug is active, change response code. |
596 | 596 | $code = $dcode; |
597 | 597 | } |
598 | 598 | |
599 | - if ( 200 == $code ) { |
|
599 | + if (200 == $code) { |
|
600 | 600 | // Response code is OK. |
601 | - if ( is_array( $body ) && ( array_key_exists( 'status', $body ) || array_key_exists( 'job', $body ) ) && ( 'JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) ) { |
|
601 | + if (is_array($body) && (array_key_exists('status', $body) || array_key_exists('job', $body)) && ('JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'])) { |
|
602 | 602 | // Workaround criticalcss.com non-RESTful reponses |
603 | 603 | // Log successful and return encoded request body. |
604 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 ); |
|
604 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully', 3); |
|
605 | 605 | return $body; |
606 | - } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) { |
|
606 | + } elseif (is_array($body) && (array_key_exists('error', $body) && 'This css no longer exists. Please re-generate it.' == $body['error'])) { |
|
607 | 607 | // Handle no CSS reply |
608 | 608 | // Log no CSS error and return encoded request body. |
609 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully but the CSS for it does not exist anymore', 3 ); |
|
609 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully but the CSS for it does not exist anymore', 3); |
|
610 | 610 | return $body; |
611 | 611 | } else { |
612 | 612 | // Log failed request and return false. |
613 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
614 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
613 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
614 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
615 | 615 | return false; |
616 | 616 | } |
617 | 617 | } else { |
618 | 618 | // Response code is anything else |
619 | 619 | // Log failed request with a valid response code and return body. |
620 | - if ( $code ) { |
|
621 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
622 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
623 | - if ( 401 == $code ) { |
|
620 | + if ($code) { |
|
621 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with error code <'.$code.'>, body follows...', 2); |
|
622 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
623 | + if (401 == $code) { |
|
624 | 624 | // If request is unauthorized, also clear key status. |
625 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
626 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
625 | + update_option('autoptimize_ccss_keyst', 1); |
|
626 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
627 | 627 | } |
628 | 628 | |
629 | 629 | // Return the request body. |
630 | 630 | return $body; |
631 | 631 | } else { |
632 | 632 | // Log failed request with no response and return false. |
633 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> has no response, this could be a service timeout', 2 ); |
|
633 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> has no response, this could be a service timeout', 2); |
|
634 | 634 | return false; |
635 | 635 | } |
636 | 636 | } |
637 | 637 | } |
638 | 638 | |
639 | - public function ao_ccss_save_file( $ccss, $target, $review ) { |
|
639 | + public function ao_ccss_save_file($ccss, $target, $review) { |
|
640 | 640 | // Save critical CSS into the filesystem and return its filename |
641 | 641 | // Prepare review mark. |
642 | - if ( $review ) { |
|
642 | + if ($review) { |
|
643 | 643 | $rmark = '_R'; |
644 | 644 | } else { |
645 | 645 | $rmark = ''; |
@@ -649,21 +649,21 @@ discard block |
||
649 | 649 | $filename = false; |
650 | 650 | $content = $ccss; |
651 | 651 | |
652 | - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) { |
|
652 | + if (autoptimizeCriticalCSSCore::ao_ccss_check_contents($content)) { |
|
653 | 653 | // Sanitize content, set filename and try to save file. |
654 | - $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css'; |
|
655 | - $status = file_put_contents( $file, $content, LOCK_EX ); |
|
656 | - $filename = pathinfo( $file, PATHINFO_BASENAME ); |
|
657 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file for the rule <' . $target[0] . '|' . $target[1] . '> was saved as <' . $filename . '>, size in bytes is <' . $status . '>', 3 ); |
|
654 | + $file = AO_CCSS_DIR.'ccss_'.md5($ccss.$target[1]).$rmark.'.css'; |
|
655 | + $status = file_put_contents($file, $content, LOCK_EX); |
|
656 | + $filename = pathinfo($file, PATHINFO_BASENAME); |
|
657 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file for the rule <'.$target[0].'|'.$target[1].'> was saved as <'.$filename.'>, size in bytes is <'.$status.'>', 3); |
|
658 | 658 | |
659 | - if ( ! $status ) { |
|
659 | + if (!$status) { |
|
660 | 660 | // If file has not been saved, reset filename. |
661 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 ); |
|
661 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file <'.$filename.'> could not be not saved', 2); |
|
662 | 662 | $filename = false; |
663 | 663 | return $filename; |
664 | 664 | } |
665 | 665 | } else { |
666 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 ); |
|
666 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not pass content check', 2); |
|
667 | 667 | return $filename; |
668 | 668 | } |
669 | 669 | |
@@ -673,15 +673,15 @@ discard block |
||
673 | 673 | global $ao_ccss_rules; |
674 | 674 | |
675 | 675 | // Prepare rule variables. |
676 | - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ]; |
|
676 | + $srule = $ao_ccss_rules[$target[0]][$target[1]]; |
|
677 | 677 | $oldfile = $srule['file']; |
678 | 678 | |
679 | - if ( $oldfile && $oldfile !== $filename ) { |
|
680 | - $delfile = AO_CCSS_DIR . $oldfile; |
|
681 | - if ( file_exists( $delfile ) ) { |
|
682 | - $unlinkst = unlink( $delfile ); |
|
683 | - if ( $unlinkst ) { |
|
684 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 ); |
|
679 | + if ($oldfile && $oldfile !== $filename) { |
|
680 | + $delfile = AO_CCSS_DIR.$oldfile; |
|
681 | + if (file_exists($delfile)) { |
|
682 | + $unlinkst = unlink($delfile); |
|
683 | + if ($unlinkst) { |
|
684 | + autoptimizeCriticalCSSCore::ao_ccss_log('A previous critical CSS file <'.$oldfile.'> was removed for the rule <'.$target[0].'|'.$target[1].'>', 3); |
|
685 | 685 | } |
686 | 686 | } |
687 | 687 | } |
@@ -690,29 +690,29 @@ discard block |
||
690 | 690 | return $filename; |
691 | 691 | } |
692 | 692 | |
693 | - public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) { |
|
693 | + public function ao_ccss_rule_update($ljid, $srule, $file, $hash) { |
|
694 | 694 | // Update or create a rule |
695 | 695 | // Attach required arrays. |
696 | 696 | global $ao_ccss_rules; |
697 | 697 | |
698 | 698 | // Prepare rule variables. |
699 | - $trule = explode( '|', $srule ); |
|
700 | - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
699 | + $trule = explode('|', $srule); |
|
700 | + $rule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
701 | 701 | $action = false; |
702 | 702 | $rtype = ''; |
703 | 703 | |
704 | - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) { |
|
704 | + if (0 === $rule['hash'] && 0 !== $rule['file']) { |
|
705 | 705 | // manual rule, don't ever overwrite. |
706 | 706 | $action = 'NOT UPDATED'; |
707 | 707 | $rtype = 'MANUAL'; |
708 | - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) { |
|
708 | + } elseif (0 === $rule['hash'] && 0 === $rule['file']) { |
|
709 | 709 | // If this is an user created AUTO rule with no hash and file yet, update its hash and filename |
710 | 710 | // Set rule hash, file and action flag. |
711 | 711 | $rule['hash'] = $hash; |
712 | 712 | $rule['file'] = $file; |
713 | 713 | $action = 'UPDATED'; |
714 | 714 | $rtype = 'AUTO'; |
715 | - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) { |
|
715 | + } elseif (0 !== $rule['hash'] && ctype_alnum($rule['hash'])) { |
|
716 | 716 | // If this is an genuine AUTO rule, update its hash and filename |
717 | 717 | // Set rule hash, file and action flag. |
718 | 718 | $rule['hash'] = $hash; |
@@ -722,7 +722,7 @@ discard block |
||
722 | 722 | } else { |
723 | 723 | // If rule doesn't exist, create an AUTO rule |
724 | 724 | // AUTO rules were only for types, but will now also work for paths. |
725 | - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) { |
|
725 | + if ('types' == $trule[0] || 'paths' == $trule[0]) { |
|
726 | 726 | // Set rule hash and file and action flag. |
727 | 727 | $rule['hash'] = $hash; |
728 | 728 | $rule['file'] = $file; |
@@ -730,47 +730,47 @@ discard block |
||
730 | 730 | $rtype = 'AUTO'; |
731 | 731 | } else { |
732 | 732 | // Log that no rule was created. |
733 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 ); |
|
733 | + autoptimizeCriticalCSSCore::ao_ccss_log('Exception, no AUTO rule created', 3); |
|
734 | 734 | } |
735 | 735 | } |
736 | 736 | |
737 | - if ( $action ) { |
|
737 | + if ($action) { |
|
738 | 738 | // If a rule creation/update is required, persist updated rules object. |
739 | - $ao_ccss_rules[ $trule[0] ][ $trule[1] ] = $rule; |
|
740 | - $ao_ccss_rules_raw = json_encode( $ao_ccss_rules ); |
|
741 | - update_option( 'autoptimize_ccss_rules', $ao_ccss_rules_raw ); |
|
742 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 ); |
|
739 | + $ao_ccss_rules[$trule[0]][$trule[1]] = $rule; |
|
740 | + $ao_ccss_rules_raw = json_encode($ao_ccss_rules); |
|
741 | + update_option('autoptimize_ccss_rules', $ao_ccss_rules_raw); |
|
742 | + autoptimizeCriticalCSSCore::ao_ccss_log('Target rule <'.$srule.'> of type <'.$rtype.'> was '.$action.' for job id <'.$ljid.'>', 3); |
|
743 | 743 | } else { |
744 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 ); |
|
744 | + autoptimizeCriticalCSSCore::ao_ccss_log('No rule action required', 3); |
|
745 | 745 | } |
746 | 746 | } |
747 | 747 | |
748 | - function ao_ccss_finclude( $finclude_raw ) { |
|
748 | + function ao_ccss_finclude($finclude_raw) { |
|
749 | 749 | // Prepare forceInclude object. |
750 | - if ( ! empty( $finclude_raw ) ) { |
|
750 | + if (!empty($finclude_raw)) { |
|
751 | 751 | // If there are any content |
752 | 752 | // Convert raw string into arra and initialize the returning object. |
753 | - $fincludes = explode( ',', $finclude_raw ); |
|
753 | + $fincludes = explode(',', $finclude_raw); |
|
754 | 754 | $finclude = array(); |
755 | 755 | |
756 | 756 | // Interacts over every rule. |
757 | 757 | $i = 0; |
758 | - foreach ( $fincludes as $include ) { |
|
758 | + foreach ($fincludes as $include) { |
|
759 | 759 | // Trim leading and trailing whitespaces. |
760 | - $include = trim( $include ); |
|
760 | + $include = trim($include); |
|
761 | 761 | |
762 | - if ( substr( $include, 0, 2 ) === '//' ) { |
|
762 | + if (substr($include, 0, 2) === '//') { |
|
763 | 763 | // Regex rule |
764 | 764 | // Format value as required. |
765 | - $include = str_replace( '//', '/', $include ); |
|
766 | - $include = $include . '/i'; |
|
765 | + $include = str_replace('//', '/', $include); |
|
766 | + $include = $include.'/i'; |
|
767 | 767 | |
768 | 768 | // Store regex object. |
769 | - $finclude[ $i ]['type'] = 'RegExp'; |
|
770 | - $finclude[ $i ]['value'] = $include; |
|
769 | + $finclude[$i]['type'] = 'RegExp'; |
|
770 | + $finclude[$i]['value'] = $include; |
|
771 | 771 | } else { |
772 | 772 | // Simple value rule. |
773 | - $finclude[ $i ]['value'] = $include; |
|
773 | + $finclude[$i]['value'] = $include; |
|
774 | 774 | } |
775 | 775 | |
776 | 776 | $i++; |
@@ -787,54 +787,54 @@ discard block |
||
787 | 787 | public function ao_ccss_cleaning() { |
788 | 788 | // Perform plugin maintenance |
789 | 789 | // Truncate log file >= 1MB . |
790 | - if ( file_exists( AO_CCSS_LOG ) ) { |
|
791 | - if ( filesize( AO_CCSS_LOG ) >= 1048576 ) { |
|
792 | - $logfile = fopen( AO_CCSS_LOG, 'w' ); |
|
793 | - fclose( $logfile ); |
|
790 | + if (file_exists(AO_CCSS_LOG)) { |
|
791 | + if (filesize(AO_CCSS_LOG) >= 1048576) { |
|
792 | + $logfile = fopen(AO_CCSS_LOG, 'w'); |
|
793 | + fclose($logfile); |
|
794 | 794 | } |
795 | 795 | } |
796 | 796 | |
797 | 797 | // Remove lock file. |
798 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
799 | - unlink( AO_CCSS_LOCK ); |
|
798 | + if (file_exists(AO_CCSS_LOCK)) { |
|
799 | + unlink(AO_CCSS_LOCK); |
|
800 | 800 | } |
801 | 801 | |
802 | 802 | // Make sure queue processing is scheduled, recreate if not. |
803 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
804 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
803 | + if (!wp_next_scheduled('ao_ccss_queue')) { |
|
804 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
805 | 805 | } |
806 | 806 | |
807 | 807 | // Queue cleaning. |
808 | 808 | global $ao_ccss_queue; |
809 | 809 | $queue_purge_threshold = 100; |
810 | - $queue_purge_age = 24 * 60 * 60; |
|
811 | - $queue_length = count( $ao_ccss_queue ); |
|
812 | - $timestamp_yesterday = microtime( true ) - $queue_purge_age; |
|
810 | + $queue_purge_age = 24*60*60; |
|
811 | + $queue_length = count($ao_ccss_queue); |
|
812 | + $timestamp_yesterday = microtime(true) - $queue_purge_age; |
|
813 | 813 | $remove_old_new = false; |
814 | 814 | $queue_altered = false; |
815 | 815 | |
816 | - if ( $queue_length > $queue_purge_threshold ) { |
|
816 | + if ($queue_length > $queue_purge_threshold) { |
|
817 | 817 | $remove_old_new = true; |
818 | 818 | } |
819 | 819 | |
820 | - foreach ( $ao_ccss_queue as $path => $job ) { |
|
821 | - if ( ( $remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday ) || in_array( $job['jqstat'], array( 'JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE' ) ) ) { |
|
822 | - unset( $ao_ccss_queue[ $path ] ); |
|
820 | + foreach ($ao_ccss_queue as $path => $job) { |
|
821 | + if (($remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday) || in_array($job['jqstat'], array('JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE'))) { |
|
822 | + unset($ao_ccss_queue[$path]); |
|
823 | 823 | $queue_altered = true; |
824 | 824 | } |
825 | 825 | } |
826 | 826 | |
827 | 827 | // save queue to options! |
828 | - if ( $queue_altered ) { |
|
829 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
830 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
831 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue cleaning done.', 3 ); |
|
828 | + if ($queue_altered) { |
|
829 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
830 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
831 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue cleaning done.', 3); |
|
832 | 832 | } |
833 | 833 | |
834 | 834 | // re-check key if invalid. |
835 | 835 | global $ao_ccss_keyst; |
836 | - if ( 1 == $ao_ccss_keyst ) { |
|
837 | - $this->ao_ccss_api_generate( '', '', '' ); |
|
836 | + if (1 == $ao_ccss_keyst) { |
|
837 | + $this->ao_ccss_api_generate('', '', ''); |
|
838 | 838 | } |
839 | 839 | } |
840 | 840 | } |
@@ -17,66 +17,66 @@ discard block |
||
17 | 17 | */ |
18 | 18 | |
19 | 19 | |
20 | -if ( ! defined( 'ABSPATH' ) ) { |
|
20 | +if (!defined('ABSPATH')) { |
|
21 | 21 | exit; |
22 | 22 | } |
23 | 23 | |
24 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.8.0' ); |
|
24 | +define('AUTOPTIMIZE_PLUGIN_VERSION', '2.8.0'); |
|
25 | 25 | |
26 | 26 | // plugin_dir_path() returns the trailing slash! |
27 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|
28 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); |
|
27 | +define('AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
|
28 | +define('AUTOPTIMIZE_PLUGIN_FILE', __FILE__); |
|
29 | 29 | |
30 | 30 | // Bail early if attempting to run on non-supported php versions. |
31 | -if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { |
|
31 | +if (version_compare(PHP_VERSION, '5.6', '<')) { |
|
32 | 32 | function autoptimize_incompatible_admin_notice() { |
33 | - echo '<div class="error"><p>' . __( 'Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize' ) . '</p></div>'; |
|
34 | - if ( isset( $_GET['activate'] ) ) { |
|
35 | - unset( $_GET['activate'] ); |
|
33 | + echo '<div class="error"><p>'.__('Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize').'</p></div>'; |
|
34 | + if (isset($_GET['activate'])) { |
|
35 | + unset($_GET['activate']); |
|
36 | 36 | } |
37 | 37 | } |
38 | 38 | function autoptimize_deactivate_self() { |
39 | - deactivate_plugins( plugin_basename( AUTOPTIMIZE_PLUGIN_FILE ) ); |
|
39 | + deactivate_plugins(plugin_basename(AUTOPTIMIZE_PLUGIN_FILE)); |
|
40 | 40 | } |
41 | - add_action( 'admin_notices', 'autoptimize_incompatible_admin_notice' ); |
|
42 | - add_action( 'admin_init', 'autoptimize_deactivate_self' ); |
|
41 | + add_action('admin_notices', 'autoptimize_incompatible_admin_notice'); |
|
42 | + add_action('admin_init', 'autoptimize_deactivate_self'); |
|
43 | 43 | return; |
44 | 44 | } |
45 | 45 | |
46 | -function autoptimize_autoload( $class_name ) { |
|
47 | - if ( in_array( $class_name, array( 'Minify_HTML', 'JSMin' ) ) ) { |
|
48 | - $file = strtolower( $class_name ); |
|
49 | - $file = str_replace( '_', '-', $file ); |
|
50 | - $path = dirname( __FILE__ ) . '/classes/external/php/'; |
|
51 | - $filepath = $path . $file . '.php'; |
|
52 | - } elseif ( false !== strpos( $class_name, 'Autoptimize\\tubalmartin\\CssMin' ) ) { |
|
53 | - $file = str_replace( 'Autoptimize\\tubalmartin\\CssMin\\', '', $class_name ); |
|
54 | - $path = dirname( __FILE__ ) . '/classes/external/php/yui-php-cssmin-bundled/'; |
|
55 | - $filepath = $path . $file . '.php'; |
|
56 | - } elseif ( 'autoptimize' === substr( $class_name, 0, 11 ) ) { |
|
46 | +function autoptimize_autoload($class_name) { |
|
47 | + if (in_array($class_name, array('Minify_HTML', 'JSMin'))) { |
|
48 | + $file = strtolower($class_name); |
|
49 | + $file = str_replace('_', '-', $file); |
|
50 | + $path = dirname(__FILE__).'/classes/external/php/'; |
|
51 | + $filepath = $path.$file.'.php'; |
|
52 | + } elseif (false !== strpos($class_name, 'Autoptimize\\tubalmartin\\CssMin')) { |
|
53 | + $file = str_replace('Autoptimize\\tubalmartin\\CssMin\\', '', $class_name); |
|
54 | + $path = dirname(__FILE__).'/classes/external/php/yui-php-cssmin-bundled/'; |
|
55 | + $filepath = $path.$file.'.php'; |
|
56 | + } elseif ('autoptimize' === substr($class_name, 0, 11)) { |
|
57 | 57 | // One of our "old" classes. |
58 | 58 | $file = $class_name; |
59 | - $path = dirname( __FILE__ ) . '/classes/'; |
|
60 | - $filepath = $path . $file . '.php'; |
|
61 | - } elseif ( 'PAnD' === $class_name ) { |
|
59 | + $path = dirname(__FILE__).'/classes/'; |
|
60 | + $filepath = $path.$file.'.php'; |
|
61 | + } elseif ('PAnD' === $class_name) { |
|
62 | 62 | $file = 'persist-admin-notices-dismissal'; |
63 | - $path = dirname( __FILE__ ) . '/classes/external/php/persist-admin-notices-dismissal/'; |
|
64 | - $filepath = $path . $file . '.php'; |
|
63 | + $path = dirname(__FILE__).'/classes/external/php/persist-admin-notices-dismissal/'; |
|
64 | + $filepath = $path.$file.'.php'; |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // If we didn't match one of our rules, bail! |
68 | - if ( ! isset( $filepath ) ) { |
|
68 | + if (!isset($filepath)) { |
|
69 | 69 | return; |
70 | 70 | } |
71 | 71 | |
72 | 72 | require $filepath; |
73 | 73 | } |
74 | 74 | |
75 | -spl_autoload_register( 'autoptimize_autoload' ); |
|
75 | +spl_autoload_register('autoptimize_autoload'); |
|
76 | 76 | |
77 | 77 | // Load WP CLI command(s) on demand. |
78 | -if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
79 | - require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
|
78 | +if (defined('WP_CLI') && WP_CLI) { |
|
79 | + require AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCLI.php'; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | function autoptimize() { |
88 | 88 | static $plugin = null; |
89 | 89 | |
90 | - if ( null === $plugin ) { |
|
91 | - $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); |
|
90 | + if (null === $plugin) { |
|
91 | + $plugin = new autoptimizeMain(AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | return $plugin; |