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

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