WordPress 4.0
[autoinstalls/wordpress.git] / wp-includes / class-feed.php
1 <?php
2
3 if ( !class_exists('SimplePie') )
4         require_once( ABSPATH . WPINC . '/class-simplepie.php' );
5
6 class WP_Feed_Cache extends SimplePie_Cache {
7         /**
8          * Create a new SimplePie_Cache object
9          *
10          * @static
11          * @access public
12          */
13         public function create($location, $filename, $extension) {
14                 return new WP_Feed_Cache_Transient($location, $filename, $extension);
15         }
16 }
17
18 class WP_Feed_Cache_Transient {
19         public $name;
20         public $mod_name;
21         public $lifetime = 43200; //Default lifetime in cache of 12 hours
22
23         public function __construct($location, $filename, $extension) {
24                 $this->name = 'feed_' . $filename;
25                 $this->mod_name = 'feed_mod_' . $filename;
26
27                 $lifetime = $this->lifetime;
28                 /**
29                  * Filter the transient lifetime of the feed cache.
30                  *
31                  * @since 2.8.0
32                  *
33                  * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
34                  * @param string $filename Unique identifier for the cache object.
35                  */
36                 $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename);
37         }
38
39         public function save($data) {
40                 if ( is_a($data, 'SimplePie') )
41                         $data = $data->data;
42
43                 set_transient($this->name, $data, $this->lifetime);
44                 set_transient($this->mod_name, time(), $this->lifetime);
45                 return true;
46         }
47
48         public function load() {
49                 return get_transient($this->name);
50         }
51
52         public function mtime() {
53                 return get_transient($this->mod_name);
54         }
55
56         public function touch() {
57                 return set_transient($this->mod_name, time(), $this->lifetime);
58         }
59
60         public function unlink() {
61                 delete_transient($this->name);
62                 delete_transient($this->mod_name);
63                 return true;
64         }
65 }
66
67 class WP_SimplePie_File extends SimplePie_File {
68
69         public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
70                 $this->url = $url;
71                 $this->timeout = $timeout;
72                 $this->redirects = $redirects;
73                 $this->headers = $headers;
74                 $this->useragent = $useragent;
75
76                 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
77
78                 if ( preg_match('/^http(s)?:\/\//i', $url) ) {
79                         $args = array(
80                                 'timeout' => $this->timeout,
81                                 'redirection' => $this->redirects,
82                         );
83
84                         if ( !empty($this->headers) )
85                                 $args['headers'] = $this->headers;
86
87                         if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
88                                 $args['user-agent'] = $this->useragent;
89
90                         $res = wp_safe_remote_request($url, $args);
91
92                         if ( is_wp_error($res) ) {
93                                 $this->error = 'WP HTTP Error: ' . $res->get_error_message();
94                                 $this->success = false;
95                         } else {
96                                 $this->headers = wp_remote_retrieve_headers( $res );
97                                 $this->body = wp_remote_retrieve_body( $res );
98                                 $this->status_code = wp_remote_retrieve_response_code( $res );
99                         }
100                 } else {
101                         $this->error = '';
102                         $this->success = false;
103                 }
104         }
105 }
106
107 /**
108  * WordPress SimplePie Sanitization Class
109  *
110  * Extension of the SimplePie_Sanitize class to use KSES, because
111  * we cannot universally count on DOMDocument being available
112  *
113  * @package WordPress
114  * @since 3.5.0
115  */
116 class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
117         public function sanitize( $data, $type, $base = '' ) {
118                 $data = trim( $data );
119                 if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
120                         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)) {
121                                 $type |= SIMPLEPIE_CONSTRUCT_HTML;
122                         }
123                         else {
124                                 $type |= SIMPLEPIE_CONSTRUCT_TEXT;
125                         }
126                 }
127                 if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
128                         $data = base64_decode( $data );
129                 }
130                 if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
131                         $data = wp_kses_post( $data );
132                         if ( $this->output_encoding !== 'UTF-8' ) {
133                                 $data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
134                         }
135                         return $data;
136                 } else {
137                         return parent::sanitize( $data, $type, $base );
138                 }
139         }
140 }