Use SimplePie for widget and dashboard feeds. First cut. see #9198

git-svn-id: https://develop.svn.wordpress.org/trunk@10666 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2009-02-27 19:32:50 +00:00
parent 8bab50a649
commit 7062db47c2
5 changed files with 13886 additions and 150 deletions

View File

@ -598,60 +598,55 @@ function wp_dashboard_incoming_links() {
function wp_dashboard_incoming_links_output() {
$widgets = get_option( 'dashboard_widget_options' );
@extract( @$widgets['dashboard_incoming_links'], EXTR_SKIP );
$rss = @fetch_rss( $url );
if ( isset($rss->items) && 0 < count($rss->items) ) {
$rss = fetch_feed( $url );
echo "<ul>\n";
if ( !$rss->get_item_quantity() ) {
echo '<p>' . __('This dashboard widget queries <a href="http://blogsearch.google.com/">Google Blog Search</a> so that when another blog links to your site it will show up here. It has found no incoming links&hellip; yet. It&#8217;s okay &#8212; there is no rush.') . "</p>\n";
return;
}
$rss->items = array_slice($rss->items, 0, $items);
foreach ( $rss->items as $item ) {
$publisher = '';
$site_link = '';
$link = '';
$content = '';
$date = '';
$link = clean_url( strip_tags( $item['link'] ) );
echo "<ul>\n";
if ( isset( $item['author_uri'] ) )
$site_link = clean_url( strip_tags( $item['author_uri'] ) );
$count = 0;
foreach ( $rss->get_items() as $item ) {
$publisher = '';
$site_link = '';
$link = '';
$content = '';
$date = '';
$link = clean_url( strip_tags( $item->get_link() ) );
if ( !$publisher = wp_specialchars( strip_tags( isset($item['dc']['publisher']) ? $item['dc']['publisher'] : $item['author_name'] ) ) )
$publisher = __( 'Somebody' );
if ( $site_link )
$publisher = "<a href='$site_link'>$publisher</a>";
else
$publisher = "<strong>$publisher</strong>";
$author = $item->get_author();
$site_link = clean_url( strip_tags( $author->get_link() ) );
if ( isset($item['description']) )
$content = $item['description'];
elseif ( isset($item['summary']) )
$content = $item['summary'];
elseif ( isset($item['atom_content']) )
$content = $item['atom_content'];
else
$content = __( 'something' );
$content = wp_html_excerpt($content, 50) . ' ...';
if ( $link )
$text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
else
$text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
if ( !$publisher = wp_specialchars( strip_tags( $author->get_name() ) ) )
$publisher = __( 'Somebody' );
if ( $site_link )
$publisher = "<a href='$site_link'>$publisher</a>";
else
$publisher = "<strong>$publisher</strong>";
if ( $show_date ) {
if ( $show_author || $show_summary )
$text .= _c( ' on %4$s|feed_display' );
$date = wp_specialchars( strip_tags( isset($item['pubdate']) ? $item['pubdate'] : $item['published'] ) );
$date = strtotime( $date );
$date = gmdate( get_option( 'date_format' ), $date );
}
$content = $item->get_content();
$content = wp_html_excerpt($content, 50) . ' ...';
echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
if ( $link )
$text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
else
$text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
if ( $show_date ) {
if ( $show_author || $show_summary )
$text .= _c( ' on %4$s|feed_display' );
$date = wp_specialchars( strip_tags( $item->get_date() ) );
$date = strtotime( $date );
$date = gmdate( get_option( 'date_format' ), $date );
}
echo "</ul>\n";
} else {
echo '<p>' . __('This dashboard widget queries <a href="http://blogsearch.google.com/">Google Blog Search</a> so that when another blog links to your site it will show up here. It has found no incoming links&hellip; yet. It&#8217;s okay &#8212; there is no rush.') . "</p>\n";
echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
}
echo "</ul>\n";
}
function wp_dashboard_incoming_links_control() {
@ -698,21 +693,11 @@ function wp_dashboard_secondary_control() {
function wp_dashboard_secondary_output() {
$widgets = get_option( 'dashboard_widget_options' );
@extract( @$widgets['dashboard_secondary'], EXTR_SKIP );
$rss = @fetch_rss( $url );
$rss = @fetch_feed( $url );
if ( !isset($rss->items) || 0 == count($rss->items) )
if ( !$rss->get_item_quantity() )
return false;
$rss->items = array_slice($rss->items, 0, $items);
if ( 'http://planet.wordpress.org/' == $rss->channel['link'] ) {
foreach ( array_keys($rss->items) as $i ) {
list($site, $description) = explode( ':', wp_specialchars($rss->items[$i]['title']), 2 );
$rss->items[$i]['dc']['creator'] = trim($site);
$rss->items[$i]['title'] = trim($description);
}
}
echo "<div class='rss-widget'>";
wp_widget_rss_output( $rss, $widgets['dashboard_secondary'] );
echo "</div>";
@ -732,36 +717,36 @@ function wp_dashboard_plugins() {
* @since unknown
*/
function wp_dashboard_plugins_output() {
$popular = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
$new = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
$updated = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
$popular = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
$new = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
$updated = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins'), 'updated' => __('Recently Updated') ) as $feed => $label ) {
if ( !isset($$feed->items) || 0 == count($$feed->items) )
if ( !$$feed->get_item_quantity() )
continue;
$$feed->items = array_slice($$feed->items, 0, 5);
$item_key = array_rand($$feed->items);
$items = $$feed->get_items(0, 5);
$item_key = array_rand($items);
// Eliminate some common badly formed plugin descriptions
while ( ( null !== $item_key = array_rand($$feed->items) ) && false !== strpos( $$feed->items[$item_key]['description'], 'Plugin Name:' ) )
unset($$feed->items[$item_key]);
while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
unset($items[$item_key]);
if ( !isset($$feed->items[$item_key]) )
if ( !isset($items[$item_key]) )
continue;
$item = $$feed->items[$item_key];
$item = $items[$item_key];
// current bbPress feed item titles are: user on "topic title"
if ( preg_match( '/"(.*)"/s', $item['title'], $matches ) )
if ( preg_match( '/&quot;(.*)&quot;/s', $item->get_title(), $matches ) )
$title = $matches[1];
else // but let's make it forward compatible if things change
$title = $item['title'];
$title = $item->get_title();
$title = wp_specialchars( $title );
$description = wp_specialchars( strip_tags(html_entity_decode($item['description'], ENT_QUOTES)) );
$description = wp_specialchars( strip_tags(html_entity_decode($item->get_description(), ENT_QUOTES)) );
list($link, $frag) = explode( '#', $item['link'] );
list($link, $frag) = explode( '#', $item->get_link() );
$link = clean_url($link);
if( preg_match('|/([^/]+?)/?$|', $link, $matches) )
@ -806,18 +791,19 @@ function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = ar
}
require_once( ABSPATH . WPINC . '/rss.php' );
init(); // initialize rss constants
$cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
/* TODO Cache check here.
foreach ( $check_urls as $check_url ) {
$status = $cache->check_cache( $check_url );
if ( 'HIT' !== $status ) {
echo $loading;
return false;
}
}
*/
// Always load async until above fixed.
echo $loading;
return false;
if ( $callback && is_callable( $callback ) ) {
$args = array_slice( func_get_args(), 2 );
@ -872,9 +858,8 @@ function wp_dashboard_rss_control( $widget_id, $form_inputs = array() ) {
$widget_options[$widget_id] = wp_widget_rss_process( $_POST['widget-rss'][$number] );
// title is optional. If black, fill it if possible
if ( !$widget_options[$widget_id]['title'] && isset($_POST['widget-rss'][$number]['title']) ) {
require_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss($widget_options[$widget_id]['url']);
$widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->channel['title']));
$rss = fetch_feed($widget_options[$widget_id]['url']);
$widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->get_title()));
}
update_option( 'dashboard_widget_options', $widget_options );
}

View File

@ -0,0 +1,79 @@
<?php
require_once (ABSPATH . WPINC . '/simplepie.inc');
class WP_Feed_Cache extends SimplePie_Cache
{
/**
* Don't call the constructor. Please.
*
* @access private
*/
function WP_Feed_Cache()
{
trigger_error('Please call SimplePie_Cache::create() instead of the constructor', E_USER_ERROR);
}
/**
* Create a new SimplePie_Cache object
*
* @static
* @access public
*/
function create($location, $filename, $extension)
{
return new WP_Feed_Cache_Transient($location, $filename, $extension);
}
}
class WP_Feed_Cache_Transient
{
var $location;
var $filename;
var $extension;
var $name;
function WP_Feed_Cache_Transient($location, $filename, $extension)
{
//$this->location = $location;
//$this->filename = rawurlencode($filename);
//$this->extension = rawurlencode($extension);
//$this->name = "$location/$this->filename.$this->extension";
$this->name = 'feed_' . $filename;
$this->mod_name = 'feed_mod_' . $filename;
}
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()
{
return get_transient($this->name);
}
function mtime()
{
return get_transient($this->mod_name);
}
function touch()
{
return set_transient($this->mod_name, time(), 43200);
}
function unlink()
{
delete_transient($this->name);
delete_transient($this->mod_name);
return true;
}
}

View File

@ -534,4 +534,18 @@ function feed_content_type( $type = '' ) {
return apply_filters( 'feed_content_type', $content_type, $type );
}
function fetch_feed($url) {
require_once (ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->set_cache_class('WP_Feed_Cache');
$feed->set_cache_duration(43200);
$feed->set_useragent('WordPress/' . $GLOBALS['wp_version']);
$feed->init();
$feed->handle_content_type();
return $feed;
}
?>

13672
wp-includes/simplepie.inc Normal file

File diff suppressed because one or more lines are too long

View File

@ -1501,16 +1501,14 @@ function wp_widget_rss($args, $widget_args = 1) {
if ( empty($url) )
return;
require_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss($url);
$link = clean_url(strip_tags($rss->channel['link']));
$rss = fetch_feed($url);
$link = clean_url(strip_tags($rss->get_permalink()));
while ( strstr($link, 'http') != $link )
$link = substr($link, 1);
$desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
$desc = attribute_escape(strip_tags(html_entity_decode($rss->get_description(), ENT_QUOTES)));
$title = $options[$number]['title'];
if ( empty($title) )
$title = htmlentities(strip_tags($rss->channel['title']));
$title = htmlentities(strip_tags($rss->get_title()));
if ( empty($title) )
$title = $desc;
if ( empty($title) )
@ -1541,13 +1539,11 @@ function wp_widget_rss($args, $widget_args = 1) {
*/
function wp_widget_rss_output( $rss, $args = array() ) {
if ( is_string( $rss ) ) {
require_once(ABSPATH . WPINC . '/rss.php');
if ( !$rss = fetch_rss($rss) )
if ( !$rss = fetch_feed($rss) )
return;
} elseif ( is_array($rss) && isset($rss['url']) ) {
require_once(ABSPATH . WPINC . '/rss.php');
$args = $rss;
if ( !$rss = fetch_rss($rss['url']) )
if ( !$rss = fetch_feed($rss['url']) )
return;
} elseif ( !is_object($rss) ) {
return;
@ -1564,66 +1560,57 @@ function wp_widget_rss_output( $rss, $args = array() ) {
$show_author = (int) $show_author;
$show_date = (int) $show_date;
if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
$rss->items = array_slice($rss->items, 0, $items);
echo '<ul>';
foreach ( (array) $rss->items as $item ) {
while ( strstr($item['link'], 'http') != $item['link'] )
$item['link'] = substr($item['link'], 1);
$link = clean_url(strip_tags($item['link']));
$title = attribute_escape(strip_tags($item['title']));
if ( empty($title) )
$title = __('Untitled');
$desc = '';
if ( isset( $item['description'] ) && is_string( $item['description'] ) )
$desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
$desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
if ( 360 < strlen( $desc ) )
$desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
$summary = $desc;
if ( $show_summary ) {
$desc = '';
$summary = wp_specialchars( $summary );
$summary = "<div class='rssSummary'>$summary</div>";
} else {
$summary = '';
}
$date = '';
if ( $show_date ) {
if ( isset($item['pubdate']) )
$date = $item['pubdate'];
elseif ( isset($item['published']) )
$date = $item['published'];
if ( $date ) {
if ( $date_stamp = strtotime( $date ) )
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
else
$date = '';
}
}
$author = '';
if ( $show_author ) {
if ( isset($item['dc']['creator']) )
$author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
elseif ( isset($item['author_name']) )
$author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
}
if ( $link == '' ) {
echo "<li>$title{$date}{$summary}{$author}</li>";
} else {
echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
}
}
echo '</ul>';
} else {
if ( !$rss->get_item_quantity() ) {
echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
return;
}
echo '<ul>';
foreach ( $rss->get_items(0, $items) as $item ) {
$link = $item->get_link();
while ( strstr($link, 'http') != $link )
$link = substr($link, 1);
$link = clean_url(strip_tags($link));
$title = attribute_escape(strip_tags($item->get_title()));
if ( empty($title) )
$title = __('Untitled');
$desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item->get_description(), ENT_QUOTES))));
$desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
$desc = wp_specialchars( $desc );
if ( $show_summary ) {
$summary = "<div class='rssSummary'>$desc</div>";
} else {
$summary = '';
}
$date = '';
if ( $show_date ) {
$date = $item->get_date();
if ( $date ) {
if ( $date_stamp = strtotime( $date ) )
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
else
$date = '';
}
}
$author = '';
if ( $show_author ) {
$author = $item->get_author();
$author = $author->get_name();
$author = ' <cite>' . wp_specialchars( strip_tags( $author ) ) . '</cite>';
}
if ( $link == '' ) {
echo "<li>$title{$date}{$summary}{$author}</li>";
} else {
echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
}
}
echo '</ul>';
}
/**
@ -1810,15 +1797,14 @@ function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
$show_date = (int) $widget_rss['show_date'];
if ( $check_feed ) {
require_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss($url);
$rss = fetch_feed($url);
$error = false;
$link = '';
if ( !is_object($rss) ) {
$url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
$error = sprintf(__('Error in RSS %1$d'), $widget_number );
} else {
$link = clean_url(strip_tags($rss->channel['link']));
$link = clean_url(strip_tags($rss->get_permalink()));
while ( strstr($link, 'http') != $link )
$link = substr($link, 1);
}