wp/wp-includes/class-wp-feed-cache-transient.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
     9 
     9 
    10 /**
    10 /**
    11  * Core class used to implement feed cache transients.
    11  * Core class used to implement feed cache transients.
    12  *
    12  *
    13  * @since 2.8.0
    13  * @since 2.8.0
       
    14  * @since 6.7.0 Now properly implements the SimplePie\Cache\Base interface.
    14  */
    15  */
    15 #[AllowDynamicProperties]
    16 #[AllowDynamicProperties]
    16 class WP_Feed_Cache_Transient {
    17 class WP_Feed_Cache_Transient implements SimplePie\Cache\Base {
    17 
    18 
    18 	/**
    19 	/**
    19 	 * Holds the transient name.
    20 	 * Holds the transient name.
    20 	 *
    21 	 *
    21 	 * @since 2.8.0
    22 	 * @since 2.8.0
    40 	 * @var int
    41 	 * @var int
    41 	 */
    42 	 */
    42 	public $lifetime = 43200;
    43 	public $lifetime = 43200;
    43 
    44 
    44 	/**
    45 	/**
    45 	 * Constructor.
    46 	 * Creates a new (transient) cache object.
    46 	 *
    47 	 *
    47 	 * @since 2.8.0
    48 	 * @since 2.8.0
    48 	 * @since 3.2.0 Updated to use a PHP5 constructor.
    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.
    49 	 *
    51 	 *
    50 	 * @param string $location  URL location (scheme is used to determine handler).
    52 	 * @param string                           $location URL location (scheme is used to determine handler).
    51 	 * @param string $filename  Unique identifier for cache object.
    53 	 * @param string                           $name     Unique identifier for cache object.
    52 	 * @param string $extension 'spi' or 'spc'.
    54 	 * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type     Either `TYPE_FEED` ('spc') for SimplePie data,
       
    55 	 *                                                   or `TYPE_IMAGE` ('spi') for image data.
    53 	 */
    56 	 */
    54 	public function __construct( $location, $filename, $extension ) {
    57 	public function __construct( $location, $name, $type ) {
    55 		$this->name     = 'feed_' . $filename;
    58 		$this->name     = 'feed_' . $name;
    56 		$this->mod_name = 'feed_mod_' . $filename;
    59 		$this->mod_name = 'feed_mod_' . $name;
    57 
    60 
    58 		$lifetime = $this->lifetime;
    61 		$lifetime = $this->lifetime;
    59 		/**
    62 		/**
    60 		 * Filters the transient lifetime of the feed cache.
    63 		 * Filters the transient lifetime of the feed cache.
    61 		 *
    64 		 *
    62 		 * @since 2.8.0
    65 		 * @since 2.8.0
    63 		 *
    66 		 *
    64 		 * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
    67 		 * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
    65 		 * @param string $filename Unique identifier for the cache object.
    68 		 * @param string $name     Unique identifier for the cache object.
    66 		 */
    69 		 */
    67 		$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename );
    70 		$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $name );
    68 	}
    71 	}
    69 
    72 
    70 	/**
    73 	/**
    71 	 * Sets the transient.
    74 	 * Saves data to the transient.
    72 	 *
    75 	 *
    73 	 * @since 2.8.0
    76 	 * @since 2.8.0
    74 	 *
    77 	 *
    75 	 * @param SimplePie $data Data to save.
    78 	 * @param array|SimplePie\SimplePie $data Data to save. If passed a SimplePie object,
       
    79 	 *                                        only cache the `$data` property.
    76 	 * @return true Always true.
    80 	 * @return true Always true.
    77 	 */
    81 	 */
    78 	public function save( $data ) {
    82 	public function save( $data ) {
    79 		if ( $data instanceof SimplePie ) {
    83 		if ( $data instanceof SimplePie\SimplePie ) {
    80 			$data = $data->data;
    84 			$data = $data->data;
    81 		}
    85 		}
    82 
    86 
    83 		set_transient( $this->name, $data, $this->lifetime );
    87 		set_transient( $this->name, $data, $this->lifetime );
    84 		set_transient( $this->mod_name, time(), $this->lifetime );
    88 		set_transient( $this->mod_name, time(), $this->lifetime );
    85 		return true;
    89 		return true;
    86 	}
    90 	}
    87 
    91 
    88 	/**
    92 	/**
    89 	 * Gets the transient.
    93 	 * Retrieves the data saved in the transient.
    90 	 *
    94 	 *
    91 	 * @since 2.8.0
    95 	 * @since 2.8.0
    92 	 *
    96 	 *
    93 	 * @return mixed Transient value.
    97 	 * @return array Data for `SimplePie::$data`.
    94 	 */
    98 	 */
    95 	public function load() {
    99 	public function load() {
    96 		return get_transient( $this->name );
   100 		return get_transient( $this->name );
    97 	}
   101 	}
    98 
   102 
    99 	/**
   103 	/**
   100 	 * Gets mod transient.
   104 	 * Gets mod transient.
   101 	 *
   105 	 *
   102 	 * @since 2.8.0
   106 	 * @since 2.8.0
   103 	 *
   107 	 *
   104 	 * @return mixed Transient value.
   108 	 * @return int Timestamp.
   105 	 */
   109 	 */
   106 	public function mtime() {
   110 	public function mtime() {
   107 		return get_transient( $this->mod_name );
   111 		return get_transient( $this->mod_name );
   108 	}
   112 	}
   109 
   113