wp/wp-includes/class-feed.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
       
     2 /**
       
     3  * Feed API
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Feed
       
     7  */
     2 
     8 
     3 if ( !class_exists('SimplePie') )
     9 _deprecated_file( basename( __FILE__ ), '4.7.0', 'fetch_feed()' );
       
    10 
       
    11 if ( ! class_exists( 'SimplePie', false ) ) {
     4 	require_once( ABSPATH . WPINC . '/class-simplepie.php' );
    12 	require_once( ABSPATH . WPINC . '/class-simplepie.php' );
     5 
       
     6 class WP_Feed_Cache extends SimplePie_Cache {
       
     7 	/**
       
     8 	 * Create a new SimplePie_Cache object
       
     9 	 *
       
    10 	 * @static
       
    11 	 * @access public
       
    12 	 */
       
    13 	public function create($location, $filename, $extension) {
       
    14 		return new WP_Feed_Cache_Transient($location, $filename, $extension);
       
    15 	}
       
    16 }
    13 }
    17 
    14 
    18 class WP_Feed_Cache_Transient {
    15 require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
    19 	public $name;
    16 require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
    20 	public $mod_name;
    17 require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
    21 	public $lifetime = 43200; //Default lifetime in cache of 12 hours
    18 require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
    22 
       
    23 	public function __construct($location, $filename, $extension) {
       
    24 		$this->name = 'feed_' . $filename;
       
    25 		$this->mod_name = 'feed_mod_' . $filename;
       
    26 
       
    27 		$lifetime = $this->lifetime;
       
    28 		/**
       
    29 		 * Filter the transient lifetime of the feed cache.
       
    30 		 *
       
    31 		 * @since 2.8.0
       
    32 		 *
       
    33 		 * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
       
    34 		 * @param string $filename Unique identifier for the cache object.
       
    35 		 */
       
    36 		$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename);
       
    37 	}
       
    38 
       
    39 	public function save($data) {
       
    40 		if ( $data instanceof SimplePie ) {
       
    41 			$data = $data->data;
       
    42 		}
       
    43 
       
    44 		set_transient($this->name, $data, $this->lifetime);
       
    45 		set_transient($this->mod_name, time(), $this->lifetime);
       
    46 		return true;
       
    47 	}
       
    48 
       
    49 	public function load() {
       
    50 		return get_transient($this->name);
       
    51 	}
       
    52 
       
    53 	public function mtime() {
       
    54 		return get_transient($this->mod_name);
       
    55 	}
       
    56 
       
    57 	public function touch() {
       
    58 		return set_transient($this->mod_name, time(), $this->lifetime);
       
    59 	}
       
    60 
       
    61 	public function unlink() {
       
    62 		delete_transient($this->name);
       
    63 		delete_transient($this->mod_name);
       
    64 		return true;
       
    65 	}
       
    66 }
       
    67 
       
    68 class WP_SimplePie_File extends SimplePie_File {
       
    69 
       
    70 	public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
       
    71 		$this->url = $url;
       
    72 		$this->timeout = $timeout;
       
    73 		$this->redirects = $redirects;
       
    74 		$this->headers = $headers;
       
    75 		$this->useragent = $useragent;
       
    76 
       
    77 		$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
       
    78 
       
    79 		if ( preg_match('/^http(s)?:\/\//i', $url) ) {
       
    80 			$args = array(
       
    81 				'timeout' => $this->timeout,
       
    82 				'redirection' => $this->redirects,
       
    83 			);
       
    84 
       
    85 			if ( !empty($this->headers) )
       
    86 				$args['headers'] = $this->headers;
       
    87 
       
    88 			if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
       
    89 				$args['user-agent'] = $this->useragent;
       
    90 
       
    91 			$res = wp_safe_remote_request($url, $args);
       
    92 
       
    93 			if ( is_wp_error($res) ) {
       
    94 				$this->error = 'WP HTTP Error: ' . $res->get_error_message();
       
    95 				$this->success = false;
       
    96 			} else {
       
    97 				$this->headers = wp_remote_retrieve_headers( $res );
       
    98 				$this->body = wp_remote_retrieve_body( $res );
       
    99 				$this->status_code = wp_remote_retrieve_response_code( $res );
       
   100 			}
       
   101 		} else {
       
   102 			$this->error = '';
       
   103 			$this->success = false;
       
   104 		}
       
   105 	}
       
   106 }
       
   107 
       
   108 /**
       
   109  * WordPress SimplePie Sanitization Class
       
   110  *
       
   111  * Extension of the SimplePie_Sanitize class to use KSES, because
       
   112  * we cannot universally count on DOMDocument being available
       
   113  *
       
   114  * @package WordPress
       
   115  * @since 3.5.0
       
   116  */
       
   117 class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
       
   118 	public function sanitize( $data, $type, $base = '' ) {
       
   119 		$data = trim( $data );
       
   120 		if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
       
   121 			if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
       
   122 				$type |= SIMPLEPIE_CONSTRUCT_HTML;
       
   123 			}
       
   124 			else {
       
   125 				$type |= SIMPLEPIE_CONSTRUCT_TEXT;
       
   126 			}
       
   127 		}
       
   128 		if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
       
   129 			$data = base64_decode( $data );
       
   130 		}
       
   131 		if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
       
   132 			$data = wp_kses_post( $data );
       
   133 			if ( $this->output_encoding !== 'UTF-8' ) {
       
   134 				$data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
       
   135 			}
       
   136 			return $data;
       
   137 		} else {
       
   138 			return parent::sanitize( $data, $type, $base );
       
   139 		}
       
   140 	}
       
   141 }