[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-feed-cache-transient.php (source)

   1  <?php
   2  /**
   3   * Feed API: WP_Feed_Cache_Transient class
   4   *
   5   * @package WordPress
   6   * @subpackage Feed
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class used to implement feed cache transients.
  12   *
  13   * @since 2.8.0
  14   * @since 6.7.0 Now properly implements the SimplePie\Cache\Base interface.
  15   */
  16  #[AllowDynamicProperties]
  17  class WP_Feed_Cache_Transient implements SimplePie\Cache\Base {
  18  
  19      /**
  20       * Holds the transient name.
  21       *
  22       * @since 2.8.0
  23       * @var string
  24       */
  25      public $name;
  26  
  27      /**
  28       * Holds the transient mod name.
  29       *
  30       * @since 2.8.0
  31       * @var string
  32       */
  33      public $mod_name;
  34  
  35      /**
  36       * Holds the cache duration in seconds.
  37       *
  38       * Defaults to 43200 seconds (12 hours).
  39       *
  40       * @since 2.8.0
  41       * @var int
  42       */
  43      public $lifetime = 43200;
  44  
  45      /**
  46       * Creates a new (transient) cache object.
  47       *
  48       * @since 2.8.0
  49       * @since 3.2.0 Updated to use a PHP5 constructor.
  50       * @since 6.7.0 Parameter names have been updated to be in line with the `SimplePie\Cache\Base` interface.
  51       *
  52       * @param string                           $location URL location (scheme is used to determine handler).
  53       * @param string                           $name     Unique identifier for cache object.
  54       * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type     Either `TYPE_FEED` ('spc') for SimplePie data,
  55       *                                                   or `TYPE_IMAGE` ('spi') for image data.
  56       */
  57  	public function __construct( $location, $name, $type ) {
  58          $this->name     = 'feed_' . $name;
  59          $this->mod_name = 'feed_mod_' . $name;
  60  
  61          $lifetime = $this->lifetime;
  62          /**
  63           * Filters the transient lifetime of the feed cache.
  64           *
  65           * @since 2.8.0
  66           *
  67           * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
  68           * @param string $name     Unique identifier for the cache object.
  69           */
  70          $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $name );
  71      }
  72  
  73      /**
  74       * Saves data to the transient.
  75       *
  76       * @since 2.8.0
  77       *
  78       * @param array|SimplePie\SimplePie $data Data to save. If passed a SimplePie object,
  79       *                                        only cache the `$data` property.
  80       * @return true Always true.
  81       */
  82  	public function save( $data ) {
  83          if ( $data instanceof SimplePie\SimplePie ) {
  84              $data = $data->data;
  85          }
  86  
  87          set_transient( $this->name, $data, $this->lifetime );
  88          set_transient( $this->mod_name, time(), $this->lifetime );
  89          return true;
  90      }
  91  
  92      /**
  93       * Retrieves the data saved in the transient.
  94       *
  95       * @since 2.8.0
  96       *
  97       * @return array Data for `SimplePie::$data`.
  98       */
  99  	public function load() {
 100          return get_transient( $this->name );
 101      }
 102  
 103      /**
 104       * Gets mod transient.
 105       *
 106       * @since 2.8.0
 107       *
 108       * @return int Timestamp.
 109       */
 110  	public function mtime() {
 111          return get_transient( $this->mod_name );
 112      }
 113  
 114      /**
 115       * Sets mod transient.
 116       *
 117       * @since 2.8.0
 118       *
 119       * @return bool False if value was not set and true if value was set.
 120       */
 121  	public function touch() {
 122          return set_transient( $this->mod_name, time(), $this->lifetime );
 123      }
 124  
 125      /**
 126       * Deletes transients.
 127       *
 128       * @since 2.8.0
 129       *
 130       * @return true Always true.
 131       */
 132  	public function unlink() {
 133          delete_transient( $this->name );
 134          delete_transient( $this->mod_name );
 135          return true;
 136      }
 137  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref