Cache
1 day ago
DBPermissions.php
1 day ago
DataEncoder.php
1 day ago
DatabaseOptions.php
1 day ago
Env.php
1 day ago
Escape.php
1 day ago
Glob.php
1 day ago
Hooks.php
1 day ago
Math.php
1 day ago
PluginInfo.php
1 day ago
Sanitize.php
1 day ago
ServerVars.php
1 day ago
SlashMode.php
1 day ago
Strings.php
1 day ago
Times.php
1 day ago
Urls.php
1 day ago
Version.php
1 day ago
WpDefaultDirectories.php
1 day ago
DatabaseOptions.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | use Throwable; |
| 6 | use WPStaging\Framework\Adapter\Database; |
| 7 | use WPStaging\Framework\Traits\SerializeTrait; |
| 8 | use wpdb; |
| 9 | |
| 10 | use function WPStaging\functions\debug_log; |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | class DatabaseOptions |
| 22 | { |
| 23 | use SerializeTrait; |
| 24 | |
| 25 | |
| 26 | private $db; |
| 27 | |
| 28 | |
| 29 | private $optionsTable; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | public function __construct(Database $database) |
| 35 | { |
| 36 | $this->db = $database->getWpdb(); |
| 37 | $this->optionsTable = $database->getPrefix() . 'options'; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | public function getOption(string $optionName, $defaultValue = false) |
| 46 | { |
| 47 | if (!$this->optionExists($optionName)) { |
| 48 | return $defaultValue; |
| 49 | } |
| 50 | |
| 51 | $sql = $this->db->prepare( |
| 52 | "SELECT option_value FROM `{$this->optionsTable}` WHERE option_name = %s LIMIT 1", |
| 53 | $optionName |
| 54 | ); |
| 55 | |
| 56 | $result = $this->db->get_var($sql); |
| 57 | |
| 58 | if ($result === null) { |
| 59 | return $defaultValue; |
| 60 | } |
| 61 | |
| 62 | $rejected = false; |
| 63 | $value = $this->safeMaybeUnserialize($result, [], $rejected); |
| 64 | |
| 65 | if ($rejected) { |
| 66 | debug_log(sprintf('Refused to unserialize database option "%s". Falling back to the default value.', $optionName)); |
| 67 | |
| 68 | return $defaultValue; |
| 69 | } |
| 70 | |
| 71 | return $value; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | public function updateOption(string $optionName, $optionValue, bool $autoload = true): bool |
| 81 | { |
| 82 | try { |
| 83 | $serializedValue = serialize($optionValue); |
| 84 | $autoloadValue = $autoload ? 'yes' : 'no'; |
| 85 | if ($this->optionExists($optionName)) { |
| 86 | $sql = $this->db->prepare( |
| 87 | "UPDATE `{$this->optionsTable}` |
| 88 | SET option_value = %s, autoload = %s |
| 89 | WHERE option_name = %s", |
| 90 | $serializedValue, |
| 91 | $autoloadValue, |
| 92 | $optionName |
| 93 | ); |
| 94 | } else { |
| 95 | $sql = $this->db->prepare( |
| 96 | "INSERT INTO `{$this->optionsTable}` (option_name, option_value, autoload) |
| 97 | VALUES (%s, %s, %s)", |
| 98 | $optionName, |
| 99 | $serializedValue, |
| 100 | $autoloadValue |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | $result = $this->db->query($sql); |
| 105 | if ($result === false) { |
| 106 | debug_log(sprintf('Failed to update database option "%s". DB Error: %s', $optionName, $this->getLastDbError())); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | $this->clearOptionCache($optionName); |
| 112 | |
| 113 | return true; |
| 114 | } catch (Throwable $ex) { |
| 115 | debug_log(sprintf('Exception while updating database option "%s": %s', $optionName, $ex->getMessage())); |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | public function deleteOption(string $optionName): bool |
| 126 | { |
| 127 | if (!$this->optionExists($optionName)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | $sql = $this->db->prepare( |
| 132 | "DELETE FROM `{$this->optionsTable}` WHERE option_name = %s LIMIT 1", |
| 133 | $optionName |
| 134 | ); |
| 135 | |
| 136 | $result = $this->db->query($sql); |
| 137 | if ($result === false) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | $this->clearOptionCache($optionName); |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | private function optionExists(string $optionName): bool |
| 151 | { |
| 152 | $sql = $this->db->prepare( |
| 153 | "SELECT 1 FROM `{$this->optionsTable}` WHERE option_name = %s LIMIT 1", |
| 154 | $optionName |
| 155 | ); |
| 156 | |
| 157 | return (bool) $this->db->get_var($sql); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | private function getLastDbError(): string |
| 164 | { |
| 165 | return !empty($this->db->last_error) ? $this->db->last_error : 'Unknown database error'; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | private function clearOptionCache(string $optionName) |
| 173 | { |
| 174 | if (!function_exists('wp_cache_delete')) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | wp_cache_delete($optionName, 'options'); |
| 179 | wp_cache_delete('alloptions', 'options'); |
| 180 | wp_cache_delete('notoptions', 'options'); |
| 181 | } |
| 182 | } |
| 183 |