Skip to content

WordPress PostTransient Class

PostTransient Class

Tiny class for WordPress plugins that stores transient data that's post specific in the post meta.

WordPress transients are great for storing data that's, well, transient. API call results, that sort of thing. But I've found that most of the time I'm storing transient data it's specific to a post. In the past what I've done is to include the post id in the transient name. And while that works, it just feels messy.

What this class does is store transient data in the post meta table along with the expiration so that it's invalidated when that expiration passes.

Installation

For now you'll need to include the file somewhere in your plugin or theme and either include or require it.

I'd like to make it a composer library at some point, but haven't found a good way to keep the dependency from overlapping if multiple plugins use this class.

The class definition is wrapped in a if class_exists so it should play nice if there are other plugins in your installation using the same class.

Usage

Setting a transient

use Aelora\WordPress\PostTransient;

PostTransient::set($postID, $key, $value, $expiration);
Parameter Description
$postID The id for the post to store transient. This will probably be $post->ID in most cases.
$key Identifier for the transient. Needs to be unique per post. _transient_ is appended to the key name before storing in the database.
$value The value to store. This can be either a simple piece of data or a structure like an array or object.
$expiration (optional) Number of seconds before the transient expires. If you leave this off it defaults to 24 hours.

Retrieving a transient

use Aelora\WordPress\PostTransient;

$x = PostTransient::get($postID, $key, $default);
Parameter Description
$postID The id for the post to retrieve the transient.
$key Identifier for the transient to retrieve.
$default Value to return if the transient has expired or is not found. Defaults to an empty string.

Deleting a transient

use Aelora\WordPress\PostTransient;

PostTransient::delete($postID, $key);
Parameter Description
$postID The id for the post to remove the transient
$key The key to remove

If the key does not exist this method silently does nothing.

PostTransient.php

<?php
// This software is copyright 2020 Ryan Nutt - https://www.nutt.net
//
// This software is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// See http://www.gnu.org/licenses/ for more information

namespace Aelora\WordPress;

if (!class_exists(__NAMESPACE__ . '\PostTransient')) {
    class PostTransient
    {

        public static function get($post_id, $key, $default = '')
        {
            $transient = get_post_meta($post_id, '_transient_' . $key, true);

            if (empty($transient['expiration'])) {
                return $default;
            } else if ($transient['expiration'] < time()) {
                self::delete($post_id, $key);
                return $default;
            }
            return $transient['value'];
        }

        public static function set($post_id, $key, $value, $expiration = 24 * HOUR_IN_SECONDS)
        {
            update_post_meta($post_id, '_transient_' . $key, [
                'expiration' => time() + $expiration,
                'value' => $value
            ]);
        }

        public static function delete($post_id, $key)
        {
            delete_post_meta($post_id, '_transient_' . $key);
        }
    }
}
Published inCoding

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *