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 |
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 |
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. |