dev-master
9999999-dev https://github.com/dimadin/wp-temporaryWP_Temporary
GPL GPL-2.0-or-later
by Milan Dinić
cache
WP_Temporary
WP_Temporary
is a helper class with a group of static methods that are used the same way as counterpart transient functions for storing data in the database until they expire. Basically, it's the same as when transient is stored in the database (with object cache disabled) so it isn't deleted until it expires., (*2)
Transients use object cache if it's available and only fallback to the database if it's not. Because of that, transients might disappear at any time before their expiration. WP_Temporary
solves this because data stored with it does not disappear until it expires., (*3)
Additionally, WP_Temporary
has a separate method that changes value of already set temporary data, but without changing expiration time of that data. This is different from transients that only allow setting of data that always changes expiration time so it start counting from the moment of setting., (*4)
WP_Temporary
has built-in garbage collector and WP-CLI command., (*5)
WP_Temporary
is available as Composer package that you can use in your project., (*6)
composer require dimadin/wp-temporary
Alternately, you can download file class-wp-temporary.php
and include it in your project., (*7)
Full code reference is available at http://api.milandinic.com/wp-temporary/, (*8)
Equivalent of set_transient()
., (*9)
Equivalent of get_transient()
., (*10)
Equivalent of delete_transient()
., (*11)
This method is identical to WP_Temporary::set()
with one difference: if temporary already exists, it will not change expiration time, just change its value (without using third parameter $expiration
); if it doesn't exist, it will fallback to WP_Temporary::set()
., (*12)
Equivalent of set_site_transient()
., (*13)
Equivalent of get_site_transient()
., (*14)
Equivalent of delete_site_transient()
., (*15)
This method is identical to WP_Temporary::set_site()
with one difference: if temporary already exists, it will not change expiration time, just change its value (without using third parameter $expiration
); if it doesn't exist, it will fallback to WP_Temporary::set_site()
., (*16)
This is garbage collector for WP_Temporary
. It cleans up database from all expired temporaries (whose expiration was more than one minute ago)., (*17)
Note that it isn't run by default, you need to either call it in your code with, (*18)
WP_Temporary::clean();
, (*19)
or hook it to some action. For example, this calls it once daily using WP Cron:, (*20)
add_action( 'wp_scheduled_delete', array( 'WP_Temporary', 'clean' ) );
, (*21)
This method that initializes WP-CLI command wp temporary
. It isn't run by default, you need to either call it in your code with, (*22)
WP_Temporary::init_wp_cli();
, (*23)
or hook it to some action. For example, this calls only when WP-CLI is initialized:, (*24)
add_action( 'cli_init', array( 'WP_Temporary', 'init_wp_cli' ) );
, (*25)
Note that if you are not using Composer for including WP_Temporary()
, you must manually include file /cli/Temporary_Command.php
., (*26)
WP_Temporary
implements the following commands:, (*27)
Adds, gets, updates, and deletes temporary data., (*28)
The temporary data uses the WordPress database to persist values between requests. On a single site installation, values are stored in the wp_options
table. On a multisite installation, values are stored in the wp_options
or the wp_sitemeta
table, depending on use of the --network
flag., (*29)
EXAMPLES, (*30)
# Set temporary. $ wp temporary set sample_key "test data" 3600 Success: Temporary added. # Update temporary. $ wp temporary update sample_key "test data" 3600 Success: Temporary updated. # Get temporary. $ wp temporary get sample_key test data # Get all temporaries. $ wp temporary get --all # Delete temporary. $ wp temporary delete sample_key Success: Temporary deleted. # Delete all temporaries. $ wp temporary delete --all Success: 14 temporaries deleted from the database.
Get a temporary value., (*31)
wp temporary get [<key>] [--format=<format>] [--network] [--all]
OPTIONS, (*32)
[<key>] Key for the temporary. [--format=<format>] Render output in a particular format. --- options: - json - yaml --- [--network] Get the value of a network|site temporary. [--all] Get all temporaries.
EXAMPLES, (*33)
# Get temporary. $ wp temporary get sample_key test data # Get temporary. $ wp temporary get random_key Warning: Temporary with key "random_key" is not set. # Get all temporaries. $ wp temporary get --all
Set a temporary value., (*34)
wp temporary set <key> <value> [<expiration>] [--network]
OPTIONS, (*35)
<key> Key for the temporary. <value> Value to be set for the temporary. [<expiration>] Time until expiration, in seconds. [--network] Set the value of a network|site temporary.
EXAMPLES, (*36)
$ wp temporary set sample_key "test data" 3600 Success: Temporary added.
Update a temporary value., (*37)
Change value of existing temporary without affecting expiration, or set new temporary with provided expiration if temporary doesn't exist., (*38)
wp temporary update <key> <value> [<expiration>] [--network]
OPTIONS, (*39)
<key> Key for the temporary. <value> Value to be set for the temporary. [<expiration>] Time until expiration, in seconds. [--network] Set the value of a network|site temporary.
EXAMPLES, (*40)
$ wp temporary update sample_key "test data" 3600 Success: Temporary updated.
Get a temporary value., (*41)
wp temporary delete [<key>] [--network] [--all]
OPTIONS, (*42)
[<key>] Key for the temporary. [--network] Delete the value of a network|site temporary. [--all] Delete all temporaries.
EXAMPLES, (*43)
# Delete temporary. $ wp temporary delete sample_key Success: Temporary deleted. # Delete all temporaries. $ wp temporary delete --all Success: 14 temporaries deleted from the database.
Delete all expired temporaries., (*44)
wp temporary clean
EXAMPLES, (*45)
$ wp temporary clean Success: Expired temporaries deleted from the database.
WP_Temporary
GPL GPL-2.0-or-later
cache