Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1.4.0 #56

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ dfm_transient_meta_update_cb( $args ) {
}
```
## Retrieve the transient data
You can use the `dfm_get_transient()` function to retrieve the data for a transient. The first parameter passed to this function is the name of the transient you are trying to retrieve (should be the same name that you registered the transient with). The second parameter is the "modifier" for the transient. You can read more about modifiers below. The third parameter is the object_id (if you are using post_meta, term_meta, or user_meta as the cache type).
You can use the `dfm_get_transient()` function to retrieve the data for a transient. The first parameter passed to this function is the name of the transient you are trying to retrieve (should be the same name that you registered the transient with). The second parameter is the "modifier" for the transient. You can read more about modifiers below.
```php
$result = dfm_get_transient( 'sample_transient' );
```
```php
$post_transient = dfm_get_transient( 'sample_transient', '', $post_id );
$result = dfm_get_transient( 'sample_transient', '' );
```

## Arguments for registering a transient
Expand All @@ -81,7 +78,7 @@ $post_transient = dfm_get_transient( 'sample_transient', '', $post_id );
8. **soft_expiration** (bool) - Whether or not the data should soft expire or not. If this is set to true, it will check to see if the data has expired when retrieving it. If it is expired, it will spawn a background process to update the transient data. *Default: False*

## Transient Modifier
The Transient modifier (second parameter passed to the `dfm_get_transient` function) is used to store variations of the same type of transient. It will append the $modifier to the end of the transient key. This way you could store and retrieve different variations of the same transient that are mostly the same without registering a whole new transient. You can use the modifier to change the data saved to the transient by using it to alter your logic in your callback (the modifier is passed as the first argument to your callback function).
The transient modifier, (second parameter passed to the `dfm_get_transient` function) is used in a variety of different ways throughout this library. For a transient stored in metadata, it will be used as the object ID the transient is attached to. It will be used when using the `get_metadata()` and `save_metadata()` functions, so it is crucial that it is passed for transients stored in metadata. For global transients, it can be used to store variations of the same type of transient. It will append the `$modifier` to the end of the transient key. This way you could store and retrieve different variations of the same transient that are mostly the same without registering a whole new transient. You can use the modifier to change the data saved to the transient by using it to alter your logic in your callback (the modifier is passed as the only argument to your callback function).

## Debugging
To help with debugging, you can set a constant in your codebase called `DFM_TRANSIENTS_HOT_RELOAD` and set it to `true` to enable "hot reload" mode. This will essentially make it so that transient data will be regenerated every time it is called. This is handy if you are working on adding a transient, and want it to keep regenerating while you are working on it. This saves the need from manually deleting it from your database, or setting an extremely short timeout. **NOTE:** This constant should only ever be used on a development environment. Using this on production could cause serious performance issues depending on the data you are storing in your transients.
Expand Down
4 changes: 1 addition & 3 deletions dfm-transients.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Transient Control
* Plugin URI: https://github.com/dfmedia/DFM-Transients
* Description: Better control for transients
* Version: 1.3.1
* Version: 1.4.0
* Author: Ryan Kanner, Digital First Media
* License: GPL-3
*/
Expand All @@ -12,8 +12,6 @@
exit;
}

require_once( plugin_dir_path( __FILE__ ) . 'includes/class-dfm-transient-utils.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-dfm-async-nonce.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-dfm-async-handler.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-dfm-transient-hook.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-dfm-transient-scheduler.php' );
Expand Down
53 changes: 23 additions & 30 deletions includes/class-dfm-async-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DFM_Async_Handler {
* @var string
* @access private
*/
private $modifier;
private $modifiers;

/**
* Lock key for matching the update locking lock.
Expand All @@ -34,58 +34,51 @@ class DFM_Async_Handler {
/**
* DFM_Async_Handler constructor.
*
* @param string $transient Name of the transient
* @param string $modifier Unique modifier for the transient
* @param int $object_id ID of the object where the transient data is stored
* @param string $lock_key Key for matching the update locking
*
* @return void
* @param string $transient Name of the transient
* @param string|array $modifiers Unique modifier for the transient
* @param string $lock_key Key for matching the update locking
*/
function __construct( $transient, $modifier, $object_id = 0, $lock_key = '' ) {
function __construct( $transient, $modifiers, $lock_key = '' ) {

$this->transient_name = $transient;
$this->modifier = $modifier;
$this->object_id = $object_id;
$this->modifiers = $modifiers;
$this->lock_key = $lock_key;
// Spawn the event on shutdown so we are less likely to run into timeouts, or block other processes
add_action( 'shutdown', array( $this, 'spawn_event' ) );

if ( ! defined( 'DOING_CRON' ) && ! defined( 'REST_REQUEST' ) ) {
if ( 'shutdown' === current_action() ) {
$this->spawn_event();
} else {
add_action( 'shutdown', array( $this, 'spawn_event' ) );
}
}

}

/**
* Sends off a request to process and update the transient data asynchronously
*
* @return array|void|WP_Error
* @return array|WP_Error
* @access public
*/
public function spawn_event() {

// Prevents infinite loops if we are debugging transients in the init hook, or another hook that would run
// when handling the async post data
$is_async_action = ( isset( $_POST['async_action'] ) ) ? true : false;

if ( true === $is_async_action ) {
return;
if ( ! defined( 'DFM_TRANSIENTS_SECRET' ) ) {
return new \WP_Error( 'no-secret', __( 'You need to define the DFM_TRANSIENTS_SECRET constant in order to use this feature', 'dfm-transients' ) );
}

$nonce = new DFM_Async_Nonce( $this->transient_name );
$nonce = $nonce->create();

$request_args = array(
'timeout' => 0.01,
'blocking' => false, // don't wait for a response
'body' => array(
'transient_name' => $this->transient_name,
'modifier' => $this->modifier,
'object_id' => $this->object_id,
'action' => 'dfm_' . $this->transient_name,
'_nonce' => $nonce,
'async_action' => true,
'method' => 'PUT',
'body' => wp_json_encode( array(
'secret' => DFM_TRANSIENTS_SECRET,
'modifiers' => $this->modifiers,
'lock_key' => $this->lock_key,
),
) ),
);

$url = admin_url( 'admin-post.php' );
$url = get_rest_url( null, DFM_Transient_Scheduler::API_NAMESPACE . '/' . DFM_Transient_Scheduler::ENDPOINT_RUN . '/' . $this->transient_name );
return wp_safe_remote_post( $url, $request_args );

}
Expand Down
78 changes: 0 additions & 78 deletions includes/class-dfm-async-nonce.php

This file was deleted.

Loading