PluginProbe ʕ •ᴥ•ʔ
LiteSpeed Cache / 7.6.1
LiteSpeed Cache v7.6.1
trunk 1.0.15 1.9.1.1 2.9.9.2 3.6.4 4.6 5.7.0.1 6.5.4 7.0.0.1 7.0.1 7.1 7.2 7.3 7.3.0.1 7.4 7.5 7.5.0.1 7.6 7.6.1 7.6.2 7.7 7.8 7.8.0.1 7.8.1
litespeed-cache / src / root.cls.php
litespeed-cache / src Last commit date
cdn 7 months ago data_structure 7 months ago activation.cls.php 7 months ago admin-display.cls.php 7 months ago admin-settings.cls.php 7 months ago admin.cls.php 7 months ago api.cls.php 7 months ago avatar.cls.php 7 months ago base.cls.php 7 months ago cdn.cls.php 7 months ago cloud.cls.php 7 months ago conf.cls.php 7 months ago control.cls.php 7 months ago core.cls.php 7 months ago crawler-map.cls.php 7 months ago crawler.cls.php 7 months ago css.cls.php 7 months ago data.cls.php 7 months ago data.upgrade.func.php 7 months ago db-optm.cls.php 7 months ago debug2.cls.php 7 months ago doc.cls.php 7 months ago error.cls.php 7 months ago esi.cls.php 7 months ago file.cls.php 7 months ago gui.cls.php 7 months ago health.cls.php 7 months ago htaccess.cls.php 7 months ago img-optm.cls.php 7 months ago import.cls.php 7 months ago import.preset.cls.php 7 months ago lang.cls.php 7 months ago localization.cls.php 7 months ago media.cls.php 7 months ago metabox.cls.php 7 months ago object-cache-wp.cls.php 7 months ago object-cache.cls.php 7 months ago object.lib.php 7 months ago optimize.cls.php 7 months ago optimizer.cls.php 7 months ago placeholder.cls.php 7 months ago purge.cls.php 7 months ago report.cls.php 7 months ago rest.cls.php 7 months ago root.cls.php 7 months ago router.cls.php 7 months ago str.cls.php 7 months ago tag.cls.php 7 months ago task.cls.php 7 months ago tool.cls.php 7 months ago ucss.cls.php 7 months ago utility.cls.php 7 months ago vary.cls.php 7 months ago vpi.cls.php 7 months ago
root.cls.php
670 lines
1 <?php
2 // phpcs:ignoreFile
3
4 /**
5 * The abstract instance
6 *
7 * @since 3.0
8 */
9
10 namespace LiteSpeed;
11
12 defined('WPINC') || exit();
13
14 abstract class Root {
15
16 const CONF_FILE = '.litespeed_conf.dat';
17 // Instance set
18 private static $_instances;
19
20 private static $_options = array();
21 private static $_const_options = array();
22 private static $_primary_options = array();
23 private static $_network_options = array();
24
25 /**
26 * Check if need to separate ccss for mobile
27 *
28 * @since 4.7
29 * @access protected
30 */
31 protected function _separate_mobile() {
32 return (wp_is_mobile() || apply_filters('litespeed_is_mobile', false)) && $this->conf(Base::O_CACHE_MOBILE);
33 }
34
35 /**
36 * Log an error message
37 *
38 * @since 7.0
39 */
40 public static function debugErr( $msg, $backtrace_limit = false ) {
41 $msg = '' . $msg;
42 self::debug($msg, $backtrace_limit);
43 }
44
45 /**
46 * Log a debug message.
47 *
48 * @since 4.4
49 * @access public
50 */
51 public static function debug( $msg, $backtrace_limit = false ) {
52 if (!defined('LSCWP_LOG')) {
53 return;
54 }
55
56 if (defined('static::LOG_TAG')) {
57 $msg = static::LOG_TAG . ' ' . $msg;
58 }
59
60 Debug2::debug($msg, $backtrace_limit);
61 }
62
63 /**
64 * Log an advanced debug message.
65 *
66 * @since 4.4
67 * @access public
68 */
69 public static function debug2( $msg, $backtrace_limit = false ) {
70 if (!defined('LSCWP_LOG_MORE')) {
71 return;
72 }
73
74 if (defined('static::LOG_TAG')) {
75 $msg = static::LOG_TAG . ' ' . $msg;
76 }
77 Debug2::debug2($msg, $backtrace_limit);
78 }
79
80 /**
81 * Check if there is cache folder for that type
82 *
83 * @since 3.0
84 */
85 public function has_cache_folder( $type ) {
86 $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : '';
87
88 if (file_exists(LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id)) {
89 return true;
90 }
91 return false;
92 }
93
94 /**
95 * Maybe make the cache folder if not existed
96 *
97 * @since 4.4.2
98 */
99 protected function _maybe_mk_cache_folder( $type ) {
100 if (!$this->has_cache_folder($type)) {
101 $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : '';
102 $path = LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id;
103 mkdir($path, 0755, true);
104 }
105 }
106
107 /**
108 * Delete file-based cache folder for that type
109 *
110 * @since 3.0
111 */
112 public function rm_cache_folder( $type ) {
113 if (!$this->has_cache_folder($type)) {
114 return;
115 }
116
117 $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : '';
118
119 File::rrmdir(LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id);
120
121 // Clear All summary data
122 self::save_summary(false, false, true);
123
124 if ($type == 'ccss' || $type == 'ucss') {
125 Debug2::debug('[CSS] Cleared ' . $type . ' queue');
126 } elseif ($type == 'avatar') {
127 Debug2::debug('[Avatar] Cleared ' . $type . ' queue');
128 } elseif ($type == 'css' || $type == 'js') {
129 return;
130 } else {
131 Debug2::debug('[' . strtoupper($type) . '] Cleared ' . $type . ' queue');
132 }
133 }
134
135 /**
136 * Build the static filepath
137 *
138 * @since 4.0
139 */
140 protected function _build_filepath_prefix( $type ) {
141 $filepath_prefix = '/' . $type . '/';
142 if (is_multisite()) {
143 $filepath_prefix .= get_current_blog_id() . '/';
144 }
145
146 return $filepath_prefix;
147 }
148
149 /**
150 * Load current queues from data file
151 *
152 * @since 4.1
153 * @since 4.3 Elevated to root.cls
154 */
155 public function load_queue( $type ) {
156 $filepath_prefix = $this->_build_filepath_prefix($type);
157 $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat';
158
159 $queue = array();
160 if (file_exists($static_path)) {
161 $queue = \json_decode(file_get_contents($static_path), true) ?: array();
162 }
163
164 return $queue;
165 }
166
167 /**
168 * Save current queues to data file
169 *
170 * @since 4.1
171 * @since 4.3 Elevated to root.cls
172 */
173 public function save_queue( $type, $list ) {
174 $filepath_prefix = $this->_build_filepath_prefix($type);
175 $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat';
176
177 $data = \json_encode($list);
178
179 File::save($static_path, $data, true);
180 }
181
182 /**
183 * Clear all waiting queues
184 *
185 * @since 3.4
186 * @since 4.3 Elevated to root.cls
187 */
188 public function clear_q( $type, $silent = false ) {
189 $filepath_prefix = $this->_build_filepath_prefix($type);
190 $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat';
191
192 if (file_exists($static_path)) {
193 $silent = false;
194 unlink($static_path);
195 }
196
197 if (!$silent) {
198 $msg = __('All QUIC.cloud service queues have been cleared.', 'litespeed-cache');
199 Admin_Display::success($msg);
200 }
201 }
202
203 /**
204 * Load an instance or create it if not existed
205 *
206 * @since 4.0
207 */
208 public static function cls( $cls = false, $unset = false, $data = false ) {
209 if (!$cls) {
210 $cls = self::ori_cls();
211 }
212 $cls = __NAMESPACE__ . '\\' . $cls;
213
214 $cls_tag = strtolower($cls);
215
216 if (!isset(self::$_instances[$cls_tag])) {
217 if ($unset) {
218 return;
219 }
220
221 self::$_instances[$cls_tag] = new $cls($data);
222 } elseif ($unset) {
223 unset(self::$_instances[$cls_tag]);
224 return;
225 }
226
227 return self::$_instances[$cls_tag];
228 }
229
230 /**
231 * Set one conf or confs
232 */
233 public function set_conf( $id, $val = null ) {
234 if (is_array($id)) {
235 foreach ($id as $k => $v) {
236 $this->set_conf($k, $v);
237 }
238 return;
239 }
240 self::$_options[$id] = $val;
241 }
242
243 /**
244 * Set one primary conf or confs
245 */
246 public function set_primary_conf( $id, $val = null ) {
247 if (is_array($id)) {
248 foreach ($id as $k => $v) {
249 $this->set_primary_conf($k, $v);
250 }
251 return;
252 }
253 self::$_primary_options[$id] = $val;
254 }
255
256 /**
257 * Set one network conf
258 */
259 public function set_network_conf( $id, $val = null ) {
260 if (is_array($id)) {
261 foreach ($id as $k => $v) {
262 $this->set_network_conf($k, $v);
263 }
264 return;
265 }
266 self::$_network_options[$id] = $val;
267 }
268
269 /**
270 * Set one const conf
271 */
272 public function set_const_conf( $id, $val ) {
273 self::$_const_options[$id] = $val;
274 }
275
276 /**
277 * Check if is overwritten by const
278 *
279 * @since 3.0
280 */
281 public function const_overwritten( $id ) {
282 if (!isset(self::$_const_options[$id]) || self::$_const_options[$id] == self::$_options[$id]) {
283 return null;
284 }
285 return self::$_const_options[$id];
286 }
287
288 /**
289 * Check if is overwritten by primary site
290 *
291 * @since 3.2.2
292 */
293 public function primary_overwritten( $id ) {
294 if (!isset(self::$_primary_options[$id]) || self::$_primary_options[$id] == self::$_options[$id]) {
295 return null;
296 }
297
298 // Network admin settings is impossible to be overwritten by primary
299 if (is_network_admin()) {
300 return null;
301 }
302
303 return self::$_primary_options[$id];
304 }
305
306 /**
307 * Check if is overwritten by code filter
308 *
309 * @since 7.4
310 */
311 public function filter_overwritten( $id ) {
312 $cls_admin_display = Admin_Display::$settings_filters;
313 // Check if filter name is set.
314 if(!isset($cls_admin_display[$id]) || !isset($cls_admin_display[$id]['filter']) || is_array($cls_admin_display[$id]['filter']) ){
315 return null;
316 }
317
318 $val_setting = $this->conf($id, true);
319 // if setting not found
320 if( null === $val_setting ){
321 $val_setting = '';
322 }
323
324 $val_filter = apply_filters($cls_admin_display[$id]['filter'], $val_setting );
325
326 if ($val_setting === $val_filter) {
327 // If the value is the same, return null.
328 return null;
329 }
330
331 return $val_filter;
332 }
333
334 /**
335 * Check if is overwritten by $SERVER variable
336 *
337 * @since 7.4
338 */
339 public function server_overwritten( $id ) {
340 $cls_admin_display = Admin_Display::$settings_filters;
341 if(!isset($cls_admin_display[$id]['filter'])){
342 return null;
343 }
344
345 if(!is_array($cls_admin_display[$id]['filter'])) {
346 $cls_admin_display[$id]['filter'] = array( $cls_admin_display[$id]['filter'] );
347 }
348
349 foreach( $cls_admin_display[$id]['filter'] as $variable ){
350 if(isset($_SERVER[$variable])) {
351 return [ $variable , $_SERVER[$variable] ] ;
352 }
353 }
354
355 return null;
356 }
357
358 /**
359 * Get the list of configured options for the blog.
360 *
361 * @since 1.0
362 */
363 public function get_options( $ori = false ) {
364 if (!$ori) {
365 return array_merge(self::$_options, self::$_primary_options, self::$_network_options, self::$_const_options);
366 }
367
368 return self::$_options;
369 }
370
371 /**
372 * If has a conf or not
373 */
374 public function has_conf( $id ) {
375 return array_key_exists($id, self::$_options);
376 }
377
378 /**
379 * If has a primary conf or not
380 */
381 public function has_primary_conf( $id ) {
382 return array_key_exists($id, self::$_primary_options);
383 }
384
385 /**
386 * If has a network conf or not
387 */
388 public function has_network_conf( $id ) {
389 return array_key_exists($id, self::$_network_options);
390 }
391
392 /**
393 * Get conf
394 */
395 public function conf( $id, $ori = false ) {
396 if (isset(self::$_options[$id])) {
397 if (!$ori) {
398 $val = $this->const_overwritten($id);
399 if ($val !== null) {
400 defined('LSCWP_LOG') && Debug2::debug('[Conf] 🏛️ const option ' . $id . '=' . var_export($val, true));
401 return $val;
402 }
403
404 $val = $this->primary_overwritten($id); // Network Use primary site settings
405 if ($val !== null) {
406 return $val;
407 }
408 }
409
410 // Network original value will be in _network_options
411 if (!is_network_admin() || !$this->has_network_conf($id)) {
412 return self::$_options[$id];
413 }
414 }
415
416 if ($this->has_network_conf($id)) {
417 if (!$ori) {
418 $val = $this->const_overwritten($id);
419 if ($val !== null) {
420 defined('LSCWP_LOG') && Debug2::debug('[Conf] 🏛️ const option ' . $id . '=' . var_export($val, true));
421 return $val;
422 }
423 }
424
425 return $this->network_conf($id);
426 }
427
428 defined('LSCWP_LOG') && Debug2::debug('[Conf] Invalid option ID ' . $id);
429
430 return null;
431 }
432
433 /**
434 * Get primary conf
435 */
436 public function primary_conf( $id ) {
437 return self::$_primary_options[$id];
438 }
439
440 /**
441 * Get network conf
442 */
443 public function network_conf( $id ) {
444 if (!$this->has_network_conf($id)) {
445 return null;
446 }
447
448 return self::$_network_options[$id];
449 }
450
451 /**
452 * Get called class short name
453 */
454 public static function ori_cls() {
455 $cls = new \ReflectionClass(get_called_class());
456 $shortname = $cls->getShortName();
457 $namespace = str_replace(__NAMESPACE__ . '\\', '', $cls->getNamespaceName() . '\\');
458 if ($namespace) {
459 // the left namespace after dropped LiteSpeed
460 $shortname = $namespace . $shortname;
461 }
462
463 return $shortname;
464 }
465
466 /**
467 * Generate conf name for wp_options record
468 *
469 * @since 3.0
470 */
471 public static function name( $id ) {
472 $name = strtolower(self::ori_cls());
473 return 'litespeed.' . $name . '.' . $id;
474 }
475
476 /**
477 * Dropin with prefix for WP's get_option
478 *
479 * @since 3.0
480 */
481 public static function get_option( $id, $default_v = false ) {
482 $v = get_option(self::name($id), $default_v);
483
484 // Maybe decode array
485 if (is_array($default_v)) {
486 $v = self::_maybe_decode($v);
487 }
488
489 return $v;
490 }
491
492 /**
493 * Dropin with prefix for WP's get_site_option
494 *
495 * @since 3.0
496 */
497 public static function get_site_option( $id, $default_v = false ) {
498 $v = get_site_option(self::name($id), $default_v);
499
500 // Maybe decode array
501 if (is_array($default_v)) {
502 $v = self::_maybe_decode($v);
503 }
504
505 return $v;
506 }
507
508 /**
509 * Dropin with prefix for WP's get_blog_option
510 *
511 * @since 3.0
512 */
513 public static function get_blog_option( $blog_id, $id, $default_v = false ) {
514 $v = get_blog_option($blog_id, self::name($id), $default_v);
515
516 // Maybe decode array
517 if (is_array($default_v)) {
518 $v = self::_maybe_decode($v);
519 }
520
521 return $v;
522 }
523
524 /**
525 * Dropin with prefix for WP's add_option
526 *
527 * @since 3.0
528 */
529 public static function add_option( $id, $v ) {
530 add_option(self::name($id), self::_maybe_encode($v));
531 }
532
533 /**
534 * Dropin with prefix for WP's add_site_option
535 *
536 * @since 3.0
537 */
538 public static function add_site_option( $id, $v ) {
539 add_site_option(self::name($id), self::_maybe_encode($v));
540 }
541
542 /**
543 * Dropin with prefix for WP's update_option
544 *
545 * @since 3.0
546 */
547 public static function update_option( $id, $v ) {
548 update_option(self::name($id), self::_maybe_encode($v));
549 }
550
551 /**
552 * Dropin with prefix for WP's update_site_option
553 *
554 * @since 3.0
555 */
556 public static function update_site_option( $id, $v ) {
557 update_site_option(self::name($id), self::_maybe_encode($v));
558 }
559
560 /**
561 * Decode an array
562 *
563 * @since 4.0
564 */
565 private static function _maybe_decode( $v ) {
566 if (!is_array($v)) {
567 $v2 = \json_decode($v, true);
568 if ($v2 !== null) {
569 $v = $v2;
570 }
571 }
572 return $v;
573 }
574
575 /**
576 * Encode an array
577 *
578 * @since 4.0
579 */
580 private static function _maybe_encode( $v ) {
581 if (is_array($v)) {
582 $v = \json_encode($v) ?: $v; // Non utf-8 encoded value will get failed, then used ori value
583 }
584 return $v;
585 }
586
587 /**
588 * Dropin with prefix for WP's delete_option
589 *
590 * @since 3.0
591 */
592 public static function delete_option( $id ) {
593 delete_option(self::name($id));
594 }
595
596 /**
597 * Dropin with prefix for WP's delete_site_option
598 *
599 * @since 3.0
600 */
601 public static function delete_site_option( $id ) {
602 delete_site_option(self::name($id));
603 }
604
605 /**
606 * Read summary
607 *
608 * @since 3.0
609 * @access public
610 */
611 public static function get_summary( $field = false ) {
612 $summary = self::get_option('_summary', array());
613
614 if (!is_array($summary)) {
615 $summary = array();
616 }
617
618 if (!$field) {
619 return $summary;
620 }
621
622 if (array_key_exists($field, $summary)) {
623 return $summary[$field];
624 }
625
626 return null;
627 }
628
629 /**
630 * Save summary
631 *
632 * @since 3.0
633 * @access public
634 */
635 public static function save_summary( $data = false, $reload = false, $overwrite = false ) {
636 if ($reload || empty(static::cls()->_summary)) {
637 self::reload_summary();
638 }
639
640 $existing_summary = static::cls()->_summary;
641 if ($overwrite || !is_array($existing_summary)) {
642 $existing_summary = array();
643 }
644 $new_summary = array_merge($existing_summary, $data ?: array());
645 // self::debug2('Save after Reloaded summary', $new_summary);
646 static::cls()->_summary = $new_summary;
647
648 self::update_option('_summary', $new_summary);
649 }
650
651 /**
652 * Reload summary
653 *
654 * @since 5.0
655 */
656 public static function reload_summary() {
657 static::cls()->_summary = self::get_summary();
658 // self::debug2( 'Reloaded summary', static::cls()->_summary );
659 }
660
661 /**
662 * Get the current instance object. To be inherited.
663 *
664 * @since 3.0
665 */
666 public static function get_instance() {
667 return static::cls();
668 }
669 }
670