Last active
August 29, 2015 14:17
-
-
Save aaroneaton/e7810a5c6e457ab38c6a to your computer and use it in GitHub Desktop.
Storing ArrayObject as WordPress transient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** ..... */ | |
public function all() { | |
if ( false === ( $collection = get_transient( '_optin_cache_' . $this->user->ID . '_all' ) ) ) { | |
$optins = get_posts( | |
array( | |
'post_type' => 'optin_saas', | |
'author' => $this->user->ID, | |
'no_found_rows' => true, | |
'cache_results' => false, | |
'nopaging' => true | |
) | |
); | |
if ( empty( $optins ) ) { | |
throw new \Exception( 'Optin forms were not found for this account' ); | |
} | |
// Create the collection object. Extends ArrayObject. | |
$collection = new OptinCollection(); | |
// Create the optin object and add to the collection | |
foreach ( $optins as $optin ) { | |
$o = new Optin( $this->user ); | |
$collection->append( $o->build_from_wp_post( $optin ) ); | |
} | |
// Save the collection as a transient | |
set_transient( '_optin_cache_' . $this->user->ID . '_all', $collection, DAY_IN_SECONDS ); | |
} | |
return $collection; | |
} | |
/** ....... */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment