WordPress.org

Make WordPress Core

Changeset 10687


Ignore:
Timestamp:
03/03/2009 05:41:01 PM (9 years ago)
Author:
ryan
Message:

Use WordPress HTTP class for SimplePie requests. Props DD32. fixes #9247

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-feed.php

    r10666 r10687  
    33require_once (ABSPATH . WPINC . '/simplepie.inc');
    44
    5 class WP_Feed_Cache extends SimplePie_Cache
    6 {
     5class WP_Feed_Cache extends SimplePie_Cache {
    76    /**
    87     * Don't call the constructor. Please.
     
    109     * @access private
    1110     */
    12     function WP_Feed_Cache()
    13     {
     11    function WP_Feed_Cache() {
    1412        trigger_error('Please call SimplePie_Cache::create() instead of the constructor', E_USER_ERROR);
    1513    }
     
    2119     * @access public
    2220     */
    23     function create($location, $filename, $extension)
    24     {
     21    function create($location, $filename, $extension) {
    2522        return new WP_Feed_Cache_Transient($location, $filename, $extension);
    2623    }
    2724}
    2825
    29 class WP_Feed_Cache_Transient
    30 {
     26class WP_Feed_Cache_Transient {
    3127    var $location;
    3228    var $filename;
     
    3430    var $name;
    3531
    36     function WP_Feed_Cache_Transient($location, $filename, $extension)
    37     {
     32    function WP_Feed_Cache_Transient($location, $filename, $extension) {
    3833        //$this->location = $location;
    3934        //$this->filename = rawurlencode($filename);
     
    4439    }
    4540
    46     function save($data)
    47     {
    48         if (is_a($data, 'SimplePie'))
    49         {
     41    function save($data) {
     42        if ( is_a($data, 'SimplePie') )
    5043            $data = $data->data;
    51         }
    5244
    5345        set_transient($this->name, $data, 43200);
     
    5648    }
    5749
    58     function load()
    59     {
     50    function load() {
    6051        return get_transient($this->name);
    6152    }
    6253
    63     function mtime()
    64     {
     54    function mtime() {
    6555        return get_transient($this->mod_name);
    6656    }
    6757
    68     function touch()
    69     {
     58    function touch() {
    7059        return set_transient($this->mod_name, time(), 43200);
    7160    }
    7261
    73     function unlink()
    74     {
     62    function unlink() {
    7563        delete_transient($this->name);
    7664        delete_transient($this->mod_name);
     
    7866    }
    7967}
     68
     69class WP_SimplePie_File extends SimplePie_File {
     70
     71    function WP_SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
     72        $this->url = $url;
     73        $this->timeout = $timeout;
     74        $this->redirects = $redirects;
     75        $this->headers = $headers;
     76        $this->useragent = $useragent;
     77
     78        $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
     79
     80        if ( preg_match('/^http(s)?:\/\//i', $url) ) {
     81            $args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects);
     82
     83            if ( !empty($this->headers) )
     84                $args['headers'] = $this->headers;
     85
     86            if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
     87                $args['user-agent'] = $this->useragent;
     88
     89            $res = wp_remote_request($url, $args);
     90
     91            if ( is_wp_error($res) ) {
     92                $this->error = 'WP HTTP Error: ' . $res->get_error_message();
     93                $this->success = false;
     94            } else {
     95                $this->headers = $res['headers'];
     96                $this->body = $res['body'];
     97                $this->status_code = $res['response']['code'];
     98            }
     99        } else {
     100            if ( ! $this->body = file_get_contents($url) ) {
     101                $this->error = 'file_get_contents could not read the file';
     102                $this->success = false;
     103            }
     104        }
     105    }
     106}
  • trunk/wp-includes/feed.php

    r10671 r10687  
    549549    $feed->set_feed_url($url);
    550550    $feed->set_cache_class('WP_Feed_Cache');
     551    $feed->set_file_class('WP_SimplePie_File');
    551552    $feed->set_cache_duration(43200);
    552     $feed->set_useragent('WordPress/' . $GLOBALS['wp_version']);
    553553    $feed->init();
    554554    $feed->handle_content_type();
     
    559559    return $feed;
    560560}
    561 
    562 ?>
Note: See TracChangeset for help on using the changeset viewer.