Make WordPress Core

Changeset 10666


Ignore:
Timestamp:
02/27/2009 07:32:50 PM (18 years ago)
Author:
ryan
Message:

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

Location:
trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/dashboard.php

    r10641 r10666  
    599599        $widgets = get_option( 'dashboard_widget_options' );
    600600        @extract( @$widgets['dashboard_incoming_links'], EXTR_SKIP );
    601         $rss = @fetch_rss( $url );
    602         if ( isset($rss->items) && 0 < count($rss->items) )  {
    603 
    604                 echo "<ul>\n";
    605 
    606                 $rss->items = array_slice($rss->items, 0, $items);
    607                 foreach ( $rss->items as $item ) {
    608                         $publisher = '';
    609                         $site_link = '';
    610                         $link = '';
    611                         $content = '';
    612                         $date = '';
    613                         $link = clean_url( strip_tags( $item['link'] ) );
    614 
    615                         if ( isset( $item['author_uri'] ) )
    616                                 $site_link = clean_url( strip_tags( $item['author_uri'] ) );
    617 
    618                         if ( !$publisher = wp_specialchars( strip_tags( isset($item['dc']['publisher']) ? $item['dc']['publisher'] : $item['author_name'] ) ) )
    619                                 $publisher = __( 'Somebody' );
    620                         if ( $site_link )
    621                                 $publisher = "<a href='$site_link'>$publisher</a>";
    622                         else
    623                                 $publisher = "<strong>$publisher</strong>";
    624 
    625                         if ( isset($item['description']) )
    626                                 $content = $item['description'];
    627                         elseif ( isset($item['summary']) )
    628                                 $content = $item['summary'];
    629                         elseif ( isset($item['atom_content']) )
    630                                 $content = $item['atom_content'];
    631                         else
    632                                 $content = __( 'something' );
    633                         $content = wp_html_excerpt($content, 50) . ' ...';
    634                         if ( $link )
    635                                 $text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
    636                         else
    637                                 $text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
    638 
    639                         if ( $show_date ) {
    640                                 if ( $show_author || $show_summary )
    641                                         $text .= _c( ' on %4$s|feed_display' );
    642                                 $date = wp_specialchars( strip_tags( isset($item['pubdate']) ? $item['pubdate'] : $item['published'] ) );
    643                                 $date = strtotime( $date );
    644                                 $date = gmdate( get_option( 'date_format' ), $date );
    645                         }
    646 
    647                         echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
     601        $rss = fetch_feed( $url );
     602
     603        if ( !$rss->get_item_quantity() ) {
     604                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";
     605                return;
     606        }
     607
     608        echo "<ul>\n";
     609
     610        $count = 0;
     611        foreach ( $rss->get_items() as $item ) {
     612                $publisher = '';
     613                $site_link = '';
     614                $link = '';
     615                $content = '';
     616                $date = '';
     617                $link = clean_url( strip_tags( $item->get_link() ) );
     618
     619                $author = $item->get_author();
     620                $site_link = clean_url( strip_tags( $author->get_link() ) );
     621
     622                if ( !$publisher = wp_specialchars( strip_tags( $author->get_name() ) ) )
     623                        $publisher = __( 'Somebody' );
     624                if ( $site_link )
     625                        $publisher = "<a href='$site_link'>$publisher</a>";
     626                else
     627                        $publisher = "<strong>$publisher</strong>";
     628
     629                $content = $item->get_content();
     630                $content = wp_html_excerpt($content, 50) . ' ...';
     631
     632                if ( $link )
     633                        $text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
     634                else
     635                        $text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
     636
     637                if ( $show_date ) {
     638                        if ( $show_author || $show_summary )
     639                                $text .= _c( ' on %4$s|feed_display' );
     640                        $date = wp_specialchars( strip_tags( $item->get_date() ) );
     641                        $date = strtotime( $date );
     642                        $date = gmdate( get_option( 'date_format' ), $date );
    648643                }
    649644
    650                 echo "</ul>\n";
    651 
    652         } else {
    653                 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";
    654         }
     645                echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
     646        }
     647
     648        echo "</ul>\n";
     649
    655650}
    656651
     
    699694        $widgets = get_option( 'dashboard_widget_options' );
    700695        @extract( @$widgets['dashboard_secondary'], EXTR_SKIP );
    701         $rss = @fetch_rss( $url );
    702 
    703         if ( !isset($rss->items) || 0 == count($rss->items) )
     696        $rss = @fetch_feed( $url );
     697
     698        if ( !$rss->get_item_quantity() )
    704699                return false;
    705 
    706         $rss->items = array_slice($rss->items, 0, $items);
    707 
    708         if ( 'http://planet.wordpress.org/' == $rss->channel['link'] ) {
    709                 foreach ( array_keys($rss->items) as $i ) {
    710                         list($site, $description) = explode( ':', wp_specialchars($rss->items[$i]['title']), 2 );
    711                         $rss->items[$i]['dc']['creator'] = trim($site);
    712                         $rss->items[$i]['title'] = trim($description);
    713                 }
    714         }
    715700
    716701        echo "<div class='rss-widget'>";
     
    733718 */
    734719function wp_dashboard_plugins_output() {
    735         $popular = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
    736         $new     = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
    737         $updated = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
     720        $popular = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
     721        $new     = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
     722        $updated = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
    738723
    739724        foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins'), 'updated' => __('Recently Updated') ) as $feed => $label ) {
    740                 if ( !isset($$feed->items) || 0 == count($$feed->items) )
     725                if ( !$$feed->get_item_quantity() )
    741726                        continue;
    742727
    743                 $$feed->items = array_slice($$feed->items, 0, 5);
    744                 $item_key = array_rand($$feed->items);
     728                $items = $$feed->get_items(0, 5);
     729                $item_key = array_rand($items);
    745730
    746731                // Eliminate some common badly formed plugin descriptions
    747                 while ( ( null !== $item_key = array_rand($$feed->items) ) && false !== strpos( $$feed->items[$item_key]['description'], 'Plugin Name:' ) )
    748                         unset($$feed->items[$item_key]);
    749 
    750                 if ( !isset($$feed->items[$item_key]) )
     732                while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
     733                        unset($items[$item_key]);
     734
     735                if ( !isset($items[$item_key]) )
    751736                        continue;
    752737
    753                 $item = $$feed->items[$item_key];
     738                $item = $items[$item_key];
    754739
    755740                // current bbPress feed item titles are: user on "topic title"
    756                 if ( preg_match( '/"(.*)"/s', $item['title'], $matches ) )
     741                if ( preg_match( '/&quot;(.*)&quot;/s', $item->get_title(), $matches ) )
    757742                        $title = $matches[1];
    758743                else // but let's make it forward compatible if things change
    759                         $title = $item['title'];
     744                        $title = $item->get_title();
    760745                $title = wp_specialchars( $title );
    761746
    762                 $description = wp_specialchars( strip_tags(html_entity_decode($item['description'], ENT_QUOTES)) );
    763 
    764                 list($link, $frag) = explode( '#', $item['link'] );
     747                $description = wp_specialchars( strip_tags(html_entity_decode($item->get_description(), ENT_QUOTES)) );
     748
     749                list($link, $frag) = explode( '#', $item->get_link() );
    765750
    766751                $link = clean_url($link);
     
    807792
    808793
    809         require_once( ABSPATH . WPINC . '/rss.php' );
    810         init(); // initialize rss constants
    811 
    812         $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
    813 
     794        /* TODO Cache check here.
    814795        foreach ( $check_urls as $check_url ) {
    815                 $status = $cache->check_cache( $check_url );
     796
    816797                if ( 'HIT' !== $status ) {
    817798                        echo $loading;
     
    819800                }
    820801        }
     802        */
     803
     804        // Always load async until above fixed.
     805        echo $loading;
     806        return false;
    821807
    822808        if ( $callback && is_callable( $callback ) ) {
     
    873859                // title is optional.  If black, fill it if possible
    874860                if ( !$widget_options[$widget_id]['title'] && isset($_POST['widget-rss'][$number]['title']) ) {
    875                         require_once(ABSPATH . WPINC . '/rss.php');
    876                         $rss = fetch_rss($widget_options[$widget_id]['url']);
    877                         $widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->channel['title']));
     861                        $rss = fetch_feed($widget_options[$widget_id]['url']);
     862                        $widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->get_title()));
    878863                }
    879864                update_option( 'dashboard_widget_options', $widget_options );
  • trunk/wp-includes/feed.php

    r10615 r10666  
    535535}
    536536
     537function fetch_feed($url) {
     538        require_once (ABSPATH . WPINC . '/class-feed.php');
     539
     540        $feed = new SimplePie();
     541        $feed->set_feed_url($url);
     542        $feed->set_cache_class('WP_Feed_Cache');
     543        $feed->set_cache_duration(43200);
     544        $feed->set_useragent('WordPress/' . $GLOBALS['wp_version']);
     545        $feed->init();
     546        $feed->handle_content_type();
     547
     548        return $feed;
     549}
     550
    537551?>
  • trunk/wp-includes/widgets.php

    r10535 r10666  
    15021502                return;
    15031503
    1504         require_once(ABSPATH . WPINC . '/rss.php');
    1505 
    1506         $rss = fetch_rss($url);
    1507         $link = clean_url(strip_tags($rss->channel['link']));
     1504        $rss = fetch_feed($url);
     1505        $link = clean_url(strip_tags($rss->get_permalink()));
    15081506        while ( strstr($link, 'http') != $link )
    15091507                $link = substr($link, 1);
    1510         $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
     1508        $desc = attribute_escape(strip_tags(html_entity_decode($rss->get_description(), ENT_QUOTES)));
    15111509        $title = $options[$number]['title'];
    15121510        if ( empty($title) )
    1513                 $title = htmlentities(strip_tags($rss->channel['title']));
     1511                $title = htmlentities(strip_tags($rss->get_title()));
    15141512        if ( empty($title) )
    15151513                $title = $desc;
     
    15421540function wp_widget_rss_output( $rss, $args = array() ) {
    15431541        if ( is_string( $rss ) ) {
    1544                 require_once(ABSPATH . WPINC . '/rss.php');
    1545                 if ( !$rss = fetch_rss($rss) )
     1542                if ( !$rss = fetch_feed($rss) )
    15461543                        return;
    15471544        } elseif ( is_array($rss) && isset($rss['url']) ) {
    1548                 require_once(ABSPATH . WPINC . '/rss.php');
    15491545                $args = $rss;
    1550                 if ( !$rss = fetch_rss($rss['url']) )
     1546                if ( !$rss = fetch_feed($rss['url']) )
    15511547                        return;
    15521548        } elseif ( !is_object($rss) ) {
     
    15651561        $show_date     = (int) $show_date;
    15661562
    1567         if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
    1568                 $rss->items = array_slice($rss->items, 0, $items);
    1569                 echo '<ul>';
    1570                 foreach ( (array) $rss->items as $item ) {
    1571                         while ( strstr($item['link'], 'http') != $item['link'] )
    1572                                 $item['link'] = substr($item['link'], 1);
    1573                         $link = clean_url(strip_tags($item['link']));
    1574                         $title = attribute_escape(strip_tags($item['title']));
    1575                         if ( empty($title) )
    1576                                 $title = __('Untitled');
    1577                         $desc = '';
    1578                         if ( isset( $item['description'] ) && is_string( $item['description'] ) )
    1579                                 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
    1580                         elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
    1581                                 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
    1582                         if ( 360 < strlen( $desc ) )
    1583                                 $desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
    1584                         $summary = $desc;
    1585 
    1586                         if ( $show_summary ) {
    1587                                 $desc = '';
    1588                                 $summary = wp_specialchars( $summary );
    1589                                 $summary = "<div class='rssSummary'>$summary</div>";
    1590                         } else {
    1591                                 $summary = '';
     1563        if ( !$rss->get_item_quantity() ) {
     1564                echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
     1565                return;
     1566        }
     1567
     1568        echo '<ul>';
     1569        foreach ( $rss->get_items(0, $items) as $item ) {
     1570                $link = $item->get_link();
     1571                while ( strstr($link, 'http') != $link )
     1572                        $link = substr($link, 1);
     1573                $link = clean_url(strip_tags($link));
     1574                $title = attribute_escape(strip_tags($item->get_title()));
     1575                if ( empty($title) )
     1576                        $title = __('Untitled');
     1577
     1578                $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item->get_description(), ENT_QUOTES))));
     1579                $desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
     1580                $desc = wp_specialchars( $desc );
     1581
     1582                if ( $show_summary ) {
     1583                        $summary = "<div class='rssSummary'>$desc</div>";
     1584                } else {
     1585                        $summary = '';
     1586                }
     1587
     1588                $date = '';
     1589                if ( $show_date ) {
     1590                        $date = $item->get_date();
     1591
     1592                        if ( $date ) {
     1593                                if ( $date_stamp = strtotime( $date ) )
     1594                                        $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
     1595                                else
     1596                                        $date = '';
    15921597                        }
    1593 
    1594                         $date = '';
    1595                         if ( $show_date ) {
    1596                                 if ( isset($item['pubdate']) )
    1597                                         $date = $item['pubdate'];
    1598                                 elseif ( isset($item['published']) )
    1599                                         $date = $item['published'];
    1600 
    1601                                 if ( $date ) {
    1602                                         if ( $date_stamp = strtotime( $date ) )
    1603                                                 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
    1604                                         else
    1605                                                 $date = '';
    1606                                 }
    1607                         }
    1608 
    1609                         $author = '';
    1610                         if ( $show_author ) {
    1611                                 if ( isset($item['dc']['creator']) )
    1612                                         $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
    1613                                 elseif ( isset($item['author_name']) )
    1614                                         $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
    1615                         }
    1616 
    1617                         if ( $link == '' ) {
    1618                                 echo "<li>$title{$date}{$summary}{$author}</li>";
    1619                         } else {
    1620                                 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
    1621                         }
    1622 }
    1623                 echo '</ul>';
    1624         } else {
    1625                 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
    1626         }
     1598                }
     1599
     1600                $author = '';
     1601                if ( $show_author ) {
     1602                        $author = $item->get_author();
     1603                        $author = $author->get_name();
     1604                        $author = ' <cite>' . wp_specialchars( strip_tags( $author ) ) . '</cite>';
     1605                }
     1606
     1607                if ( $link == '' ) {
     1608                        echo "<li>$title{$date}{$summary}{$author}</li>";
     1609                } else {
     1610                        echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
     1611                }
     1612        }
     1613        echo '</ul>';
    16271614}
    16281615
     
    18111798
    18121799        if ( $check_feed ) {
    1813                 require_once(ABSPATH . WPINC . '/rss.php');
    1814                 $rss = fetch_rss($url);
     1800                $rss = fetch_feed($url);
    18151801                $error = false;
    18161802                $link = '';
     
    18191805                        $error = sprintf(__('Error in RSS %1$d'), $widget_number );
    18201806                } else {
    1821                         $link = clean_url(strip_tags($rss->channel['link']));
     1807                        $link = clean_url(strip_tags($rss->get_permalink()));
    18221808                        while ( strstr($link, 'http') != $link )
    18231809                                $link = substr($link, 1);
Note: See TracChangeset for help on using the changeset viewer.