DatabaseStorage.php
47 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Analyst\Storage; |
| 4 | |
| 5 | use Analyst\Contracts\StorageContract; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 8 | |
| 9 | /** |
| 10 | * Class DatabaseStorage |
| 11 | * |
| 12 | * Persists key-value data using the WordPress wp_options table. |
| 13 | */ |
| 14 | class DatabaseStorage implements StorageContract |
| 15 | { |
| 16 | /** |
| 17 | * @param string $key The wp_options option name. |
| 18 | * @param mixed $default |
| 19 | * @return mixed |
| 20 | */ |
| 21 | public function get($key, $default = null) |
| 22 | { |
| 23 | $value = get_option($key, null); |
| 24 | |
| 25 | return $value !== null ? $value : $default; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param string $key The wp_options option name. |
| 30 | * @param mixed $value |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function put($key, $value) |
| 34 | { |
| 35 | return update_option($key, $value); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param string $key The wp_options option name. |
| 40 | * @return bool |
| 41 | */ |
| 42 | public function delete($key) |
| 43 | { |
| 44 | return delete_option($key); |
| 45 | } |
| 46 | } |
| 47 |