3 if ( ! class_exists( 'SimplePie', false ) )
4 require_once( ABSPATH . WPINC . '/class-simplepie.php' );
7 * Core class used to implement a feed cache.
11 * @see SimplePie_Cache
13 class WP_Feed_Cache extends SimplePie_Cache {
16 * Creates a new SimplePie_Cache object.
21 * @param string $location URL location (scheme is used to determine handler).
22 * @param string $filename Unique identifier for cache object.
23 * @param string $extension 'spi' or 'spc'.
24 * @return WP_Feed_Cache_Transient Feed cache handler object that uses transients.
26 public function create($location, $filename, $extension) {
27 return new WP_Feed_Cache_Transient($location, $filename, $extension);
32 * Core class used to implement feed cache transients.
36 class WP_Feed_Cache_Transient {
39 * Holds the transient name.
48 * Holds the transient mod name.
57 * Holds the cache duration in seconds.
59 * Defaults to 43200 seconds (12 hours).
65 public $lifetime = 43200;
71 * @since 3.2.0 Updated to use a PHP5 constructor.
74 * @param string $location URL location (scheme is used to determine handler).
75 * @param string $filename Unique identifier for cache object.
76 * @param string $extension 'spi' or 'spc'.
78 public function __construct($location, $filename, $extension) {
79 $this->name = 'feed_' . $filename;
80 $this->mod_name = 'feed_mod_' . $filename;
82 $lifetime = $this->lifetime;
84 * Filters the transient lifetime of the feed cache.
88 * @param int $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
89 * @param string $filename Unique identifier for the cache object.
91 $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename);
100 * @param SimplePie $data Data to save.
101 * @return true Always true.
103 public function save($data) {
104 if ( $data instanceof SimplePie ) {
108 set_transient($this->name, $data, $this->lifetime);
109 set_transient($this->mod_name, time(), $this->lifetime);
114 * Gets the transient.
119 * @return mixed Transient value.
121 public function load() {
122 return get_transient($this->name);
126 * Gets mod transient.
131 * @return mixed Transient value.
133 public function mtime() {
134 return get_transient($this->mod_name);
138 * Sets mod transient.
143 * @return bool False if value was not set and true if value was set.
145 public function touch() {
146 return set_transient($this->mod_name, time(), $this->lifetime);
150 * Deletes transients.
155 * @return true Always true.
157 public function unlink() {
158 delete_transient($this->name);
159 delete_transient($this->mod_name);
165 * Core class for fetching remote files and reading local files with SimplePie.
169 * @see SimplePie_File
171 class WP_SimplePie_File extends SimplePie_File {
177 * @since 3.2.0 Updated to use a PHP5 constructor.
180 * @param string $url Remote file URL.
181 * @param integer $timeout Optional. How long the connection should stay open in seconds.
183 * @param integer $redirects Optional. The number of allowed redirects. Default 5.
184 * @param string|array $headers Optional. Array or string of headers to send with the request.
186 * @param string $useragent Optional. User-agent value sent. Default null.
187 * @param boolean $force_fsockopen Optional. Whether to force opening internet or unix domain socket
188 * connection or not. Default false.
190 public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
192 $this->timeout = $timeout;
193 $this->redirects = $redirects;
194 $this->headers = $headers;
195 $this->useragent = $useragent;
197 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
199 if ( preg_match('/^http(s)?:\/\//i', $url) ) {
201 'timeout' => $this->timeout,
202 'redirection' => $this->redirects,
205 if ( !empty($this->headers) )
206 $args['headers'] = $this->headers;
208 if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
209 $args['user-agent'] = $this->useragent;
211 $res = wp_safe_remote_request($url, $args);
213 if ( is_wp_error($res) ) {
214 $this->error = 'WP HTTP Error: ' . $res->get_error_message();
215 $this->success = false;
217 $this->headers = wp_remote_retrieve_headers( $res );
218 $this->body = wp_remote_retrieve_body( $res );
219 $this->status_code = wp_remote_retrieve_response_code( $res );
223 $this->success = false;
229 * Core class used to implement SimpliePie feed sanitization.
231 * Extends the SimplePie_Sanitize class to use KSES, because
232 * we cannot universally count on DOMDocument being available.
236 * @see SimplePie_Sanitize
238 class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
241 * WordPress SimplePie sanitization using KSES.
243 * Sanitizes the incoming data, to ensure that it matches the type of data expected, using KSES.
248 * @param mixed $data The data that needs to be sanitized.
249 * @param integer $type The type of data that it's supposed to be.
250 * @param string $base Optional. The `xml:base` value to use when converting relative
251 * URLs to absolute ones. Default empty.
252 * @return mixed Sanitized data.
254 public function sanitize( $data, $type, $base = '' ) {
255 $data = trim( $data );
256 if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
257 if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
258 $type |= SIMPLEPIE_CONSTRUCT_HTML;
261 $type |= SIMPLEPIE_CONSTRUCT_TEXT;
264 if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
265 $data = base64_decode( $data );
267 if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
268 $data = wp_kses_post( $data );
269 if ( $this->output_encoding !== 'UTF-8' ) {
270 $data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
274 return parent::sanitize( $data, $type, $base );