WordPress 4.7
[autoinstalls/wordpress.git] / wp-includes / class-wp-simplepie-file.php
1 <?php
2 /**
3  * Feed API: WP_SimplePie_File class
4  *
5  * @package WordPress
6  * @subpackage Feed
7  * @since 4.7.0
8  */
9
10 /**
11  * Core class for fetching remote files and reading local files with SimplePie.
12  *
13  * @since 2.8.0
14  *
15  * @see SimplePie_File
16  */
17 class WP_SimplePie_File extends SimplePie_File {
18
19         /**
20          * Constructor.
21          *
22          * @since 2.8.0
23          * @since 3.2.0 Updated to use a PHP5 constructor.
24          * @access public
25          *
26          * @param string       $url             Remote file URL.
27          * @param integer      $timeout         Optional. How long the connection should stay open in seconds.
28          *                                      Default 10.
29          * @param integer      $redirects       Optional. The number of allowed redirects. Default 5.
30          * @param string|array $headers         Optional. Array or string of headers to send with the request.
31          *                                      Default null.
32          * @param string       $useragent       Optional. User-agent value sent. Default null.
33          * @param boolean      $force_fsockopen Optional. Whether to force opening internet or unix domain socket
34          *                                      connection or not. Default false.
35          */
36         public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
37                 $this->url = $url;
38                 $this->timeout = $timeout;
39                 $this->redirects = $redirects;
40                 $this->headers = $headers;
41                 $this->useragent = $useragent;
42
43                 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
44
45                 if ( preg_match('/^http(s)?:\/\//i', $url) ) {
46                         $args = array(
47                                 'timeout' => $this->timeout,
48                                 'redirection' => $this->redirects,
49                         );
50
51                         if ( !empty($this->headers) )
52                                 $args['headers'] = $this->headers;
53
54                         if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
55                                 $args['user-agent'] = $this->useragent;
56
57                         $res = wp_safe_remote_request($url, $args);
58
59                         if ( is_wp_error($res) ) {
60                                 $this->error = 'WP HTTP Error: ' . $res->get_error_message();
61                                 $this->success = false;
62                         } else {
63                                 $this->headers = wp_remote_retrieve_headers( $res );
64                                 $this->body = wp_remote_retrieve_body( $res );
65                                 $this->status_code = wp_remote_retrieve_response_code( $res );
66                         }
67                 } else {
68                         $this->error = '';
69                         $this->success = false;
70                 }
71         }
72 }