No Description

class-wp-simplepie-file.php 3.2KB

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