PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.01
Docket Cache – Object Cache Accelerator v22.07.01
26.04.05 trunk 22.07.01 22.07.02 22.07.03 22.07.04 22.07.05 23.08.01 23.08.02 24.07.01 24.07.02 24.07.03 24.07.04 24.07.05 24.07.06 24.07.07 26.04.03 26.04.04
docket-cache / includes / compat.php

compat.php in Docket Cache – Object Cache Accelerator 22.07.01, at includes/compat.php

537 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Docket Cache.
4 *
5 * @author Nawawi Jamili
6 * @license MIT
7 *
8 * @see https://github.com/nawawi/docket-cache
9 */
10 \defined('ABSPATH') || exit;
11
12 if (class_exists('Nawawi\\Symfony\\Component\\VarExporter\\VarExporter')) {
13 class_alias('Nawawi\Symfony\Component\VarExporter\VarExporter', 'Nawawi\DocketCache\Exporter\VarExporter', false);
14 }
15 if (class_exists('Nawawi\\Symfony\\Component\\VarExporter\\Internal\\Hydrator')) {
16 class_alias('Nawawi\Symfony\Component\VarExporter\Internal\Hydrator', 'Nawawi\DocketCache\Exporter\Hydrator', false);
17 }
18 if (class_exists('Nawawi\\Symfony\\Component\\VarExporter\\Internal\\Registry')) {
19 class_alias('Nawawi\Symfony\Component\VarExporter\Internal\Registry', 'Nawawi\DocketCache\Exporter\Registry', false);
20 }
21
22 if (!\function_exists('nwdcx_arraymap')) {
23 function nwdcx_arraymap($func, $arr)
24 {
25 $new = [];
26 foreach ($arr as $key => $value) {
27 $new[$key] = (\is_array($value) ? nwdcx_arraymap($func, $value) : (\is_array($func) ? \call_user_func_array($func, $value) : $func($value)));
28 }
29
30 return $new;
31 }
32 }
33
34 if (!\function_exists('nwdcx_serialized')) {
35 function nwdcx_serialized($data)
36 {
37 if (!\function_exists('is_serialized')) {
38 // 16072021: rare opcache issue with some hosting ABSPATH not defined.
39 if (\defined('ABSPATH') && \defined('WPINC')) {
40 @include_once ABSPATH.WPINC.'/functions.php';
41 }
42 }
43
44 if (!\function_exists('is_serialized')) {
45 return false;
46 }
47
48 return is_serialized($data);
49 }
50 }
51
52 if (!\function_exists('nwdcx_unserialize')) {
53 function nwdcx_unserialize($data)
54 {
55 if (!nwdcx_serialized($data)) {
56 return $data;
57 }
58
59 $ok = true;
60
61 // if the string has object format, check it if has stdClass,
62 // other than that set it as false and return the original data
63 if (false !== strpos($data, 'O:') && @preg_match_all('@O:\d+:"([^"]+)"@', $data, $mm)) {
64 if (!empty($mm) && !empty($mm[1])) {
65 foreach ($mm[1] as $v) {
66 if ('stdClass' !== $v) {
67 $ok = false;
68 break;
69 }
70 }
71 unset($mm);
72 }
73 }
74
75 return !$ok ? $data : @unserialize(trim($data));
76 }
77 }
78
79 if (!\function_exists('nwdcx_fixhost')) {
80 function nwdcx_fixhost($hostname)
81 {
82 if (false !== strpos($hostname, ':')) {
83 return @preg_replace('@:\d+$@', '', $hostname);
84 }
85
86 return $hostname;
87 }
88 }
89
90 if (!\function_exists('nwdcx_fixscheme')) {
91 // replace http scheme
92 function nwdcx_fixscheme($url, $scheme = 'http://')
93 {
94 return @preg_replace('@^((([a-zA-Z]+)?:)?(//))?@', $scheme, trim($url));
95 }
96 }
97
98 if (!\function_exists('nwdcx_noscheme')) {
99 // replace http scheme
100 function nwdcx_noscheme($url)
101 {
102 return nwdcx_fixscheme($url, '');
103 }
104 }
105
106 if (!\function_exists('nwdcx_wpdb')) {
107 function nwdcx_wpdb(&$wpdb = '')
108 {
109 if (!isset($GLOBALS['wpdb']) || !\is_object($GLOBALS['wpdb']) || (isset($GLOBALS['wpdb']->ready) && !$GLOBALS['wpdb']->ready)) {
110 $wpdb = false;
111
112 return false;
113 }
114
115 $wpdb = $GLOBALS['wpdb'];
116
117 return $wpdb;
118 }
119 }
120
121 if (!\function_exists('nwdcx_optget')) {
122 function nwdcx_optget($key)
123 {
124 if (!nwdcx_wpdb($wpdb)) {
125 return false;
126 }
127
128 static $cache = [];
129
130 if (isset($cache[$key])) {
131 return $cache[$key];
132 }
133
134 $suppress = $wpdb->suppress_errors(true);
135
136 $query = $wpdb->prepare('SELECT option_value FROM `'.$wpdb->options.'` WHERE option_name=%s ORDER BY option_id ASC LIMIT 1', $key);
137 $option = $wpdb->get_var($query);
138 $cache[$key] = !empty($option) ? nwdcx_unserialize($option) : false;
139
140 $wpdb->suppress_errors($suppress);
141
142 return $cache[$key];
143 }
144 }
145
146 if (!\function_exists('nwdcx_cleanuptransient')) {
147 function nwdcx_cleanuptransient()
148 {
149 if (!nwdcx_wpdb($wpdb)) {
150 return false;
151 }
152
153 $suppress = $wpdb->suppress_errors(true);
154
155 $collect = [];
156
157 $results = $wpdb->get_results('SELECT `option_id`,`option_name`,`option_value` FROM `'.$wpdb->options.'` WHERE `option_name` LIKE "_transient_%" OR `option_name` LIKE "_site_transient_%" ORDER BY `option_id` ASC LIMIT 1000', ARRAY_A);
158 if (!empty($results) && \is_array($results)) {
159 while ($row = @array_shift($results)) {
160 $key = @preg_replace('@^(_site)?(_transient)(_timeout)?_@', '', $row['option_name']);
161 $collect[$key] = $key;
162
163 if (false !== strpos($row['option_name'], '_transient_timeout_') && (int) $row['option_value'] > time()) {
164 unset($collect[$key]);
165 }
166 }
167
168 if (!empty($collect)) {
169 $wpdb->query('START TRANSACTION');
170 foreach ($collect as $key) {
171 $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name`='_transient_{$key}'");
172 $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name`='_transient_timeout_{$key}'");
173 $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name`='_site_transient_{$key}'");
174 $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name`='_site_transient_timeout_{$key}'");
175 }
176 $wpdb->query('COMMIT');
177 }
178 }
179
180 $collect = [];
181
182 if (is_multisite() && isset($wpdb->sitemeta)) {
183 $results = $wpdb->get_results('SELECT `meta_id`,`meta_key`,`meta_value` FROM `'.$wpdb->sitemeta.'` WHERE `meta_key` LIKE "_site_transient_%" ORDER BY `meta_id` ASC LIMIT 1000', ARRAY_A);
184 if (!empty($results) && \is_array($results)) {
185 while ($row = @array_shift($results)) {
186 $key = @preg_replace('@^(_site)?(_transient)(_timeout)?_@', '', $row['meta_key']);
187 $collect[$key] = $key;
188
189 if (false !== strpos($row['meta_key'], '_site_transient_timeout_') && (int) $row['meta_value'] > time()) {
190 unset($collect[$key]);
191 }
192 }
193
194 if (!empty($collect)) {
195 $wpdb->query('START TRANSACTION');
196 foreach ($collect as $key) {
197 $wpdb->query("DELETE FROM `{$wpdb->sitemeta}` WHERE `meta_key`='_site_transient_{$key}'");
198 $wpdb->query("DELETE FROM `{$wpdb->sitemeta}` WHERE `meta_key`='_site_transient_timeout_{$key}'");
199 }
200 $wpdb->query('COMMIT');
201 }
202 }
203 }
204
205 unset($collect, $results);
206 $wpdb->suppress_errors($suppress);
207
208 return true;
209 }
210 }
211
212 if (!\function_exists('nwdcx_runaction')) {
213 function nwdcx_runaction(...$args)
214 {
215 add_action(
216 'plugins_loaded',
217 function () use ($args) {
218 \call_user_func_array('do_action', $args);
219 }
220 );
221 }
222 }
223
224 if (!\function_exists('nwdcx_throwable')) {
225 function nwdcx_throwable($name, $error)
226 {
227 if (\defined('WP_DEBUG') && WP_DEBUG) {
228 if (!isset($GLOBALS['docketcache_throwable'])) {
229 $GLOBALS['docketcache_throwable'] = [];
230 }
231 $GLOBALS['docketcache_throwable'][$name] = $error;
232 }
233 }
234 }
235
236 if (!\function_exists('nwdcx_microtimetofloat')) {
237 function nwdcx_microtimetofloat($second)
238 {
239 list($usec, $sec) = explode(' ', $second);
240
241 return (float) $usec + (float) $sec;
242 }
243 }
244
245 if (!\function_exists('nwdcx_constfx')) {
246 function nwdcx_constfx($name, $is_strip = false)
247 {
248 if (!$is_strip) {
249 return strtoupper('docket_cache_'.$name);
250 }
251
252 return substr($name, 13);
253 }
254 }
255
256 if (!\function_exists('nwdcx_construe')) {
257 function nwdcx_construe($name)
258 {
259 $name = nwdcx_constfx($name);
260 if (\defined($name)) {
261 $value = (bool) \constant($name);
262 if (true === $value || 1 === $value) {
263 return true;
264 }
265 }
266
267 return false;
268 }
269 }
270
271 if (!\function_exists('nwdcx_consfalse')) {
272 function nwdcx_consfalse($name)
273 {
274 $name = nwdcx_constfx($name);
275
276 return !\defined($name) || !\constant($name);
277 }
278 }
279
280 if (!\function_exists('nwdcx_constval')) {
281 function nwdcx_constval($name)
282 {
283 $name = nwdcx_constfx($name);
284 $value = '';
285 if (\defined($name)) {
286 $value = \constant($name);
287 }
288
289 return $value;
290 }
291 }
292
293 if (!\function_exists('nwdcx_consdef')) {
294 function nwdcx_consdef($name, $value)
295 {
296 $name = nwdcx_constfx($name);
297
298 return !\defined($name) && \define($name, $value);
299 }
300 }
301
302 if (!\function_exists('nwdcx_suppresserrors')) {
303 function nwdcx_suppresserrors($level = true)
304 {
305 $errlevel = error_reporting();
306 $erropt = true === $level ? 0 : $level;
307 error_reporting($erropt);
308
309 return $errlevel;
310 }
311 }
312
313 if (!\function_exists('nwdcx_normalizepath')) {
314 function nwdcx_normalizepath($path)
315 {
316 if (false === strpos($path, '\\')) {
317 return $path;
318 }
319
320 if (\function_exists('wp_normalize_path')) {
321 return wp_normalize_path($path);
322 }
323
324 return str_replace('\\', '/', $path);
325 }
326 }
327
328 // network specific
329
330 if (!\function_exists('nwdcx_network_multi')) {
331 function nwdcx_network_multi()
332 {
333 if (!is_multisite()) {
334 return false;
335 }
336
337 if (\defined('MULTINETWORK')) {
338 return MULTINETWORK;
339 }
340
341 if (\defined('DOCKET_CACHE_MULTINETWORK')) {
342 return DOCKET_CACHE_MULTINETWORK;
343 }
344
345 if (!nwdcx_wpdb($wpdb)) {
346 return false;
347 }
348
349 static $ok = null;
350
351 if (\is_bool($ok)) {
352 return $ok;
353 }
354
355 // this lock file should only exists if network more than 1
356 // see Dropino::multinet_active
357 $lock_file = nwdcx_normalizepath(DOCKET_CACHE_CONTENT_PATH).'/.object-cache-network-multi.txt';
358 $timeout = time() + 86400;
359 if (@is_file($lock_file) && @is_readable($lock_file) && $timeout > @filemtime($lock_file)) {
360 $ok = !empty(@file_get_contents($lock_file)) ? true : false;
361
362 return $ok;
363 }
364
365 $table = $wpdb->base_prefix.'site';
366
367 $suppress = $wpdb->suppress_errors(true);
368 $query = "SELECT id FROM `{$table}` WHERE id > 0 ORDER BY id ASC LIMIT 2";
369 $check = $wpdb->query($query);
370 $wpdb->suppress_errors($suppress);
371 $ok = !empty($check) && $check > 1 ? true : false;
372
373 return $ok;
374 }
375 }
376
377 if (!\function_exists('nwdcx_network_ignore')) {
378 function nwdcx_network_ignore()
379 {
380 if (nwdcx_network_multi()) {
381 if (!@is_file(nwdcx_normalizepath(DOCKET_CACHE_CONTENT_PATH).'/.object-cache-network-'.nwdcx_network_id().'.txt')) {
382 return true;
383 }
384
385 return false;
386 }
387 }
388 }
389
390 if (!\function_exists('nwdcx_network_main')) {
391 function nwdcx_network_main()
392 {
393 if (!is_multisite()) {
394 return true;
395 }
396
397 if (\defined('PRIMARY_NETWORK_ID') && PRIMARY_NETWORK_ID === nwdcx_network_id()) {
398 return true;
399 }
400
401 if (\defined('SITE_ID_CURRENT_SITE') && SITE_ID_CURRENT_SITE === nwdcx_network_id()) {
402 return true;
403 }
404
405 if (empty($_SERVER['HTTP_HOST'])) {
406 return true;
407 }
408
409 if (!nwdcx_wpdb($wpdb)) {
410 return true;
411 }
412
413 $hostname = nwdcx_fixhost($_SERVER['HTTP_HOST']);
414
415 if (\defined('DOMAIN_CURRENT_SITE') && DOMAIN_CURRENT_SITE === $hostname) {
416 return true;
417 }
418
419 $lock_file = nwdcx_normalizepath(DOCKET_CACHE_CONTENT_PATH).'/.object-cache-network-main.txt';
420 $timeout = time() + 86400;
421 if (@is_file($lock_file) && @is_readable($lock_file) && $timeout > @filemtime($lock_file)) {
422 $data = @file_get_contents($lock_file);
423 if (!empty($data) && trim($data) === $hostname) {
424 return true;
425 }
426 }
427
428 static $cache = [];
429
430 if (isset($cache[$hostname])) {
431 return $cache[$hostname];
432 }
433
434 $table = $wpdb->base_prefix.'site';
435
436 $suppress = $wpdb->suppress_errors(true);
437 $query = "SELECT domain FROM `{$table}` WHERE id > 0 ORDER BY id ASC LIMIT 1";
438 $domain = $wpdb->get_var($query);
439 $wpdb->suppress_errors($suppress);
440
441 if ($hostname === $domain) {
442 $cache[$hostname] = true;
443 } elseif (@preg_match('@(.*?\.)?'.preg_quote($domain, '@').'@', $hostname)) {
444 $cache[$hostname] = true;
445 } else {
446 $cache[$hostname] = false;
447 }
448
449 if ($cache[$hostname] && !@is_file($lock_file)) {
450 @file_put_contents($lock_file, $hostname, \LOCK_EX);
451 }
452
453 return $cache[$hostname];
454 }
455 }
456
457 if (!\function_exists('nwdcx_network_id')) {
458 function nwdcx_network_id()
459 {
460 $network_id = \defined('SITE_ID_CURRENT_SITE') ? SITE_ID_CURRENT_SITE : 1;
461
462 if (!is_multisite()) {
463 return $network_id;
464 }
465
466 if (!nwdcx_wpdb($wpdb)) {
467 return $network_id;
468 }
469
470 if (empty($_SERVER['HTTP_HOST'])) {
471 return $network_id;
472 }
473
474 static $cache = [];
475
476 $hostname = nwdcx_fixhost($_SERVER['HTTP_HOST']);
477
478 if (isset($cache[$hostname])) {
479 return $cache[$hostname];
480 }
481
482 $table = $wpdb->base_prefix.'site';
483 $suppress = $wpdb->suppress_errors(true);
484
485 $query = "SELECT `id`,`domain`,`path` FROM `{$table}` ORDER BY id ASC LIMIT 100";
486 $networks = $wpdb->get_results($query, ARRAY_A);
487
488 if (!empty($networks) && \is_array($networks)) {
489 while ($row = @array_shift($networks)) {
490 $id = $row['id'];
491 $domain = $row['domain'];
492 $path = $row['path'];
493
494 if ($hostname === $domain) {
495 $network_id = $id;
496 break;
497 }
498
499 if (false !== strpos($hostname, $domain) && @preg_match('@(.*?\.)?'.preg_quote($domain, '@').'$@', $hostname)) {
500 $network_id = $id;
501 break;
502 }
503 }
504 }
505 $wpdb->suppress_errors($suppress);
506
507 $cache[$hostname] = $network_id;
508
509 return $cache[$hostname];
510 }
511 }
512
513 if (!\function_exists('nwdcx_network_dirpath')) {
514 function nwdcx_network_dirpath($save_path)
515 {
516 if (nwdcx_network_multi() && !nwdcx_network_main()) {
517 $save_path = rtrim($save_path, '/').'/network-'.nwdcx_network_id().'/';
518 }
519
520 return $save_path;
521 }
522 }
523
524 if (!\function_exists('nwdcx_network_filepath')) {
525 function nwdcx_network_filepath($file_path)
526 {
527 if (nwdcx_network_multi() && !nwdcx_network_main()) {
528 $ext = substr($file_path, -4);
529 $fname = substr($file_path, 0, -4);
530
531 $file_path = $fname.'-'.nwdcx_network_id().$ext;
532 }
533
534 return $file_path;
535 }
536 }
537