PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.11
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.11
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / v2 / PageProfiler / Storage / Transients.php

Transients.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.11, at inc/v2/PageProfiler/Storage/Transients.php

94 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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