Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active May 24, 2019 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/f6f57f45405f609fcd14905c54b30f25 to your computer and use it in GitHub Desktop.
Save tommcfarlin/f6f57f45405f609fcd14905c54b30f25 to your computer and use it in GitHub Desktop.
[PHP] DataStore Interface for the Transients API
<?php
/**
* A wrapper for the Transients API. Because using `setcookie` only works in the `init`
* hook of WordPress, then we need a way to simulate it. That's what the purpose of this class
* does.
*
* It will use the ID of the user who is currently logged in (or the PHP's get_current_user value if
* they are not logged in).
*
* @author Tom McFarlin <tom@tommcfarlin.com>
*/
interface DataStore {
/**
* Determines if a transient value already exists identified with the incoming key.
*
* @param string $key The key used to identify the key in the database.
*
* @return bool True if a transient exists; otherwise, false.
*/
public function has( string $key );
/**
* Saves the specified value for 24 hours.
*
* @param string $key The key used to identify the key in the database.
* @param string $value The value to save for 24 hours.
*/
public function set( string $key, string $value );
/**
* Retrieves the transient value from the database.
*
* @param string $key The key used to identify the key in the database.
*
* @return string The value associated with the incoming transient.
*/
public function get( string $key );
/**
* Deletes the transient data.
*
* @param string $key The key used to identify the key in the database.
*/
public function delete( string $key );
}
<?php
/**
* A wrapper for the Transients API. Because using `setcookie` only works in the `init`
* hook of WordPress, then we need a way to simulate it. That's what the purpose of this class
* does.
*
* It will use the ID of the user who is currently logged in (or the PHP's get_current_user value if
* they are not logged in).
*
* @author Tom McFarlin <tom@tommcfarlin.com>
*/
interface DataStore {
/**
* Determines if a transient value already exists identified with the incoming key.
*
* @param string $key The key used to identify the key in the database.
*
* @return bool True if a transient exists; otherwise, false.
*/
public function has(string $key);
/**
* Saves the specified value for 24 hours.
*
* @param string $key The key used to identify the key in the database.
* @param string $value The value to save for 24 hours.
*/
public function set(string $key, string $value);
/**
* Retrieves the transient value from the database.
*
* @param string $key The key used to identify the key in the database.
*
* @return string The value associated with the incoming transient.
*/
public function get(string $key);
/**
* Deletes the transient data.
*
* @param string $key The key used to identify the key in the database.
*/
public function delete(string $key);
/**
* Creates a unique key using a combination of the incoming database key and the user's ID.
*
* @param string $key The key used to store transient data.
*
* @return string The unique key used to store values associated with the data store.
*
*/
private function get_unique_key(string $key);
}
<?php
/**
* Initializes the User ID properly this is what's going to be appended to incoming
* keys when saving or checking for values.
*
* Use PHP's native get_current_user function then fall back to WP's
* and use that to help generate the key.
*/
public function __construct()
{
$this->user_id = get_current_user();
if (get_current_user_id()) {
$this->user_id = get_current_user_id();
}
}
<?php
/**
* Creates a unique key using a combination of the incoming database key and the user's ID.
*
* @param string $key The key used to store transient data.
* @return string The unique key used to store values associated with the data store.
*/
private function getUniqueKey(string $key)
{
return $key .= '-' . $this->user_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment