web/wp-includes/rss.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     9  * @license GPL
     9  * @license GPL
    10  *
    10  *
    11  * @package External
    11  * @package External
    12  * @subpackage MagpieRSS
    12  * @subpackage MagpieRSS
    13  */
    13  */
       
    14 
       
    15 /**
       
    16  * Deprecated. Use SimplePie (class-simplepie.php) instead.
       
    17  */
       
    18 _deprecated_file( basename( __FILE__ ), '3.0', WPINC . '/class-simplepie.php' );
    14 
    19 
    15 /*
    20 /*
    16  * Hook to use another RSS object instead of MagpieRSS
    21  * Hook to use another RSS object instead of MagpieRSS
    17  */
    22  */
    18 do_action('load_feed_engine');
    23 do_action('load_feed_engine');
    56 		$parser = @xml_parser_create();
    61 		$parser = @xml_parser_create();
    57 
    62 
    58 		if ( !is_resource($parser) )
    63 		if ( !is_resource($parser) )
    59 			trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
    64 			trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
    60 
    65 
    61 
       
    62 		$this->parser = $parser;
    66 		$this->parser = $parser;
    63 
    67 
    64 		# pass in parser, and a reference to this object
    68 		# pass in parser, and a reference to this object
    65 		# setup handlers
    69 		# set up handlers
    66 		#
    70 		#
    67 		xml_set_object( $this->parser, $this );
    71 		xml_set_object( $this->parser, $this );
    68 		xml_set_element_handler($this->parser,
    72 		xml_set_element_handler($this->parser,
    69 				'feed_start_element', 'feed_end_element' );
    73 				'feed_start_element', 'feed_end_element' );
    70 
    74 
   160 				$el = 'atom_content';
   164 				$el = 'atom_content';
   161 			}
   165 			}
   162 
   166 
   163 			$this->incontent = $el;
   167 			$this->incontent = $el;
   164 
   168 
   165 
       
   166 		}
   169 		}
   167 
   170 
   168 		// if inside an Atom content construct (e.g. content or summary) field treat tags as text
   171 		// if inside an Atom content construct (e.g. content or summary) field treat tags as text
   169 		elseif ($this->feed_type == ATOM and $this->incontent )
   172 		elseif ($this->feed_type == ATOM and $this->incontent )
   170 		{
   173 		{
   198 		// set stack[0] to current element
   201 		// set stack[0] to current element
   199 		else {
   202 		else {
   200 			array_unshift($this->stack, $el);
   203 			array_unshift($this->stack, $el);
   201 		}
   204 		}
   202 	}
   205 	}
   203 
       
   204 
       
   205 
   206 
   206 	function feed_cdata ($p, $text) {
   207 	function feed_cdata ($p, $text) {
   207 
   208 
   208 		if ($this->feed_type == ATOM and $this->incontent)
   209 		if ($this->feed_type == ATOM and $this->incontent)
   209 		{
   210 		{
   387 
   388 
   388 if ( !function_exists('fetch_rss') ) :
   389 if ( !function_exists('fetch_rss') ) :
   389 /**
   390 /**
   390  * Build Magpie object based on RSS from URL.
   391  * Build Magpie object based on RSS from URL.
   391  *
   392  *
   392  * @since unknown
   393  * @since 1.5.0
   393  * @package External
   394  * @package External
   394  * @subpackage MagpieRSS
   395  * @subpackage MagpieRSS
   395  *
   396  *
   396  * @param string $url URL to retrieve feed
   397  * @param string $url URL to retrieve feed
   397  * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
   398  * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
   429 
   430 
   430 		if (MAGPIE_DEBUG and $cache->ERROR) {
   431 		if (MAGPIE_DEBUG and $cache->ERROR) {
   431 			debug($cache->ERROR, E_USER_WARNING);
   432 			debug($cache->ERROR, E_USER_WARNING);
   432 		}
   433 		}
   433 
   434 
   434 
       
   435 		$cache_status 	 = 0;		// response of check_cache
   435 		$cache_status 	 = 0;		// response of check_cache
   436 		$request_headers = array(); // HTTP headers to send with fetch
   436 		$request_headers = array(); // HTTP headers to send with fetch
   437 		$rss 			 = 0;		// parsed RSS object
   437 		$rss 			 = 0;		// parsed RSS object
   438 		$errormsg		 = 0;		// errors, if any
   438 		$errormsg		 = 0;		// errors, if any
   439 
   439 
   454 			}
   454 			}
   455 		}
   455 		}
   456 
   456 
   457 		// else attempt a conditional get
   457 		// else attempt a conditional get
   458 
   458 
   459 		// setup headers
   459 		// set up headers
   460 		if ( $cache_status == 'STALE' ) {
   460 		if ( $cache_status == 'STALE' ) {
   461 			$rss = $cache->get( $url );
   461 			$rss = $cache->get( $url );
   462 			if ( isset($rss->etag) and $rss->last_modified ) {
   462 			if ( isset($rss->etag) and $rss->last_modified ) {
   463 				$request_headers['If-None-Match'] = $rss->etag;
   463 				$request_headers['If-None-Match'] = $rss->etag;
   464 				$request_headers['If-Last-Modified'] = $rss->last_modified;
   464 				$request_headers['If-Last-Modified'] = $rss->last_modified;
   525 endif;
   525 endif;
   526 
   526 
   527 /**
   527 /**
   528  * Retrieve URL headers and content using WP HTTP Request API.
   528  * Retrieve URL headers and content using WP HTTP Request API.
   529  *
   529  *
   530  * @since unknown
   530  * @since 1.5.0
   531  * @package External
   531  * @package External
   532  * @subpackage MagpieRSS
   532  * @subpackage MagpieRSS
   533  *
   533  *
   534  * @param string $url URL to retrieve
   534  * @param string $url URL to retrieve
   535  * @param array $headers Optional. Headers to send to the URL.
   535  * @param array $headers Optional. Headers to send to the URL.
   536  * @return Snoopy style response
   536  * @return Snoopy style response
   537  */
   537  */
   538 function _fetch_remote_file ($url, $headers = "" ) {
   538 function _fetch_remote_file($url, $headers = "" ) {
   539 	$resp = wp_remote_request($url, array('headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT));
   539 	$resp = wp_remote_request($url, array('headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT));
   540 	if ( is_wp_error($resp) ) {
   540 	if ( is_wp_error($resp) ) {
   541 		$error = array_shift($resp->errors);
   541 		$error = array_shift($resp->errors);
   542 
   542 
   543 		$resp = new stdClass;
   543 		$resp = new stdClass;
   544 		$resp->status = 500;
   544 		$resp->status = 500;
   545 		$resp->response_code = 500;
   545 		$resp->response_code = 500;
   546 		$resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
   546 		$resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
   547 		return $resp;
   547 		return $resp;
   548 	}
   548 	}
       
   549 
       
   550 	// Snoopy returns headers unprocessed.
       
   551 	// Also note, WP_HTTP lowercases all keys, Snoopy did not.
       
   552 	$return_headers = array();
       
   553 	foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
       
   554 		if ( !is_array($value) ) {
       
   555 			$return_headers[] = "$key: $value";
       
   556 		} else {
       
   557 			foreach ( $value as $v )
       
   558 				$return_headers[] = "$key: $v";
       
   559 		}
       
   560 	}
       
   561 
   549 	$response = new stdClass;
   562 	$response = new stdClass;
   550 	$response->status = $resp['response']['code'];
   563 	$response->status = wp_remote_retrieve_response_code( $resp );
   551 	$response->response_code = $resp['response']['code'];
   564 	$response->response_code = wp_remote_retrieve_response_code( $resp );
   552 	$response->headers = $resp['headers'];
   565 	$response->headers = $return_headers;
   553 	$response->results = $resp['body'];
   566 	$response->results = wp_remote_retrieve_body( $resp );
   554 
   567 
   555 	return $response;
   568 	return $response;
   556 }
   569 }
   557 
   570 
   558 /**
   571 /**
   559  * Retrieve
   572  * Retrieve
   560  *
   573  *
   561  * @since unknown
   574  * @since 1.5.0
   562  * @package External
   575  * @package External
   563  * @subpackage MagpieRSS
   576  * @subpackage MagpieRSS
   564  *
   577  *
   565  * @param unknown_type $resp
   578  * @param unknown_type $resp
   566  * @return unknown
   579  * @return unknown
   580 			else {
   593 			else {
   581 				$field = $h;
   594 				$field = $h;
   582 				$val = "";
   595 				$val = "";
   583 			}
   596 			}
   584 
   597 
   585 			if ( $field == 'ETag' ) {
   598 			if ( $field == 'etag' ) {
   586 				$rss->etag = $val;
   599 				$rss->etag = $val;
   587 			}
   600 			}
   588 
   601 
   589 			if ( $field == 'Last-Modified' ) {
   602 			if ( $field == 'last-modified' ) {
   590 				$rss->last_modified = $val;
   603 				$rss->last_modified = $val;
   591 			}
   604 			}
   592 		}
   605 		}
   593 
   606 
   594 		return $rss;
   607 		return $rss;
   604 		return false;
   617 		return false;
   605 	} // end if ($rss and !$rss->error)
   618 	} // end if ($rss and !$rss->error)
   606 }
   619 }
   607 
   620 
   608 /**
   621 /**
   609  * Setup constants with default values, unless user overrides.
   622  * Set up constants with default values, unless user overrides.
   610  *
   623  *
   611  * @since unknown
   624  * @since 1.5.0
   612  * @package External
   625  * @package External
   613  * @subpackage MagpieRSS
   626  * @subpackage MagpieRSS
   614  */
   627  */
   615 function init () {
   628 function init () {
   616 	if ( defined('MAGPIE_INITALIZED') ) {
   629 	if ( defined('MAGPIE_INITALIZED') ) {
   708 	Purpose:	add an item to the cache, keyed on url
   721 	Purpose:	add an item to the cache, keyed on url
   709 	Input:		url from wich the rss file was fetched
   722 	Input:		url from wich the rss file was fetched
   710 	Output:		true on sucess
   723 	Output:		true on sucess
   711 \*=======================================================================*/
   724 \*=======================================================================*/
   712 	function set ($url, $rss) {
   725 	function set ($url, $rss) {
   713 		global $wpdb;
       
   714 		$cache_option = 'rss_' . $this->file_name( $url );
   726 		$cache_option = 'rss_' . $this->file_name( $url );
   715 
   727 
   716 		set_transient($cache_option, $rss, $this->MAX_AGE);
   728 		set_transient($cache_option, $rss, $this->MAX_AGE);
   717 
   729 
   718 		return $cache_option;
   730 		return $cache_option;
   852 
   864 
   853 if ( !function_exists('wp_rss') ) :
   865 if ( !function_exists('wp_rss') ) :
   854 /**
   866 /**
   855  * Display all RSS items in a HTML ordered list.
   867  * Display all RSS items in a HTML ordered list.
   856  *
   868  *
   857  * @since unknown
   869  * @since 1.5.0
   858  * @package External
   870  * @package External
   859  * @subpackage MagpieRSS
   871  * @subpackage MagpieRSS
   860  *
   872  *
   861  * @param string $url URL of feed to display. Will not auto sense feed URL.
   873  * @param string $url URL of feed to display. Will not auto sense feed URL.
   862  * @param int $num_items Optional. Number of items to display, default is all.
   874  * @param int $num_items Optional. Number of items to display, default is all.
   872 		foreach ( (array) $rss->items as $item ) {
   884 		foreach ( (array) $rss->items as $item ) {
   873 			printf(
   885 			printf(
   874 				'<li><a href="%1$s" title="%2$s">%3$s</a></li>',
   886 				'<li><a href="%1$s" title="%2$s">%3$s</a></li>',
   875 				esc_url( $item['link'] ),
   887 				esc_url( $item['link'] ),
   876 				esc_attr( strip_tags( $item['description'] ) ),
   888 				esc_attr( strip_tags( $item['description'] ) ),
   877 				htmlentities( $item['title'] )
   889 				esc_html( $item['title'] )
   878 			);
   890 			);
   879 		}
   891 		}
   880 
   892 
   881 		echo '</ul>';
   893 		echo '</ul>';
   882 	} else {
   894 	} else {
   892  * You have to specify which HTML list you want, either ordered or unordered
   904  * You have to specify which HTML list you want, either ordered or unordered
   893  * before using the function. You also have to specify how many items you wish
   905  * before using the function. You also have to specify how many items you wish
   894  * to display. You can't display all of them like you can with wp_rss()
   906  * to display. You can't display all of them like you can with wp_rss()
   895  * function.
   907  * function.
   896  *
   908  *
   897  * @since unknown
   909  * @since 1.5.0
   898  * @package External
   910  * @package External
   899  * @subpackage MagpieRSS
   911  * @subpackage MagpieRSS
   900  *
   912  *
   901  * @param string $url URL of feed to display. Will not auto sense feed URL.
   913  * @param string $url URL of feed to display. Will not auto sense feed URL.
   902  * @param int $num_items Optional. Number of items to display, default is all.
   914  * @param int $num_items Optional. Number of items to display, default is all.
   907 	if ( $rss ) {
   919 	if ( $rss ) {
   908 		$rss->items = array_slice($rss->items, 0, $num_items);
   920 		$rss->items = array_slice($rss->items, 0, $num_items);
   909 		foreach ( (array) $rss->items as $item ) {
   921 		foreach ( (array) $rss->items as $item ) {
   910 			echo "<li>\n";
   922 			echo "<li>\n";
   911 			echo "<a href='$item[link]' title='$item[description]'>";
   923 			echo "<a href='$item[link]' title='$item[description]'>";
   912 			echo htmlentities($item['title']);
   924 			echo esc_html($item['title']);
   913 			echo "</a><br />\n";
   925 			echo "</a><br />\n";
   914 			echo "</li>\n";
   926 			echo "</li>\n";
   915 		}
   927 		}
   916 	} else {
   928 	} else {
   917 		return false;
   929 		return false;
   918 	}
   930 	}
   919 }
   931 }
   920 endif;
   932 endif;
   921 
       
   922 ?>