Advertisement
qlstudio

4 Trees - class FTFtransients

Nov 28th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.95 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * WP API Transient Control Class ##
  5.  *
  6.  * @package WordPress
  7.  * @subpackage 4Trees
  8.  * @since 0.4
  9.  */
  10.  
  11. class FTFtransients {
  12.    
  13.     public $ftf_transients;
  14.     public $ftf_transients_comments;
  15.     public $decay = array(
  16.                         'twelve_hours'  => 43200,
  17.                         'one_day'       => 86400,
  18.                         'two_days'      => 172800,
  19.                         'one_week'      => 604800,
  20.                     );
  21.    
  22.     public function __construct(){
  23.         $this->build();
  24.         $this->decay = ftf_array_to_object( $this->decay ); // convert to object ##
  25.     }
  26.    
  27.     public function __destruct(){
  28.        
  29.         // all transients ##
  30.         $ftf_transients = get_option('ftf_transients'); // load saved options ##
  31.         $ftf_transients = ( is_array($ftf_transients) ? array_merge( $ftf_transients, $this->ftf_transients ) : $this->ftf_transients ); // merge with new ##
  32.         $ftf_transients = array_unique($ftf_transients); // delete duplicate entries ##
  33.         #echo 'all: '; pr($ftf_transients); // test ##
  34.        ftf_add_update_option( 'ftf_transients', $ftf_transients, '', 'yes' ); // update ##
  35.        
  36.         // comment transients
  37.         $ftf_transients_comments = get_option('ftf_transients_comments'); // load saved options ##
  38.         $ftf_transients_comments = ( is_array($ftf_transients_comments) ? array_merge( $ftf_transients_comments, $this->ftf_transients_comments ) : $this->ftf_transients_comments ); // merge
  39.         $ftf_transients_comments = array_unique($ftf_transients_comments); // delete duplicate entries ##
  40.         #echo 'comments: '; pr($ftf_transients_comments); // test ##
  41.        ftf_add_update_option( 'ftf_transients_comments', $ftf_transients_comments, '', 'yes' ); // update ##
  42.        
  43.     }
  44.  
  45.     public function build( ){
  46.         $this->ftf_transients = array();
  47.         $this->ftf_transients_comments = array();
  48.     }
  49.  
  50.     public function add( $key ){
  51.         if ( $key ) {
  52.             $this->ftf_transients[] = $key; // add to internal array ##
  53.             #echo 'added: '.$key;
  54.        }
  55.     }
  56.  
  57.     public function comments( $key ) {
  58.         if ( $key ) {
  59.             $this->ftf_transients_comments[] = $key; // add to internal array ##
  60.         }
  61.     }
  62.    
  63.     public function get( $key, $type = 'array' ){
  64.         if ( $key ) {
  65.            
  66.             $this->add( $key ); // add key ##
  67.             $get = get_transient( $key ); // get transient data ##
  68.             if ( $type === 'object' ) {
  69.                 $get = ftf_array_to_object( $get ); // convert to object ##    
  70.             }
  71.             if ( $get === false ) {
  72.                
  73.                 #echo '"'.$key.'" is empty: <br />';
  74.                return false;
  75.                
  76.             } else {
  77.                
  78.                 return $get;
  79.                
  80.             }
  81.         }
  82.     }
  83.    
  84.     public function __toString()  
  85.     {  
  86.         #echo "Using the toString method: ";  
  87.        return $this->get();  
  88.     }  
  89.    
  90.     public function set( $key, $value, $decay = '', $comments = false ){
  91.         if ( $key && $value ) {
  92.             // work out decay ##
  93.             $decay = ( $decay ? $decay : $this->decay->one_day );
  94.             #echo 'set: '.$key.' // for: '.intval($decay);
  95.            set_transient( $key, $value, intval($decay) );
  96.             #$this->add( $key ); // add key ##
  97.            if ( $comments ) {
  98.                 $this->comments( $key ); // add comment note ##
  99.             }
  100.         }
  101.     }
  102.  
  103.     public function delete( $key = false ){
  104.        
  105.         if ( $key == 'all' ) { // clear all ##
  106.             $all_ftf_transients = get_option('ftf_transients');
  107.             #echo 'all transients:'; pr($all_ftf_transients);
  108.            if ( $all_ftf_transients ) {
  109.                 foreach ( $all_ftf_transients as $key ) {
  110.                     #echo 'delete: '.$key.'<br />';
  111.                    delete_transient( $key );
  112.                 }
  113.             }
  114.            
  115.             // delete option also ##
  116.             delete_option('ftf_transients');
  117.        
  118.         } elseif ( $key == 'comments' ) { // clear all transients with comments ##
  119.             $all_ftf_transients_comments = get_option('ftf_transients_comments');
  120.             #echo 'all transients with comments:'; pr($all_ftf_transients_comments);
  121.            if ( $all_ftf_transients_comments ) {
  122.                 foreach ( $all_ftf_transients_comments as $key ) {
  123.                     #echo 'delete: '.$key.'<br />';
  124.                    delete_transient( $key );
  125.                 }
  126.             }
  127.            
  128.             // delete option also ##
  129.             delete_option('ftf_transients_comments');
  130.            
  131.         } elseif ( $key ) { // remove one field ##
  132.             #echo 'delete: '.$key;
  133.            delete_transient( $key );
  134.         }
  135.        
  136.     }
  137.    
  138. }
  139.  
  140. // instatiate global class object ##
  141. global $ftf_transients;
  142. $ftf_transients = new FTFtransients();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement