| @@ -15,15 +15,47 @@ | ||
| 15 | 15 | * @var string |
| 16 | 16 | */ |
| 17 | 17 | protected $filepath = null; |
| 18 | 18 | |
| 19 | + /** | |
| 20 | + * Critical CSS options | |
| 21 | + * | |
| 22 | + * @var array | |
| 23 | + */ | |
| 24 | + protected $_options = null; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Core object | |
| 28 | + * | |
| 29 | + * @var object | |
| 30 | + */ | |
| 31 | + protected $_core = null; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Cron object | |
| 35 | + * | |
| 36 | + * @var object | |
| 37 | + */ | |
| 38 | + protected $_cron = null; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Settings object | |
| 42 | + * | |
| 43 | + * @var object | |
| 44 | + */ | |
| 45 | + protected $_settings = null; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Enqueue object | |
| 49 | + * | |
| 50 | + * @var object | |
| 51 | + */ | |
| 52 | + protected $_enqueue = null; | |
| 53 | + | |
| 19 | 54 | public function __construct() |
| 20 | 55 | { |
| 21 | 56 | // define constant, but only once. |
| 22 | 57 | if ( ! defined( 'AO_CCSS_DIR' ) ) { |
| 23 | - // Define plugin version. | |
| 24 | - define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); | |
| 25 | - | |
| 26 | 58 | // Define a constant with the directory to store critical CSS in. |
| 27 | 59 | if ( is_multisite() ) { |
| 28 | 60 | $blog_id = get_current_blog_id(); |
| 29 | 61 | define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
| @@ -29,126 +61,269 @@ | ||
| 29 | 61 | define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
| 30 | 62 | } else { |
| 31 | 63 | define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' ); |
| 32 | 64 | } |
| 65 | + } | |
| 66 | + if ( ! defined( 'AO_CCSS_VER' ) ) { | |
| 67 | + // Define plugin version. | |
| 68 | + define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); | |
| 33 | 69 | |
| 34 | - // Define support files locations. | |
| 70 | + // Define constants for criticalcss.com base path and API endpoints. | |
| 71 | + // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option. | |
| 72 | + define( 'AO_CCSS_URL', 'https://criticalcss.com' ); | |
| 73 | + define( 'AO_CCSS_API', apply_filters( 'autoptimize_filter_ccss_service_url', AO_CCSS_URL . '/api/premium/' ) ); | |
| 74 | + define( 'AO_CCSS_SLEEP', 10 ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + // Define support files locations, in case they are not already defined. | |
| 78 | + if ( ! defined( 'AO_CCSS_LOCK' ) ) { | |
| 35 | 79 | define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' ); |
| 80 | + } | |
| 81 | + if ( ! defined( 'AO_CCSS_LOG' ) ) { | |
| 36 | 82 | define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' ); |
| 83 | + } | |
| 84 | + if ( ! defined( 'AO_CCSS_DEBUG' ) ) { | |
| 37 | 85 | define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' ); |
| 38 | - | |
| 39 | - // Define constants for criticalcss.com base path and API endpoints. | |
| 40 | - // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option. | |
| 41 | - define( 'AO_CCSS_URL', 'https://criticalcss.com' ); | |
| 42 | - define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' ); | |
| 43 | 86 | } |
| 44 | 87 | |
| 45 | 88 | $this->filepath = __FILE__; |
| 89 | + | |
| 90 | + // Add keychecker action for scheduled use. | |
| 91 | + add_action( 'ao_ccss_keychecker', array( $this, 'ao_ccss_check_key' ) ); | |
| 92 | + } | |
| 46 | 93 | |
| 47 | - $this->setup(); | |
| 48 | - $this->load_requires(); | |
| 49 | - } | |
| 94 | + public function setup() { | |
| 95 | + // check if we need to upgrade. | |
| 96 | + $this->check_upgrade(); | |
| 50 | 97 | |
| 51 | - public function setup() | |
| 52 | - { | |
| 53 | - // get all options. | |
| 54 | - $all_options = $this->fetch_options(); | |
| 55 | - foreach ( $all_options as $option => $value ) { | |
| 56 | - ${$option} = $value; | |
| 57 | - } | |
| 98 | + // add/ remove scheduled jobs. | |
| 99 | + if ( $this->is_api_active() ) { | |
| 100 | + // make sure the 10 minutes cron schedule is added. | |
| 101 | + add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); | |
| 58 | 102 | |
| 59 | - // make sure the 10 minutes cron schedule is added. | |
| 60 | - add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); | |
| 103 | + if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { | |
| 104 | + // make sure ao_ccss_queue is scheduled OK if an API key is active. | |
| 105 | + wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); | |
| 106 | + } | |
| 61 | 107 | |
| 62 | - // check if we need to upgrade. | |
| 63 | - $this->check_upgrade(); | |
| 108 | + if ( ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { | |
| 109 | + // and schedule maintenance job. | |
| 110 | + wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + if ( wp_next_scheduled( 'ao_ccss_keychecker' ) ) { | |
| 114 | + // api is active now, no need to check key as it is checked by using the API. | |
| 115 | + wp_clear_scheduled_hook( 'ao_ccss_keychecker' ); | |
| 116 | + } | |
| 117 | + } else { | |
| 118 | + if ( wp_next_scheduled( 'ao_ccss_queue' ) ) { | |
| 119 | + wp_clear_scheduled_hook( 'ao_ccss_queue' ); | |
| 120 | + } | |
| 64 | 121 | |
| 65 | - // make sure ao_ccss_queue is scheduled OK if an API key is set. | |
| 66 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { | |
| 67 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); | |
| 122 | + if ( wp_next_scheduled( 'ao_ccss_maintenance' ) ) { | |
| 123 | + wp_clear_scheduled_hook( 'ao_ccss_maintenance' ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + // add keychecker logic if api is not active but we have a key so maybe this is a temporary issue, check if key is OK daily. | |
| 127 | + $ao_ccss_key = $this->get_option( 'key' ); | |
| 128 | + if ( ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_keychecker' ) ) { | |
| 129 | + wp_schedule_event( time(), 'twicedaily', 'ao_ccss_keychecker' ); | |
| 130 | + } else if ( empty( $ao_ccss_key ) && wp_next_scheduled( 'ao_ccss_keychecker' ) ) { | |
| 131 | + // edge case: we had a inactive key that was checked daily, but it is now removed, so remove keychecker from schedule. | |
| 132 | + wp_clear_scheduled_hook( 'ao_ccss_keychecker' ); | |
| 133 | + } | |
| 68 | 134 | } |
| 135 | + | |
| 136 | + // check/ create AO_CCSS_DIR. | |
| 137 | + if ( ! file_exists( AO_CCSS_DIR ) ) { | |
| 138 | + $this->create_ao_ccss_dir(); | |
| 139 | + } | |
| 69 | 140 | } |
| 70 | 141 | |
| 71 | 142 | public function load_requires() { |
| 72 | 143 | // Required libs, core is always needed. |
| 73 | - $criticalcss_core = new autoptimizeCriticalCSSCore(); | |
| 144 | + $this->_core = new autoptimizeCriticalCSSCore(); | |
| 74 | 145 | |
| 75 | - if ( defined( 'DOING_CRON' ) || is_admin() ) { | |
| 76 | - // TODO: also include if overridden somehow to force queue processing to be executed? | |
| 77 | - $criticalcss_cron = new autoptimizeCriticalCSSCron(); | |
| 146 | + if ( ( defined( 'WP_CLI' ) || defined( 'DOING_CRON' ) || is_admin() ) && $this->is_api_active() ) { | |
| 147 | + // cron only initiated when doing cron (or wp_cli or is_amdin) and when we have an active API key. | |
| 148 | + $this->_cron = new autoptimizeCriticalCSSCron(); | |
| 78 | 149 | } |
| 79 | 150 | |
| 80 | 151 | if ( is_admin() ) { |
| 81 | - $criticalcss_settings = new autoptimizeCriticalCSSSettings(); | |
| 82 | - } else { | |
| 83 | - // enqueuing only done when not wp-admin. | |
| 84 | - $criticalcss_enqueue = new autoptimizeCriticalCSSEnqueue(); | |
| 152 | + $this->_settings = new autoptimizeCriticalCSSSettings(); | |
| 153 | + } else if ( $this->is_api_active() ) { | |
| 154 | + // enqueuing only done when not wp-admin and when API is active. | |
| 155 | + $this->_enqueue = new autoptimizeCriticalCSSEnqueue(); | |
| 85 | 156 | } |
| 86 | 157 | } |
| 87 | 158 | |
| 88 | - public static function fetch_options() { | |
| 89 | - // Get options. | |
| 90 | - $autoptimize_ccss_options['ao_css_defer'] = get_option( 'autoptimize_css_defer' ); | |
| 91 | - $autoptimize_ccss_options['ao_css_defer_inline'] = get_option( 'autoptimize_css_defer_inline' ); | |
| 92 | - $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option( 'autoptimize_ccss_rules', false ); | |
| 93 | - $autoptimize_ccss_options['ao_ccss_additional'] = get_option( 'autoptimize_ccss_additional' ); | |
| 94 | - $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option( 'autoptimize_ccss_queue', false ); | |
| 95 | - $autoptimize_ccss_options['ao_ccss_viewport'] = get_option( 'autoptimize_ccss_viewport', false ); | |
| 96 | - $autoptimize_ccss_options['ao_ccss_finclude'] = get_option( 'autoptimize_ccss_finclude', false ); | |
| 97 | - $autoptimize_ccss_options['ao_ccss_rlimit'] = get_option( 'autoptimize_ccss_rlimit', '5' ); | |
| 98 | - $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option( 'autoptimize_ccss_noptimize', false ); | |
| 99 | - $autoptimize_ccss_options['ao_ccss_debug'] = get_option( 'autoptimize_ccss_debug', false ); | |
| 100 | - $autoptimize_ccss_options['ao_ccss_key'] = get_option( 'autoptimize_ccss_key' ); | |
| 101 | - $autoptimize_ccss_options['ao_ccss_keyst'] = get_option( 'autoptimize_ccss_keyst' ); | |
| 102 | - $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option( 'autoptimize_ccss_loggedin', '1' ); | |
| 103 | - $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option( 'autoptimize_ccss_forcepath', '1' ); | |
| 104 | - $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' ); | |
| 105 | - $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option( 'autoptimize_ccss_deferjquery', false ); | |
| 106 | - $autoptimize_ccss_options['ao_ccss_domain'] = get_option( 'autoptimize_ccss_domain' ); | |
| 159 | + /** | |
| 160 | + * Log a message via CCSS Core object | |
| 161 | + * | |
| 162 | + * @param string $msg | |
| 163 | + * @param int $lvl | |
| 164 | + * | |
| 165 | + * @return void | |
| 166 | + */ | |
| 167 | + public function log( $msg, $lvl ) { | |
| 168 | + return $this->_core->ao_ccss_log( $msg, $lvl ); | |
| 169 | + } | |
| 107 | 170 | |
| 108 | - if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) { | |
| 109 | - $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ); | |
| 110 | - } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) { | |
| 171 | + /** | |
| 172 | + * Get viewport from CCSS Core object | |
| 173 | + * | |
| 174 | + * @return array | |
| 175 | + */ | |
| 176 | + public function viewport() { | |
| 177 | + return $this->_core->ao_ccss_viewport(); | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Check CCSS contents from Core object | |
| 182 | + * | |
| 183 | + * @param string $ccss | |
| 184 | + * | |
| 185 | + * @return bool | |
| 186 | + */ | |
| 187 | + public function check_contents( $ccss ) { | |
| 188 | + return $this->_core->ao_ccss_check_contents( $ccss ); | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Get key status from Core object | |
| 193 | + * | |
| 194 | + * @param bool $render | |
| 195 | + * | |
| 196 | + * @return array | |
| 197 | + */ | |
| 198 | + public function key_status( $render ) { | |
| 199 | + return $this->_core->ao_ccss_key_status( $render ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Return valid types from core object | |
| 204 | + * | |
| 205 | + * @return array | |
| 206 | + */ | |
| 207 | + public function get_types() { | |
| 208 | + return $this->_core->get_types(); | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Run enqueue in CCSS Enqueue object | |
| 213 | + */ | |
| 214 | + public function enqueue( $hash = '', $path = '', $type = 'is_page' ) { | |
| 215 | + // Enqueue is sometimes required on wp-admin requests, load it just-in-time. | |
| 216 | + if ( is_null( $this->_enqueue ) && $this->is_api_active() ) { | |
| 217 | + $this->_enqueue = new autoptimizeCriticalCSSEnqueue(); | |
| 218 | + } | |
| 219 | + | |
| 220 | + return $this->_enqueue->ao_ccss_enqueue( $hash, $path, $type ); | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Check auto-rules in CCSS Settings object | |
| 225 | + */ | |
| 226 | + public function has_autorules() { | |
| 227 | + return $this->_settings->ao_ccss_has_autorules(); | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Get a Critical CSS option | |
| 232 | + * | |
| 233 | + * @param string $name The option name | |
| 234 | + * | |
| 235 | + * @return mixed | |
| 236 | + */ | |
| 237 | + public function get_option( $name ) { | |
| 238 | + if ( is_null( $this->_options ) ) { | |
| 239 | + $this->fetch_options(); | |
| 240 | + } | |
| 241 | + | |
| 242 | + if ( isset( $this->_options[ $name ] ) ) { | |
| 243 | + return $this->_options[ $name ]; | |
| 244 | + } | |
| 245 | + | |
| 246 | + return null; | |
| 247 | + } | |
| 248 | + | |
| 249 | + public function flush_options() { | |
| 250 | + $this->_options = null; | |
| 251 | + } | |
| 252 | + | |
| 253 | + protected function fetch_options() { | |
| 254 | + if ( ! is_null( $this->_options ) ) { | |
| 255 | + return $this->_options; | |
| 256 | + } | |
| 257 | + | |
| 258 | + $this->_options = array( | |
| 259 | + 'css_defer' => autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' ), | |
| 260 | + 'css_defer_inline' => autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer_inline' ), | |
| 261 | + 'rules_raw' => get_option( 'autoptimize_ccss_rules', false ), | |
| 262 | + 'additional' => get_option( 'autoptimize_ccss_additional' ), | |
| 263 | + 'queue_raw' => get_option( 'autoptimize_ccss_queue', false ), | |
| 264 | + 'viewport' => get_option( 'autoptimize_ccss_viewport', false ), | |
| 265 | + 'finclude' => get_option( 'autoptimize_ccss_finclude', false ), | |
| 266 | + 'rtimelimit' => get_option( 'autoptimize_ccss_rtimelimit', '30' ), | |
| 267 | + 'noptimize' => get_option( 'autoptimize_ccss_noptimize', false ), | |
| 268 | + 'debug' => get_option( 'autoptimize_ccss_debug', false ), | |
| 269 | + 'key' => apply_filters( 'autoptimize_filter_ccss_key', get_option( 'autoptimize_ccss_key' ) ), | |
| 270 | + 'keyst' => get_option( 'autoptimize_ccss_keyst' ), | |
| 271 | + 'loggedin' => get_option( 'autoptimize_ccss_loggedin', '1' ), | |
| 272 | + 'forcepath' => get_option( 'autoptimize_ccss_forcepath', '1' ), | |
| 273 | + 'servicestatus' => get_option( 'autoptimize_service_availablity' ), | |
| 274 | + 'deferjquery' => get_option( 'autoptimize_ccss_deferjquery', false ), | |
| 275 | + 'domain' => get_option( 'autoptimize_ccss_domain' ), | |
| 276 | + 'unloadccss' => get_option( 'autoptimize_ccss_unloadccss', false ), | |
| 277 | + ); | |
| 278 | + | |
| 279 | + if ( strpos( $this->_options['domain'], 'http' ) === false && strpos( $this->_options['domain'], 'uggc' ) === 0 ) { | |
| 280 | + $this->_options['domain'] = str_rot13( $this->_options['domain'] ); | |
| 281 | + } elseif ( strpos( $this->_options['domain'], 'http' ) !== false ) { | |
| 111 | 282 | // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain). |
| 112 | - update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) ); | |
| 283 | + update_option( 'autoptimize_ccss_domain', str_rot13( $this->_options['domain'] ) ); | |
| 113 | 284 | } |
| 114 | 285 | |
| 115 | 286 | // Setup the rules array. |
| 116 | - if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) { | |
| 117 | - $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array(); | |
| 118 | - $autoptimize_ccss_options['ao_ccss_rules']['types'] = array(); | |
| 287 | + if ( empty( $this->_options['rules_raw'] ) ) { | |
| 288 | + $this->_options['rules'] = array( | |
| 289 | + 'paths' => array(), | |
| 290 | + 'types' => array(), | |
| 291 | + ); | |
| 119 | 292 | } else { |
| 120 | - $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true ); | |
| 293 | + $this->_options['rules'] = json_decode( $this->_options['rules_raw'], true ); | |
| 121 | 294 | } |
| 122 | 295 | |
| 123 | 296 | // Setup the queue array. |
| 124 | - if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) { | |
| 125 | - $autoptimize_ccss_options['ao_ccss_queue'] = array(); | |
| 297 | + if ( empty( $this->_options['queue_raw'] ) ) { | |
| 298 | + $this->_options['queue'] = array(); | |
| 126 | 299 | } else { |
| 127 | - $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true ); | |
| 300 | + $this->_options['queue'] = json_decode( $this->_options['queue_raw'], true ); | |
| 128 | 301 | } |
| 129 | 302 | |
| 130 | 303 | // Override API key if constant is defined. |
| 131 | 304 | if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) { |
| 132 | - $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; | |
| 305 | + $this->_options['key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; | |
| 133 | 306 | } |
| 134 | 307 | |
| 135 | - return $autoptimize_ccss_options; | |
| 308 | + return $this->_options; | |
| 136 | 309 | } |
| 137 | 310 | |
| 138 | 311 | public function on_upgrade() { |
| 312 | + $key = $this->get_option( 'key' ); | |
| 313 | + | |
| 139 | 314 | // Create the cache directory if it doesn't exist already. |
| 140 | 315 | if ( ! file_exists( AO_CCSS_DIR ) ) { |
| 141 | - mkdir( AO_CCSS_DIR, 0755, true ); | |
| 316 | + $this->create_ao_ccss_dir(); | |
| 142 | 317 | } |
| 143 | 318 | |
| 144 | 319 | // Create a scheduled event for the queue. |
| 145 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { | |
| 320 | + if ( $this->is_api_active() && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { | |
| 146 | 321 | wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
| 147 | 322 | } |
| 148 | 323 | |
| 149 | 324 | // Create a scheduled event for log maintenance. |
| 150 | - if ( ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { | |
| 325 | + if ( $this->is_api_active() && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { | |
| 151 | 326 | wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' ); |
| 152 | 327 | } |
| 153 | 328 | } |
| 154 | 329 | |
| @@ -165,8 +340,9 @@ | ||
| 165 | 340 | public function ao_ccss_interval( $schedules ) { |
| 166 | 341 | // Let interval be configurable. |
| 167 | 342 | if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) { |
| 168 | 343 | $intsec = 600; |
| 344 | + $inttxt = '10 minutes'; | |
| 169 | 345 | } else { |
| 170 | 346 | $intsec = AO_CCSS_DEBUG_INTERVAL; |
| 171 | 347 | if ( $intsec >= 120 ) { |
| 172 | 348 | $inttxt = $intsec / 60 . ' minutes'; |
| @@ -172,15 +348,82 @@ | ||
| 172 | 348 | $inttxt = $intsec / 60 . ' minutes'; |
| 173 | 349 | } else { |
| 174 | 350 | $inttxt = $intsec . ' second(s)'; |
| 175 | 351 | } |
| 176 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); | |
| 352 | + $this->log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); | |
| 177 | 353 | } |
| 178 | 354 | |
| 179 | 355 | // Attach interval to schedule. |
| 180 | 356 | $schedules['ao_ccss'] = array( |
| 181 | 357 | 'interval' => $intsec, |
| 182 | - 'display' => __( 'Autoptimize CriticalCSS' ), | |
| 358 | + 'display' => __( 'Every ' . $inttxt . ' (Autoptimize Crit. CSS)' ), | |
| 183 | 359 | ); |
| 184 | 360 | return $schedules; |
| 361 | + } | |
| 362 | + | |
| 363 | + public function create_ao_ccss_dir() { | |
| 364 | + // Make sure dir to write ao_ccss exists and is writable. | |
| 365 | + if ( ! is_dir( AO_CCSS_DIR ) ) { | |
| 366 | + // TODO: use wp_mkdir_p() | |
| 367 | + $mkdirresp = @mkdir( AO_CCSS_DIR, 0775, true ); // @codingStandardsIgnoreLine | |
| 368 | + } else { | |
| 369 | + $mkdirresp = true; | |
| 370 | + } | |
| 371 | + | |
| 372 | + // Make sure our index.html is there. | |
| 373 | + if ( ! is_file( AO_CCSS_DIR . 'index.html' ) ) { | |
| 374 | + $fileresp = file_put_contents( AO_CCSS_DIR . 'index.html', '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); | |
| 375 | + } else { | |
| 376 | + $fileresp = true; | |
| 377 | + } | |
| 378 | + | |
| 379 | + if ( true === $fileresp && true === $mkdirresp ) { | |
| 380 | + return true; | |
| 381 | + } else { | |
| 382 | + return false; | |
| 383 | + } | |
| 384 | + } | |
| 385 | + | |
| 386 | + /** | |
| 387 | + * Helper function to determine if there is an active API key. | |
| 388 | + * | |
| 389 | + * @return bool | |
| 390 | + */ | |
| 391 | + public function is_api_active() { | |
| 392 | + // using options instead of more complex $this->key_status (which gave some dependancy issues ... ;-) ). | |
| 393 | + $ao_ccss_key = $this->get_option( 'key' ); | |
| 394 | + $ao_ccss_keyst = $this->get_option( 'keyst' ); | |
| 395 | + | |
| 396 | + if ( ! empty( $ao_ccss_key ) && $ao_ccss_keyst && 2 == $ao_ccss_keyst ) { | |
| 397 | + return true; | |
| 398 | + } | |
| 399 | + | |
| 400 | + return false; | |
| 401 | + } | |
| 402 | + | |
| 403 | + /** | |
| 404 | + * Helper function to determine if a rule is MANUAL. | |
| 405 | + * | |
| 406 | + * @param array $rule | |
| 407 | + * | |
| 408 | + * @return bool | |
| 409 | + */ | |
| 410 | + public function is_rule_manual( $rule ) { | |
| 411 | + if ( is_array( $rule ) && false == $rule['hash'] && false != $rule['file'] ) { | |
| 412 | + return true; | |
| 413 | + } | |
| 414 | + | |
| 415 | + return false; | |
| 416 | + } | |
| 417 | + | |
| 418 | + /** | |
| 419 | + * Scheduled action to check an inactive key. Not part of autoptimizeCriticalCSSCron.php | |
| 420 | + * to allow us to only load the main cron logic if we have an active key to begin with. | |
| 421 | + * | |
| 422 | + */ | |
| 423 | + public function ao_ccss_check_key() { | |
| 424 | + $ao_ccss_key = $this->get_option( 'key' ); | |
| 425 | + $_result = $this->_core->ao_ccss_key_validation( $ao_ccss_key ); | |
| 426 | + $_resmsg = ( true === $_result) ? 'ok' : 'nok'; | |
| 427 | + $this->log( 'Inactive key checked, result was ' . $_resmsg, 3 ); | |
| 185 | 428 | } |
| 186 | 429 | } |