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