class-wp-simplepie-file.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * This uses Core's HTTP API to make requests, which gives plugins the ability
  13. * to hook into the process.
  14. *
  15. * @since 2.8.0
  16. *
  17. * @see SimplePie_File
  18. */
  19. #[AllowDynamicProperties]
  20. class WP_SimplePie_File extends SimplePie_File {
  21. /**
  22. * Timeout.
  23. *
  24. * @var int How long the connection should stay open in seconds.
  25. */
  26. public $timeout = 10;
  27. /**
  28. * Constructor.
  29. *
  30. * @since 2.8.0
  31. * @since 3.2.0 Updated to use a PHP5 constructor.
  32. * @since 5.6.1 Multiple headers are concatenated into a comma-separated string,
  33. * rather than remaining an array.
  34. *
  35. * @param string $url Remote file URL.
  36. * @param int $timeout Optional. How long the connection should stay open in seconds.
  37. * Default 10.
  38. * @param int $redirects Optional. The number of allowed redirects. Default 5.
  39. * @param string|array $headers Optional. Array or string of headers to send with the request.
  40. * Default null.
  41. * @param string $useragent Optional. User-agent value sent. Default null.
  42. * @param bool $force_fsockopen Optional. Whether to force opening internet or unix domain socket
  43. * connection or not. Default false.
  44. */
  45. public function __construct( $url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false ) {
  46. $this->url = $url;
  47. $this->timeout = $timeout;
  48. $this->redirects = $redirects;
  49. $this->headers = $headers;
  50. $this->useragent = $useragent;
  51. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
  52. if ( preg_match( '/^http(s)?:\/\//i', $url ) ) {
  53. $args = array(
  54. 'timeout' => $this->timeout,
  55. 'redirection' => $this->redirects,
  56. );
  57. if ( ! empty( $this->headers ) ) {
  58. $args['headers'] = $this->headers;
  59. }
  60. if ( SIMPLEPIE_USERAGENT != $this->useragent ) { // Use default WP user agent unless custom has been specified.
  61. $args['user-agent'] = $this->useragent;
  62. }
  63. $res = wp_safe_remote_request( $url, $args );
  64. if ( is_wp_error( $res ) ) {
  65. $this->error = 'WP HTTP Error: ' . $res->get_error_message();
  66. $this->success = false;
  67. } else {
  68. $this->headers = wp_remote_retrieve_headers( $res );
  69. /*
  70. * SimplePie expects multiple headers to be stored as a comma-separated string,
  71. * but `wp_remote_retrieve_headers()` returns them as an array, so they need
  72. * to be converted.
  73. *
  74. * The only exception to that is the `content-type` header, which should ignore
  75. * any previous values and only use the last one.
  76. *
  77. * @see SimplePie_HTTP_Parser::new_line().
  78. */
  79. foreach ( $this->headers as $name => $value ) {
  80. if ( ! is_array( $value ) ) {
  81. continue;
  82. }
  83. if ( 'content-type' === $name ) {
  84. $this->headers[ $name ] = array_pop( $value );
  85. } else {
  86. $this->headers[ $name ] = implode( ', ', $value );
  87. }
  88. }
  89. $this->body = wp_remote_retrieve_body( $res );
  90. $this->status_code = wp_remote_retrieve_response_code( $res );
  91. }
  92. } else {
  93. $this->error = '';
  94. $this->success = false;
  95. }
  96. }
  97. }