PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/autoptimizeCriticalCSSCron.php +190 -179 2.7.73.0.3 View file →
@@ -8,16 +8,10 @@
8 8 exit;
9 9 }
10 10
11 11 class autoptimizeCriticalCSSCron {
12 - public function __construct()
13 - {
14 - // fetch all options at once and populate them individually explicitely as globals.
15 - $all_options = autoptimizeCriticalCSSBase::fetch_options();
16 - foreach ( $all_options as $_option => $_value ) {
17 - global ${$_option};
18 - ${$_option} = $_value;
19 - }
12 + public function __construct() {
13 + $this->criticalcss = autoptimize()->criticalcss();
20 14
21 15 // Add queue control to a registered event.
22 16 add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) );
23 17 // Add cleaning job to a registered event.
@@ -25,12 +19,13 @@
25 19 }
26 20
27 21 public function ao_ccss_queue_control() {
28 22 // The queue execution backend.
29 - global $ao_ccss_key;
30 - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) {
23 + $key = $this->criticalcss->get_option( 'key' );
24 +
25 + if ( empty( $key ) ) {
31 26 // no key set, not processing the queue!
32 - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 );
27 + $this->criticalcss->log( 'No key set, so not processing queue.', 3 );
33 28 return;
34 29 }
35 30
36 31 /**
@@ -55,9 +50,9 @@
55 50 $qdobj = json_decode( $qdobj_raw, true );
56 51 if ( $qdobj ) {
57 52 if ( 1 === $qdobj['enable'] ) {
58 53 $queue_debug = true;
59 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 );
54 + $this->criticalcss->log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 );
60 55 }
61 56 }
62 57 }
63 58
@@ -75,27 +70,32 @@
75 70 // Proceed with the queue if it's not already running.
76 71 if ( ! $queue_lock ) {
77 72
78 73 // Log queue start and create the lock file.
79 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 );
74 + $this->criticalcss->log( 'Queue control started', 3 );
80 75 if ( touch( AO_CCSS_LOCK ) ) {
81 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 );
76 + $this->criticalcss->log( 'Queue control locked', 3 );
82 77 }
83 78
84 79 // Attach required variables.
85 - global $ao_ccss_queue;
86 - global $ao_ccss_rlimit;
80 + $queue = $this->criticalcss->get_option( 'queue' );
81 + $rtimelimit = $this->criticalcss->get_option( 'rtimelimit' );
87 82
88 - // Initialize job counters.
89 - $jc = 1;
90 - $jr = 1;
91 - $jt = count( $ao_ccss_queue );
83 + // Initialize counters.
84 + if ( $rtimelimit == 0 ) {
85 + // no time limit set, let's go with 1000 seconds.
86 + $rtimelimit = 1000;
87 + }
88 + $mt = time() + $rtimelimit; // maxtime queue processing can run.
89 + $jc = 1; // job count number.
90 + $jr = 1; // jobs requests number.
91 + $jt = count( $queue ); // number of jobs in queue.
92 92
93 93 // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...).
94 - array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine
94 + array_multisort( array_column( $queue, 'jqstat' ), $queue ); // @codingStandardsIgnoreLine
95 95
96 96 // Iterates over the entire queue.
97 - foreach ( $ao_ccss_queue as $path => $jprops ) {
97 + foreach ( $queue as $path => $jprops ) {
98 98 // Prepare flags and target rule.
99 99 $update = false;
100 100 $deljob = false;
101 101 $rule_update = false;
@@ -102,15 +102,15 @@
102 102 $oldccssfile = false;
103 103 $trule = explode( '|', $jprops['rtarget'] );
104 104
105 105 // Log job count.
106 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 );
106 + $this->criticalcss->log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 );
107 107
108 108 // Process NEW jobs.
109 109 if ( 'NEW' == $jprops['jqstat'] ) {
110 110
111 111 // Log the new job.
112 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 );
112 + $this->criticalcss->log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 );
113 113
114 114 // Compare job and rule hashes (if any).
115 115 $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] );
116 116
@@ -115,28 +115,45 @@
115 115 $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] );
116 116
117 117 // If job hash is new or different of a previous one.
118 118 if ( $hash ) {
119 + if ( $jr > 2 ) {
120 + // we already posted 2 jobs to criticalcss.com, don't post more this run
121 + // but we can keep on processing the queue to keep it tidy.
122 + $this->criticalcss->log( 'Holding off on generating request for job with local ID <' . $jprops['ljid'] . '>, maximum number of POSTS reached.', 3 );
123 + continue;
124 + }
125 +
119 126 // Set job hash.
120 127 $jprops['hash'] = $hash;
121 128
122 - // If this is not the first job, wait 10 seconds before process next job due criticalcss.com API limits.
123 - if ( $jr > 1 ) {
124 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting ' . AO_CCSS_SLEEP . ' seconds due to criticalcss.com API limits', 3 );
125 - sleep( AO_CCSS_SLEEP );
126 - }
127 -
128 129 // Dispatch the job generate request and increment request count.
129 130 $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] );
130 131 $jr++;
131 132
132 133 // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php.
133 - if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) {
134 + if ( empty( $apireq ) ) {
135 + // ERROR: no response
136 + // Update job properties.
137 + $jprops['jqstat'] = 'NO_RESPONSE';
138 + $jprops['jrstat'] = 'NONE';
139 + $jprops['jvstat'] = 'NONE';
140 + $jprops['jftime'] = microtime( true );
141 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 );
142 + } elseif ( array_key_exists( 'errorCode', $apireq ) && 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) {
143 + // ERROR: key validation
144 + // Update job properties.
145 + $jprops['jqstat'] = $apireq['errorCode'];
146 + $jprops['jrstat'] = $apireq['error'];
147 + $jprops['jvstat'] = 'NONE';
148 + $jprops['jftime'] = microtime( true );
149 + $this->criticalcss->log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 );
150 + } elseif ( array_key_exists( 'job', $apireq ) && 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) {
134 151 // SUCCESS: request has a valid result.
135 152 // Update job properties.
136 153 $jprops['jid'] = $apireq['job']['id'];
137 154 $jprops['jqstat'] = $apireq['job']['status'];
138 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 );
155 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 );
139 156 } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) {
140 157 // ERROR: concurrent requests
141 158 // Update job properties.
142 159 $jprops['jid'] = $apireq['job']['id'];
@@ -147,25 +164,9 @@
147 164 $jprops['jrstat'] = 'Baby did a bad bad thing';
148 165 }
149 166 $jprops['jvstat'] = 'NONE';
150 167 $jprops['jftime'] = microtime( true );
151 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 );
152 - } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) {
153 - // ERROR: key validation
154 - // Update job properties.
155 - $jprops['jqstat'] = $apireq['errorCode'];
156 - $jprops['jrstat'] = $apireq['error'];
157 - $jprops['jvstat'] = 'NONE';
158 - $jprops['jftime'] = microtime( true );
159 - autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 );
160 - } elseif ( empty( $apireq ) ) {
161 - // ERROR: no response
162 - // Update job properties.
163 - $jprops['jqstat'] = 'NO_RESPONSE';
164 - $jprops['jrstat'] = 'NONE';
165 - $jprops['jvstat'] = 'NONE';
166 - $jprops['jftime'] = microtime( true );
167 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 );
168 + $this->criticalcss->log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 );
168 169 } else {
169 170 // UNKNOWN: unhandled generate exception
170 171 // Update job properties.
171 172 $jprops['jqstat'] = 'JOB_UNKNOWN';
@@ -171,10 +172,10 @@
171 172 $jprops['jqstat'] = 'JOB_UNKNOWN';
172 173 $jprops['jrstat'] = 'NONE';
173 174 $jprops['jvstat'] = 'NONE';
174 175 $jprops['jftime'] = microtime( true );
175 - 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 );
176 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 );
176 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 );
177 + $this->criticalcss->log( 'Job response was: ' . json_encode( $apireq ), 3 );
177 178 }
178 179 } else {
179 180 // SUCCESS: Job hash is equal to a previous one, so it's done
180 181 // Update job status and finish time.
@@ -179,9 +180,9 @@
179 180 // SUCCESS: Job hash is equal to a previous one, so it's done
180 181 // Update job status and finish time.
181 182 $jprops['jqstat'] = 'JOB_DONE';
182 183 $jprops['jftime'] = microtime( true );
183 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 );
184 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 );
184 185 }
185 186
186 187 // Set queue update flag.
187 188 $update = true;
@@ -188,19 +189,12 @@
188 189
189 190 } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) {
190 191 // Process QUEUED and ONGOING jobs
191 192 // Log the pending job.
192 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 );
193 + $this->criticalcss->log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 );
193 194
194 - // If this is not the first job, wait before process next job due criticalcss.com API limits.
195 - if ( $jr > 1 ) {
196 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting ' . AO_CCSS_SLEEP . ' seconds due to criticalcss.com API limits', 3 );
197 - sleep( AO_CCSS_SLEEP );
198 - }
199 -
200 195 // Dispatch the job result request and increment request count.
201 196 $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] );
202 - $jr++;
203 197
204 198 // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php
205 199 // Replace API response values if queue debugging is enabled and some value is set.
206 200 if ( $queue_debug ) {
@@ -219,9 +213,9 @@
219 213 // SUCCESS: request has a valid result
220 214 // Process a PENDING job
221 215 // Update job properties.
222 216 $jprops['jqstat'] = $apireq['status'];
223 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 );
217 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 );
224 218 } elseif ( 'JOB_DONE' == $apireq['status'] ) {
225 219 // Process a DONE job
226 220 // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now.
227 221 if ( 'HTML_404' == $apireq['resultStatus'] ) {
@@ -227,10 +221,10 @@
227 221 if ( 'HTML_404' == $apireq['resultStatus'] ) {
228 222 $apireq['resultStatus'] = 'GOOD';
229 223 }
230 224
231 - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) {
232 - // SUCCESS: GOOD job with GOOD validation
225 + if ( 'GOOD' == $apireq['resultStatus'] && ( 'GOOD' == $apireq['validationStatus'] || 'WARN' == $apireq['validationStatus'] ) ) {
226 + // SUCCESS: GOOD job with GOOD or WARN validation
233 227 // Update job properties.
234 228 $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false );
235 229 $jprops['jqstat'] = $apireq['status'];
236 230 $jprops['jrstat'] = $apireq['resultStatus'];
@@ -236,19 +230,23 @@
236 230 $jprops['jrstat'] = $apireq['resultStatus'];
237 231 $jprops['jvstat'] = $apireq['validationStatus'];
238 232 $jprops['jftime'] = microtime( true );
239 233 $rule_update = true;
240 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 );
241 - } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) {
242 - // SUCCESS: GOOD job with WARN or BAD validation
234 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 );
235 + } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) {
236 + // SUCCESS: GOOD job with BAD or SCREENSHOT_WARN_BLANK validation
243 237 // Update job properties.
244 - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true );
245 238 $jprops['jqstat'] = $apireq['status'];
246 239 $jprops['jrstat'] = $apireq['resultStatus'];
247 240 $jprops['jvstat'] = $apireq['validationStatus'];
248 241 $jprops['jftime'] = microtime( true );
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 );
242 + if ( apply_filters( 'autoptimize_filter_ccss_save_review_rules', true ) ) {
243 + $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true );
244 + $rule_update = true;
245 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 );
246 + } else {
247 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file not saved because it required REVIEW.', 3 );
248 + }
251 249 } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) {
252 250 // ERROR: no GOOD, WARN or BAD results
253 251 // Update job properties.
254 252 $jprops['jqstat'] = $apireq['status'];
@@ -254,11 +252,11 @@
254 252 $jprops['jqstat'] = $apireq['status'];
255 253 $jprops['jrstat'] = $apireq['resultStatus'];
256 254 $jprops['jvstat'] = $apireq['validationStatus'];
257 255 $jprops['jftime'] = microtime( true );
258 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
256 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
259 257 $apireq['css'] = '/* critical css removed for DEBUG logging purposes */';
260 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 );
258 + $this->criticalcss->log( 'Job response was: ' . json_encode( $apireq ), 3 );
261 259 } else {
262 260 // UNKNOWN: unhandled JOB_DONE exception
263 261 // Update job properties.
264 262 $jprops['jqstat'] = 'JOB_UNKNOWN';
@@ -264,11 +262,11 @@
264 262 $jprops['jqstat'] = 'JOB_UNKNOWN';
265 263 $jprops['jrstat'] = $apireq['resultStatus'];
266 264 $jprops['jvstat'] = $apireq['validationStatus'];
267 265 $jprops['jftime'] = microtime( true );
268 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 );
266 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 );
269 267 $apireq['css'] = '/* critical css removed for DEBUG logging purposes */';
270 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 );
268 + $this->criticalcss->log( 'Job response was: ' . json_encode( $apireq ), 3 );
271 269 }
272 270 } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) {
273 271 // ERROR: failed job
274 272 // Update job properties.
@@ -279,9 +277,9 @@
279 277 $jprops['jrstat'] = 'Baby did a bad bad thing';
280 278 }
281 279 $jprops['jvstat'] = 'NONE';
282 280 $jprops['jftime'] = microtime( true );
283 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
281 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
284 282 } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) {
285 283 // ERROR: CSS doesn't exist
286 284 // Update job properties.
287 285 $jprops['jqstat'] = 'NO_CSS';
@@ -287,9 +285,9 @@
287 285 $jprops['jqstat'] = 'NO_CSS';
288 286 $jprops['jrstat'] = $apireq['error'];
289 287 $jprops['jvstat'] = 'NONE';
290 288 $jprops['jftime'] = microtime( true );
291 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
289 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 );
292 290 } elseif ( empty( $apireq ) ) {
293 291 // ERROR: no response
294 292 // Update job properties.
295 293 $jprops['jqstat'] = 'NO_RESPONSE';
@@ -295,9 +293,9 @@
295 293 $jprops['jqstat'] = 'NO_RESPONSE';
296 294 $jprops['jrstat'] = 'NONE';
297 295 $jprops['jvstat'] = 'NONE';
298 296 $jprops['jftime'] = microtime( true );
299 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 );
297 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 );
300 298 } else {
301 299 // UNKNOWN: unhandled results exception
302 300 // Update job properties.
303 301 $jprops['jqstat'] = 'JOB_UNKNOWN';
@@ -303,9 +301,9 @@
303 301 $jprops['jqstat'] = 'JOB_UNKNOWN';
304 302 $jprops['jrstat'] = 'NONE';
305 303 $jprops['jvstat'] = 'NONE';
306 304 $jprops['jftime'] = microtime( true );
307 - 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 );
305 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 );
308 306 }
309 307
310 308 // Set queue update flag.
311 309 $update = true;
@@ -320,33 +318,33 @@
320 318 // Persist updated queue object.
321 319 if ( $update ) {
322 320 if ( ! $deljob ) {
323 321 // Update properties of a NEW or PENDING job...
324 - $ao_ccss_queue[ $path ] = $jprops;
322 + $queue[ $path ] = $jprops;
325 323 } else {
326 324 // ...or remove the DONE job.
327 - unset( $ao_ccss_queue[ $path ] );
328 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 );
325 + unset( $queue[ $path ] );
326 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 );
329 327 }
330 328
331 329 // Update queue object.
332 - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue );
333 - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false );
334 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 );
330 + $queue_raw = json_encode( $queue );
331 + update_option( 'autoptimize_ccss_queue', $queue_raw, false );
332 + $this->criticalcss->log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 );
335 333
336 334 // Update target rule.
337 335 if ( $rule_update ) {
338 336 $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] );
339 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 );
337 + $this->criticalcss->log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 );
340 338 }
341 339 } else {
342 340 // Or log no queue action.
343 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 );
341 + $this->criticalcss->log( 'Nothing to do on this job', 3 );
344 342 }
345 343
346 - // Break the loop if request limit is set and was reached.
347 - if ( $ao_ccss_rlimit && $ao_ccss_rlimit == $jr ) {
348 - autoptimizeCriticalCSSCore::ao_ccss_log( 'The limit of ' . $ao_ccss_rlimit . ' request(s) to criticalcss.com was reached, queue control must finish now', 3 );
344 + // Break the loop if request time limit is (almost exceeded).
345 + if ( time() > $mt ) {
346 + $this->criticalcss->log( 'The time limit of ' . $rtimelimit . ' seconds was exceeded, queue control must finish now', 3 );
349 347 break;
350 348 }
351 349
352 350 // Increment job counter.
@@ -355,15 +353,15 @@
355 353
356 354 // Remove the lock file and log the queue end.
357 355 if ( file_exists( AO_CCSS_LOCK ) ) {
358 356 unlink( AO_CCSS_LOCK );
359 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 );
357 + $this->criticalcss->log( 'Queue control unlocked', 3 );
360 358 }
361 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 );
359 + $this->criticalcss->log( 'Queue control finished', 3 );
362 360
363 361 // Log that queue is locked.
364 362 } else {
365 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 );
363 + $this->criticalcss->log( 'Queue is already running, skipping the attempt to run it again', 3 );
366 364 }
367 365 }
368 366
369 367 public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) {
@@ -372,9 +370,9 @@
372 370 if ( 1 == count( $hashes ) ) {
373 371 // Job with a single hash
374 372 // Set job hash.
375 373 $hash = $hashes[0];
376 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 );
374 + $this->criticalcss->log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 );
377 375 } else {
378 376 // Job with multiple hashes
379 377 // Loop through hashes to concatenate them.
380 378 $nhash = '';
@@ -383,18 +381,18 @@
383 381 }
384 382
385 383 // Set job hash.
386 384 $hash = md5( $nhash );
387 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 );
385 + $this->criticalcss->log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 );
388 386 }
389 387
390 388 // STEP 2: compare job to existing jobs to prevent double submission for same type+hash.
391 - global $ao_ccss_queue;
389 + $queue = $this->criticalcss->get_option( 'queue' );
392 390
393 - foreach ( $ao_ccss_queue as $queue_item ) {
394 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 );
391 + foreach ( $queue as $queue_item ) {
392 + $this->criticalcss->log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 );
395 393 if ( $queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array( $queue_item['jqstat'], array( 'JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE' ) ) ) {
396 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 );
394 + $this->criticalcss->log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 );
397 395 return false;
398 396 }
399 397 }
400 398
@@ -399,32 +397,42 @@
399 397 }
400 398
401 399 // STEP 3: compare job and existing rule (if any) hashes
402 400 // Attach required arrays.
403 - global $ao_ccss_rules;
401 + $rules = $this->criticalcss->get_option( 'rules' );
404 402
405 403 // Prepare rule variables.
406 404 $trule = explode( '|', $rule );
407 - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ];
405 + if ( is_array( $trule ) && ! empty( $trule ) && array_key_exists( $trule[1], $rules[ $trule[0] ] ) ) {
406 + $srule = $rules[ $trule[0] ][ $trule[1] ];
407 + } else {
408 + $srule = '';
409 + }
408 410
411 + // If hash is empty, set it to now for a "forced job".
412 + if ( empty( $hash ) ) {
413 + $hash = 'new';
414 + $this->criticalcss->log( 'Job id <' . $ljid . '> had no hash, assuming forced job so setting hash to new', 3 );
415 + }
416 +
409 417 // Check if a MANUAL rule exist and return false.
410 418 if ( ! empty( $srule ) && ( 0 == $srule['hash'] && 0 != $srule['file'] ) ) {
411 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 );
419 + $this->criticalcss->log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 );
412 420 return false;
413 421 } elseif ( ! empty( $srule ) ) {
414 422 // Check if an AUTO rule exist.
415 423 if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) {
416 424 // Check if job hash matches rule, if the CCSS file exists said file is not empty and return FALSE is so.
417 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 );
425 + $this->criticalcss->log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 );
418 426 return false;
419 427 } else {
420 428 // Or return the new hash if they differ.
421 - 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 );
429 + $this->criticalcss->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 );
422 430 return $hash;
423 431 }
424 432 } else {
425 - // Or just return the hash if no rule exist yet.
426 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 );
433 + // Return the hash for a job that has no rule yet.
434 + $this->criticalcss->log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 );
427 435 return $hash;
428 436 }
429 437 }
430 438
@@ -430,16 +438,13 @@
430 438
431 439 public function ao_ccss_api_generate( $path, $debug, $dcode ) {
432 440 // POST jobs to criticalcss.com and return responses
433 441 // Get key and key status.
434 - global $ao_ccss_key;
435 - global $ao_ccss_keyst;
436 - $key = $ao_ccss_key;
437 - $key_status = $ao_ccss_keyst;
442 + $key = $this->criticalcss->get_option( 'key' );
443 + $key_status = $this->criticalcss->get_option( 'keyst' );
444 + $noptimize = $this->criticalcss->get_option( 'noptimize' );
438 445
439 446 // Prepare full URL to request.
440 - global $ao_ccss_noptimize;
441 -
442 447 $site_host = get_site_url();
443 448 $site_path = parse_url( $site_host, PHP_URL_PATH );
444 449
445 450 if ( ! empty( $site_path ) ) {
@@ -448,15 +453,15 @@
448 453
449 454 // Logic to bind to one domain to avoid site clones of sites would
450 455 // automatically begin spawning requests to criticalcss.com which has
451 456 // a per domain cost.
452 - global $ao_ccss_domain;
453 - if ( empty( $ao_ccss_domain ) ) {
457 + $domain = $this->criticalcss->get_option( 'domain' );
458 + if ( empty( $domain ) ) {
454 459 // first request being done, update option to allow future requests are only allowed if from same domain.
455 460 update_option( 'autoptimize_ccss_domain', str_rot13( $site_host ) );
456 - } 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 ) ) {
461 + } elseif ( trim( $domain, '\'"' ) !== 'none' && parse_url( $site_host, PHP_URL_HOST ) !== parse_url( $domain, PHP_URL_HOST ) && apply_filters( 'autoptimize_filter_ccss_bind_domain', true ) ) {
457 462 // not the same domain, log as error and return without posting to criticalcss.com.
458 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $ao_ccss_domain . ' so not proceeding.', 2 );
463 + $this->criticalcss->log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $domain . ' so not proceeding.', 2 );
459 464 return false;
460 465 }
461 466
462 467 $src_url = $site_host . $path;
@@ -461,11 +466,11 @@
461 466
462 467 $src_url = $site_host . $path;
463 468
464 469 // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO.
465 - if ( ! empty( $ao_ccss_noptimize ) ) {
470 + if ( ! empty( $noptimize ) ) {
466 471 $src_url .= '?ao_noptirocket=1';
467 - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) {
472 + } elseif ( ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) || apply_filters( 'autoptimize_filter_ccss_enforce_nolazy', false ) ) {
468 473 $src_url .= '?ao_nolazy=1';
469 474 }
470 475
471 476 $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url );
@@ -476,9 +481,9 @@
476 481 $body['aff'] = 1;
477 482 $body['aocssv'] = AO_CCSS_VER;
478 483
479 484 // Prepare and add viewport size to the body if available.
480 - $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport();
485 + $viewport = $this->criticalcss->viewport();
481 486 if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) {
482 487 $body['width'] = $viewport['w'];
483 488 $body['height'] = $viewport['h'];
484 489 }
@@ -483,10 +488,10 @@
483 488 $body['height'] = $viewport['h'];
484 489 }
485 490
486 491 // Prepare and add forceInclude to the body if available.
487 - global $ao_ccss_finclude;
488 - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude );
492 + $finclude = $this->criticalcss->get_option( 'finclude' );
493 + $finclude = $this->ao_ccss_finclude( $finclude );
489 494 if ( ! empty( $finclude ) ) {
490 495 $body['forceInclude'] = $finclude;
491 496 }
492 497
@@ -494,19 +499,19 @@
494 499 $body = apply_filters( 'autoptimize_ccss_cron_api_generate_body', $body );
495 500
496 501 // Body must be json and log it.
497 502 $body = json_encode( $body );
498 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 );
503 + $this->criticalcss->log( 'criticalcss.com: POST generate request body is ' . $body, 3 );
499 504
500 505 // Prepare the request.
501 506 $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER );
502 507 $args = array(
503 - 'headers' => array(
508 + 'headers' => apply_filters( 'autoptimize_ccss_cron_api_generate_headers', array(
504 509 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER,
505 510 'Content-type' => 'application/json; charset=utf-8',
506 511 'Authorization' => 'JWT ' . $key,
507 512 'Connection' => 'close',
508 - ),
513 + ) ),
509 514 'body' => $body,
510 515 );
511 516
512 517 // Dispatch the request and store its response code.
@@ -523,14 +528,14 @@
523 528 // Response code is OK.
524 529 // Workaround criticalcss.com non-RESTful reponses.
525 530 if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) {
526 531 // Log successful and return encoded request body.
527 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 );
532 + $this->criticalcss->log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 );
528 533
529 534 // This code also means the key is valid, so cache key status for 24h if not already cached.
530 535 if ( ( ! $key_status || 2 != $key_status ) && $key ) {
531 536 update_option( 'autoptimize_ccss_keyst', 2 );
532 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 );
537 + $this->criticalcss->log( 'criticalcss.com: API key is valid, updating key status', 3 );
533 538 }
534 539
535 540 // Return the request body.
536 541 return $body;
@@ -535,10 +540,10 @@
535 540 // Return the request body.
536 541 return $body;
537 542 } else {
538 543 // Log successful requests with invalid reponses.
539 - 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 );
540 - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 );
544 + $this->criticalcss->log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 );
545 + $this->criticalcss->log( print_r( $body, true ), 2 );
541 546 return $body;
542 547 }
543 548 } else {
544 549 // Response code is anything else.
@@ -543,15 +548,15 @@
543 548 } else {
544 549 // Response code is anything else.
545 550 // Log failed request with a valid response code and return body.
546 551 if ( $code ) {
547 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 );
548 - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 );
552 + $this->criticalcss->log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 );
553 + $this->criticalcss->log( print_r( $body, true ), 2 );
549 554
550 555 if ( 401 == $code ) {
551 556 // If request is unauthorized, also clear key status.
552 557 update_option( 'autoptimize_ccss_keyst', 1 );
553 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 );
558 + $this->criticalcss->log( 'criticalcss.com: API key is invalid, updating key status', 3 );
554 559 }
555 560
556 561 // Return the request body.
557 562 return $body;
@@ -556,11 +561,11 @@
556 561 // Return the request body.
557 562 return $body;
558 563 } else {
559 564 // Log failed request with no response and return false.
560 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 );
565 + $this->criticalcss->log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 );
561 566 if ( is_wp_error( $req ) ) {
562 - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 );
567 + $this->criticalcss->log( $req->get_error_message(), 2 );
563 568 }
564 569
565 570 return false;
566 571 }
@@ -569,19 +574,18 @@
569 574
570 575 public function ao_ccss_api_results( $jobid, $debug, $dcode ) {
571 576 // GET jobs from criticalcss.com and return responses
572 577 // Get key.
573 - global $ao_ccss_key;
574 - $key = $ao_ccss_key;
578 + $key = $this->criticalcss->get_option( 'key' );
575 579
576 580 // Prepare the request.
577 581 $url = AO_CCSS_API . 'results?resultId=' . $jobid;
578 582 $args = array(
579 - 'headers' => array(
583 + 'headers' => apply_filters( 'autoptimize_ccss_cron_api_generate_headers', array(
580 584 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER,
581 585 'Authorization' => 'JWT ' . $key,
582 586 'Connection' => 'close',
583 - ),
587 + ) ),
584 588 );
585 589
586 590 // Dispatch the request and store its response code.
587 591 $req = wp_safe_remote_get( $url, $args );
@@ -597,19 +601,19 @@
597 601 // Response code is OK.
598 602 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'] ) ) {
599 603 // Workaround criticalcss.com non-RESTful reponses
600 604 // Log successful and return encoded request body.
601 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 );
605 + $this->criticalcss->log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 );
602 606 return $body;
603 607 } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) {
604 608 // Handle no CSS reply
605 609 // Log no CSS error and return encoded request body.
606 - 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 + $this->criticalcss->log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully but the CSS for it does not exist anymore', 3 );
607 611 return $body;
608 612 } else {
609 613 // Log failed request and return false.
610 - 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 );
611 - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 );
614 + $this->criticalcss->log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 );
615 + $this->criticalcss->log( print_r( $body, true ), 2 );
612 616 return false;
613 617 }
614 618 } else {
615 619 // Response code is anything else
@@ -614,14 +618,14 @@
614 618 } else {
615 619 // Response code is anything else
616 620 // Log failed request with a valid response code and return body.
617 621 if ( $code ) {
618 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 );
619 - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 );
622 + $this->criticalcss->log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 );
623 + $this->criticalcss->log( print_r( $body, true ), 2 );
620 624 if ( 401 == $code ) {
621 625 // If request is unauthorized, also clear key status.
622 626 update_option( 'autoptimize_ccss_keyst', 1 );
623 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 );
627 + $this->criticalcss->log( 'criticalcss.com: API key is invalid, updating key status', 3 );
624 628 }
625 629
626 630 // Return the request body.
627 631 return $body;
@@ -626,9 +630,9 @@
626 630 // Return the request body.
627 631 return $body;
628 632 } else {
629 633 // Log failed request with no response and return false.
630 - 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 + $this->criticalcss->log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> has no response, this could be a service timeout', 2 );
631 635 return false;
632 636 }
633 637 }
634 638 }
@@ -645,41 +649,42 @@
645 649 // Prepare target rule, filename and content.
646 650 $filename = false;
647 651 $content = $ccss;
648 652
649 - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) {
653 + if ( $this->criticalcss->check_contents( $content ) ) {
650 654 // Sanitize content, set filename and try to save file.
651 655 $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css';
652 656 $status = file_put_contents( $file, $content, LOCK_EX );
653 657 $filename = pathinfo( $file, PATHINFO_BASENAME );
654 - 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 + $this->criticalcss->log( 'Critical CSS file for the rule <' . $target[0] . '|' . $target[1] . '> was saved as <' . $filename . '>, size in bytes is <' . $status . '>', 3 );
655 659
656 660 if ( ! $status ) {
657 661 // If file has not been saved, reset filename.
658 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 );
662 + $this->criticalcss->log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 );
659 663 $filename = false;
660 664 return $filename;
661 665 }
662 666 } else {
663 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 );
667 + $this->criticalcss->log( 'Critical CSS received did not pass content check', 2 );
664 668 return $filename;
665 669 }
666 670
667 671 // Remove old critical CSS if a previous one existed in the rule and if that file exists in filesystem
668 - // NOTE: out of scope critical CSS file removal (issue #5)
669 672 // Attach required arrays.
670 - global $ao_ccss_rules;
673 + $rules = $this->criticalcss->get_option( 'rules' );
671 674
672 - // Prepare rule variables.
673 - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ];
674 - $oldfile = $srule['file'];
675 + // Only proceed if the rule already existed.
676 + if ( array_key_exists( $target[1], $rules[ $target[0] ] ) ) {
677 + $srule = $rules[ $target[0] ][ $target[1] ];
678 + $oldfile = $srule['file'];
675 679
676 - if ( $oldfile && $oldfile !== $filename ) {
677 - $delfile = AO_CCSS_DIR . $oldfile;
678 - if ( file_exists( $delfile ) ) {
679 - $unlinkst = unlink( $delfile );
680 - if ( $unlinkst ) {
681 - autoptimizeCriticalCSSCore::ao_ccss_log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 );
680 + if ( $oldfile && $oldfile !== $filename ) {
681 + $delfile = AO_CCSS_DIR . $oldfile;
682 + if ( file_exists( $delfile ) ) {
683 + $unlinkst = unlink( $delfile );
684 + if ( $unlinkst ) {
685 + $this->criticalcss->log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 );
686 + }
682 687 }
683 688 }
684 689 }
685 690
@@ -689,21 +694,25 @@
689 694
690 695 public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) {
691 696 // Update or create a rule
692 697 // Attach required arrays.
693 - global $ao_ccss_rules;
698 + $rules = $this->criticalcss->get_option( 'rules' );
694 699
695 700 // Prepare rule variables.
696 701 $trule = explode( '|', $srule );
697 - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ];
702 + if ( array_key_exists( $trule[1], $rules[$trule[0]] ) ) {
703 + $rule = $rules[ $trule[0] ][ $trule[1] ];
704 + } else {
705 + $rule = array();
706 + }
698 707 $action = false;
699 708 $rtype = '';
700 709
701 - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) {
710 + if ( is_array( $rule ) && 0 === $rule['hash'] && 0 !== $rule['file'] ) {
702 711 // manual rule, don't ever overwrite.
703 712 $action = 'NOT UPDATED';
704 713 $rtype = 'MANUAL';
705 - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) {
714 + } elseif ( is_array( $rule ) && 0 === $rule['hash'] && 0 === $rule['file'] ) {
706 715 // If this is an user created AUTO rule with no hash and file yet, update its hash and filename
707 716 // Set rule hash, file and action flag.
708 717 $rule['hash'] = $hash;
709 718 $rule['file'] = $file;
@@ -708,9 +717,9 @@
708 717 $rule['hash'] = $hash;
709 718 $rule['file'] = $file;
710 719 $action = 'UPDATED';
711 720 $rtype = 'AUTO';
712 - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) {
721 + } elseif ( is_array( $rule ) && 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) {
713 722 // If this is an genuine AUTO rule, update its hash and filename
714 723 // Set rule hash, file and action flag.
715 724 $rule['hash'] = $hash;
716 725 $rule['file'] = $file;
@@ -718,9 +727,9 @@
718 727 $rtype = 'AUTO';
719 728 } else {
720 729 // If rule doesn't exist, create an AUTO rule
721 730 // AUTO rules were only for types, but will now also work for paths.
722 - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) {
731 + if ( ( 'types' == $trule[0] || 'paths' == $trule[0] ) && ! empty( $trule[1] ) ) {
723 732 // Set rule hash and file and action flag.
724 733 $rule['hash'] = $hash;
725 734 $rule['file'] = $file;
726 735 $action = 'CREATED';
@@ -726,20 +735,21 @@
726 735 $action = 'CREATED';
727 736 $rtype = 'AUTO';
728 737 } else {
729 738 // Log that no rule was created.
730 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 );
739 + $this->criticalcss->log( 'Exception, no AUTO rule created', 3 );
731 740 }
732 741 }
733 742
734 743 if ( $action ) {
735 744 // If a rule creation/update is required, persist updated rules object.
736 - $ao_ccss_rules[ $trule[0] ][ $trule[1] ] = $rule;
737 - $ao_ccss_rules_raw = json_encode( $ao_ccss_rules );
738 - update_option( 'autoptimize_ccss_rules', $ao_ccss_rules_raw );
739 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 );
745 + $rules[ $trule[0] ][ $trule[1] ] = $rule;
746 + $rules_raw = json_encode( $rules );
747 + update_option( 'autoptimize_ccss_rules', $rules_raw );
748 + $this->criticalcss->flush_options();
749 + $this->criticalcss->log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 );
740 750 } else {
741 - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 );
751 + $this->criticalcss->log( 'No rule action required', 3 );
742 752 }
743 753 }
744 754
745 755 function ao_ccss_finclude( $finclude_raw ) {
@@ -801,12 +811,13 @@
801 811 wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
802 812 }
803 813
804 814 // Queue cleaning.
805 - global $ao_ccss_queue;
815 + $queue = $this->criticalcss->get_option( 'queue' );
816 +
806 817 $queue_purge_threshold = 100;
807 818 $queue_purge_age = 24 * 60 * 60;
808 - $queue_length = count( $ao_ccss_queue );
819 + $queue_length = count( $queue );
809 820 $timestamp_yesterday = microtime( true ) - $queue_purge_age;
810 821 $remove_old_new = false;
811 822 $queue_altered = false;
812 823
@@ -813,11 +824,11 @@
813 824 if ( $queue_length > $queue_purge_threshold ) {
814 825 $remove_old_new = true;
815 826 }
816 827
817 - foreach ( $ao_ccss_queue as $path => $job ) {
828 + foreach ( $queue as $path => $job ) {
818 829 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' ) ) ) {
819 - unset( $ao_ccss_queue[ $path ] );
830 + unset( $queue[ $path ] );
820 831 $queue_altered = true;
821 832 }
822 833 }
823 834
@@ -822,16 +833,16 @@
822 833 }
823 834
824 835 // save queue to options!
825 836 if ( $queue_altered ) {
826 - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue );
827 - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false );
828 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue cleaning done.', 3 );
837 + $queue_raw = json_encode( $queue );
838 + update_option( 'autoptimize_ccss_queue', $queue_raw, false );
839 + $this->criticalcss->log( 'Queue cleaning done.', 3 );
829 840 }
830 841
831 842 // re-check key if invalid.
832 - global $ao_ccss_keyst;
833 - if ( 1 == $ao_ccss_keyst ) {
843 + $keyst = $this->criticalcss->get_option( 'keyst' );
844 + if ( 1 == $keyst ) {
834 845 $this->ao_ccss_api_generate( '', '', '' );
835 846 }
836 847 }
837 848 }