web/wp-includes/class-feed.php
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 
       
     3 if ( !class_exists('SimplePie') )
       
     4 	require_once (ABSPATH . WPINC . '/class-simplepie.php');
       
     5 
       
     6 class WP_Feed_Cache extends SimplePie_Cache {
       
     7 	/**
       
     8 	 * Don't call the constructor. Please.
       
     9 	 *
       
    10 	 * @access private
       
    11 	 */
       
    12 	function WP_Feed_Cache() {
       
    13 		trigger_error('Please call SimplePie_Cache::create() instead of the constructor', E_USER_ERROR);
       
    14 	}
       
    15 
       
    16 	/**
       
    17 	 * Create a new SimplePie_Cache object
       
    18 	 *
       
    19 	 * @static
       
    20 	 * @access public
       
    21 	 */
       
    22 	function create($location, $filename, $extension) {
       
    23 		return new WP_Feed_Cache_Transient($location, $filename, $extension);
       
    24 	}
       
    25 }
       
    26 
       
    27 class WP_Feed_Cache_Transient {
       
    28 	var $name;
       
    29 	var $mod_name;
       
    30 	var $lifetime = 43200; //Default lifetime in cache of 12 hours
       
    31 
       
    32 	function WP_Feed_Cache_Transient($location, $filename, $extension) {
       
    33 		$this->name = 'feed_' . $filename;
       
    34 		$this->mod_name = 'feed_mod_' . $filename;
       
    35 		$this->lifetime = apply_filters('wp_feed_cache_transient_lifetime', $this->lifetime, $filename);
       
    36 	}
       
    37 
       
    38 	function save($data) {
       
    39 		if ( is_a($data, 'SimplePie') )
       
    40 			$data = $data->data;
       
    41 
       
    42 		set_transient($this->name, $data, $this->lifetime);
       
    43 		set_transient($this->mod_name, time(), $this->lifetime);
       
    44 		return true;
       
    45 	}
       
    46 
       
    47 	function load() {
       
    48 		return get_transient($this->name);
       
    49 	}
       
    50 
       
    51 	function mtime() {
       
    52 		return get_transient($this->mod_name);
       
    53 	}
       
    54 
       
    55 	function touch() {
       
    56 		return set_transient($this->mod_name, time(), $this->lifetime);
       
    57 	}
       
    58 
       
    59 	function unlink() {
       
    60 		delete_transient($this->name);
       
    61 		delete_transient($this->mod_name);
       
    62 		return true;
       
    63 	}
       
    64 }
       
    65 
       
    66 class WP_SimplePie_File extends SimplePie_File {
       
    67 
       
    68 	function WP_SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
       
    69 		$this->url = $url;
       
    70 		$this->timeout = $timeout;
       
    71 		$this->redirects = $redirects;
       
    72 		$this->headers = $headers;
       
    73 		$this->useragent = $useragent;
       
    74 
       
    75 		$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
       
    76 
       
    77 		if ( preg_match('/^http(s)?:\/\//i', $url) ) {
       
    78 			$args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects);
       
    79 
       
    80 			if ( !empty($this->headers) )
       
    81 				$args['headers'] = $this->headers;
       
    82 
       
    83 			if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
       
    84 				$args['user-agent'] = $this->useragent;
       
    85 
       
    86 			$res = wp_remote_request($url, $args);
       
    87 
       
    88 			if ( is_wp_error($res) ) {
       
    89 				$this->error = 'WP HTTP Error: ' . $res->get_error_message();
       
    90 				$this->success = false;
       
    91 			} else {
       
    92 				$this->headers = $res['headers'];
       
    93 				$this->body = $res['body'];
       
    94 				$this->status_code = $res['response']['code'];
       
    95 			}
       
    96 		} else {
       
    97 			if ( ! $this->body = file_get_contents($url) ) {
       
    98 				$this->error = 'file_get_contents could not read the file';
       
    99 				$this->success = false;
       
   100 			}
       
   101 		}
       
   102 	}
       
   103 }