ScopedOptionStorage.php
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Storage;
use WPMenuEditor;
class ScopedOptionStorage extends LazyArrayStorage implements StorageInterface, CompressedStorage {
const GLOBAL_SCOPE = 'global';
const SITE_SCOPE = 'site';
const COMPRESSED_VALUE_PREFIX = 'gzcompress:';
protected $scope = self::SITE_SCOPE;
protected $optionName;
protected $data = null;
protected $doneLoading = false;
protected $isMarkedForDeletion = false;
protected $jsonSerializationEnabled = false;
protected $compressionSupported = true;
protected $compressionEnabled = false;
public function __construct(
$optionName,
$scope = self::SITE_SCOPE,
$compressionEnabled = false
) {
parent::__construct();
$this->optionName = $optionName;
$this->scope = $scope;
$this->compressionEnabled = $compressionEnabled;
}
protected function loadData() {
$defaultValue = null;
if ( $this->scope === self::SITE_SCOPE ) {
$value = get_option($this->optionName, $defaultValue);
} else {
$value = get_site_option($this->optionName, $defaultValue);
}
//Decompress gzipped data.
if (
$this->compressionSupported
&& is_string($value)
&& (substr($value, 0, strlen(self::COMPRESSED_VALUE_PREFIX)) === self::COMPRESSED_VALUE_PREFIX)
&& function_exists('gzuncompress')
) {
//phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- For back-compat with older plugin versions.
$value = unserialize(gzuncompress(base64_decode(
substr($value, strlen(self::COMPRESSED_VALUE_PREFIX)
))));
}
//Parse JSON.
if ( $this->jsonSerializationEnabled && is_string($value) ) {
$value = json_decode($value, true);
}
return $value;
}
protected function storeData($newData) {
$storedData = $newData;
//Optionally, serialize to JSON.
if ( $this->jsonSerializationEnabled ) {
$storedData = wp_json_encode($storedData);
}
//Compress the data.
if (
$this->compressionSupported
&& $this->compressionEnabled
&& function_exists('gzcompress')
) {
//This presents a migration risk: if the database is migrated from a site
//that has the zlib extension to one that does not, the plugin won't be able
//to load the compressed data.
//phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$storedData = self::COMPRESSED_VALUE_PREFIX
. base64_encode(gzcompress(serialize($storedData)));
//phpcs:enable
}
if ( ($this->scope === self::GLOBAL_SCOPE) && is_multisite() ) {
if ( class_exists('\\WPMenuEditor', false) ) {
return WPMenuEditor::atomic_update_site_option($this->optionName, $storedData);
}
return update_site_option($this->optionName, $storedData);
} else {
return update_option($this->optionName, $storedData);
}
}
protected function deleteStoredData() {
if ( ($this->scope === self::GLOBAL_SCOPE) && is_multisite() ) {
return delete_site_option($this->optionName);
} else {
return delete_option($this->optionName);
}
}
/**
* Toggle gzip compression.
*
* This only works if the zlib extension is available. If it's not, the method will
* always return false.
*
* @param boolean $enabled
* @return boolean Whether compression was actually enabled or not.
*/
public function setCompressionEnabled($enabled) {
if ( !function_exists('gzcompress') ) {
$this->compressionEnabled = false;
} else {
$this->compressionEnabled = $enabled;
}
return $this->compressionEnabled;
}
public function setJsonSerialization($enabled) {
$this->jsonSerializationEnabled = $enabled;
}
public function getStorageKey() {
return $this->optionName;
}
}