[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-simplepie-file.php (source)

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


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1