| 1 |
<?php |
| 2 |
/** |
| 3 |
* FeedWordPie_Cache class. Provides customized feed caching for FeedWordPress. |
| 4 |
* This is derived (currently, just copied and combined) from WordPress Core |
| 5 |
* wp-includes/class-wp-feed-cache.php ("Feed API: WP_Feed_Cache_Transient class") |
| 6 |
* and from wp-includes/SimplePie/Cache.php ("SimplePie_Cache"), pursuant to the |
| 7 |
* terms of the GPL. |
| 8 |
* |
| 9 |
* The wrapper class WP_Feed_Cache was deprecated in WordPress 5.6, but its intended |
| 10 |
* replacement, WP_Feed_Cache_Transient, is currently NOT a subclass of SimplePie_Cache |
| 11 |
* which causes problems registering it as a cache class in some versions of SimplePie. |
| 12 |
* Solution: For now, let's copy the class over under a new name, but this time, inherit |
| 13 |
* from SimplePie_Cache. (I am doing it this way because I might want to make some real |
| 14 |
* changs to the implementation of caching, in order to better support typical FeedWordPress |
| 15 |
* use cases. In the meantime, this should at least stop the PHP warnings and the failure |
| 16 |
* to correctly cache feed contents that users are encountering with WordPress 5.6.) |
| 17 |
* |
| 18 |
* @version 2021.0118 |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* Core class used to implement feed cache transients. |
| 23 |
* |
| 24 |
* @since 2.8.0 |
| 25 |
*/ |
| 26 |
class FeedWordPie_Cache extends SimplePie_Cache { |
| 27 |
|
| 28 |
/** |
| 29 |
* Holds the transient name. |
| 30 |
* |
| 31 |
* @since 2.8.0 |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $name; |
| 35 |
|
| 36 |
/** |
| 37 |
* Holds the transient mod name. |
| 38 |
* |
| 39 |
* @since 2.8.0 |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $mod_name; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds the cache duration in seconds. |
| 46 |
* |
| 47 |
* Defaults to 43200 seconds (12 hours). |
| 48 |
* |
| 49 |
* @since 2.8.0 |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
public $lifetime = 43200; |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor. |
| 56 |
* |
| 57 |
* @since 2.8.0 |
| 58 |
* @since 3.2.0 Updated to use a PHP5 constructor. |
| 59 |
* |
| 60 |
* @param string $location URL location (scheme is used to determine handler). |
| 61 |
* @param string $filename Unique identifier for cache object. |
| 62 |
* @param string $extension 'spi' or 'spc'. |
| 63 |
*/ |
| 64 |
public function __construct( $location, $filename, $extension ) { |
| 65 |
$this->name = 'feed_' . $filename; |
| 66 |
$this->mod_name = 'feed_mod_' . $filename; |
| 67 |
|
| 68 |
$lifetime = $this->lifetime; |
| 69 |
/** |
| 70 |
* Filters the transient lifetime of the feed cache. |
| 71 |
* |
| 72 |
* @since 2.8.0 |
| 73 |
* |
| 74 |
* @param int $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours). |
| 75 |
* @param string $filename Unique identifier for the cache object. |
| 76 |
*/ |
| 77 |
$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Sets the transient. |
| 82 |
* |
| 83 |
* @since 2.8.0 |
| 84 |
* |
| 85 |
* @param SimplePie $data Data to save. |
| 86 |
* @return true Always true. |
| 87 |
*/ |
| 88 |
public function save( $data ) { |
| 89 |
if ( $data instanceof SimplePie ) { |
| 90 |
$data = $data->data; |
| 91 |
} |
| 92 |
|
| 93 |
set_transient( $this->name, $data, $this->lifetime ); |
| 94 |
set_transient( $this->mod_name, time(), $this->lifetime ); |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Gets the transient. |
| 100 |
* |
| 101 |
* @since 2.8.0 |
| 102 |
* |
| 103 |
* @return mixed Transient value. |
| 104 |
*/ |
| 105 |
public function load() { |
| 106 |
return get_transient( $this->name ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Gets mod transient. |
| 111 |
* |
| 112 |
* @since 2.8.0 |
| 113 |
* |
| 114 |
* @return mixed Transient value. |
| 115 |
*/ |
| 116 |
public function mtime() { |
| 117 |
return get_transient( $this->mod_name ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Sets mod transient. |
| 122 |
* |
| 123 |
* @since 2.8.0 |
| 124 |
* |
| 125 |
* @return bool False if value was not set and true if value was set. |
| 126 |
*/ |
| 127 |
public function touch() { |
| 128 |
return set_transient( $this->mod_name, time(), $this->lifetime ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Deletes transients. |
| 133 |
* |
| 134 |
* @since 2.8.0 |
| 135 |
* |
| 136 |
* @return true Always true. |
| 137 |
*/ |
| 138 |
public function unlink() { |
| 139 |
delete_transient( $this->name ); |
| 140 |
delete_transient( $this->mod_name ); |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Create a new SimplePie_Cache object |
| 146 |
* |
| 147 |
* @param string $location URL location (scheme is used to determine handler) |
| 148 |
* @param string $filename Unique identifier for cache object |
| 149 |
* @param string $extension 'spi' or 'spc' |
| 150 |
* @return SimplePie_Cache_Base Type of object depends on scheme of `$location` |
| 151 |
*/ |
| 152 |
public static function get_handler($location, $filename, $extension) |
| 153 |
{ |
| 154 |
|
| 155 |
$type = explode(':', $location, 2); |
| 156 |
$type = $type[0]; |
| 157 |
if (!empty(self::$handlers[$type])) |
| 158 |
{ |
| 159 |
$class = self::$handlers[$type]; |
| 160 |
return $class($location, $filename, $extension); |
| 161 |
} |
| 162 |
|
| 163 |
return new FeedWordPie_Cache($location, $filename, $extension); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Create a new SimplePie_Cache object |
| 168 |
* |
| 169 |
* @deprecated Use {@see get_handler} instead |
| 170 |
*/ |
| 171 |
public function create($location, $filename, $extension) |
| 172 |
{ |
| 173 |
trigger_error('Cache::create() has been replaced with Cache::get_handler(). Switch to the registry system to use this.', E_USER_DEPRECATED); |
| 174 |
return self::get_handler($location, $filename, $extension); |
| 175 |
} |
| 176 |
|
| 177 |
} |