Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * WP API Transient Control Class ##
- *
- * @package WordPress
- * @subpackage 4Trees
- * @since 0.4
- */
- class FTFtransients {
- public $ftf_transients;
- public $ftf_transients_comments;
- public $decay = array(
- 'twelve_hours' => 43200,
- 'one_day' => 86400,
- 'two_days' => 172800,
- 'one_week' => 604800,
- );
- public function __construct(){
- $this->build();
- $this->decay = ftf_array_to_object( $this->decay ); // convert to object ##
- }
- public function __destruct(){
- // all transients ##
- $ftf_transients = get_option('ftf_transients'); // load saved options ##
- $ftf_transients = ( is_array($ftf_transients) ? array_merge( $ftf_transients, $this->ftf_transients ) : $this->ftf_transients ); // merge with new ##
- $ftf_transients = array_unique($ftf_transients); // delete duplicate entries ##
- #echo 'all: '; pr($ftf_transients); // test ##
- ftf_add_update_option( 'ftf_transients', $ftf_transients, '', 'yes' ); // update ##
- // comment transients
- $ftf_transients_comments = get_option('ftf_transients_comments'); // load saved options ##
- $ftf_transients_comments = ( is_array($ftf_transients_comments) ? array_merge( $ftf_transients_comments, $this->ftf_transients_comments ) : $this->ftf_transients_comments ); // merge
- $ftf_transients_comments = array_unique($ftf_transients_comments); // delete duplicate entries ##
- #echo 'comments: '; pr($ftf_transients_comments); // test ##
- ftf_add_update_option( 'ftf_transients_comments', $ftf_transients_comments, '', 'yes' ); // update ##
- }
- public function build( ){
- $this->ftf_transients = array();
- $this->ftf_transients_comments = array();
- }
- public function add( $key ){
- if ( $key ) {
- $this->ftf_transients[] = $key; // add to internal array ##
- #echo 'added: '.$key;
- }
- }
- public function comments( $key ) {
- if ( $key ) {
- $this->ftf_transients_comments[] = $key; // add to internal array ##
- }
- }
- public function get( $key, $type = 'array' ){
- if ( $key ) {
- $this->add( $key ); // add key ##
- $get = get_transient( $key ); // get transient data ##
- if ( $type === 'object' ) {
- $get = ftf_array_to_object( $get ); // convert to object ##
- }
- if ( $get === false ) {
- #echo '"'.$key.'" is empty: <br />';
- return false;
- } else {
- return $get;
- }
- }
- }
- public function __toString()
- {
- #echo "Using the toString method: ";
- return $this->get();
- }
- public function set( $key, $value, $decay = '', $comments = false ){
- if ( $key && $value ) {
- // work out decay ##
- $decay = ( $decay ? $decay : $this->decay->one_day );
- #echo 'set: '.$key.' // for: '.intval($decay);
- set_transient( $key, $value, intval($decay) );
- #$this->add( $key ); // add key ##
- if ( $comments ) {
- $this->comments( $key ); // add comment note ##
- }
- }
- }
- public function delete( $key = false ){
- if ( $key == 'all' ) { // clear all ##
- $all_ftf_transients = get_option('ftf_transients');
- #echo 'all transients:'; pr($all_ftf_transients);
- if ( $all_ftf_transients ) {
- foreach ( $all_ftf_transients as $key ) {
- #echo 'delete: '.$key.'<br />';
- delete_transient( $key );
- }
- }
- // delete option also ##
- delete_option('ftf_transients');
- } elseif ( $key == 'comments' ) { // clear all transients with comments ##
- $all_ftf_transients_comments = get_option('ftf_transients_comments');
- #echo 'all transients with comments:'; pr($all_ftf_transients_comments);
- if ( $all_ftf_transients_comments ) {
- foreach ( $all_ftf_transients_comments as $key ) {
- #echo 'delete: '.$key.'<br />';
- delete_transient( $key );
- }
- }
- // delete option also ##
- delete_option('ftf_transients_comments');
- } elseif ( $key ) { // remove one field ##
- #echo 'delete: '.$key;
- delete_transient( $key );
- }
- }
- }
- // instatiate global class object ##
- global $ftf_transients;
- $ftf_transients = new FTFtransients();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement