WordPress 4.3
[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         /**
40          * @access public
41          */
42         public function save($data) {
43                 if ( $data instanceof SimplePie ) {
44                         $data = $data->data;
45                 }
46
47                 set_transient($this->name, $data, $this->lifetime);
48                 set_transient($this->mod_name, time(), $this->lifetime);
49                 return true;
50         }
51
52         /**
53          * @access public
54          */
55         public function load() {
56                 return get_transient($this->name);
57         }
58
59         /**
60          * @access public
61          */
62         public function mtime() {
63                 return get_transient($this->mod_name);
64         }
65
66         /**
67          * @access public
68          */
69         public function touch() {
70                 return set_transient($this->mod_name, time(), $this->lifetime);
71         }
72
73         /**
74          * @access public
75          */
76         public function unlink() {
77                 delete_transient($this->name);
78                 delete_transient($this->mod_name);
79                 return true;
80         }
81 }
82
83 class WP_SimplePie_File extends SimplePie_File {
84
85         public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
86                 $this->url = $url;
87                 $this->timeout = $timeout;
88                 $this->redirects = $redirects;
89                 $this->headers = $headers;
90                 $this->useragent = $useragent;
91
92                 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
93
94                 if ( preg_match('/^http(s)?:\/\//i', $url) ) {
95                         $args = array(
96                                 'timeout' => $this->timeout,
97                                 'redirection' => $this->redirects,
98                         );
99
100                         if ( !empty($this->headers) )
101                                 $args['headers'] = $this->headers;
102
103                         if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
104                                 $args['user-agent'] = $this->useragent;
105
106                         $res = wp_safe_remote_request($url, $args);
107
108                         if ( is_wp_error($res) ) {
109                                 $this->error = 'WP HTTP Error: ' . $res->get_error_message();
110                                 $this->success = false;
111                         } else {
112                                 $this->headers = wp_remote_retrieve_headers( $res );
113                                 $this->body = wp_remote_retrieve_body( $res );
114                                 $this->status_code = wp_remote_retrieve_response_code( $res );
115                         }
116                 } else {
117                         $this->error = '';
118                         $this->success = false;
119                 }
120         }
121 }
122
123 /**
124  * WordPress SimplePie Sanitization Class
125  *
126  * Extension of the SimplePie_Sanitize class to use KSES, because
127  * we cannot universally count on DOMDocument being available
128  *
129  * @package WordPress
130  * @since 3.5.0
131  */
132 class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
133         public function sanitize( $data, $type, $base = '' ) {
134                 $data = trim( $data );
135                 if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
136                         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)) {
137                                 $type |= SIMPLEPIE_CONSTRUCT_HTML;
138                         }
139                         else {
140                                 $type |= SIMPLEPIE_CONSTRUCT_TEXT;
141                         }
142                 }
143                 if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
144                         $data = base64_decode( $data );
145                 }
146                 if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
147                         $data = wp_kses_post( $data );
148                         if ( $this->output_encoding !== 'UTF-8' ) {
149                                 $data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
150                         }
151                         return $data;
152                 } else {
153                         return parent::sanitize( $data, $type, $base );
154                 }
155         }
156 }