PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.18
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.18
4.1.19 4.1.18 4.1.17 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 All 173 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.1.18, at inc/class-berqconfigs.php

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