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

git-svn-id: http://svn.automattic.com/wordpress/trunk@10687 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-03-03 17:41:01 +00:00
parent 5777900e02
commit 8fb1093aad
2 changed files with 51 additions and 26 deletions

View File

@ -2,15 +2,13 @@
require_once (ABSPATH . WPINC . '/simplepie.inc');
class WP_Feed_Cache extends SimplePie_Cache
{
class WP_Feed_Cache extends SimplePie_Cache {
/**
* Don't call the constructor. Please.
*
* @access private
*/
function WP_Feed_Cache()
{
function WP_Feed_Cache() {
trigger_error('Please call SimplePie_Cache::create() instead of the constructor', E_USER_ERROR);
}
@ -20,21 +18,18 @@ class WP_Feed_Cache extends SimplePie_Cache
* @static
* @access public
*/
function create($location, $filename, $extension)
{
function create($location, $filename, $extension) {
return new WP_Feed_Cache_Transient($location, $filename, $extension);
}
}
class WP_Feed_Cache_Transient
{
class WP_Feed_Cache_Transient {
var $location;
var $filename;
var $extension;
var $name;
function WP_Feed_Cache_Transient($location, $filename, $extension)
{
function WP_Feed_Cache_Transient($location, $filename, $extension) {
//$this->location = $location;
//$this->filename = rawurlencode($filename);
//$this->extension = rawurlencode($extension);
@ -43,37 +38,69 @@ class WP_Feed_Cache_Transient
$this->mod_name = 'feed_mod_' . $filename;
}
function save($data)
{
if (is_a($data, 'SimplePie'))
{
function save($data) {
if ( is_a($data, 'SimplePie') )
$data = $data->data;
}
set_transient($this->name, $data, 43200);
set_transient($this->mod_name, time(), 43200);
return true;
}
function load()
{
function load() {
return get_transient($this->name);
}
function mtime()
{
function mtime() {
return get_transient($this->mod_name);
}
function touch()
{
function touch() {
return set_transient($this->mod_name, time(), 43200);
}
function unlink()
{
function unlink() {
delete_transient($this->name);
delete_transient($this->mod_name);
return true;
}
}
class WP_SimplePie_File extends SimplePie_File {
function WP_SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
$this->url = $url;
$this->timeout = $timeout;
$this->redirects = $redirects;
$this->headers = $headers;
$this->useragent = $useragent;
$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
if ( preg_match('/^http(s)?:\/\//i', $url) ) {
$args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects);
if ( !empty($this->headers) )
$args['headers'] = $this->headers;
if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
$args['user-agent'] = $this->useragent;
$res = wp_remote_request($url, $args);
if ( is_wp_error($res) ) {
$this->error = 'WP HTTP Error: ' . $res->get_error_message();
$this->success = false;
} else {
$this->headers = $res['headers'];
$this->body = $res['body'];
$this->status_code = $res['response']['code'];
}
} else {
if ( ! $this->body = file_get_contents($url) ) {
$this->error = 'file_get_contents could not read the file';
$this->success = false;
}
}
}
}

View File

@ -548,8 +548,8 @@ function fetch_feed($url) {
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->set_cache_class('WP_Feed_Cache');
$feed->set_file_class('WP_SimplePie_File');
$feed->set_cache_duration(43200);
$feed->set_useragent('WordPress/' . $GLOBALS['wp_version']);
$feed->init();
$feed->handle_content_type();
@ -558,5 +558,3 @@ function fetch_feed($url) {
return $feed;
}
?>