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