| 1 |
<?php |
| 2 |
|
| 3 |
namespace OptimoleWP\PageProfiler\Storage; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Transients |
| 7 |
* |
| 8 |
* Handles storage and retrieval of page profiler data using WordPress transients. |
| 9 |
* Transients provide a way to temporarily store cached data with an expiration time. |
| 10 |
* |
| 11 |
* @package OptimoleWP\PageProfiler\Storage |
| 12 |
*/ |
| 13 |
class Transients extends Base { |
| 14 |
/** |
| 15 |
* Prefix for all transient keys to avoid conflicts with other plugins. |
| 16 |
*/ |
| 17 |
const PREFIX = '_oprof_'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Default expiration time for transients (7 days in seconds). |
| 21 |
*/ |
| 22 |
const EXPIRATION_TIME = 7 * DAY_IN_SECONDS; |
| 23 |
|
| 24 |
/** |
| 25 |
* The actual expiration time to use, which can be filtered. |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
private $expiration_time; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* Sets up the expiration time, allowing it to be modified via WordPress filters. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->expiration_time = apply_filters( 'optml_page_profiler_transient_expiration', self::EXPIRATION_TIME ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generates the full transient key with prefix. |
| 42 |
* |
| 43 |
* @param string $key The base key to prefix. |
| 44 |
* @return string The prefixed key. |
| 45 |
*/ |
| 46 |
private function get_key( string $key ) { |
| 47 |
return self::PREFIX . $key; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Stores data in a transient. |
| 52 |
* |
| 53 |
* @param string $key The key to store the data under. |
| 54 |
* @param array $data The data to store. |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function store( string $key, array $data ) { |
| 58 |
set_transient( $this->get_key( $key ), $data, $this->expiration_time ); |
| 59 |
if ( OPTML_DEBUG ) { |
| 60 |
do_action( 'optml_log', 'stored: ' . $this->get_key( $key ) . '|' . print_r( $data, true ) ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Retrieves data from a transient. |
| 66 |
* |
| 67 |
* @param string $key The key to retrieve data for. |
| 68 |
* @return mixed The stored data or false if the transient doesn't exist or has expired. |
| 69 |
*/ |
| 70 |
public function get( string $key ) { |
| 71 |
return get_transient( $this->get_key( $key ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Deletes a specific transient. |
| 76 |
* |
| 77 |
* @param string $key The key of the transient to delete. |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function delete( string $key ) { |
| 81 |
delete_transient( $this->get_key( $key ) ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Deletes all transients created by this class. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
* @todo Implement this method to clear all transients with the defined prefix. |
| 89 |
*/ |
| 90 |
public function delete_all() { |
| 91 |
// Not implemented for now. |
| 92 |
} |
| 93 |
} |
| 94 |
|