PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / diagnostics / CanonicalHookCensusStore.php

CanonicalHookCensusStore.php in 404 Solution trunk, at includes/diagnostics/CanonicalHookCensusStore.php

83 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /** WordPress option persistence for the canonical-redirect hook census. */
8 final class ABJ_404_Solution_CanonicalHookCensusStore {
9
10 const OPTION_NAME = 'abj404_canonical_hook_census';
11
12 /**
13 * @return array<string, mixed>
14 */
15 public function read(): array {
16 try {
17 if (!function_exists('get_option')) {
18 return array();
19 }
20 $raw = get_option(self::OPTION_NAME, '');
21 if ($raw === '') {
22 return array();
23 }
24 if (!is_string($raw)) {
25 abj404_logPhpFallback('canonical-hook-census',
26 'canonical hook census option has a malformed non-string value. Recovery: delete the '
27 . self::OPTION_NAME . ' option and retry from a front-end 404.');
28 return array();
29 }
30 $decoded = json_decode($raw, true);
31 if (!is_array($decoded)) {
32 abj404_logPhpFallback('canonical-hook-census',
33 'canonical hook census option contains malformed JSON: ' . json_last_error_msg()
34 . '. Recovery: delete the ' . self::OPTION_NAME
35 . ' option and retry from a front-end 404.');
36 return array();
37 }
38 $record = array();
39 foreach ($decoded as $key => $value) {
40 if (!is_string($key)) {
41 abj404_logPhpFallback('canonical-hook-census',
42 'canonical hook census option contains a non-string field name. Recovery: delete the '
43 . self::OPTION_NAME . ' option and retry from a front-end 404.');
44 return array();
45 }
46 $record[$key] = $value;
47 }
48 return $record;
49 } catch (Throwable $e) {
50 abj404_logPhpFallback('canonical-hook-census',
51 'canonical hook census read failed (code ' . $e->getCode() . '): ' . $e->getMessage());
52 return array();
53 }
54 }
55
56 /** @param array<string, mixed> $record */
57 public function write(array $record): void {
58 if (!function_exists('update_option')) {
59 return;
60 }
61 $encoded = json_encode($record);
62 if (!is_string($encoded)) {
63 abj404_logPhpFallback('canonical-hook-census',
64 'canonical hook census could not be encoded: ' . json_last_error_msg());
65 return;
66 }
67 if (!update_option(self::OPTION_NAME, $encoded, false)) {
68 abj404_logPhpFallback('canonical-hook-census',
69 'canonical hook census option write was not persisted. Recovery: inspect the WordPress '
70 . 'options table and object-cache error logs, then retry from a front-end 404.');
71 }
72 }
73
74 /** Drop cached values that can hide a concurrent refresh. */
75 public function refreshReads(): void {
76 if (!function_exists('wp_cache_delete')) {
77 return;
78 }
79 wp_cache_delete(self::OPTION_NAME, 'options');
80 wp_cache_delete('notoptions', 'options');
81 }
82 }
83