PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.27
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.27
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / class-berqconfigs.php

class-berqconfigs.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.27, at inc/class-berqconfigs.php

184 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 class berqConfigs {
5 public $config_file;
6 private $defaults = [
7 'site_id' => '',
8 'secret' => '',
9 'home_url' => '',
10 'optimization_method' => '',
11 'exclude_cookies' => [],
12 'exclude_urls' => [],
13 'cache_lifespan' => MONTH_IN_SECONDS,
14 'page_compression' => false,
15 ];
16
17 private static $cached_config = null;
18 private static $cached_blog_id = null;
19 private static $instance = null;
20
21 function __construct() {
22 $base_dir = WP_CONTENT_DIR . '/cache/berqwp/';
23
24 if (function_exists('is_multisite') && is_multisite()) {
25 $blog_id = $this->get_blog_id();
26 // Store per-site config inside the site's cache directory
27 $config_dir = $base_dir . 'site-' . $blog_id;
28 $this->config_file = $config_dir . '/config.json';
29
30 // Reset static cache if blog context changed
31 if (self::$cached_blog_id !== null && self::$cached_blog_id !== $blog_id) {
32 self::$cached_config = null;
33 }
34 self::$cached_blog_id = $blog_id;
35 } else {
36 $this->config_file = $base_dir . 'config.json';
37 }
38
39 $config_dir = dirname($this->config_file);
40
41 // Ensure the cache directory exists
42 if (!is_dir($config_dir)) {
43 @mkdir($config_dir, 0755, true);
44 }
45
46 // If dir still doesn't exist (e.g. permissions failure), use defaults and skip disk I/O
47 if (!is_dir($config_dir)) {
48 self::$cached_config = $this->defaults;
49 return;
50 }
51
52 if (!file_exists($this->config_file)) {
53 // Create config file with current defaults if it doesn't exist
54 $this->save_config($this->defaults);
55 self::$cached_config = $this->defaults;
56 } else if (self::$cached_config === null) {
57 // Only read from disk if not already cached this request
58 $existing_config = $this->get_file_config();
59 $merged_config = $this->merge_with_defaults($existing_config);
60
61 if ($merged_config !== $existing_config) {
62 $this->save_config($merged_config);
63 }
64
65 self::$cached_config = $merged_config;
66 }
67 }
68
69 static function getInstance() {
70 if (self::$instance === null) {
71 self::$instance = new berqConfigs();
72 }
73 return self::$instance;
74 }
75
76 /**
77 * Get the current blog ID, with fallback for early-load (drop-in) context.
78 */
79 private function get_blog_id() {
80 if (function_exists('get_current_blog_id')) {
81 return get_current_blog_id();
82 }
83 return self::detect_blog_id_from_request();
84 }
85
86 /**
87 * Detect blog_id from the HTTP request before WordPress is fully loaded.
88 * Uses a cached blog-map.json file for lookup.
89 */
90 public static function detect_blog_id_from_request() {
91 // If WordPress has already set the global, use it
92 global $blog_id;
93 if (!empty($blog_id)) {
94 return (int) $blog_id;
95 }
96
97 $map_file = WP_CONTENT_DIR . '/cache/berqwp/blog-map.json';
98 if (!file_exists($map_file)) {
99 return 1;
100 }
101
102 $map = json_decode(file_get_contents($map_file), true);
103 if (empty($map)) {
104 return 1;
105 }
106
107 $host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : '';
108 $path = isset($_SERVER['REQUEST_URI']) ? strtolower($_SERVER['REQUEST_URI']) : '/';
109
110 // Subdomain match
111 if (!empty($map['subdomains']) && isset($map['subdomains'][$host])) {
112 return (int) $map['subdomains'][$host];
113 }
114
115 // Subdirectory match (longest prefix wins)
116 if (!empty($map['subdirs'])) {
117 $best_match = 1;
118 $best_len = 0;
119 foreach ($map['subdirs'] as $prefix => $id) {
120 if (strpos($path, $prefix) === 0 && strlen($prefix) > $best_len) {
121 $best_match = (int) $id;
122 $best_len = strlen($prefix);
123 }
124 }
125 return $best_match;
126 }
127
128 return 1;
129 }
130
131 private function merge_with_defaults($config) {
132 // Ensure all default keys exist in the config, preserving existing values
133 return array_merge($this->defaults, $config);
134 }
135
136 private function get_file_config() {
137 if (file_exists($this->config_file)) {
138 $contents = file_get_contents($this->config_file);
139
140 if ($contents === false) {
141 return false;
142 }
143
144 return json_decode($contents, true) ?: [];
145 }
146 return [];
147 }
148
149 private function save_config($config) {
150 file_put_contents($this->config_file, json_encode($config, JSON_PRETTY_PRINT));
151 self::$cached_config = null; // invalidate cache on write
152 }
153
154 public function get_configs() {
155 if (self::$cached_config !== null) {
156 return self::$cached_config;
157 }
158
159 $file_config = $this->get_file_config();
160
161 if ($file_config === false) {
162 return false;
163 }
164
165 self::$cached_config = $this->merge_with_defaults($file_config);
166 return self::$cached_config;
167 }
168
169 public function update_configs($new_config) {
170 // Get current config (with defaults)
171 $current_config = $this->get_configs();
172
173 if ($current_config === false) {
174 return false;
175 }
176
177 // Merge new values with existing config
178 $updated_config = array_merge($current_config, $new_config);
179
180 // Save the complete configuration
181 return $this->save_config($updated_config);
182 }
183 }
184