1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Critical CSS base file (initializes all ccss files). |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
class autoptimizeCriticalCSSBase { |
11
|
|
|
/** |
12
|
|
|
* Main plugin filepath. |
13
|
|
|
* Used for activation/deactivation/uninstall hooks. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $filepath = null; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
// define constant, but only once. |
22
|
|
|
if ( ! defined( 'AO_CCSS_DIR' ) ) { |
23
|
|
|
// Define a constant with the directory to store critical CSS in. |
24
|
|
|
if ( is_multisite() ) { |
25
|
|
|
$blog_id = get_current_blog_id(); |
26
|
|
|
define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
27
|
|
|
} else { |
28
|
|
|
define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' ); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
if ( ! defined( 'AO_CCSS_VER' ) ) { |
32
|
|
|
// Define plugin version. |
33
|
|
|
define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); |
34
|
|
|
|
35
|
|
|
// Define constants for criticalcss.com base path and API endpoints. |
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 ); |
40
|
|
|
} |
41
|
|
|
|
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' ); |
45
|
|
|
} |
46
|
|
|
if ( ! defined( 'AO_CCSS_LOG' ) ) { |
47
|
|
|
define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' ); |
48
|
|
|
} |
49
|
|
|
if ( ! defined( 'AO_CCSS_DEBUG' ) ) { |
50
|
|
|
define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->filepath = __FILE__; |
54
|
|
|
|
55
|
|
|
$this->setup(); |
56
|
|
|
$this->load_requires(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setup() |
60
|
|
|
{ |
61
|
|
|
// get all options. |
62
|
|
|
$all_options = $this->fetch_options(); |
|
|
|
|
63
|
|
|
foreach ( $all_options as $option => $value ) { |
|
|
|
|
64
|
|
|
${$option} = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// make sure the 10 minutes cron schedule is added. |
68
|
|
|
add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); |
69
|
|
|
|
70
|
|
|
// check if we need to upgrade. |
71
|
|
|
$this->check_upgrade(); |
72
|
|
|
|
73
|
|
|
// make sure ao_ccss_queue is scheduled OK if an API key is set. |
74
|
|
View Code Duplication |
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
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function load_requires() { |
80
|
|
|
// Required libs, core is always needed. |
81
|
|
|
$criticalcss_core = new autoptimizeCriticalCSSCore(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
if ( defined( 'DOING_CRON' ) || is_admin() ) { |
84
|
|
|
// TODO: also include if overridden somehow to force queue processing to be executed? |
85
|
|
|
$criticalcss_cron = new autoptimizeCriticalCSSCron(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ( is_admin() ) { |
89
|
|
|
$criticalcss_settings = new autoptimizeCriticalCSSSettings(); |
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
// enqueuing only done when not wp-admin. |
92
|
|
|
$criticalcss_enqueue = new autoptimizeCriticalCSSEnqueue(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public static function fetch_options() { |
97
|
|
|
static $autoptimize_ccss_options = null; |
98
|
|
|
|
99
|
|
|
if ( null === $autoptimize_ccss_options ) { |
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 ) { |
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'] ) ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Setup the rules array. |
128
|
|
|
if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) { |
129
|
|
|
$autoptimize_ccss_options['ao_ccss_rules']['paths'] = array(); |
130
|
|
|
$autoptimize_ccss_options['ao_ccss_rules']['types'] = array(); |
131
|
|
|
} else { |
132
|
|
|
$autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Setup the queue array. |
136
|
|
|
if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) { |
137
|
|
|
$autoptimize_ccss_options['ao_ccss_queue'] = array(); |
138
|
|
|
} else { |
139
|
|
|
$autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Override API key if constant is defined. |
143
|
|
|
if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) { |
144
|
|
|
$autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $autoptimize_ccss_options; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function on_upgrade() { |
152
|
|
|
global $ao_ccss_key; |
153
|
|
|
|
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 ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Create a scheduled event for the queue. |
160
|
|
View Code Duplication |
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
|
|
|
} |
163
|
|
|
|
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' ); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function check_upgrade() { |
171
|
|
|
$db_version = get_option( 'autoptimize_ccss_version', '' ); |
172
|
|
|
if ( AO_CCSS_VER !== $db_version ) { |
173
|
|
|
// check schedules & re-schedule if needed. |
174
|
|
|
$this->on_upgrade(); |
175
|
|
|
// and update db_version. |
176
|
|
|
update_option( 'autoptimize_ccss_version', AO_CCSS_VER ); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function ao_ccss_interval( $schedules ) { |
181
|
|
|
// Let interval be configurable. |
182
|
|
|
if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) { |
183
|
|
|
$intsec = 600; |
184
|
|
|
} else { |
185
|
|
|
$intsec = AO_CCSS_DEBUG_INTERVAL; |
186
|
|
|
if ( $intsec >= 120 ) { |
187
|
|
|
$inttxt = $intsec / 60 . ' minutes'; |
188
|
|
|
} else { |
189
|
|
|
$inttxt = $intsec . ' second(s)'; |
190
|
|
|
} |
191
|
|
|
autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Attach interval to schedule. |
195
|
|
|
$schedules['ao_ccss'] = array( |
196
|
|
|
'interval' => $intsec, |
197
|
|
|
'display' => __( 'Autoptimize CriticalCSS' ), |
198
|
|
|
); |
199
|
|
|
return $schedules; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.