Acf
1 day ago
Admin
1 day ago
Ajax
1 day ago
ApplyFilter
1 day ago
Asset
1 day ago
Capabilities
1 day ago
Check
1 day ago
Collection
1 day ago
Column
1 day ago
ColumnFactories
1 day ago
ColumnFactory
1 day ago
ColumnIterator
1 day ago
ColumnRepository
1 day ago
ColumnSize
1 day ago
DI
1 day ago
DefaultColumnHandler
1 day ago
Deprecated
1 day ago
Entity
1 day ago
Exception
1 day ago
Expression
1 day ago
Form
1 day ago
Formatter
1 day ago
Helper
1 day ago
Integration
1 day ago
ListScreenRepository
1 day ago
ListTable
1 day ago
Message
1 day ago
Meta
1 day ago
Nonce
1 day ago
Notice
1 day ago
Plugin
1 day ago
Preferences
1 day ago
Promo
1 day ago
Request
1 day ago
RequestHandler
1 day ago
Response
1 day ago
Sanitize
1 day ago
Service
1 day ago
Setting
1 day ago
Settings
1 day ago
Storage
1 day ago
Table
1 day ago
TableIdsFactory
1 day ago
TableScreen
1 day ago
TableScreenFactory
1 day ago
ThirdParty
1 day ago
Type
1 day ago
Value
1 day ago
View
1 day ago
Acf.php
1 day ago
AdminColumns.php
1 day ago
ArrayIterator.php
1 day ago
Capabilities.php
1 day ago
Collection.php
1 day ago
CollectionFormatter.php
1 day ago
Column.php
1 day ago
ColumnCollection.php
1 day ago
ColumnFactoryCollectionFactory.php
1 day ago
ColumnFactoryDefinitionCollection.php
1 day ago
ColumnGroups.php
1 day ago
ColumnIterator.php
1 day ago
ColumnNamesTrait.php
1 day ago
ColumnRepository.php
1 day ago
ColumnTypeRepository.php
1 day ago
Config.php
1 day ago
DateFormats.php
1 day ago
DefaultColumnHandler.php
1 day ago
Expirable.php
1 day ago
Formatter.php
1 day ago
FormatterCollection.php
1 day ago
Helper.php
1 day ago
ListScreen.php
1 day ago
ListScreenCollection.php
1 day ago
ListScreenRepository.php
1 day ago
ListScreenRepositoryWritable.php
1 day ago
ListTable.php
1 day ago
ListTableFactory.php
1 day ago
Loader.php
1 day ago
Message.php
1 day ago
MetaType.php
1 day ago
Middleware.php
1 day ago
OpCacheInvalidateTrait.php
1 day ago
PluginActionLinks.php
1 day ago
PluginActionUpgrade.php
1 day ago
PostType.php
1 day ago
PostTypeRepository.php
1 day ago
Registerable.php
1 day ago
Registry.php
1 day ago
Renderable.php
1 day ago
Request.php
1 day ago
RequestAjaxHandler.php
1 day ago
RequestAjaxHandlers.php
1 day ago
RequestAjaxParser.php
1 day ago
RequestHandler.php
1 day ago
RequestHandlerFactory.php
1 day ago
Screen.php
1 day ago
Services.php
1 day ago
TableIdsFactory.php
1 day ago
TableScreen.php
1 day ago
TableScreenFactory.php
1 day ago
Taxonomy.php
1 day ago
Transient.php
1 day ago
TypedArrayIterator.php
1 day ago
View.php
1 day ago
WooCommerce.php
1 day ago
WpListTableFactory.php
1 day ago
Transient.php
60 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC; |
| 6 | |
| 7 | use AC\Storage\KeyValue; |
| 8 | |
| 9 | final class Transient implements Expirable |
| 10 | { |
| 11 | protected KeyValue $option; |
| 12 | |
| 13 | protected Storage\Timestamp $timestamp; |
| 14 | |
| 15 | public function __construct(string $key, bool $network_only = false) |
| 16 | { |
| 17 | $option_factory = $network_only |
| 18 | ? new Storage\SiteOptionFactory() |
| 19 | : new Storage\OptionFactory(); |
| 20 | |
| 21 | $this->option = $option_factory->create($key); |
| 22 | $this->timestamp = new Storage\Timestamp( |
| 23 | $option_factory->create($key . '_timestamp') |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | public function is_expired(?int $timestamp = null): bool |
| 28 | { |
| 29 | return $this->timestamp->is_expired($timestamp); |
| 30 | } |
| 31 | |
| 32 | public function has_expiration_time(): bool |
| 33 | { |
| 34 | return false !== $this->timestamp->get(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return mixed |
| 39 | */ |
| 40 | public function get() |
| 41 | { |
| 42 | return $this->option->get(); |
| 43 | } |
| 44 | |
| 45 | public function delete(): void |
| 46 | { |
| 47 | $this->option->delete(); |
| 48 | $this->timestamp->delete(); |
| 49 | } |
| 50 | |
| 51 | public function save($data, int $expiration): void |
| 52 | { |
| 53 | // Always store timestamp before option data. |
| 54 | $this->timestamp->save(time() + $expiration); |
| 55 | |
| 56 | $this->option->save($data); |
| 57 | } |
| 58 | |
| 59 | } |
| 60 |