Completed
Push — master ( 2ef6f7...6fcb0f )
by frank
01:45
created
classes/autoptimizeCriticalCSSSettingsAjax.php 1 patch
Spacing   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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['rlimit']     = get_option( 'autoptimize_ccss_rlimit' );
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['rlimit']     = get_option('autoptimize_ccss_rlimit');
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,44 +275,44 @@  discard block
 block discarded – undo
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 ( ! $_FILES['file']['error'] ) {
284
+        if (!$_FILES['file']['error']) {
285 285
             // Save file to the cache directory.
286
-            $zipfile = AO_CCSS_DIR . $_FILES['file']['name'];
287
-            move_uploaded_file( $_FILES['file']['tmp_name'], $zipfile );
286
+            $zipfile = AO_CCSS_DIR.$_FILES['file']['name'];
287
+            move_uploaded_file($_FILES['file']['tmp_name'], $zipfile);
288 288
 
289 289
             // Extract archive.
290 290
             $zip = new ZipArchive;
291
-            if ( $zip->open( $zipfile ) === true ) {
292
-                $zip->extractTo( AO_CCSS_DIR );
291
+            if ($zip->open($zipfile) === true) {
292
+                $zip->extractTo(AO_CCSS_DIR);
293 293
                 $zip->close();
294 294
             } else {
295 295
                 $error = 'extracting';
296 296
             }
297 297
 
298
-            if ( ! $error ) {
298
+            if (!$error) {
299 299
                 // Archive extraction ok, continue settings importing
300 300
                 // Settings file.
301
-                $importfile = AO_CCSS_DIR . 'settings.json';
301
+                $importfile = AO_CCSS_DIR.'settings.json';
302 302
 
303
-                if ( file_exists( $importfile ) ) {
303
+                if (file_exists($importfile)) {
304 304
                     // Get settings and turn them into an object.
305
-                    $settings = json_decode( file_get_contents( $importfile ), true );
305
+                    $settings = json_decode(file_get_contents($importfile), true);
306 306
 
307 307
                     // Update options.
308
-                    update_option( 'autoptimize_ccss_rules', $settings['rules'] );
309
-                    update_option( 'autoptimize_ccss_additional', $settings['additional'] );
310
-                    update_option( 'autoptimize_ccss_viewport', $settings['viewport'] );
311
-                    update_option( 'autoptimize_ccss_finclude', $settings['finclude'] );
312
-                    update_option( 'autoptimize_ccss_rlimit', $settings['rlimit'] );
313
-                    update_option( 'autoptimize_ccss_noptimize', $settings['noptimize'] );
314
-                    update_option( 'autoptimize_ccss_debug', $settings['debug'] );
315
-                    update_option( 'autoptimize_ccss_key', $settings['key'] );
308
+                    update_option('autoptimize_ccss_rules', $settings['rules']);
309
+                    update_option('autoptimize_ccss_additional', $settings['additional']);
310
+                    update_option('autoptimize_ccss_viewport', $settings['viewport']);
311
+                    update_option('autoptimize_ccss_finclude', $settings['finclude']);
312
+                    update_option('autoptimize_ccss_rlimit', $settings['rlimit']);
313
+                    update_option('autoptimize_ccss_noptimize', $settings['noptimize']);
314
+                    update_option('autoptimize_ccss_debug', $settings['debug']);
315
+                    update_option('autoptimize_ccss_key', $settings['key']);
316 316
                 } else {
317 317
                     // Settings file doesn't exist, update error flag.
318 318
                     $error = 'settings file does not exist';
@@ -321,28 +321,28 @@  discard block
 block discarded – undo
321 321
         }
322 322
 
323 323
         // Prepare response.
324
-        if ( $error ) {
324
+        if ($error) {
325 325
             $response['code'] = '500';
326
-            $response['msg']  = 'Error importing settings: ' . $error;
326
+            $response['msg']  = 'Error importing settings: '.$error;
327 327
         } else {
328 328
             $response['code'] = '200';
329 329
             $response['msg']  = 'Settings imported successfully';
330 330
         }
331 331
 
332 332
         // Dispatch respose.
333
-        echo json_encode( $response );
333
+        echo json_encode($response);
334 334
 
335 335
         // Close ajax request.
336 336
         wp_die();
337 337
     }
338 338
 
339
-    public function critcss_check_filename( $filename ) {
339
+    public function critcss_check_filename($filename) {
340 340
         // Try to avoid directory traversal when reading/writing/deleting critical CSS files.
341
-        if ( strpos( $filename, 'ccss_' ) !== 0 ) {
341
+        if (strpos($filename, 'ccss_') !== 0) {
342 342
             return false;
343
-        } elseif ( substr( $filename, -4, 4 ) !== '.css' ) {
343
+        } elseif (substr($filename, -4, 4) !== '.css') {
344 344
             return false;
345
-        } elseif ( sanitize_file_name( $filename ) !== $filename ) {
345
+        } elseif (sanitize_file_name($filename) !== $filename) {
346 346
             // Use WordPress core's sanitize_file_name to see if anything fishy is going on.
347 347
             return false;
348 348
         } else {
Please login to merge, or discard this patch.
autoptimize.php 1 patch
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -16,66 +16,66 @@  discard block
 block discarded – undo
16 16
  */
17 17
 
18 18
 
19
-if ( ! defined( 'ABSPATH' ) ) {
19
+if (!defined('ABSPATH')) {
20 20
     exit;
21 21
 }
22 22
 
23
-define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.7.1' );
23
+define('AUTOPTIMIZE_PLUGIN_VERSION', '2.7.1');
24 24
 
25 25
 // plugin_dir_path() returns the trailing slash!
26
-define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
27
-define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ );
26
+define('AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path(__FILE__));
27
+define('AUTOPTIMIZE_PLUGIN_FILE', __FILE__);
28 28
 
29 29
 // Bail early if attempting to run on non-supported php versions.
30
-if ( version_compare( PHP_VERSION, '5.6', '<' ) ) {
30
+if (version_compare(PHP_VERSION, '5.6', '<')) {
31 31
     function autoptimize_incompatible_admin_notice() {
32
-        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>';
33
-        if ( isset( $_GET['activate'] ) ) {
34
-            unset( $_GET['activate'] );
32
+        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>';
33
+        if (isset($_GET['activate'])) {
34
+            unset($_GET['activate']);
35 35
         }
36 36
     }
37 37
     function autoptimize_deactivate_self() {
38
-        deactivate_plugins( plugin_basename( AUTOPTIMIZE_PLUGIN_FILE ) );
38
+        deactivate_plugins(plugin_basename(AUTOPTIMIZE_PLUGIN_FILE));
39 39
     }
40
-    add_action( 'admin_notices', 'autoptimize_incompatible_admin_notice' );
41
-    add_action( 'admin_init', 'autoptimize_deactivate_self' );
40
+    add_action('admin_notices', 'autoptimize_incompatible_admin_notice');
41
+    add_action('admin_init', 'autoptimize_deactivate_self');
42 42
     return;
43 43
 }
44 44
 
45
-function autoptimize_autoload( $class_name ) {
46
-    if ( in_array( $class_name, array( 'Minify_HTML', 'JSMin' ) ) ) {
47
-        $file     = strtolower( $class_name );
48
-        $file     = str_replace( '_', '-', $file );
49
-        $path     = dirname( __FILE__ ) . '/classes/external/php/';
50
-        $filepath = $path . $file . '.php';
51
-    } elseif ( false !== strpos( $class_name, 'Autoptimize\\tubalmartin\\CssMin' ) ) {
52
-        $file     = str_replace( 'Autoptimize\\tubalmartin\\CssMin\\', '', $class_name );
53
-        $path     = dirname( __FILE__ ) . '/classes/external/php/yui-php-cssmin-bundled/';
54
-        $filepath = $path . $file . '.php';
55
-    } elseif ( 'autoptimize' === substr( $class_name, 0, 11 ) ) {
45
+function autoptimize_autoload($class_name) {
46
+    if (in_array($class_name, array('Minify_HTML', 'JSMin'))) {
47
+        $file     = strtolower($class_name);
48
+        $file     = str_replace('_', '-', $file);
49
+        $path     = dirname(__FILE__).'/classes/external/php/';
50
+        $filepath = $path.$file.'.php';
51
+    } elseif (false !== strpos($class_name, 'Autoptimize\\tubalmartin\\CssMin')) {
52
+        $file     = str_replace('Autoptimize\\tubalmartin\\CssMin\\', '', $class_name);
53
+        $path     = dirname(__FILE__).'/classes/external/php/yui-php-cssmin-bundled/';
54
+        $filepath = $path.$file.'.php';
55
+    } elseif ('autoptimize' === substr($class_name, 0, 11)) {
56 56
         // One of our "old" classes.
57 57
         $file     = $class_name;
58
-        $path     = dirname( __FILE__ ) . '/classes/';
59
-        $filepath = $path . $file . '.php';
60
-    } elseif ( 'PAnD' === $class_name ) {
58
+        $path     = dirname(__FILE__).'/classes/';
59
+        $filepath = $path.$file.'.php';
60
+    } elseif ('PAnD' === $class_name) {
61 61
         $file     = 'persist-admin-notices-dismissal';
62
-        $path     = dirname( __FILE__ ) . '/classes/external/php/persist-admin-notices-dismissal/';
63
-        $filepath = $path . $file . '.php';
62
+        $path     = dirname(__FILE__).'/classes/external/php/persist-admin-notices-dismissal/';
63
+        $filepath = $path.$file.'.php';
64 64
     }
65 65
 
66 66
     // If we didn't match one of our rules, bail!
67
-    if ( ! isset( $filepath ) ) {
67
+    if (!isset($filepath)) {
68 68
         return;
69 69
     }
70 70
 
71 71
     require $filepath;
72 72
 }
73 73
 
74
-spl_autoload_register( 'autoptimize_autoload' );
74
+spl_autoload_register('autoptimize_autoload');
75 75
 
76 76
 // Load WP CLI command(s) on demand.
77
-if ( defined( 'WP_CLI' ) && WP_CLI ) {
78
-    require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php';
77
+if (defined('WP_CLI') && WP_CLI) {
78
+    require AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCLI.php';
79 79
 }
80 80
 
81 81
 /**
@@ -86,8 +86,8 @@  discard block
 block discarded – undo
86 86
 function autoptimize() {
87 87
     static $plugin = null;
88 88
 
89
-    if ( null === $plugin ) {
90
-        $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE );
89
+    if (null === $plugin) {
90
+        $plugin = new autoptimizeMain(AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE);
91 91
     }
92 92
 
93 93
     return $plugin;
Please login to merge, or discard this patch.
classes/autoptimizeCriticalCSSBase.php 1 patch
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
  * Critical CSS base file (initializes all ccss files).
4 4
  */
5 5
 
6
-if ( ! defined( 'ABSPATH' ) ) {
6
+if (!defined('ABSPATH')) {
7 7
     exit;
8 8
 }
9 9
 
@@ -19,27 +19,27 @@  discard block
 block discarded – undo
19 19
     public function __construct()
20 20
     {
21 21
         // define constant, but only once.
22
-        if ( ! defined( 'AO_CCSS_DIR' ) ) {
22
+        if (!defined('AO_CCSS_DIR')) {
23 23
             // Define plugin version.
24
-            define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION );
24
+            define('AO_CCSS_VER', 'AO_'.AUTOPTIMIZE_PLUGIN_VERSION);
25 25
 
26 26
             // Define a constant with the directory to store critical CSS in.
27
-            if ( is_multisite() ) {
27
+            if (is_multisite()) {
28 28
                 $blog_id = get_current_blog_id();
29
-                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' );
29
+                define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'.$blog_id.'/');
30 30
             } else {
31
-                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' );
31
+                define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/');
32 32
             }
33 33
 
34 34
             // Define support files locations.
35
-            define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' );
36
-            define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' );
37
-            define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' );
35
+            define('AO_CCSS_LOCK', AO_CCSS_DIR.'queue.lock');
36
+            define('AO_CCSS_LOG', AO_CCSS_DIR.'queuelog.html');
37
+            define('AO_CCSS_DEBUG', AO_CCSS_DIR.'queue.json');
38 38
 
39 39
             // Define constants for criticalcss.com base path and API endpoints.
40 40
             // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option.
41
-            define( 'AO_CCSS_URL', 'https://criticalcss.com' );
42
-            define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' );
41
+            define('AO_CCSS_URL', 'https://criticalcss.com');
42
+            define('AO_CCSS_API', AO_CCSS_URL.'/api/premium/');
43 43
         }
44 44
 
45 45
         $this->filepath = __FILE__;
@@ -52,19 +52,19 @@  discard block
 block discarded – undo
52 52
     {
53 53
         // get all options.
54 54
         $all_options = $this->fetch_options();
55
-        foreach ( $all_options as $option => $value ) {
55
+        foreach ($all_options as $option => $value) {
56 56
             ${$option} = $value;
57 57
         }
58 58
 
59 59
         // make sure the 10 minutes cron schedule is added.
60
-        add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) );
60
+        add_filter('cron_schedules', array($this, 'ao_ccss_interval'));
61 61
 
62 62
         // check if we need to upgrade.
63 63
         $this->check_upgrade();
64 64
 
65 65
         // make sure ao_ccss_queue is scheduled OK if an API key is set.
66
-        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
67
-            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
66
+        if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) {
67
+            wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue');
68 68
         }
69 69
     }
70 70
 
@@ -72,12 +72,12 @@  discard block
 block discarded – undo
72 72
         // Required libs, core is always needed.
73 73
         $criticalcss_core = new autoptimizeCriticalCSSCore();
74 74
 
75
-        if ( defined( 'DOING_CRON' ) || is_admin() ) {
75
+        if (defined('DOING_CRON') || is_admin()) {
76 76
             // TODO: also include if overridden somehow to force queue processing to be executed?
77 77
             $criticalcss_cron = new autoptimizeCriticalCSSCron();
78 78
         }
79 79
 
80
-        if ( is_admin() ) {
80
+        if (is_admin()) {
81 81
             $criticalcss_settings = new autoptimizeCriticalCSSSettings();
82 82
         } else {
83 83
             // enqueuing only done when not wp-admin.
@@ -87,48 +87,48 @@  discard block
 block discarded – undo
87 87
 
88 88
     public static function fetch_options() {
89 89
         // Get options.
90
-        $autoptimize_ccss_options['ao_css_defer']          = get_option( 'autoptimize_css_defer' );
91
-        $autoptimize_ccss_options['ao_css_defer_inline']   = get_option( 'autoptimize_css_defer_inline' );
92
-        $autoptimize_ccss_options['ao_ccss_rules_raw']     = get_option( 'autoptimize_ccss_rules', false );
93
-        $autoptimize_ccss_options['ao_ccss_additional']    = get_option( 'autoptimize_ccss_additional' );
94
-        $autoptimize_ccss_options['ao_ccss_queue_raw']     = get_option( 'autoptimize_ccss_queue', false );
95
-        $autoptimize_ccss_options['ao_ccss_viewport']      = get_option( 'autoptimize_ccss_viewport', false );
96
-        $autoptimize_ccss_options['ao_ccss_finclude']      = get_option( 'autoptimize_ccss_finclude', false );
97
-        $autoptimize_ccss_options['ao_ccss_rlimit']        = get_option( 'autoptimize_ccss_rlimit', '5' );
98
-        $autoptimize_ccss_options['ao_ccss_noptimize']     = get_option( 'autoptimize_ccss_noptimize', false );
99
-        $autoptimize_ccss_options['ao_ccss_debug']         = get_option( 'autoptimize_ccss_debug', false );
100
-        $autoptimize_ccss_options['ao_ccss_key']           = get_option( 'autoptimize_ccss_key' );
101
-        $autoptimize_ccss_options['ao_ccss_keyst']         = get_option( 'autoptimize_ccss_keyst' );
102
-        $autoptimize_ccss_options['ao_ccss_loggedin']      = get_option( 'autoptimize_ccss_loggedin', '1' );
103
-        $autoptimize_ccss_options['ao_ccss_forcepath']     = get_option( 'autoptimize_ccss_forcepath', '1' );
104
-        $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' );
105
-        $autoptimize_ccss_options['ao_ccss_deferjquery']   = get_option( 'autoptimize_ccss_deferjquery', false );
106
-        $autoptimize_ccss_options['ao_ccss_domain']        = get_option( 'autoptimize_ccss_domain' );
107
-
108
-        if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) {
109
-            $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] );
110
-        } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) {
90
+        $autoptimize_ccss_options['ao_css_defer']          = get_option('autoptimize_css_defer');
91
+        $autoptimize_ccss_options['ao_css_defer_inline']   = get_option('autoptimize_css_defer_inline');
92
+        $autoptimize_ccss_options['ao_ccss_rules_raw']     = get_option('autoptimize_ccss_rules', false);
93
+        $autoptimize_ccss_options['ao_ccss_additional']    = get_option('autoptimize_ccss_additional');
94
+        $autoptimize_ccss_options['ao_ccss_queue_raw']     = get_option('autoptimize_ccss_queue', false);
95
+        $autoptimize_ccss_options['ao_ccss_viewport']      = get_option('autoptimize_ccss_viewport', false);
96
+        $autoptimize_ccss_options['ao_ccss_finclude']      = get_option('autoptimize_ccss_finclude', false);
97
+        $autoptimize_ccss_options['ao_ccss_rlimit']        = get_option('autoptimize_ccss_rlimit', '5');
98
+        $autoptimize_ccss_options['ao_ccss_noptimize']     = get_option('autoptimize_ccss_noptimize', false);
99
+        $autoptimize_ccss_options['ao_ccss_debug']         = get_option('autoptimize_ccss_debug', false);
100
+        $autoptimize_ccss_options['ao_ccss_key']           = get_option('autoptimize_ccss_key');
101
+        $autoptimize_ccss_options['ao_ccss_keyst']         = get_option('autoptimize_ccss_keyst');
102
+        $autoptimize_ccss_options['ao_ccss_loggedin']      = get_option('autoptimize_ccss_loggedin', '1');
103
+        $autoptimize_ccss_options['ao_ccss_forcepath']     = get_option('autoptimize_ccss_forcepath', '1');
104
+        $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option('autoptimize_service_availablity');
105
+        $autoptimize_ccss_options['ao_ccss_deferjquery']   = get_option('autoptimize_ccss_deferjquery', false);
106
+        $autoptimize_ccss_options['ao_ccss_domain']        = get_option('autoptimize_ccss_domain');
107
+
108
+        if (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') === false && strpos($autoptimize_ccss_options['ao_ccss_domain'], 'uggc') === 0) {
109
+            $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13($autoptimize_ccss_options['ao_ccss_domain']);
110
+        } elseif (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') !== false) {
111 111
             // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain).
112
-            update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) );
112
+            update_option('autoptimize_ccss_domain', str_rot13($autoptimize_ccss_options['ao_ccss_domain']));
113 113
         }
114 114
 
115 115
         // Setup the rules array.
116
-        if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) {
116
+        if (empty($autoptimize_ccss_options['ao_ccss_rules_raw'])) {
117 117
             $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array();
118 118
             $autoptimize_ccss_options['ao_ccss_rules']['types'] = array();
119 119
         } else {
120
-            $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true );
120
+            $autoptimize_ccss_options['ao_ccss_rules'] = json_decode($autoptimize_ccss_options['ao_ccss_rules_raw'], true);
121 121
         }
122 122
 
123 123
         // Setup the queue array.
124
-        if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) {
124
+        if (empty($autoptimize_ccss_options['ao_ccss_queue_raw'])) {
125 125
             $autoptimize_ccss_options['ao_ccss_queue'] = array();
126 126
         } else {
127
-            $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true );
127
+            $autoptimize_ccss_options['ao_ccss_queue'] = json_decode($autoptimize_ccss_options['ao_ccss_queue_raw'], true);
128 128
         }
129 129
 
130 130
         // Override API key if constant is defined.
131
-        if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) {
131
+        if (defined('AUTOPTIMIZE_CRITICALCSS_API_KEY')) {
132 132
             $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY;
133 133
         }
134 134
 
@@ -139,49 +139,49 @@  discard block
 block discarded – undo
139 139
         global $ao_ccss_key;
140 140
 
141 141
         // Create the cache directory if it doesn't exist already.
142
-        if ( ! file_exists( AO_CCSS_DIR ) ) {
143
-            mkdir( AO_CCSS_DIR, 0755, true );
142
+        if (!file_exists(AO_CCSS_DIR)) {
143
+            mkdir(AO_CCSS_DIR, 0755, true);
144 144
         }
145 145
 
146 146
         // Create a scheduled event for the queue.
147
-        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
148
-            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
147
+        if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) {
148
+            wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue');
149 149
         }
150 150
 
151 151
         // Create a scheduled event for log maintenance.
152
-        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) {
153
-            wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' );
152
+        if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_maintenance')) {
153
+            wp_schedule_event(time(), 'twicedaily', 'ao_ccss_maintenance');
154 154
         }
155 155
     }
156 156
 
157 157
     public function check_upgrade() {
158
-        $db_version = get_option( 'autoptimize_ccss_version', '' );
159
-        if ( AO_CCSS_VER !== $db_version ) {
158
+        $db_version = get_option('autoptimize_ccss_version', '');
159
+        if (AO_CCSS_VER !== $db_version) {
160 160
             // check schedules & re-schedule if needed.
161 161
             $this->on_upgrade();
162 162
             // and update db_version.
163
-            update_option( 'autoptimize_ccss_version', AO_CCSS_VER );
163
+            update_option('autoptimize_ccss_version', AO_CCSS_VER);
164 164
         }
165 165
     }
166 166
 
167
-    public function ao_ccss_interval( $schedules ) {
167
+    public function ao_ccss_interval($schedules) {
168 168
         // Let interval be configurable.
169
-        if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) {
169
+        if (!defined('AO_CCSS_DEBUG_INTERVAL')) {
170 170
             $intsec = 600;
171 171
         } else {
172 172
             $intsec = AO_CCSS_DEBUG_INTERVAL;
173
-            if ( $intsec >= 120 ) {
174
-                $inttxt = $intsec / 60 . ' minutes';
173
+            if ($intsec >= 120) {
174
+                $inttxt = $intsec/60.' minutes';
175 175
             } else {
176
-                $inttxt = $intsec . ' second(s)';
176
+                $inttxt = $intsec.' second(s)';
177 177
             }
178
-            autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 );
178
+            autoptimizeCriticalCSSCore::ao_ccss_log('Using custom WP-Cron interval of '.$inttxt, 3);
179 179
         }
180 180
 
181 181
         // Attach interval to schedule.
182 182
         $schedules['ao_ccss'] = array(
183 183
             'interval' => $intsec,
184
-            'display'  => __( 'Autoptimize CriticalCSS' ),
184
+            'display'  => __('Autoptimize CriticalCSS'),
185 185
         );
186 186
         return $schedules;
187 187
     }
Please login to merge, or discard this patch.