• Stars
    star
    338
  • Rank 124,609 (Top 3 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created about 13 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TLC Transients

A WordPress transients interface with support for soft-expiration (use old content until new content is available), background updating of the transients (without having to wait for a cron job), and a chainable syntax that allows for one liners.

License

TLC Transients is licensed under the GPL, version 2.0 or any later version. See LICENSE.

Examples

In this simple example, we're defining a feed-fetching callback, and then using tlc_transient with a chain to point to that callback and use it, all in one line. Note that since we haven't used background_only(), the initial load of this will cause the page to pause.

<?php
// Define your callback (other examples use this)
function my_callback() {
	return wp_remote_retrieve_body(
		wp_remote_get( 'http://example.com/feed.xml', array( 'timeout' => 30 ) )
	);
}

// Grab that feed
echo tlc_transient( 'example-feed' )
	->updates_with( 'my_callback' )
	->expires_in( 300 )
	->get();
?>

This time, we'll set background_only() in the chain. This means that if there has been a hard cache flush, or this is the first-ever request, it will return false. So your code will have to be written to gracefully degrade if the feed isn't yet available. This, of course, triggers a background update. And once it is available, it will start returning the content.

<?php
echo tlc_transient( 'example-feed' )
	->updates_with( 'my_callback' )
	->expires_in( 300 )
	->background_only()
	->get();
?>

We don't have to chain, of course.

<?php
$t = tlc_transient( 'example-feed' );
if ( true ) {
	$t->updates_with( 'my_callback' );
} else {
	$t->updates_with( 'some_other_callback' );
}

$t->expires_in( 300 );
echo $t->get();
?>

We can even pass parameters to our callback.

<?php
// Define your callback
function my_callback_with_param( $param ) {
	return str_replace(
		'foo',
		$param,
		wp_remote_retrieve_body( wp_remote_get( 'http://example.com/feed.xml', array( 'timeout' => 30 ) ) ),
	);
}

// Grab that feed
echo tlc_transient( 'example-feed' )
	->updates_with( 'my_callback_with_param', array( 'bar' ) )
	->expires_in( 300 )
	->background_only()
	->get();
?>

Notes

Context

It should be noted that when a callback runs asynchronously, you are not in control of context. The context that existed when you registered the callback has no bearing on the context when the callback is actually run. So if there is anything that you're assuming in your callback function (whether a certain user being current, a certain post having been queried, etc), you must rewrite your calllback function so that these assumptions are not made, and instead pass in this context in the form of parameters, which your callback then uses to recreate your desired context.

More Repositories

1

WordPress-Skeleton

Basic layout of a WordPress Git repository. I use this as a base when creating a new repo.
PHP
1,836
star
2

WP-Stack

A toolkit for creating professional WordPress deployments
PHP
1,113
star
3

WordPress

This is legacy. Use github.com/WordPress/WordPress/ instead!
PHP
229
star
4

WordPress-Plugin-Directory-Slurper

Slurps down every stable version of every plugin in the WordPress plugin directory.
PHP
163
star
5

clerk-sveltekit

Clerk adapter for SvelteKit
Svelte
137
star
6

encute

WordPress plugin for fluent management of scripts and styles.
PHP
107
star
7

page-links-to

#WordPressPlugin: Lets you make a WordPress page (or other content type) link to an external URL of your choosing, instead of its WordPress URL.
PHP
106
star
8

wp-help

#WordPressPlugin: Site operators can create detailed, hierarchical documentation for the site's authors, editors, and contributors, viewable in the WordPress admin
PHP
104
star
9

cache-buddy

#WordPressPlugin: makes more WordPress pages cacheable
PHP
78
star
10

BetterBible

The Bible has some issues. Let's make it better.
72
star
11

Showdown

Example WordPress plugin created for WordCamp San Francisco 2014, to show off wp.Backbone.View
PHP
52
star
12

WordPress-Plugin-Readme-Parser

PHP
42
star
13

gifdrop

#WordPressPlugin GIF repository plugin for WordPress
PHP
36
star
14

Localdev

Simple Ruby command line utility to point domains to your localhost
Ruby
34
star
15

markdown-on-save

#WordPressPlugin: allows you to author posts in Markdown, but stores rendered HTML in post_content so you can deactivate with impunity.
PHP
29
star
16

slug-control

#WordPressPlugin: Helps you craft amazing post URL slugs, for that hand-crafted URL feel.
PHP
27
star
17

i-make-plugins

#WordPressPlugin: For plugin authors. Showcase your plugins on your WordPress site. You only update your readme.txt files!
PHP
19
star
18

WordPress-Jetpack-Valet-Driver

A Laravel Valet driver that routes missing images to your public site, via Jetpack's Site Accelerator (formerly "Photon") CDN.
PHP
18
star
19

wpsql

DEPRECATED. Use WP-CLI instead! Old: Bash script that, given a wp-config.php file, opens a MySQL session to the DB server described within
17
star
20

scheduled-post-guardian

#WordPressPlugin: Watches over scheduled posts, so that no funny business interferes with their mission
PHP
16
star
21

WordPress-Plugin-Installer

PHP
16
star
22

nice-search

#WordPressPlugin: Makes search URLs pretty
PHP
15
star
23

login-logo

#WordPressPlugin: Customize the logo on the WP login screen by simply dropping a file named login-logo.png into your WP content directory. CSS is automatic!
PHP
15
star
24

wp-feature-better-passwords

PHP
11
star
25

WPPlugin

Command line tool for updating and managing WordPress plugins
Ruby
11
star
26

gist-indexer

#INACTIVE: Use swdyh/gisty instead!
PHP
10
star
27

workerbee

A friendly tool for composing Cloudflare Workers
TypeScript
9
star
28

cmdy

Your friendly command manager
Shell
7
star
29

BlockStub

PHP
6
star
30

BashGato

Control Elgato lights with a Bash script
Shell
6
star
31

categorized

#WordPressPlugin: Unchecks the default post category when you select a non-default category
PHP
5
star
32

stabletweet

JavaScript
5
star
33

hotfix

Provides unofficial fixes for selected WordPress bugs, so you don't have to wait for the next WordPress core release.
PHP
5
star
34

Alfred-Silent-Mention

Alfred 4 Workflow to silently type @usernames, domains.com, or #hashtags without them being linked or parsed
3
star
35

sveltekit-playwright-fetch-mock

Tools for mocking SSR fetch() requests in SvelteKit for use in Playwright
TypeScript
2
star
36

WordPress-Skeleton-Updater

PHP
2
star
37

switch-controller

PHP
2
star
38

p3-neutraliser

WordPress plugin which prevent's Pipdig's P3 plugin from updating or requesting malicious URLs
PHP
2
star
39

grunt-wp-simple-plugin

Simple WordPress Plugin Template
CoffeeScript
2
star
40

dieulot.github.io

instantclick.io
JavaScript
1
star
41

grunt-puphpet-site

Initialize a PuPHPet site with grunt-init
Ruby
1
star
42

Wherewithal

Given constraints, parses a string of conditions into a valid MySQL WHERE clause
PHP
1
star
43

BigBrain

A familiar game to help you, uh, master your... mind.
Svelte
1
star
44

prefsync

Syncs macOS preferences
Shell
1
star