PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Settings / SettingsTable.php
wp-staging / Framework / Settings Last commit date
DarkMode.php 7 months ago Settings.php 2 months ago SettingsTable.php 2 months ago
SettingsTable.php
222 lines
1 <?php
2
3 namespace WPStaging\Framework\Settings;
4
5 use WPStaging\Framework\Database\CustomTable;
6
7 /**
8 * Manages the wpstg_settings custom table for storing plugin settings.
9 *
10 * Uses WordPress object cache (wp_cache_*) for per-request caching.
11 */
12 class SettingsTable extends CustomTable
13 {
14 /** @var string */
15 const CACHE_GROUP = 'wpstg_settings';
16
17 /** @var string */
18 const CACHE_EXISTS_KEY_SUFFIX = '__exists';
19
20 /**
21 * Table name without prefix
22 * @var string
23 */
24 const TABLE_NAME = 'wpstg_settings';
25
26 /**
27 * @return string
28 */
29 protected function getTableName()
30 {
31 return self::TABLE_NAME;
32 }
33
34 /**
35 * @return string
36 */
37 protected function getTableVersionKey()
38 {
39 return 'wpstg_settings_table_version';
40 }
41
42 /**
43 * @return string
44 */
45 protected function getTableVersion()
46 {
47 return '1.0.0';
48 }
49
50 /**
51 * @return string
52 */
53 protected function getCreateTableSql()
54 {
55 global $wpdb;
56 $collate = $wpdb->collate;
57 $tableName = $this->getFullTableName();
58
59 $sql = "CREATE TABLE IF NOT EXISTS {$tableName} (
60 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
61 setting_key VARCHAR(191) NOT NULL,
62 setting_value LONGTEXT DEFAULT NULL,
63 created_at DATETIME DEFAULT NULL,
64 updated_at DATETIME DEFAULT NULL,
65 PRIMARY KEY (id),
66 UNIQUE KEY setting_key (setting_key)
67 )";
68
69 if (!empty($collate)) {
70 $sql .= " COLLATE {$collate}";
71 }
72
73 return $sql;
74 }
75
76 /**
77 * Get a setting value from the settings table.
78 *
79 * @param string $name
80 * @param mixed $default
81 * @return mixed
82 */
83 public function get($name, $default = null)
84 {
85 $existsFound = false;
86 $existsCached = wp_cache_get($this->getExistsCacheKey($name), self::CACHE_GROUP, false, $existsFound);
87 if ($existsFound && !$existsCached) {
88 return $default;
89 }
90
91 $found = false;
92 $cached = wp_cache_get($name, self::CACHE_GROUP, false, $found);
93 if ($found) {
94 return $cached;
95 }
96
97 $this->ensureTable();
98
99 $tableName = $this->getFullTableName();
100 $escapedName = $this->database->escape($name);
101 $result = $this->database->query("SELECT setting_value FROM `{$tableName}` WHERE setting_key = '{$escapedName}'");
102
103 if ($result === false) {
104 return $default;
105 }
106
107 $row = $this->database->fetchAssoc($result);
108
109 if (!$row) {
110 wp_cache_set($this->getExistsCacheKey($name), false, self::CACHE_GROUP);
111 return $default;
112 }
113
114 $value = maybe_unserialize($row['setting_value']);
115 wp_cache_set($name, $value, self::CACHE_GROUP);
116 wp_cache_set($this->getExistsCacheKey($name), true, self::CACHE_GROUP);
117
118 return $value;
119 }
120
121 /**
122 * Set a setting value in the settings table.
123 *
124 * @param string $name
125 * @param mixed $value
126 * @return bool
127 */
128 public function set($name, $value)
129 {
130 $this->ensureTable();
131
132 $tableName = $this->getFullTableName();
133 $escapedName = $this->database->escape($name);
134 $escapedValue = $this->database->escape(maybe_serialize($value));
135 $now = current_time('mysql');
136 $result = $this->database->query(
137 "INSERT INTO `{$tableName}` (setting_key, setting_value, created_at, updated_at) VALUES ('{$escapedName}', '{$escapedValue}', '{$now}', '{$now}') ON DUPLICATE KEY UPDATE setting_value = '{$escapedValue}', updated_at = '{$now}'"
138 );
139
140 if ($result !== false) {
141 wp_cache_set($name, $value, self::CACHE_GROUP);
142 wp_cache_set($this->getExistsCacheKey($name), true, self::CACHE_GROUP);
143 return true;
144 }
145
146 return false;
147 }
148
149 /**
150 * Delete a setting from the settings table.
151 *
152 * @param string $name
153 * @return bool
154 */
155 public function delete($name)
156 {
157 $this->ensureTable();
158
159 $tableName = $this->getFullTableName();
160 $escapedName = $this->database->escape($name);
161 $result = $this->database->query("DELETE FROM `{$tableName}` WHERE setting_key = '{$escapedName}'");
162
163 wp_cache_delete($name, self::CACHE_GROUP);
164 wp_cache_set($this->getExistsCacheKey($name), false, self::CACHE_GROUP);
165
166 return $result !== false;
167 }
168
169 /**
170 * Invalidates all cached data for this table.
171 */
172 public function invalidateCache()
173 {
174 if (function_exists('wp_cache_flush_group')) {
175 wp_cache_flush_group(self::CACHE_GROUP);
176 } elseif (function_exists('wp_cache_flush')) {
177 wp_cache_flush();
178 }
179 }
180
181 /**
182 * Check if a setting exists in the settings table.
183 *
184 * @param string $name
185 * @return bool
186 */
187 public function has($name)
188 {
189 $existsCacheKey = $this->getExistsCacheKey($name);
190 $found = false;
191 $cachedExists = wp_cache_get($existsCacheKey, self::CACHE_GROUP, false, $found);
192
193 if ($found) {
194 return (bool)$cachedExists;
195 }
196
197 $this->ensureTable();
198
199 $tableName = $this->getFullTableName();
200 $escapedName = $this->database->escape($name);
201 $result = $this->database->query("SELECT 1 FROM `{$tableName}` WHERE setting_key = '{$escapedName}' LIMIT 1");
202
203 if ($result === false) {
204 return false;
205 }
206
207 $exists = $this->database->numRows($result) > 0;
208 wp_cache_set($existsCacheKey, $exists, self::CACHE_GROUP);
209
210 return $exists;
211 }
212
213 /**
214 * @param string $name
215 * @return string
216 */
217 private function getExistsCacheKey($name)
218 {
219 return $name . self::CACHE_EXISTS_KEY_SUFFIX;
220 }
221 }
222