: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @copyright Copyright (c) 2023, Code Atlantic LLC
if ( ! defined( 'ABSPATH' ) ) {
* Initializes a temporary data storage engine used by core in various capacities.
class PUM_Utils_DataStorage {
* Retrieves stored data by key.
* Given a key, get the information from the database directly.
* @param string $key The stored option key.
* @param null|mixed $default Optional. A default value to retrieve should `$value` be empty.
* @return mixed|false The stored data, value of `$default` if not null, otherwise false.
public static function get( $key, $default = null ) {
$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
if ( empty( $value ) && ! is_null( $default ) ) {
return empty( $value ) ? false : maybe_unserialize( $value );
* Write some data based on key and value.
* @param string $key The option_name.
* @param mixed $value The value to store.
public static function write( $key, $value ) {
$value = maybe_serialize( $value );
'option_value' => $value,
$formats = self::get_data_formats( $value );
$wpdb->replace( $wpdb->options, $data, $formats );
* Derives the formats array based on the type of $value.
* @param mixed $value Value to store.
* @return array Formats array. First and last values will always be string ('%s').
public static function get_data_formats( $value ) {
switch ( gettype( $value ) ) {
$formats = [ '%s', '%d', '%s' ];
$formats = [ '%s', '%f', '%s' ];
$formats = [ '%s', '%s', '%s' ];
* Deletes a piece of stored data by key.
* @param string $key The stored option name to delete.
* @return int|false The number of rows deleted, or false on error.
public static function delete( $key ) {
return $wpdb->delete( $wpdb->options, [ 'option_name' => $key ] );
* Deletes all options matching a given RegEx pattern.
* @param string $pattern Pattern to match against option keys.
* @return int|false The number of rows deleted, or false on error.
public static function delete_by_match( $pattern ) {
// Double check to make sure the batch_id got included before proceeding.
if ( '^[0-9a-z\\_]+' !== $pattern && ! empty( $pattern ) ) {
$query = "DELETE FROM $wpdb->options WHERE option_name REGEXP %s";
$result = $wpdb->query( $wpdb->prepare( $query, $pattern ) );