PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-includes/class-wp-simplepie-file.php

https://bitbucket.org/skyarch-iijima/wordpress
PHP | 71 lines | 33 code | 8 blank | 30 comment | 7 complexity | 78d130d682823e6f0c8aecf5aa323fe2 MD5 | raw file
 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 */
17class 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	 *
25	 * @param string       $url             Remote file URL.
26	 * @param integer      $timeout         Optional. How long the connection should stay open in seconds.
27	 *                                      Default 10.
28	 * @param integer      $redirects       Optional. The number of allowed redirects. Default 5.
29	 * @param string|array $headers         Optional. Array or string of headers to send with the request.
30	 *                                      Default null.
31	 * @param string       $useragent       Optional. User-agent value sent. Default null.
32	 * @param boolean      $force_fsockopen Optional. Whether to force opening internet or unix domain socket
33	 *                                      connection or not. Default false.
34	 */
35	public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
36		$this->url = $url;
37		$this->timeout = $timeout;
38		$this->redirects = $redirects;
39		$this->headers = $headers;
40		$this->useragent = $useragent;
41
42		$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
43
44		if ( preg_match('/^http(s)?:\/\//i', $url) ) {
45			$args = array(
46				'timeout' => $this->timeout,
47				'redirection' => $this->redirects,
48			);
49
50			if ( !empty($this->headers) )
51				$args['headers'] = $this->headers;
52
53			if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
54				$args['user-agent'] = $this->useragent;
55
56			$res = wp_safe_remote_request($url, $args);
57
58			if ( is_wp_error($res) ) {
59				$this->error = 'WP HTTP Error: ' . $res->get_error_message();
60				$this->success = false;
61			} else {
62				$this->headers = wp_remote_retrieve_headers( $res );
63				$this->body = wp_remote_retrieve_body( $res );
64				$this->status_code = wp_remote_retrieve_response_code( $res );
65			}
66		} else {
67			$this->error = '';
68			$this->success = false;
69		}
70	}
71}