@@ -3,7 +3,7 @@ discard block |
||
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,35 +19,35 @@ discard block |
||
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 a constant with the directory to store critical CSS in. |
24 | - if ( is_multisite() ) { |
|
24 | + if (is_multisite()) { |
|
25 | 25 | $blog_id = get_current_blog_id(); |
26 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
|
26 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'.$blog_id.'/'); |
|
27 | 27 | } else { |
28 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' ); |
|
28 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'); |
|
29 | 29 | } |
30 | 30 | } |
31 | - if ( ! defined( 'AO_CCSS_VER' ) ) { |
|
31 | + if (!defined('AO_CCSS_VER')) { |
|
32 | 32 | // Define plugin version. |
33 | - define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
33 | + define('AO_CCSS_VER', 'AO_'.AUTOPTIMIZE_PLUGIN_VERSION); |
|
34 | 34 | |
35 | 35 | // Define constants for criticalcss.com base path and API endpoints. |
36 | 36 | // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option. |
37 | - define( 'AO_CCSS_URL', 'https://criticalcss.com' ); |
|
38 | - define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' ); |
|
39 | - define( 'AO_CCSS_SLEEP', 10 ); |
|
37 | + define('AO_CCSS_URL', 'https://criticalcss.com'); |
|
38 | + define('AO_CCSS_API', AO_CCSS_URL.'/api/premium/'); |
|
39 | + define('AO_CCSS_SLEEP', 10); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | // Define support files locations, in case they are not already defined. |
43 | - if ( ! defined( 'AO_CCSS_LOCK' ) ) { |
|
44 | - define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' ); |
|
43 | + if (!defined('AO_CCSS_LOCK')) { |
|
44 | + define('AO_CCSS_LOCK', AO_CCSS_DIR.'queue.lock'); |
|
45 | 45 | } |
46 | - if ( ! defined( 'AO_CCSS_LOG' ) ) { |
|
47 | - define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' ); |
|
46 | + if (!defined('AO_CCSS_LOG')) { |
|
47 | + define('AO_CCSS_LOG', AO_CCSS_DIR.'queuelog.html'); |
|
48 | 48 | } |
49 | - if ( ! defined( 'AO_CCSS_DEBUG' ) ) { |
|
50 | - define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' ); |
|
49 | + if (!defined('AO_CCSS_DEBUG')) { |
|
50 | + define('AO_CCSS_DEBUG', AO_CCSS_DIR.'queue.json'); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | $this->filepath = __FILE__; |
@@ -60,19 +60,19 @@ discard block |
||
60 | 60 | { |
61 | 61 | // get all options. |
62 | 62 | $all_options = $this->fetch_options(); |
63 | - foreach ( $all_options as $option => $value ) { |
|
63 | + foreach ($all_options as $option => $value) { |
|
64 | 64 | ${$option} = $value; |
65 | 65 | } |
66 | 66 | |
67 | 67 | // make sure the 10 minutes cron schedule is added. |
68 | - add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); |
|
68 | + add_filter('cron_schedules', array($this, 'ao_ccss_interval')); |
|
69 | 69 | |
70 | 70 | // check if we need to upgrade. |
71 | 71 | $this->check_upgrade(); |
72 | 72 | |
73 | 73 | // make sure ao_ccss_queue is scheduled OK if an API key is set. |
74 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
75 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
74 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
75 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
76 | 76 | } |
77 | 77 | } |
78 | 78 | |
@@ -80,12 +80,12 @@ discard block |
||
80 | 80 | // Required libs, core is always needed. |
81 | 81 | $criticalcss_core = new autoptimizeCriticalCSSCore(); |
82 | 82 | |
83 | - if ( defined( 'DOING_CRON' ) || is_admin() ) { |
|
83 | + if (defined('DOING_CRON') || is_admin()) { |
|
84 | 84 | // TODO: also include if overridden somehow to force queue processing to be executed? |
85 | 85 | $criticalcss_cron = new autoptimizeCriticalCSSCron(); |
86 | 86 | } |
87 | 87 | |
88 | - if ( is_admin() ) { |
|
88 | + if (is_admin()) { |
|
89 | 89 | $criticalcss_settings = new autoptimizeCriticalCSSSettings(); |
90 | 90 | } else { |
91 | 91 | // enqueuing only done when not wp-admin. |
@@ -96,51 +96,51 @@ discard block |
||
96 | 96 | public static function fetch_options() { |
97 | 97 | static $autoptimize_ccss_options = null; |
98 | 98 | |
99 | - if ( null === $autoptimize_ccss_options ) { |
|
99 | + if (null === $autoptimize_ccss_options) { |
|
100 | 100 | // not cached yet, fetching from WordPress options. |
101 | - $autoptimize_ccss_options['ao_css_defer'] = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' ); |
|
102 | - $autoptimize_ccss_options['ao_css_defer_inline'] = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer_inline' ); |
|
103 | - $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option( 'autoptimize_ccss_rules', false ); |
|
104 | - $autoptimize_ccss_options['ao_ccss_additional'] = get_option( 'autoptimize_ccss_additional' ); |
|
105 | - $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option( 'autoptimize_ccss_queue', false ); |
|
106 | - $autoptimize_ccss_options['ao_ccss_viewport'] = get_option( 'autoptimize_ccss_viewport', false ); |
|
107 | - $autoptimize_ccss_options['ao_ccss_finclude'] = get_option( 'autoptimize_ccss_finclude', false ); |
|
108 | - $autoptimize_ccss_options['ao_ccss_rlimit'] = get_option( 'autoptimize_ccss_rlimit', '5' ); |
|
109 | - $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option( 'autoptimize_ccss_noptimize', false ); |
|
110 | - $autoptimize_ccss_options['ao_ccss_debug'] = get_option( 'autoptimize_ccss_debug', false ); |
|
111 | - $autoptimize_ccss_options['ao_ccss_key'] = get_option( 'autoptimize_ccss_key' ); |
|
112 | - $autoptimize_ccss_options['ao_ccss_keyst'] = get_option( 'autoptimize_ccss_keyst' ); |
|
113 | - $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option( 'autoptimize_ccss_loggedin', '1' ); |
|
114 | - $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option( 'autoptimize_ccss_forcepath', '1' ); |
|
115 | - $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' ); |
|
116 | - $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option( 'autoptimize_ccss_deferjquery', false ); |
|
117 | - $autoptimize_ccss_options['ao_ccss_domain'] = get_option( 'autoptimize_ccss_domain' ); |
|
118 | - $autoptimize_ccss_options['ao_ccss_unloadccss'] = get_option( 'autoptimize_ccss_unloadccss', false ); |
|
119 | - |
|
120 | - if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) { |
|
121 | - $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ); |
|
122 | - } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) { |
|
101 | + $autoptimize_ccss_options['ao_css_defer'] = autoptimizeOptionWrapper::get_option('autoptimize_css_defer'); |
|
102 | + $autoptimize_ccss_options['ao_css_defer_inline'] = autoptimizeOptionWrapper::get_option('autoptimize_css_defer_inline'); |
|
103 | + $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option('autoptimize_ccss_rules', false); |
|
104 | + $autoptimize_ccss_options['ao_ccss_additional'] = get_option('autoptimize_ccss_additional'); |
|
105 | + $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option('autoptimize_ccss_queue', false); |
|
106 | + $autoptimize_ccss_options['ao_ccss_viewport'] = get_option('autoptimize_ccss_viewport', false); |
|
107 | + $autoptimize_ccss_options['ao_ccss_finclude'] = get_option('autoptimize_ccss_finclude', false); |
|
108 | + $autoptimize_ccss_options['ao_ccss_rlimit'] = get_option('autoptimize_ccss_rlimit', '5'); |
|
109 | + $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option('autoptimize_ccss_noptimize', false); |
|
110 | + $autoptimize_ccss_options['ao_ccss_debug'] = get_option('autoptimize_ccss_debug', false); |
|
111 | + $autoptimize_ccss_options['ao_ccss_key'] = get_option('autoptimize_ccss_key'); |
|
112 | + $autoptimize_ccss_options['ao_ccss_keyst'] = get_option('autoptimize_ccss_keyst'); |
|
113 | + $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option('autoptimize_ccss_loggedin', '1'); |
|
114 | + $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option('autoptimize_ccss_forcepath', '1'); |
|
115 | + $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option('autoptimize_service_availablity'); |
|
116 | + $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option('autoptimize_ccss_deferjquery', false); |
|
117 | + $autoptimize_ccss_options['ao_ccss_domain'] = get_option('autoptimize_ccss_domain'); |
|
118 | + $autoptimize_ccss_options['ao_ccss_unloadccss'] = get_option('autoptimize_ccss_unloadccss', false); |
|
119 | + |
|
120 | + if (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') === false && strpos($autoptimize_ccss_options['ao_ccss_domain'], 'uggc') === 0) { |
|
121 | + $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13($autoptimize_ccss_options['ao_ccss_domain']); |
|
122 | + } elseif (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') !== false) { |
|
123 | 123 | // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain). |
124 | - update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) ); |
|
124 | + update_option('autoptimize_ccss_domain', str_rot13($autoptimize_ccss_options['ao_ccss_domain'])); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | // Setup the rules array. |
128 | - if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) { |
|
128 | + if (empty($autoptimize_ccss_options['ao_ccss_rules_raw'])) { |
|
129 | 129 | $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array(); |
130 | 130 | $autoptimize_ccss_options['ao_ccss_rules']['types'] = array(); |
131 | 131 | } else { |
132 | - $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true ); |
|
132 | + $autoptimize_ccss_options['ao_ccss_rules'] = json_decode($autoptimize_ccss_options['ao_ccss_rules_raw'], true); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | // Setup the queue array. |
136 | - if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) { |
|
136 | + if (empty($autoptimize_ccss_options['ao_ccss_queue_raw'])) { |
|
137 | 137 | $autoptimize_ccss_options['ao_ccss_queue'] = array(); |
138 | 138 | } else { |
139 | - $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true ); |
|
139 | + $autoptimize_ccss_options['ao_ccss_queue'] = json_decode($autoptimize_ccss_options['ao_ccss_queue_raw'], true); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | // Override API key if constant is defined. |
143 | - if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) { |
|
143 | + if (defined('AUTOPTIMIZE_CRITICALCSS_API_KEY')) { |
|
144 | 144 | $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; |
145 | 145 | } |
146 | 146 | } |
@@ -152,49 +152,49 @@ discard block |
||
152 | 152 | global $ao_ccss_key; |
153 | 153 | |
154 | 154 | // Create the cache directory if it doesn't exist already. |
155 | - if ( ! file_exists( AO_CCSS_DIR ) ) { |
|
156 | - mkdir( AO_CCSS_DIR, 0755, true ); |
|
155 | + if (!file_exists(AO_CCSS_DIR)) { |
|
156 | + mkdir(AO_CCSS_DIR, 0755, true); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | // Create a scheduled event for the queue. |
160 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
161 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
160 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
161 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | // Create a scheduled event for log maintenance. |
165 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { |
|
166 | - wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' ); |
|
165 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_maintenance')) { |
|
166 | + wp_schedule_event(time(), 'twicedaily', 'ao_ccss_maintenance'); |
|
167 | 167 | } |
168 | 168 | } |
169 | 169 | |
170 | 170 | public function check_upgrade() { |
171 | - $db_version = get_option( 'autoptimize_ccss_version', '' ); |
|
172 | - if ( AO_CCSS_VER !== $db_version ) { |
|
171 | + $db_version = get_option('autoptimize_ccss_version', ''); |
|
172 | + if (AO_CCSS_VER !== $db_version) { |
|
173 | 173 | // check schedules & re-schedule if needed. |
174 | 174 | $this->on_upgrade(); |
175 | 175 | // and update db_version. |
176 | - update_option( 'autoptimize_ccss_version', AO_CCSS_VER ); |
|
176 | + update_option('autoptimize_ccss_version', AO_CCSS_VER); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
180 | - public function ao_ccss_interval( $schedules ) { |
|
180 | + public function ao_ccss_interval($schedules) { |
|
181 | 181 | // Let interval be configurable. |
182 | - if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) { |
|
182 | + if (!defined('AO_CCSS_DEBUG_INTERVAL')) { |
|
183 | 183 | $intsec = 600; |
184 | 184 | } else { |
185 | 185 | $intsec = AO_CCSS_DEBUG_INTERVAL; |
186 | - if ( $intsec >= 120 ) { |
|
187 | - $inttxt = $intsec / 60 . ' minutes'; |
|
186 | + if ($intsec >= 120) { |
|
187 | + $inttxt = $intsec/60.' minutes'; |
|
188 | 188 | } else { |
189 | - $inttxt = $intsec . ' second(s)'; |
|
189 | + $inttxt = $intsec.' second(s)'; |
|
190 | 190 | } |
191 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); |
|
191 | + autoptimizeCriticalCSSCore::ao_ccss_log('Using custom WP-Cron interval of '.$inttxt, 3); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | // Attach interval to schedule. |
195 | 195 | $schedules['ao_ccss'] = array( |
196 | 196 | 'interval' => $intsec, |
197 | - 'display' => __( 'Autoptimize CriticalCSS' ), |
|
197 | + 'display' => __('Autoptimize CriticalCSS'), |
|
198 | 198 | ); |
199 | 199 | return $schedules; |
200 | 200 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * processes the queue, submitting jobs to criticalcss.com and retrieving generated CSS from criticalcss.com and saving rules. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,23 +13,23 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
20 | 20 | |
21 | 21 | // Add queue control to a registered event. |
22 | - add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) ); |
|
22 | + add_action('ao_ccss_queue', array($this, 'ao_ccss_queue_control')); |
|
23 | 23 | // Add cleaning job to a registered event. |
24 | - add_action( 'ao_ccss_maintenance', array( $this, 'ao_ccss_cleaning' ) ); |
|
24 | + add_action('ao_ccss_maintenance', array($this, 'ao_ccss_cleaning')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function ao_ccss_queue_control() { |
28 | 28 | // The queue execution backend. |
29 | 29 | global $ao_ccss_key; |
30 | - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
|
30 | + if (!isset($ao_ccss_key) || empty($ao_ccss_key)) { |
|
31 | 31 | // no key set, not processing the queue! |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log('No key set, so not processing queue.', 3); |
|
33 | 33 | return; |
34 | 34 | } |
35 | 35 | |
@@ -50,35 +50,35 @@ discard block |
||
50 | 50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
51 | 51 | */ |
52 | 52 | $queue_debug = false; |
53 | - if ( file_exists( AO_CCSS_DEBUG ) ) { |
|
54 | - $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
|
55 | - $qdobj = json_decode( $qdobj_raw, true ); |
|
56 | - if ( $qdobj ) { |
|
57 | - if ( 1 === $qdobj['enable'] ) { |
|
53 | + if (file_exists(AO_CCSS_DEBUG)) { |
|
54 | + $qdobj_raw = file_get_contents(AO_CCSS_DEBUG); |
|
55 | + $qdobj = json_decode($qdobj_raw, true); |
|
56 | + if ($qdobj) { |
|
57 | + if (1 === $qdobj['enable']) { |
|
58 | 58 | $queue_debug = true; |
59 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
|
59 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue operating in debug mode with the following settings: <'.$qdobj_raw.'>', 3); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // Set some default values for $qdobj to avoid function call warnings. |
65 | - if ( ! $queue_debug ) { |
|
65 | + if (!$queue_debug) { |
|
66 | 66 | $qdobj['htcode'] = false; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Check if queue is already running. |
70 | 70 | $queue_lock = false; |
71 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
71 | + if (file_exists(AO_CCSS_LOCK)) { |
|
72 | 72 | $queue_lock = true; |
73 | 73 | } |
74 | 74 | |
75 | 75 | // Proceed with the queue if it's not already running. |
76 | - if ( ! $queue_lock ) { |
|
76 | + if (!$queue_lock) { |
|
77 | 77 | |
78 | 78 | // Log queue start and create the lock file. |
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
|
80 | - if ( touch( AO_CCSS_LOCK ) ) { |
|
81 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control started', 3); |
|
80 | + if (touch(AO_CCSS_LOCK)) { |
|
81 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control locked', 3); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Attach required variables. |
@@ -88,223 +88,223 @@ discard block |
||
88 | 88 | // Initialize job counters. |
89 | 89 | $jc = 1; |
90 | 90 | $jr = 1; |
91 | - $jt = count( $ao_ccss_queue ); |
|
91 | + $jt = count($ao_ccss_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($ao_ccss_queue, 'jqstat'), $ao_ccss_queue); // @codingStandardsIgnoreLine |
|
95 | 95 | |
96 | 96 | // Iterates over the entire queue. |
97 | - foreach ( $ao_ccss_queue as $path => $jprops ) { |
|
97 | + foreach ($ao_ccss_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 | 102 | $oldccssfile = false; |
103 | - $trule = explode( '|', $jprops['rtarget'] ); |
|
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 | + autoptimizeCriticalCSSCore::ao_ccss_log('Processing job '.$jc.' of '.$jt.' with id <'.$jprops['ljid'].'> and status <'.$jprops['jqstat'].'>', 3); |
|
107 | 107 | |
108 | 108 | // Process NEW jobs. |
109 | - if ( 'NEW' == $jprops['jqstat'] ) { |
|
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 | + autoptimizeCriticalCSSCore::ao_ccss_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 | - $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
|
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 | - if ( $hash ) { |
|
118 | + if ($hash) { |
|
119 | 119 | // Set job hash. |
120 | 120 | $jprops['hash'] = $hash; |
121 | 121 | |
122 | 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 ); |
|
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 | 126 | } |
127 | 127 | |
128 | 128 | // Dispatch the job generate request and increment request count. |
129 | - $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
|
129 | + $apireq = $this->ao_ccss_api_generate($path, $queue_debug, $qdobj['htcode']); |
|
130 | 130 | $jr++; |
131 | 131 | |
132 | 132 | // 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'] ) { |
|
133 | + if ('JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status']) { |
|
134 | 134 | // SUCCESS: request has a valid result. |
135 | 135 | // Update job properties. |
136 | 136 | $jprops['jid'] = $apireq['job']['id']; |
137 | 137 | $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 ); |
|
139 | - } elseif ( 'STATUS_JOB_BAD' == $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); |
|
139 | + } elseif ('STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
140 | 140 | // ERROR: concurrent requests |
141 | 141 | // Update job properties. |
142 | 142 | $jprops['jid'] = $apireq['job']['id']; |
143 | 143 | $jprops['jqstat'] = $apireq['job']['status']; |
144 | - if ( $apireq['job']['error'] ) { |
|
144 | + if ($apireq['job']['error']) { |
|
145 | 145 | $jprops['jrstat'] = $apireq['job']['error']; |
146 | 146 | } else { |
147 | 147 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
148 | 148 | } |
149 | 149 | $jprops['jvstat'] = 'NONE'; |
150 | - $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'] ) { |
|
150 | + $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 | 153 | // ERROR: key validation |
154 | 154 | // Update job properties. |
155 | 155 | $jprops['jqstat'] = $apireq['errorCode']; |
156 | 156 | $jprops['jrstat'] = $apireq['error']; |
157 | 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 ) ) { |
|
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 | 161 | // ERROR: no response |
162 | 162 | // Update job properties. |
163 | 163 | $jprops['jqstat'] = 'NO_RESPONSE'; |
164 | 164 | $jprops['jrstat'] = 'NONE'; |
165 | 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 ); |
|
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 | 168 | } else { |
169 | 169 | // UNKNOWN: unhandled generate exception |
170 | 170 | // Update job properties. |
171 | 171 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
172 | 172 | $jprops['jrstat'] = 'NONE'; |
173 | 173 | $jprops['jvstat'] = 'NONE'; |
174 | - $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 ); |
|
174 | + $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); |
|
177 | 177 | } |
178 | 178 | } else { |
179 | 179 | // SUCCESS: Job hash is equal to a previous one, so it's done |
180 | 180 | // Update job status and finish time. |
181 | 181 | $jprops['jqstat'] = 'JOB_DONE'; |
182 | - $jprops['jftime'] = microtime( true ); |
|
183 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
182 | + $jprops['jftime'] = microtime(true); |
|
183 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> requires no further processing, status now is <'.$jprops['jqstat'].'>', 3); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Set queue update flag. |
187 | 187 | $update = true; |
188 | 188 | |
189 | - } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
|
189 | + } elseif ('JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat']) { |
|
190 | 190 | // Process QUEUED and ONGOING jobs |
191 | 191 | // Log the pending job. |
192 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
|
192 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found PENDING job with local ID <'.$jprops['ljid'].'>, continuing its queue processing', 3); |
|
193 | 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 ); |
|
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 | 198 | } |
199 | 199 | |
200 | 200 | // Dispatch the job result request and increment request count. |
201 | - $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
|
201 | + $apireq = $this->ao_ccss_api_results($jprops['jid'], $queue_debug, $qdobj['htcode']); |
|
202 | 202 | $jr++; |
203 | 203 | |
204 | 204 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
205 | 205 | // Replace API response values if queue debugging is enabled and some value is set. |
206 | - if ( $queue_debug ) { |
|
207 | - if ( $qdobj['status'] ) { |
|
206 | + if ($queue_debug) { |
|
207 | + if ($qdobj['status']) { |
|
208 | 208 | $apireq['status'] = $qdobj['status']; |
209 | 209 | } |
210 | - if ( $qdobj['resultStatus'] ) { |
|
210 | + if ($qdobj['resultStatus']) { |
|
211 | 211 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
212 | 212 | } |
213 | - if ( $qdobj['validationStatus'] ) { |
|
213 | + if ($qdobj['validationStatus']) { |
|
214 | 214 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
218 | - if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
|
218 | + if ('JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status']) { |
|
219 | 219 | // SUCCESS: request has a valid result |
220 | 220 | // Process a PENDING job |
221 | 221 | // Update job properties. |
222 | 222 | $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 ); |
|
224 | - } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
|
223 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'> unchanged', 3); |
|
224 | + } elseif ('JOB_DONE' == $apireq['status']) { |
|
225 | 225 | // Process a DONE job |
226 | 226 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
227 | - if ( 'HTML_404' == $apireq['resultStatus'] ) { |
|
227 | + if ('HTML_404' == $apireq['resultStatus']) { |
|
228 | 228 | $apireq['resultStatus'] = 'GOOD'; |
229 | 229 | } |
230 | 230 | |
231 | - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
|
231 | + if ('GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus']) { |
|
232 | 232 | // SUCCESS: GOOD job with GOOD validation |
233 | 233 | // Update job properties. |
234 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
|
234 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, false); |
|
235 | 235 | $jprops['jqstat'] = $apireq['status']; |
236 | 236 | $jprops['jrstat'] = $apireq['resultStatus']; |
237 | 237 | $jprops['jvstat'] = $apireq['validationStatus']; |
238 | - $jprops['jftime'] = microtime( true ); |
|
238 | + $jprops['jftime'] = microtime(true); |
|
239 | 239 | $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'] ) ) { |
|
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 | 242 | // SUCCESS: GOOD job with WARN or BAD validation |
243 | 243 | // Update job properties. |
244 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
|
244 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, true); |
|
245 | 245 | $jprops['jqstat'] = $apireq['status']; |
246 | 246 | $jprops['jrstat'] = $apireq['resultStatus']; |
247 | 247 | $jprops['jvstat'] = $apireq['validationStatus']; |
248 | - $jprops['jftime'] = microtime( true ); |
|
248 | + $jprops['jftime'] = microtime(true); |
|
249 | 249 | $rule_update = true; |
250 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
|
251 | - } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
|
250 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but requires REVIEW', 3); |
|
251 | + } elseif ('GOOD' != $apireq['resultStatus'] && ('GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'])) { |
|
252 | 252 | // ERROR: no GOOD, WARN or BAD results |
253 | 253 | // Update job properties. |
254 | 254 | $jprops['jqstat'] = $apireq['status']; |
255 | 255 | $jprops['jrstat'] = $apireq['resultStatus']; |
256 | 256 | $jprops['jvstat'] = $apireq['validationStatus']; |
257 | - $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 ); |
|
257 | + $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); |
|
259 | 259 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
260 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
260 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
261 | 261 | } else { |
262 | 262 | // UNKNOWN: unhandled JOB_DONE exception |
263 | 263 | // Update job properties. |
264 | 264 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
265 | 265 | $jprops['jrstat'] = $apireq['resultStatus']; |
266 | 266 | $jprops['jvstat'] = $apireq['validationStatus']; |
267 | - $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 ); |
|
267 | + $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); |
|
269 | 269 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
270 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
270 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
271 | 271 | } |
272 | - } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
272 | + } elseif ('JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
273 | 273 | // ERROR: failed job |
274 | 274 | // Update job properties. |
275 | 275 | $jprops['jqstat'] = $apireq['job']['status']; |
276 | - if ( $apireq['job']['error'] ) { |
|
276 | + if ($apireq['job']['error']) { |
|
277 | 277 | $jprops['jrstat'] = $apireq['job']['error']; |
278 | 278 | } else { |
279 | 279 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
280 | 280 | } |
281 | 281 | $jprops['jvstat'] = 'NONE'; |
282 | - $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 ); |
|
284 | - } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
282 | + $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); |
|
284 | + } elseif ('This css no longer exists. Please re-generate it.' == $apireq['error']) { |
|
285 | 285 | // ERROR: CSS doesn't exist |
286 | 286 | // Update job properties. |
287 | 287 | $jprops['jqstat'] = 'NO_CSS'; |
288 | 288 | $jprops['jrstat'] = $apireq['error']; |
289 | 289 | $jprops['jvstat'] = 'NONE'; |
290 | - $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 ); |
|
292 | - } elseif ( empty( $apireq ) ) { |
|
290 | + $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); |
|
292 | + } elseif (empty($apireq)) { |
|
293 | 293 | // ERROR: no response |
294 | 294 | // Update job properties. |
295 | 295 | $jprops['jqstat'] = 'NO_RESPONSE'; |
296 | 296 | $jprops['jrstat'] = 'NONE'; |
297 | 297 | $jprops['jvstat'] = 'NONE'; |
298 | - $jprops['jftime'] = microtime( true ); |
|
299 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
298 | + $jprops['jftime'] = microtime(true); |
|
299 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
300 | 300 | } else { |
301 | 301 | // UNKNOWN: unhandled results exception |
302 | 302 | // Update job properties. |
303 | 303 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
304 | 304 | $jprops['jrstat'] = 'NONE'; |
305 | 305 | $jprops['jvstat'] = 'NONE'; |
306 | - $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 ); |
|
306 | + $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); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | // Set queue update flag. |
@@ -312,40 +312,40 @@ discard block |
||
312 | 312 | } |
313 | 313 | |
314 | 314 | // Mark DONE jobs for removal. |
315 | - if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
|
315 | + if ('JOB_DONE' == $jprops['jqstat']) { |
|
316 | 316 | $update = true; |
317 | 317 | $deljob = true; |
318 | 318 | } |
319 | 319 | |
320 | 320 | // Persist updated queue object. |
321 | - if ( $update ) { |
|
322 | - if ( ! $deljob ) { |
|
321 | + if ($update) { |
|
322 | + if (!$deljob) { |
|
323 | 323 | // Update properties of a NEW or PENDING job... |
324 | - $ao_ccss_queue[ $path ] = $jprops; |
|
324 | + $ao_ccss_queue[$path] = $jprops; |
|
325 | 325 | } else { |
326 | 326 | // ...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 ); |
|
327 | + unset($ao_ccss_queue[$path]); |
|
328 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> is DONE and was removed from the queue', 3); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | // 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 ); |
|
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); |
|
335 | 335 | |
336 | 336 | // Update target rule. |
337 | - if ( $rule_update ) { |
|
338 | - $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 | + if ($rule_update) { |
|
338 | + $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); |
|
340 | 340 | } |
341 | 341 | } else { |
342 | 342 | // Or log no queue action. |
343 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
|
343 | + autoptimizeCriticalCSSCore::ao_ccss_log('Nothing to do on this job', 3); |
|
344 | 344 | } |
345 | 345 | |
346 | 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 ); |
|
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); |
|
349 | 349 | break; |
350 | 350 | } |
351 | 351 | |
@@ -354,46 +354,46 @@ discard block |
||
354 | 354 | } |
355 | 355 | |
356 | 356 | // Remove the lock file and log the queue end. |
357 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
358 | - unlink( AO_CCSS_LOCK ); |
|
359 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
|
357 | + if (file_exists(AO_CCSS_LOCK)) { |
|
358 | + unlink(AO_CCSS_LOCK); |
|
359 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control unlocked', 3); |
|
360 | 360 | } |
361 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
|
361 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control finished', 3); |
|
362 | 362 | |
363 | 363 | // Log that queue is locked. |
364 | 364 | } else { |
365 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
|
365 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue is already running, skipping the attempt to run it again', 3); |
|
366 | 366 | } |
367 | 367 | } |
368 | 368 | |
369 | - public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) { |
|
369 | + public function ao_ccss_diff_hashes($ljid, $hash, $hashes, $rule) { |
|
370 | 370 | // Compare job hashes |
371 | 371 | // STEP 1: update job hashes. |
372 | - if ( 1 == count( $hashes ) ) { |
|
372 | + if (1 == count($hashes)) { |
|
373 | 373 | // Job with a single hash |
374 | 374 | // Set job hash. |
375 | 375 | $hash = $hashes[0]; |
376 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 ); |
|
376 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with SINGLE hash <'.$hash.'>', 3); |
|
377 | 377 | } else { |
378 | 378 | // Job with multiple hashes |
379 | 379 | // Loop through hashes to concatenate them. |
380 | 380 | $nhash = ''; |
381 | - foreach ( $hashes as $shash ) { |
|
381 | + foreach ($hashes as $shash) { |
|
382 | 382 | $nhash .= $shash; |
383 | 383 | } |
384 | 384 | |
385 | 385 | // Set job hash. |
386 | - $hash = md5( $nhash ); |
|
387 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 ); |
|
386 | + $hash = md5($nhash); |
|
387 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with a COMPOSITE hash <'.$hash.'>', 3); |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | // STEP 2: compare job to existing jobs to prevent double submission for same type+hash. |
391 | 391 | global $ao_ccss_queue; |
392 | 392 | |
393 | - foreach ( $ao_ccss_queue as $queue_item ) { |
|
394 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 ); |
|
395 | - 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 ); |
|
393 | + foreach ($ao_ccss_queue as $queue_item) { |
|
394 | + autoptimizeCriticalCSSCore::ao_ccss_log('Comparing <'.$rule.$hash.'> with <'.$queue_item['rtarget'].$queue_item['hash'].'>', 3); |
|
395 | + 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); |
|
397 | 397 | return false; |
398 | 398 | } |
399 | 399 | } |
@@ -403,32 +403,32 @@ discard block |
||
403 | 403 | global $ao_ccss_rules; |
404 | 404 | |
405 | 405 | // Prepare rule variables. |
406 | - $trule = explode( '|', $rule ); |
|
407 | - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
406 | + $trule = explode('|', $rule); |
|
407 | + $srule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
408 | 408 | |
409 | 409 | // Check if a MANUAL rule exist and return false. |
410 | - 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 ); |
|
410 | + 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); |
|
412 | 412 | return false; |
413 | - } elseif ( ! empty( $srule ) ) { |
|
413 | + } elseif (!empty($srule)) { |
|
414 | 414 | // Check if an AUTO rule exist. |
415 | - if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) { |
|
415 | + if ($hash === $srule['hash'] && is_file(AO_CCSS_DIR.$srule['file']) && 0 != filesize(AO_CCSS_DIR.$srule['file'])) { |
|
416 | 416 | // 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 ); |
|
417 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> MATCH the one in rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
418 | 418 | return false; |
419 | 419 | } else { |
420 | 420 | // 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 ); |
|
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); |
|
422 | 422 | return $hash; |
423 | 423 | } |
424 | 424 | } else { |
425 | 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 ); |
|
426 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> has no rule yet', 3); |
|
427 | 427 | return $hash; |
428 | 428 | } |
429 | 429 | } |
430 | 430 | |
431 | - public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
|
431 | + public function ao_ccss_api_generate($path, $debug, $dcode) { |
|
432 | 432 | // POST jobs to criticalcss.com and return responses |
433 | 433 | // Get key and key status. |
434 | 434 | global $ao_ccss_key; |
@@ -440,35 +440,35 @@ discard block |
||
440 | 440 | global $ao_ccss_noptimize; |
441 | 441 | |
442 | 442 | $site_host = get_site_url(); |
443 | - $site_path = parse_url( $site_host, PHP_URL_PATH ); |
|
443 | + $site_path = parse_url($site_host, PHP_URL_PATH); |
|
444 | 444 | |
445 | - if ( ! empty( $site_path ) ) { |
|
446 | - $site_host = str_replace( $site_path, '', $site_host ); |
|
445 | + if (!empty($site_path)) { |
|
446 | + $site_host = str_replace($site_path, '', $site_host); |
|
447 | 447 | } |
448 | 448 | |
449 | 449 | // Logic to bind to one domain to avoid site clones of sites would |
450 | 450 | // automatically begin spawning requests to criticalcss.com which has |
451 | 451 | // a per domain cost. |
452 | 452 | global $ao_ccss_domain; |
453 | - if ( empty( $ao_ccss_domain ) ) { |
|
453 | + if (empty($ao_ccss_domain)) { |
|
454 | 454 | // first request being done, update option to allow future requests are only allowed if from same domain. |
455 | - 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 ) ) { |
|
455 | + 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)) { |
|
457 | 457 | // 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 ); |
|
458 | + autoptimizeCriticalCSSCore::ao_ccss_log('Request for domain '.$site_host.' does not match bound domain '.$ao_ccss_domain.' so not proceeding.', 2); |
|
459 | 459 | return false; |
460 | 460 | } |
461 | 461 | |
462 | - $src_url = $site_host . $path; |
|
462 | + $src_url = $site_host.$path; |
|
463 | 463 | |
464 | 464 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
465 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
465 | + if (!empty($ao_ccss_noptimize)) { |
|
466 | 466 | $src_url .= '?ao_noptirocket=1'; |
467 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
467 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
468 | 468 | $src_url .= '?ao_nolazy=1'; |
469 | 469 | } |
470 | 470 | |
471 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
471 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
472 | 472 | |
473 | 473 | // Initialize request body. |
474 | 474 | $body = array(); |
@@ -478,88 +478,88 @@ discard block |
||
478 | 478 | |
479 | 479 | // Prepare and add viewport size to the body if available. |
480 | 480 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
481 | - if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) { |
|
481 | + if (!empty($viewport['w']) && !empty($viewport['h'])) { |
|
482 | 482 | $body['width'] = $viewport['w']; |
483 | 483 | $body['height'] = $viewport['h']; |
484 | 484 | } |
485 | 485 | |
486 | 486 | // Prepare and add forceInclude to the body if available. |
487 | 487 | global $ao_ccss_finclude; |
488 | - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude ); |
|
489 | - if ( ! empty( $finclude ) ) { |
|
488 | + $finclude = $this->ao_ccss_finclude($ao_ccss_finclude); |
|
489 | + if (!empty($finclude)) { |
|
490 | 490 | $body['forceInclude'] = $finclude; |
491 | 491 | } |
492 | 492 | |
493 | 493 | // Add filter to allow the body array to be altered (e.g. to add customPageHeaders). |
494 | - $body = apply_filters( 'autoptimize_ccss_cron_api_generate_body', $body ); |
|
494 | + $body = apply_filters('autoptimize_ccss_cron_api_generate_body', $body); |
|
495 | 495 | |
496 | 496 | // Body must be json and log it. |
497 | - $body = json_encode( $body ); |
|
498 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 ); |
|
497 | + $body = json_encode($body); |
|
498 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request body is '.$body, 3); |
|
499 | 499 | |
500 | 500 | // Prepare the request. |
501 | - $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER ); |
|
501 | + $url = esc_url_raw(AO_CCSS_API.'generate?aover='.AO_CCSS_VER); |
|
502 | 502 | $args = array( |
503 | 503 | 'headers' => array( |
504 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
504 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
505 | 505 | 'Content-type' => 'application/json; charset=utf-8', |
506 | - 'Authorization' => 'JWT ' . $key, |
|
506 | + 'Authorization' => 'JWT '.$key, |
|
507 | 507 | 'Connection' => 'close', |
508 | 508 | ), |
509 | 509 | 'body' => $body, |
510 | 510 | ); |
511 | 511 | |
512 | 512 | // Dispatch the request and store its response code. |
513 | - $req = wp_safe_remote_post( $url, $args ); |
|
514 | - $code = wp_remote_retrieve_response_code( $req ); |
|
515 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
513 | + $req = wp_safe_remote_post($url, $args); |
|
514 | + $code = wp_remote_retrieve_response_code($req); |
|
515 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
516 | 516 | |
517 | - if ( $debug && $dcode ) { |
|
517 | + if ($debug && $dcode) { |
|
518 | 518 | // If queue debug is active, change response code. |
519 | 519 | $code = $dcode; |
520 | 520 | } |
521 | 521 | |
522 | - if ( 200 == $code ) { |
|
522 | + if (200 == $code) { |
|
523 | 523 | // Response code is OK. |
524 | 524 | // Workaround criticalcss.com non-RESTful reponses. |
525 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) { |
|
525 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status']) { |
|
526 | 526 | // Log successful and return encoded request body. |
527 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 ); |
|
527 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied successfully', 3); |
|
528 | 528 | |
529 | 529 | // This code also means the key is valid, so cache key status for 24h if not already cached. |
530 | - if ( ( ! $key_status || 2 != $key_status ) && $key ) { |
|
531 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
532 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
530 | + if ((!$key_status || 2 != $key_status) && $key) { |
|
531 | + update_option('autoptimize_ccss_keyst', 2); |
|
532 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
533 | 533 | } |
534 | 534 | |
535 | 535 | // Return the request body. |
536 | 536 | return $body; |
537 | 537 | } else { |
538 | 538 | // 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 ); |
|
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); |
|
541 | 541 | return $body; |
542 | 542 | } |
543 | 543 | } else { |
544 | 544 | // Response code is anything else. |
545 | 545 | // Log failed request with a valid response code and return body. |
546 | - 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 ); |
|
546 | + 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); |
|
549 | 549 | |
550 | - if ( 401 == $code ) { |
|
550 | + if (401 == $code) { |
|
551 | 551 | // If request is unauthorized, also clear key status. |
552 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
553 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
552 | + update_option('autoptimize_ccss_keyst', 1); |
|
553 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
554 | 554 | } |
555 | 555 | |
556 | 556 | // Return the request body. |
557 | 557 | return $body; |
558 | 558 | } else { |
559 | 559 | // 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 ); |
|
561 | - if ( is_wp_error( $req ) ) { |
|
562 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
560 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> has no response, this could be a service timeout', 2); |
|
561 | + if (is_wp_error($req)) { |
|
562 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
563 | 563 | } |
564 | 564 | |
565 | 565 | return false; |
@@ -567,76 +567,76 @@ discard block |
||
567 | 567 | } |
568 | 568 | } |
569 | 569 | |
570 | - public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
|
570 | + public function ao_ccss_api_results($jobid, $debug, $dcode) { |
|
571 | 571 | // GET jobs from criticalcss.com and return responses |
572 | 572 | // Get key. |
573 | 573 | global $ao_ccss_key; |
574 | 574 | $key = $ao_ccss_key; |
575 | 575 | |
576 | 576 | // Prepare the request. |
577 | - $url = AO_CCSS_API . 'results?resultId=' . $jobid; |
|
577 | + $url = AO_CCSS_API.'results?resultId='.$jobid; |
|
578 | 578 | $args = array( |
579 | 579 | 'headers' => array( |
580 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
581 | - 'Authorization' => 'JWT ' . $key, |
|
580 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
581 | + 'Authorization' => 'JWT '.$key, |
|
582 | 582 | 'Connection' => 'close', |
583 | 583 | ), |
584 | 584 | ); |
585 | 585 | |
586 | 586 | // Dispatch the request and store its response code. |
587 | - $req = wp_safe_remote_get( $url, $args ); |
|
588 | - $code = wp_remote_retrieve_response_code( $req ); |
|
589 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
587 | + $req = wp_safe_remote_get($url, $args); |
|
588 | + $code = wp_remote_retrieve_response_code($req); |
|
589 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
590 | 590 | |
591 | - if ( $debug && $dcode ) { |
|
591 | + if ($debug && $dcode) { |
|
592 | 592 | // If queue debug is active, change response code. |
593 | 593 | $code = $dcode; |
594 | 594 | } |
595 | 595 | |
596 | - if ( 200 == $code ) { |
|
596 | + if (200 == $code) { |
|
597 | 597 | // Response code is OK. |
598 | - 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'] ) ) { |
|
598 | + 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 | 599 | // Workaround criticalcss.com non-RESTful reponses |
600 | 600 | // 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 ); |
|
601 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully', 3); |
|
602 | 602 | return $body; |
603 | - } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) { |
|
603 | + } elseif (is_array($body) && (array_key_exists('error', $body) && 'This css no longer exists. Please re-generate it.' == $body['error'])) { |
|
604 | 604 | // Handle no CSS reply |
605 | 605 | // 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 ); |
|
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); |
|
607 | 607 | return $body; |
608 | 608 | } else { |
609 | 609 | // 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 ); |
|
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); |
|
612 | 612 | return false; |
613 | 613 | } |
614 | 614 | } else { |
615 | 615 | // Response code is anything else |
616 | 616 | // Log failed request with a valid response code and return body. |
617 | - 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 ); |
|
620 | - if ( 401 == $code ) { |
|
617 | + 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); |
|
620 | + if (401 == $code) { |
|
621 | 621 | // If request is unauthorized, also clear key status. |
622 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
623 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
622 | + update_option('autoptimize_ccss_keyst', 1); |
|
623 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | // Return the request body. |
627 | 627 | return $body; |
628 | 628 | } else { |
629 | 629 | // 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 ); |
|
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); |
|
631 | 631 | return false; |
632 | 632 | } |
633 | 633 | } |
634 | 634 | } |
635 | 635 | |
636 | - public function ao_ccss_save_file( $ccss, $target, $review ) { |
|
636 | + public function ao_ccss_save_file($ccss, $target, $review) { |
|
637 | 637 | // Save critical CSS into the filesystem and return its filename |
638 | 638 | // Prepare review mark. |
639 | - if ( $review ) { |
|
639 | + if ($review) { |
|
640 | 640 | $rmark = '_R'; |
641 | 641 | } else { |
642 | 642 | $rmark = ''; |
@@ -646,21 +646,21 @@ discard block |
||
646 | 646 | $filename = false; |
647 | 647 | $content = $ccss; |
648 | 648 | |
649 | - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) { |
|
649 | + if (autoptimizeCriticalCSSCore::ao_ccss_check_contents($content)) { |
|
650 | 650 | // Sanitize content, set filename and try to save file. |
651 | - $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css'; |
|
652 | - $status = file_put_contents( $file, $content, LOCK_EX ); |
|
653 | - $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 ); |
|
651 | + $file = AO_CCSS_DIR.'ccss_'.md5($ccss.$target[1]).$rmark.'.css'; |
|
652 | + $status = file_put_contents($file, $content, LOCK_EX); |
|
653 | + $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); |
|
655 | 655 | |
656 | - if ( ! $status ) { |
|
656 | + if (!$status) { |
|
657 | 657 | // If file has not been saved, reset filename. |
658 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 ); |
|
658 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file <'.$filename.'> could not be not saved', 2); |
|
659 | 659 | $filename = false; |
660 | 660 | return $filename; |
661 | 661 | } |
662 | 662 | } else { |
663 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 ); |
|
663 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not pass content check', 2); |
|
664 | 664 | return $filename; |
665 | 665 | } |
666 | 666 | |
@@ -670,15 +670,15 @@ discard block |
||
670 | 670 | global $ao_ccss_rules; |
671 | 671 | |
672 | 672 | // Prepare rule variables. |
673 | - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ]; |
|
673 | + $srule = $ao_ccss_rules[$target[0]][$target[1]]; |
|
674 | 674 | $oldfile = $srule['file']; |
675 | 675 | |
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 ); |
|
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); |
|
682 | 682 | } |
683 | 683 | } |
684 | 684 | } |
@@ -687,29 +687,29 @@ discard block |
||
687 | 687 | return $filename; |
688 | 688 | } |
689 | 689 | |
690 | - public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) { |
|
690 | + public function ao_ccss_rule_update($ljid, $srule, $file, $hash) { |
|
691 | 691 | // Update or create a rule |
692 | 692 | // Attach required arrays. |
693 | 693 | global $ao_ccss_rules; |
694 | 694 | |
695 | 695 | // Prepare rule variables. |
696 | - $trule = explode( '|', $srule ); |
|
697 | - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
696 | + $trule = explode('|', $srule); |
|
697 | + $rule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
698 | 698 | $action = false; |
699 | 699 | $rtype = ''; |
700 | 700 | |
701 | - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) { |
|
701 | + if (0 === $rule['hash'] && 0 !== $rule['file']) { |
|
702 | 702 | // manual rule, don't ever overwrite. |
703 | 703 | $action = 'NOT UPDATED'; |
704 | 704 | $rtype = 'MANUAL'; |
705 | - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) { |
|
705 | + } elseif (0 === $rule['hash'] && 0 === $rule['file']) { |
|
706 | 706 | // If this is an user created AUTO rule with no hash and file yet, update its hash and filename |
707 | 707 | // Set rule hash, file and action flag. |
708 | 708 | $rule['hash'] = $hash; |
709 | 709 | $rule['file'] = $file; |
710 | 710 | $action = 'UPDATED'; |
711 | 711 | $rtype = 'AUTO'; |
712 | - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) { |
|
712 | + } elseif (0 !== $rule['hash'] && ctype_alnum($rule['hash'])) { |
|
713 | 713 | // If this is an genuine AUTO rule, update its hash and filename |
714 | 714 | // Set rule hash, file and action flag. |
715 | 715 | $rule['hash'] = $hash; |
@@ -719,7 +719,7 @@ discard block |
||
719 | 719 | } else { |
720 | 720 | // If rule doesn't exist, create an AUTO rule |
721 | 721 | // AUTO rules were only for types, but will now also work for paths. |
722 | - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) { |
|
722 | + if ('types' == $trule[0] || 'paths' == $trule[0]) { |
|
723 | 723 | // Set rule hash and file and action flag. |
724 | 724 | $rule['hash'] = $hash; |
725 | 725 | $rule['file'] = $file; |
@@ -727,47 +727,47 @@ discard block |
||
727 | 727 | $rtype = 'AUTO'; |
728 | 728 | } else { |
729 | 729 | // Log that no rule was created. |
730 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 ); |
|
730 | + autoptimizeCriticalCSSCore::ao_ccss_log('Exception, no AUTO rule created', 3); |
|
731 | 731 | } |
732 | 732 | } |
733 | 733 | |
734 | - if ( $action ) { |
|
734 | + if ($action) { |
|
735 | 735 | // 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 ); |
|
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); |
|
740 | 740 | } else { |
741 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 ); |
|
741 | + autoptimizeCriticalCSSCore::ao_ccss_log('No rule action required', 3); |
|
742 | 742 | } |
743 | 743 | } |
744 | 744 | |
745 | - function ao_ccss_finclude( $finclude_raw ) { |
|
745 | + function ao_ccss_finclude($finclude_raw) { |
|
746 | 746 | // Prepare forceInclude object. |
747 | - if ( ! empty( $finclude_raw ) ) { |
|
747 | + if (!empty($finclude_raw)) { |
|
748 | 748 | // If there are any content |
749 | 749 | // Convert raw string into arra and initialize the returning object. |
750 | - $fincludes = explode( ',', $finclude_raw ); |
|
750 | + $fincludes = explode(',', $finclude_raw); |
|
751 | 751 | $finclude = array(); |
752 | 752 | |
753 | 753 | // Interacts over every rule. |
754 | 754 | $i = 0; |
755 | - foreach ( $fincludes as $include ) { |
|
755 | + foreach ($fincludes as $include) { |
|
756 | 756 | // Trim leading and trailing whitespaces. |
757 | - $include = trim( $include ); |
|
757 | + $include = trim($include); |
|
758 | 758 | |
759 | - if ( substr( $include, 0, 2 ) === '//' ) { |
|
759 | + if (substr($include, 0, 2) === '//') { |
|
760 | 760 | // Regex rule |
761 | 761 | // Format value as required. |
762 | - $include = str_replace( '//', '/', $include ); |
|
763 | - $include = $include . '/i'; |
|
762 | + $include = str_replace('//', '/', $include); |
|
763 | + $include = $include.'/i'; |
|
764 | 764 | |
765 | 765 | // Store regex object. |
766 | - $finclude[ $i ]['type'] = 'RegExp'; |
|
767 | - $finclude[ $i ]['value'] = $include; |
|
766 | + $finclude[$i]['type'] = 'RegExp'; |
|
767 | + $finclude[$i]['value'] = $include; |
|
768 | 768 | } else { |
769 | 769 | // Simple value rule. |
770 | - $finclude[ $i ]['value'] = $include; |
|
770 | + $finclude[$i]['value'] = $include; |
|
771 | 771 | } |
772 | 772 | |
773 | 773 | $i++; |
@@ -784,54 +784,54 @@ discard block |
||
784 | 784 | public function ao_ccss_cleaning() { |
785 | 785 | // Perform plugin maintenance |
786 | 786 | // Truncate log file >= 1MB . |
787 | - if ( file_exists( AO_CCSS_LOG ) ) { |
|
788 | - if ( filesize( AO_CCSS_LOG ) >= 1048576 ) { |
|
789 | - $logfile = fopen( AO_CCSS_LOG, 'w' ); |
|
790 | - fclose( $logfile ); |
|
787 | + if (file_exists(AO_CCSS_LOG)) { |
|
788 | + if (filesize(AO_CCSS_LOG) >= 1048576) { |
|
789 | + $logfile = fopen(AO_CCSS_LOG, 'w'); |
|
790 | + fclose($logfile); |
|
791 | 791 | } |
792 | 792 | } |
793 | 793 | |
794 | 794 | // Remove lock file. |
795 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
796 | - unlink( AO_CCSS_LOCK ); |
|
795 | + if (file_exists(AO_CCSS_LOCK)) { |
|
796 | + unlink(AO_CCSS_LOCK); |
|
797 | 797 | } |
798 | 798 | |
799 | 799 | // Make sure queue processing is scheduled, recreate if not. |
800 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
801 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
800 | + if (!wp_next_scheduled('ao_ccss_queue')) { |
|
801 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
802 | 802 | } |
803 | 803 | |
804 | 804 | // Queue cleaning. |
805 | 805 | global $ao_ccss_queue; |
806 | 806 | $queue_purge_threshold = 100; |
807 | - $queue_purge_age = 24 * 60 * 60; |
|
808 | - $queue_length = count( $ao_ccss_queue ); |
|
809 | - $timestamp_yesterday = microtime( true ) - $queue_purge_age; |
|
807 | + $queue_purge_age = 24*60*60; |
|
808 | + $queue_length = count($ao_ccss_queue); |
|
809 | + $timestamp_yesterday = microtime(true) - $queue_purge_age; |
|
810 | 810 | $remove_old_new = false; |
811 | 811 | $queue_altered = false; |
812 | 812 | |
813 | - if ( $queue_length > $queue_purge_threshold ) { |
|
813 | + if ($queue_length > $queue_purge_threshold) { |
|
814 | 814 | $remove_old_new = true; |
815 | 815 | } |
816 | 816 | |
817 | - foreach ( $ao_ccss_queue as $path => $job ) { |
|
818 | - 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 ] ); |
|
817 | + foreach ($ao_ccss_queue as $path => $job) { |
|
818 | + 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]); |
|
820 | 820 | $queue_altered = true; |
821 | 821 | } |
822 | 822 | } |
823 | 823 | |
824 | 824 | // save queue to options! |
825 | - 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 ); |
|
825 | + 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); |
|
829 | 829 | } |
830 | 830 | |
831 | 831 | // re-check key if invalid. |
832 | 832 | global $ao_ccss_keyst; |
833 | - if ( 1 == $ao_ccss_keyst ) { |
|
834 | - $this->ao_ccss_api_generate( '', '', '' ); |
|
833 | + if (1 == $ao_ccss_keyst) { |
|
834 | + $this->ao_ccss_api_generate('', '', ''); |
|
835 | 835 | } |
836 | 836 | } |
837 | 837 | } |
@@ -17,66 +17,66 @@ discard block |
||
17 | 17 | */ |
18 | 18 | |
19 | 19 | |
20 | -if ( ! defined( 'ABSPATH' ) ) { |
|
20 | +if (!defined('ABSPATH')) { |
|
21 | 21 | exit; |
22 | 22 | } |
23 | 23 | |
24 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.7.6' ); |
|
24 | +define('AUTOPTIMIZE_PLUGIN_VERSION', '2.7.6'); |
|
25 | 25 | |
26 | 26 | // plugin_dir_path() returns the trailing slash! |
27 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|
28 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); |
|
27 | +define('AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
|
28 | +define('AUTOPTIMIZE_PLUGIN_FILE', __FILE__); |
|
29 | 29 | |
30 | 30 | // Bail early if attempting to run on non-supported php versions. |
31 | -if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { |
|
31 | +if (version_compare(PHP_VERSION, '5.6', '<')) { |
|
32 | 32 | function autoptimize_incompatible_admin_notice() { |
33 | - echo '<div class="error"><p>' . __( 'Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize' ) . '</p></div>'; |
|
34 | - if ( isset( $_GET['activate'] ) ) { |
|
35 | - unset( $_GET['activate'] ); |
|
33 | + echo '<div class="error"><p>'.__('Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize').'</p></div>'; |
|
34 | + if (isset($_GET['activate'])) { |
|
35 | + unset($_GET['activate']); |
|
36 | 36 | } |
37 | 37 | } |
38 | 38 | function autoptimize_deactivate_self() { |
39 | - deactivate_plugins( plugin_basename( AUTOPTIMIZE_PLUGIN_FILE ) ); |
|
39 | + deactivate_plugins(plugin_basename(AUTOPTIMIZE_PLUGIN_FILE)); |
|
40 | 40 | } |
41 | - add_action( 'admin_notices', 'autoptimize_incompatible_admin_notice' ); |
|
42 | - add_action( 'admin_init', 'autoptimize_deactivate_self' ); |
|
41 | + add_action('admin_notices', 'autoptimize_incompatible_admin_notice'); |
|
42 | + add_action('admin_init', 'autoptimize_deactivate_self'); |
|
43 | 43 | return; |
44 | 44 | } |
45 | 45 | |
46 | -function autoptimize_autoload( $class_name ) { |
|
47 | - if ( in_array( $class_name, array( 'Minify_HTML', 'JSMin' ) ) ) { |
|
48 | - $file = strtolower( $class_name ); |
|
49 | - $file = str_replace( '_', '-', $file ); |
|
50 | - $path = dirname( __FILE__ ) . '/classes/external/php/'; |
|
51 | - $filepath = $path . $file . '.php'; |
|
52 | - } elseif ( false !== strpos( $class_name, 'Autoptimize\\tubalmartin\\CssMin' ) ) { |
|
53 | - $file = str_replace( 'Autoptimize\\tubalmartin\\CssMin\\', '', $class_name ); |
|
54 | - $path = dirname( __FILE__ ) . '/classes/external/php/yui-php-cssmin-bundled/'; |
|
55 | - $filepath = $path . $file . '.php'; |
|
56 | - } elseif ( 'autoptimize' === substr( $class_name, 0, 11 ) ) { |
|
46 | +function autoptimize_autoload($class_name) { |
|
47 | + if (in_array($class_name, array('Minify_HTML', 'JSMin'))) { |
|
48 | + $file = strtolower($class_name); |
|
49 | + $file = str_replace('_', '-', $file); |
|
50 | + $path = dirname(__FILE__).'/classes/external/php/'; |
|
51 | + $filepath = $path.$file.'.php'; |
|
52 | + } elseif (false !== strpos($class_name, 'Autoptimize\\tubalmartin\\CssMin')) { |
|
53 | + $file = str_replace('Autoptimize\\tubalmartin\\CssMin\\', '', $class_name); |
|
54 | + $path = dirname(__FILE__).'/classes/external/php/yui-php-cssmin-bundled/'; |
|
55 | + $filepath = $path.$file.'.php'; |
|
56 | + } elseif ('autoptimize' === substr($class_name, 0, 11)) { |
|
57 | 57 | // One of our "old" classes. |
58 | 58 | $file = $class_name; |
59 | - $path = dirname( __FILE__ ) . '/classes/'; |
|
60 | - $filepath = $path . $file . '.php'; |
|
61 | - } elseif ( 'PAnD' === $class_name ) { |
|
59 | + $path = dirname(__FILE__).'/classes/'; |
|
60 | + $filepath = $path.$file.'.php'; |
|
61 | + } elseif ('PAnD' === $class_name) { |
|
62 | 62 | $file = 'persist-admin-notices-dismissal'; |
63 | - $path = dirname( __FILE__ ) . '/classes/external/php/persist-admin-notices-dismissal/'; |
|
64 | - $filepath = $path . $file . '.php'; |
|
63 | + $path = dirname(__FILE__).'/classes/external/php/persist-admin-notices-dismissal/'; |
|
64 | + $filepath = $path.$file.'.php'; |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // If we didn't match one of our rules, bail! |
68 | - if ( ! isset( $filepath ) ) { |
|
68 | + if (!isset($filepath)) { |
|
69 | 69 | return; |
70 | 70 | } |
71 | 71 | |
72 | 72 | require $filepath; |
73 | 73 | } |
74 | 74 | |
75 | -spl_autoload_register( 'autoptimize_autoload' ); |
|
75 | +spl_autoload_register('autoptimize_autoload'); |
|
76 | 76 | |
77 | 77 | // Load WP CLI command(s) on demand. |
78 | -if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
79 | - require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
|
78 | +if (defined('WP_CLI') && WP_CLI) { |
|
79 | + require AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCLI.php'; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | function autoptimize() { |
88 | 88 | static $plugin = null; |
89 | 89 | |
90 | - if ( null === $plugin ) { |
|
91 | - $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); |
|
90 | + if (null === $plugin) { |
|
91 | + $plugin = new autoptimizeMain(AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | return $plugin; |