ExpireOption
in package
WordPress itself has a so-called "Transient API" which allows you to save custom results of e. g. database queries in the database `wp_options` table. While developing the plugin Real Media Library we experienced a lot of issues with Transients as there are a lot of performance and optimization plugins which clears the transients.
Note: You should not use this for huge query results as this option gets autoloaded if non-site-wide.
Tags
Table of Contents
Constants
- EXPIRE_SUFFIX = '-expire'
- TRANSIENT_MIGRATION_DISABLED = 0
- TRANSIENT_MIGRATION_NON_SITE_WIDE = 1
- TRANSIENT_MIGRATION_SITE_WIDE = 2
Properties
- $expiration : mixed
- $keepValueAfterExpire : mixed
- $name : mixed
- $siteWide : mixed
- $transientMigration : mixed
Methods
- __construct() : mixed
- C'tor.
- delete() : mixed
- Delete option.
- enableAutoload() : mixed
- Persist the option to database if not yet available so it gets autoloaded via `wp_load_alloptions()`.
- enableTransientMigration() : mixed
- If you are migrating from a transient, use this method to allow reading the value via `get` from the transient value, when no option was found.
- get() : mixed
- Get option value. Returns `$fallback` if not found or expired.
- getExpiration() : mixed
- Get expiration in seconds.
- getExpire() : mixed
- Get the expiration timestamp. Returns `0` if the value is not yet persisted to database.
- getExpireName() : mixed
- Get "expire" option name.
- getName() : mixed
- Get option name.
- getTransientMigration() : mixed
- Get value of `transientMigration`.
- isSiteWide() : mixed
- Get if option is site-wide.
- set() : mixed
- Set option value.
- setKeepValueAfterExpire() : mixed
- Setter.
- handleMigration() : mixed
- Handle the migration and return the correct value.
Constants
EXPIRE_SUFFIX
public
mixed
EXPIRE_SUFFIX
= '-expire'
TRANSIENT_MIGRATION_DISABLED
public
mixed
TRANSIENT_MIGRATION_DISABLED
= 0
TRANSIENT_MIGRATION_NON_SITE_WIDE
public
mixed
TRANSIENT_MIGRATION_NON_SITE_WIDE
= 1
TRANSIENT_MIGRATION_SITE_WIDE
public
mixed
TRANSIENT_MIGRATION_SITE_WIDE
= 2
Properties
$expiration
private
mixed
$expiration
$keepValueAfterExpire
private
mixed
$keepValueAfterExpire
$name
private
mixed
$name
$siteWide
private
mixed
$siteWide
$transientMigration
private
mixed
$transientMigration
Methods
__construct()
C'tor.
public
__construct(string $name, bool $siteWide, int $expiration[, bool $keepValueAfterExpire = false ]) : mixed
Parameters
- $name : string
-
Your option name
- $siteWide : bool
-
Use e. g.
get_site_transient
instead ofget_transient
- $expiration : int
-
Time until expiration in seconds
- $keepValueAfterExpire : bool = false
-
If you use
get(false, $respectExpire = false)
it is recommend to keep the value in the database after expiration
Tags
delete()
Delete option.
public
delete() : mixed
Tags
enableAutoload()
Persist the option to database if not yet available so it gets autoloaded via `wp_load_alloptions()`.
public
enableAutoload() : mixed
enableTransientMigration()
If you are migrating from a transient, use this method to allow reading the value via `get` from the transient value, when no option was found.
public
enableTransientMigration(int $type) : mixed
Parameters
- $type : int
-
See class constants
TRANSIENT_MIGRATION*
Tags
get()
Get option value. Returns `$fallback` if not found or expired.
public
get([mixed $fallback = false ][, bool $respectExpire = true ]) : mixed
Parameters
- $fallback : mixed = false
- $respectExpire : bool = true
-
If
false
, you will always get the value from database (useful if you need to merge from previous data)
Tags
getExpiration()
Get expiration in seconds.
public
getExpiration() : mixed
Tags
getExpire()
Get the expiration timestamp. Returns `0` if the value is not yet persisted to database.
public
getExpire() : mixed
getExpireName()
Get "expire" option name.
public
getExpireName() : mixed
Tags
getName()
Get option name.
public
getName() : mixed
Tags
getTransientMigration()
Get value of `transientMigration`.
public
getTransientMigration() : mixed
Tags
isSiteWide()
Get if option is site-wide.
public
isSiteWide() : mixed
Tags
set()
Set option value.
public
set(mixed $value[, int $expiration = null ]) : mixed
Parameters
- $value : mixed
-
Do not pass
null
asnull
is automatically converted to an empty string inwp_options
- $expiration : int = null
Tags
setKeepValueAfterExpire()
Setter.
public
setKeepValueAfterExpire(bool $state) : mixed
Parameters
- $state : bool
Tags
handleMigration()
Handle the migration and return the correct value.
protected
handleMigration(mixed $fallback) : mixed
Parameters
- $fallback : mixed