| 1 |
<?php |
| 2 |
/** |
| 3 |
* Uses the wp_options table to store data. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Storage\Drivers; |
| 11 |
|
| 12 |
use Closure; |
| 13 |
use SolidWP\Performance\Storage\Contracts\Storage; |
| 14 |
use SolidWP\Performance\Storage\Exceptions\InvalidKeyException; |
| 15 |
use SolidWP\Performance\Storage\Traits\With_Key_Formatter; |
| 16 |
|
| 17 |
/** |
| 18 |
* Uses the wp_options table to store data. |
| 19 |
* |
| 20 |
* @package SolidWP\Performance |
| 21 |
*/ |
| 22 |
class Option_Storage implements Storage { |
| 23 |
|
| 24 |
use With_Key_Formatter; |
| 25 |
|
| 26 |
/** |
| 27 |
* Put a value in storage. |
| 28 |
* |
| 29 |
* @param string|int|float|mixed[]|object $key The storage key. Accepts any variable that can be json encoded. |
| 30 |
* @param mixed $value The value to store. |
| 31 |
* @param int $expire The storage lifespan in seconds. |
| 32 |
* |
| 33 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 34 |
*/ |
| 35 |
public function set( $key, $value, int $expire = 0 ): bool { |
| 36 |
$data = [ |
| 37 |
'value' => $value, |
| 38 |
'expiration' => $expire > 0 ? time() + $expire : 0, |
| 39 |
]; |
| 40 |
|
| 41 |
return update_option( $this->key( $key ), $data, false ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get a value from storage. |
| 46 |
* |
| 47 |
* @param string|int|float|mixed[]|object $key The storage key. Accepts any variable that can be json encoded. |
| 48 |
* |
| 49 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 50 |
* |
| 51 |
* @return null|mixed Returns null if we can't find the storage value. |
| 52 |
*/ |
| 53 |
public function get( $key ) { |
| 54 |
$data = (array) get_option( $this->key( $key ), [] ); |
| 55 |
|
| 56 |
if ( isset( $data['expiration'] ) && $data['expiration'] > 0 && $data['expiration'] < time() ) { |
| 57 |
$this->delete( $key ); |
| 58 |
|
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
return $data['value'] ?? null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Delete a value from storage. |
| 67 |
* |
| 68 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 69 |
* |
| 70 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 71 |
*/ |
| 72 |
public function delete( $key ): bool { |
| 73 |
return delete_option( $this->key( $key ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get an item from storage, or execute the given Closure and store the result. |
| 78 |
* |
| 79 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 80 |
* @param Closure $callback The callback used to generate and store the value. |
| 81 |
* @param int $expire The storage lifespan in seconds. |
| 82 |
* |
| 83 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 84 |
* |
| 85 |
* @return mixed The storage value. |
| 86 |
*/ |
| 87 |
public function remember( $key, Closure $callback, int $expire = 0 ) { |
| 88 |
$value = $this->get( $key ); |
| 89 |
|
| 90 |
if ( ! is_null( $value ) ) { |
| 91 |
return $value; |
| 92 |
} |
| 93 |
|
| 94 |
$value = $callback(); |
| 95 |
|
| 96 |
$this->set( $key, $value, $expire ); |
| 97 |
|
| 98 |
return $value; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Retrieve an item from storage and delete it. |
| 103 |
* |
| 104 |
* @param string|int|float|mixed[]|object $key The storage key. |
| 105 |
* |
| 106 |
* @throws InvalidKeyException If passed an invalid storage key. |
| 107 |
*/ |
| 108 |
public function pull( $key ) { |
| 109 |
$value = $this->get( $key ); |
| 110 |
|
| 111 |
$this->delete( $key ); |
| 112 |
|
| 113 |
return $value; |
| 114 |
} |
| 115 |
} |
| 116 |
|