You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

73 lines
2.2 KiB

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