Skip to content

Instantly share code, notes, and snippets.

@isotrope
Last active March 5, 2020 19:11
Show Gist options
  • Save isotrope/57f542be71fc011e1fa986827e7143e9 to your computer and use it in GitHub Desktop.
Save isotrope/57f542be71fc011e1fa986827e7143e9 to your computer and use it in GitHub Desktop.
A toe-dip into transients for Thierry
<?php
class Get_Words {
public static $transient_name = 'awesome_words_compiled';
public static $option_name = 'awesome_words_compiled';
public static $repeater_name_left = 'awesome_words_repeater_left';
public static $repeater_name_right = 'awesome_words_repeater_right';
public static $repeater_subfield = 'words';
public static $cpt = 'my_cpt';
public function __construct() {
}
public function init() {
add_action( 'save_post', [ $this, 'delete_transient' ] );
}
/*
* Easy to then use anywhere
*
* $words = Get_Words::get_words();
*
* Could call directly in your templates
* Can use it with wp_localize_script() to pass it to your JS
* ...
*
*/
public static function get_words() {
$words = get_transient( self::$transient_name );
// if the transient is ready, it's a single call to the DB! and
// we can move along.
// If not, then we need to regenerate it
if ( empty( $words ) ) {
$words = self::compile_words();
}
return $words;
}
/*
* Since this one is a bit longer, let's keep the get_words easier to read by splitting up
* the "compiling" routine
*/
public static function compile_words() {
$words = [
'left' => [],
'right' => [],
];
// whatever logic you need to compile it all into a nice array that you'll re-use or pass to JS
if ( function_exists( 'have_rows' ) && have_rows( self::$repeater_name_left ) ) {
while ( have_rows( self::$repeater_name_left ) ) {
the_row();
$words['left'][] = get_sub_field( self::$repeater_subfield );
}
}
// droite
if ( function_exists( 'have_rows' ) && have_rows( self::$repeater_name_right ) ) {
while ( have_rows( self::$repeater_name_right ) ) {
the_row();
$words['right'][] = get_sub_field( self::$repeater_subfield );
}
}
set_transient( self::$transient_name, $words );
return $words;
}
/*
* transients are, basically, options.
* They live in the options table
* The third param of set_transient() is expiry
* ..so, think of them like potentially-expiring options
*
* That being said, you have to think through the cases where
* your data becomes invalid... "WHEN" should they be refreshed?
*
* Most of the time, it's based on "Someone saved a post, our data might be invalid"
* ...so it's more likekely tied to a hook, probably
*
* For this example, I tied it to the save_post hook (in the init method above)
*/
public function delete_transient( $post_id, $post, $update ) {
// Only set for post_type = our cpt
if ( self::$cpt !== $post->post_type ) {
return;
}
// oh. it IS the right saving screen for our CPT! Let's delete the transient.
// We won't actually recreate it just yet... it might never be needed (although, doubtful)
// We'll let the "get_words()" method call the compilation method, if needed
delete_transient( self::$transient_name );
}
}
$get_words = new Get_Words();
$get_words->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment