arraypress / wp-storage-utils
The three storage operations WordPress lacks: cache-or-compute, delete-by-prefix, and reading a stored value as the type it is meant to be.
Requires
- php: >=8.3
Requires (Dev)
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4
README
The three storage jobs WordPress leaves you to write: cache-or-compute, delete by prefix, and reading a stored value back as the type you wanted.
What it does
get_transient() returns false both when nothing is cached and when the
cached value is false, so the cache-or-compute pattern gets written by hand,
subtly wrong, in every plugin. remember() wraps the stored value so the two
are distinguishable.
Deleting your own transients when a plugin uninstalls has no API at all, so
everyone writes a DELETE ... LIKE — usually forgetting that the timeout rows
do not carry the prefix, and leaving half of them behind forever.
And anything out of the options table is a string, so it needs casting before it is useful.
Features
- Cache-or-compute that can store
false,nullor0without confusion - Delete every option, transient or meta row sharing a prefix, on uninstall
- Remove both halves of a transient, so nothing is orphaned in
wp_options - Cast a stored value to bool, int, float, array or a list of ids
- Escapes
LIKEwildcards, so an underscore in your prefix stays literal
Installation
composer require arraypress/wp-storage-utils
Quick start
use ArrayPress\StorageUtils\Cache; use ArrayPress\StorageUtils\Cast; use ArrayPress\StorageUtils\Cleanup; // Computed once an hour, and it can legitimately return false. $is_eligible = Cache::remember( 'sc_eligible_' . $user_id, function () use ( $user_id ) { return expensive_eligibility_check( $user_id ); } ); // Options come back as strings. $limit = Cast::int( get_option( 'sc_download_limit' ), 5 ); // On uninstall, take everything with you. Cleanup::options( 'sugarcart_' ); Cleanup::transients( 'sugarcart_' ); Cleanup::meta( 'sugarcart_', 'post' );
Requirements
- PHP 8.3 or later
- WordPress 7.1 or later
License
GPL-2.0-or-later