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/autoptimizeCriticalCSSEnqueue.php +179 -142 2.7.13.0.3 View file →
@@ -7,181 +7,206 @@
7 7 exit;
8 8 }
9 9
10 10 class autoptimizeCriticalCSSEnqueue {
11 - public function __construct()
12 - {
13 - // fetch all options at once and populate them individually explicitely as globals.
14 - $all_options = autoptimizeCriticalCSSBase::fetch_options();
15 - foreach ( $all_options as $_option => $_value ) {
16 - global ${$_option};
17 - ${$_option} = $_value;
18 - }
11 + public function __construct() {
12 + $this->criticalcss = autoptimize()->criticalcss();
19 13 }
20 14
21 - public static function ao_ccss_enqueue( $hash ) {
22 - $self = new self();
15 + public function ao_ccss_enqueue( $hash = '', $path = '', $type = 'is_page' ) {
23 16 // Get key status.
24 - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( false );
17 + $key = $this->criticalcss->key_status( false );
25 18
26 19 // Queue is available to anyone...
27 20 $enqueue = true;
28 21
29 22 // ... which are not the ones below.
30 - if ( is_user_logged_in() || is_feed() || is_404() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status'] ) {
23 + if ( 'nokey' == $key['status'] || 'invalid' == $key['status'] ) {
31 24 $enqueue = false;
32 - autoptimizeCriticalCSSCore::ao_ccss_log( "Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3 );
25 + $this->criticalcss->log( "Job queuing is not available: no valid API key found.", 3 );
26 + } elseif ( ! empty( $hash ) && ( is_user_logged_in() || is_feed() || is_404() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $this->ao_ccss_ua() || false === apply_filters( 'autoptimize_filter_ccss_enqueue_should_enqueue', true ) ) ) {
27 + $enqueue = false;
28 + $this->criticalcss->log( "Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls or calls from criticalcss.com itself.", 3 );
29 + } elseif ( empty( $hash ) && empty( $path ) || ( ( 'is_single' !== $type ) && ( 'is_page' !== $type ) ) ) {
30 + $enqueue = false;
31 + $this->criticalcss->log( "Forced job queuing failed, no path or not right type", 3 );
33 32 }
34 33
35 - if ( $enqueue ) {
36 - // Continue if queue is available
37 - // Attach required arrays/ vars.
38 - global $ao_ccss_rules;
39 - global $ao_ccss_queue_raw;
40 - global $ao_ccss_queue;
41 - global $ao_ccss_forcepath;
34 + if ( ! $enqueue ) {
35 + return;
36 + }
42 37
43 - // Get request path and page type, and initialize the queue update flag.
44 - $req_path = strtok( $_SERVER['REQUEST_URI'], '?' );
45 - $req_type = $self->ao_ccss_get_type();
46 - $job_qualify = false;
47 - $target_rule = false;
48 - $rule_properties = false;
49 - $queue_update = false;
38 + // Continue if queue is available
39 + // Attach required arrays/ vars.
40 + $rules = $this->criticalcss->get_option( 'rules' );
41 + $queue_raw = $this->criticalcss->get_option( 'queue_raw' );
42 + $queue = $this->criticalcss->get_option( 'queue' );
43 + $forcepath = $this->criticalcss->get_option( 'forcepath' );
50 44
51 - // Match for paths in rules.
52 - foreach ( $ao_ccss_rules['paths'] as $path => $props ) {
45 + // Get request path and page type, and initialize the queue update flag.
46 + if ( ! empty( $hash ) ) {
47 + $req_orig = $_SERVER['REQUEST_URI'];
48 + $req_type = $this->ao_ccss_get_type();
49 + } elseif ( ! empty( $path ) ) {
50 + $req_orig = $path;
51 + if ( $path === '/' ) {
52 + $req_type = 'is_front_page';
53 + } else {
54 + $req_type = $type;
55 + }
56 + }
57 + $req_path = strtok( $req_orig, '?' );
53 58
54 - // Prepare rule target and log.
55 - $target_rule = 'paths|' . $path;
56 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 );
59 + // Check if we have a lang param. we need to keep as WPML can switch languages based on that
60 + // and that includes RTL -> LTR so diff. structure, so rules would be RTL vs LTR
61 + // but this needs changes in the structur of the rule object so off by default for now
62 + // as now this will simply result in conditional rules being overwritten.
63 + if ( apply_filters( 'autoptimize_filter_ccss_coreenqueue_honor_lang', false ) && strpos( $req_orig, 'lang=' ) !== false ) {
64 + $req_params = strtok( '?' );
65 + parse_str( $req_params, $req_params_arr );
66 + if ( array_key_exists( 'lang', $req_params_arr ) && !empty( $req_params_arr['lang'] ) ) {
67 + $req_path .= '?lang=' . $req_params_arr['lang'];
68 + }
69 + }
57 70
58 - // Path match
59 - // -> exact match needed for AUTO rules
60 - // -> partial match OK for MANUAL rules (which have empty hash and a file with CCSS).
61 - if ( $path === $req_path || ( false == $props['hash'] && false != $props['file'] && preg_match( '|' . $path . '|', $req_path ) ) ) {
71 + $job_qualify = false;
72 + $target_rule = false;
73 + $rule_properties = false;
74 + $queue_update = false;
62 75
63 - // There's a path match in the rule, so job QUALIFIES with a path rule match.
64 - $job_qualify = true;
65 - $rule_properties = $props;
66 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 );
76 + // Match for paths in rules.
77 + foreach ( $rules['paths'] as $path => $props ) {
67 78
68 - // Stop processing other path rules.
69 - break;
70 - }
79 + // Prepare rule target and log.
80 + $target_rule = 'paths|' . $path;
81 + $this->criticalcss->log( 'Qualifying path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 );
82 +
83 + // Path match
84 + // -> exact match needed for AUTO rules
85 + // -> partial match OK for MANUAL rules (which have empty hash and a file with CCSS).
86 + if ( $path === $req_path || ( false == $props['hash'] && false != $props['file'] && preg_match( '|' . $path . '|', $req_path ) ) ) {
87 +
88 + // There's a path match in the rule, so job QUALIFIES with a path rule match.
89 + $job_qualify = true;
90 + $rule_properties = $props;
91 + $this->criticalcss->log( 'Path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 );
92 +
93 + // Stop processing other path rules.
94 + break;
71 95 }
96 + }
72 97
73 - // Match for types in rules if no path rule matches and if we're not enforcing paths.
74 - if ( ! $job_qualify && ( ! $ao_ccss_forcepath || ! in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) ) {
75 - foreach ( $ao_ccss_rules['types'] as $type => $props ) {
98 + // Match for types in rules if no path rule matches and if we're not enforcing paths.
99 + if ( '' !== $hash && ! $job_qualify && ( ! $forcepath || ! in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) || ! apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) ) ) {
100 + foreach ( $rules['types'] as $type => $props ) {
76 101
77 - // Prepare rule target and log.
78 - $target_rule = 'types|' . $type;
79 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying page type <' . $req_type . '> on path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 );
102 + // Prepare rule target and log.
103 + $target_rule = 'types|' . $type;
104 + $this->criticalcss->log( 'Qualifying page type <' . $req_type . '> on path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 );
80 105
81 - if ( $req_type == $type ) {
82 - // Type match.
83 - // There's a type match in the rule, so job QUALIFIES with a type rule match.
84 - $job_qualify = true;
85 - $rule_properties = $props;
86 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Page type <' . $req_type . '> on path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 );
106 + if ( $req_type == $type ) {
107 + // Type match.
108 + // There's a type match in the rule, so job QUALIFIES with a type rule match.
109 + $job_qualify = true;
110 + $rule_properties = $props;
111 + $this->criticalcss->log( 'Page type <' . $req_type . '> on path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 );
87 112
88 - // Stop processing other type rules.
89 - break;
90 - }
113 + // Stop processing other type rules.
114 + break;
91 115 }
92 116 }
117 + }
93 118
94 - if ( $job_qualify && false == $rule_properties['hash'] && false != $rule_properties['file'] ) {
95 - // If job qualifies but rule hash is false and file isn't false (MANUAL rule), job does not qualify despite what previous evaluations says.
96 - $job_qualify = false;
97 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission DISQUALIFIED by MANUAL rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 );
98 - } elseif ( ! $job_qualify && empty( $rule_properties ) ) {
99 - // But if job does not qualify and rule properties are set, job qualifies as there is no matching rule for it yet
100 - // Fill-in the new target rule.
101 - $job_qualify = true;
119 + if ( $job_qualify && ( ( false == $rule_properties['hash'] && false != $rule_properties['file'] ) || strpos( $req_type, 'template_' ) !== false ) ) {
120 + // If job qualifies but rule hash is false and file isn't false (MANUAL rule) or if template, job does not qualify despite what previous evaluations says.
121 + $job_qualify = false;
122 + $this->criticalcss->log( 'Job submission DISQUALIFIED by MANUAL rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 );
123 + } elseif ( ! $job_qualify && empty( $rule_properties ) ) {
124 + // But if job does not qualify and rule properties are set, job qualifies as there is no matching rule for it yet
125 + // Fill-in the new target rule.
126 + $job_qualify = true;
102 127
103 - // Should we switch to path-base AUTO-rules? Conditions:
104 - // 1. forcepath option has to be enabled (off by default)
105 - // 2. request type should be (by default, but filterable) one of is_page (removed for now: woo_is_product or woo_is_product_category).
106 - if ( $ao_ccss_forcepath && in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) {
107 - if ( '/' !== $req_path ) {
108 - $target_rule = 'paths|' . $req_path;
109 - } else {
110 - // Exception; we don't want a path-based rule for "/" as that messes things up, hard-switch this to a type-based is_front_page rule.
111 - $target_rule = 'types|' . 'is_front_page';
112 - }
128 + // Should we switch to path-base AUTO-rules? Conditions:
129 + // 1. forcepath option has to be enabled (off by default)
130 + // 2. request type should be (by default, but filterable) one of is_page (removed for now: woo_is_product or woo_is_product_category).
131 + if ( ( $forcepath && in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) || apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) || empty( $hash )) {
132 + if ( '/' !== $req_path ) {
133 + $target_rule = 'paths|' . $req_path;
113 134 } else {
114 - $target_rule = 'types|' . $req_type;
135 + // Exception; we don't want a path-based rule for "/" as that messes things up, hard-switch this to a type-based is_front_page rule.
136 + $target_rule = 'types|' . 'is_front_page';
115 137 }
116 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by MISSING rule for page type <' . $req_type . '> on path <' . $req_path . '>, new rule target is <' . $target_rule . '>', 3 );
117 138 } else {
118 - // Or just log a job qualified by a matching rule.
119 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by AUTO rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 );
139 + $target_rule = 'types|' . $req_type;
120 140 }
141 + $this->criticalcss->log( 'Job submission QUALIFIED by MISSING rule for page type <' . $req_type . '> on path <' . $req_path . '>, new rule target is <' . $target_rule . '>', 3 );
142 + } else {
143 + // Or just log a job qualified by a matching rule.
144 + $this->criticalcss->log( 'Job submission QUALIFIED by AUTO rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 );
145 + }
121 146
122 - // Submit job.
123 - if ( $job_qualify ) {
124 - if ( ! array_key_exists( $req_path, $ao_ccss_queue ) ) {
125 - // This is a NEW job
126 - // Merge job into the queue.
127 - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job(
147 + // Submit job.
148 + if ( $job_qualify ) {
149 + if ( ! array_key_exists( $req_path, $queue ) ) {
150 + // This is a NEW job
151 + // Merge job into the queue.
152 + $queue[ $req_path ] = $this->ao_ccss_define_job(
153 + $req_path,
154 + $target_rule,
155 + $req_type,
156 + $hash,
157 + null,
158 + null,
159 + null,
160 + null,
161 + true
162 + );
163 + // Set update flag.
164 + $queue_update = true;
165 + } else {
166 + // This is an existing job
167 + // The job is still NEW, most likely this is extra CSS file for the same page that needs a hash.
168 + if ( 'NEW' == $queue[ $req_path ]['jqstat'] ) {
169 + // Add hash if it's not already in the job.
170 + if ( ! in_array( $hash, $queue[ $req_path ]['hashes'] ) ) {
171 + // Push new hash to its array and update flag.
172 + $queue_update = array_push( $queue[ $req_path ]['hashes'], $hash );
173 +
174 + // Log job update.
175 + $this->criticalcss->log( 'Hashes UPDATED on local job id <' . $queue[ $req_path ]['ljid'] . '>, job status NEW, target rule <' . $queue[ $req_path ]['rtarget'] . '>, hash added: ' . $hash, 3 );
176 +
177 + // Return from here as the hash array is already updated.
178 + return true;
179 + }
180 + } elseif ( 'NEW' != $queue[ $req_path ]['jqstat'] && 'JOB_QUEUED' != $queue[ $req_path ]['jqstat'] && 'JOB_ONGOING' != $queue[ $req_path ]['jqstat'] ) {
181 + // Allow requeuing jobs that are not NEW, JOB_QUEUED or JOB_ONGOING
182 + // Merge new job keeping some previous job values.
183 + $queue[ $req_path ] = $this->ao_ccss_define_job(
128 184 $req_path,
129 185 $target_rule,
130 186 $req_type,
131 187 $hash,
132 - null,
133 - null,
134 - null,
135 - null,
136 - true
188 + $queue[ $req_path ]['file'],
189 + $queue[ $req_path ]['jid'],
190 + $queue[ $req_path ]['jrstat'],
191 + $queue[ $req_path ]['jvstat'],
192 + false
137 193 );
138 194 // Set update flag.
139 195 $queue_update = true;
140 - } else {
141 - // This is an existing job
142 - // The job is still NEW, most likely this is extra CSS file for the same page that needs a hash.
143 - if ( 'NEW' == $ao_ccss_queue[ $req_path ]['jqstat'] ) {
144 - // Add hash if it's not already in the job.
145 - if ( ! in_array( $hash, $ao_ccss_queue[ $req_path ]['hashes'] ) ) {
146 - // Push new hash to its array and update flag.
147 - $queue_update = array_push( $ao_ccss_queue[ $req_path ]['hashes'], $hash );
148 -
149 - // Log job update.
150 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Hashes UPDATED on local job id <' . $ao_ccss_queue[ $req_path ]['ljid'] . '>, job status NEW, target rule <' . $ao_ccss_queue[ $req_path ]['rtarget'] . '>, hash added: ' . $hash, 3 );
151 -
152 - // Return from here as the hash array is already updated.
153 - return true;
154 - }
155 - } elseif ( 'NEW' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[ $req_path ]['jqstat'] ) {
156 - // Allow requeuing jobs that are not NEW, JOB_QUEUED or JOB_ONGOING
157 - // Merge new job keeping some previous job values.
158 - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job(
159 - $req_path,
160 - $target_rule,
161 - $req_type,
162 - $hash,
163 - $ao_ccss_queue[ $req_path ]['file'],
164 - $ao_ccss_queue[ $req_path ]['jid'],
165 - $ao_ccss_queue[ $req_path ]['jrstat'],
166 - $ao_ccss_queue[ $req_path ]['jvstat'],
167 - false
168 - );
169 - // Set update flag.
170 - $queue_update = true;
171 - }
172 196 }
197 + }
173 198
174 - if ( $queue_update ) {
175 - // Persist the job to the queue and return.
176 - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue );
177 - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false );
178 - return true;
179 - } else {
180 - // Or just return false if no job was added.
181 - autoptimizeCriticalCSSCore::ao_ccss_log( 'A job for path <' . $req_path . '> already exist with NEW or PENDING status, skipping job creation', 3 );
182 - return false;
183 - }
199 + if ( $queue_update ) {
200 + // Persist the job to the queue and return.
201 + $queue_raw = json_encode( $queue );
202 + update_option( 'autoptimize_ccss_queue', $queue_raw, false );
203 + $this->criticalcss->flush_options();
204 + return true;
205 + } else {
206 + // Or just return false if no job was added.
207 + $this->criticalcss->log( 'A job for path <' . $req_path . '> already exist with NEW or PENDING status, skipping job creation', 3 );
208 + return false;
184 209 }
185 210 }
186 211 }
187 212
@@ -187,30 +212,42 @@
187 212
188 213 public function ao_ccss_get_type() {
189 214 // Get the type of a page
190 215 // Attach the conditional tags array.
191 - global $ao_ccss_types;
216 + $types = $this->criticalcss->get_types();
217 + $forcepath = $this->criticalcss->get_option( 'forcepath' );
192 218
193 219 // By default, a page type is false.
194 220 $page_type = false;
195 221
196 222 // Iterates over the array to match a type.
197 - foreach ( $ao_ccss_types as $type ) {
198 - if ( strpos( $type, 'custom_post_' ) !== false ) {
199 - // Match custom post types.
223 + foreach ( $types as $type ) {
224 + if ( is_404() ) {
225 + $page_type = 'is_404';
226 + break;
227 + } elseif ( is_front_page() ) {
228 + // identify frontpage immediately to avoid it also matching a CPT or template.
229 + $page_type = 'is_front_page';
230 + break;
231 + } elseif ( strpos( $type, 'custom_post_' ) !== false && ( ! $forcepath || ! is_page() ) ) {
232 + // Match custom post types and not page or page not forced to path-based.
200 233 if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) {
201 234 $page_type = $type;
202 235 break;
203 236 }
204 - } elseif ( strpos( $type, 'template_' ) !== false ) {
205 - // If templates; don't break, templates become manual-only rules.
237 + } elseif ( strpos( $type, 'template_' ) !== false && ( ! $forcepath || ! is_page() ) ) {
238 + // Match templates if not page or if page is not forced to path-based.
239 + if ( is_page_template( substr( $type, 9 ) ) ) {
240 + $page_type = $type;
241 + break;
242 + }
206 243 } else {
207 244 // Match all other existing types
208 245 // but remove prefix to be able to check if the function exists & returns true.
209 246 $_type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type );
210 247 if ( function_exists( $_type ) && call_user_func( $_type ) ) {
211 - // Make sure we only return is_front_page (and is_home) for one page, not for the "paged frontpage" (/page/2 ..).
212 - if ( ( 'is_front_page' !== $_type && 'is_home' !== $_type ) || ! is_paged() ) {
248 + // Make sure we only return for one page, not for the "paged pages" (/page/2 ..).
249 + if ( ! is_page() || ! is_paged() ) {
213 250 $page_type = $type;
214 251 break;
215 252 }
216 253 }
@@ -245,9 +282,9 @@
245 282 $operation = 'UPDATED';
246 283 }
247 284
248 285 // Log job creation.
249 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job ' . $operation . ' with local job id <' . $path['ljid'] . '> for target rule <' . $target . '>', 3 );
286 + $this->criticalcss->log( 'Job ' . $operation . ' with local job id <' . $path['ljid'] . '> for target rule <' . $target . '>', 3 );
250 287
251 288 return $path;
252 289 }
253 290