Make WordPress Core


Ignore:
Timestamp:
04/27/2009 08:46:47 AM (15 years ago)
Author:
azaozz
Message:

Move RSS widget to WP_Widget, see #8441

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r11090 r11093  
    506506    function widget($args, $instance) {
    507507        $cache = wp_cache_get('widget_recent_posts', 'widget');
    508        
     508
    509509        if ( !is_array($cache) )
    510510            $cache = array();
    511        
     511
    512512        if ( isset($cache[$args['widget_id']]) )
    513513            return $cache[$args['widget_id']];
     
    548548        $instance['number'] = (int) $new_instance['number'];
    549549        $this->flush_widget_cache();
    550        
     550
    551551        $alloptions = wp_cache_get( 'alloptions', 'options' );
    552552        if ( isset($alloptions['widget_recent_entries']) )
     
    595595        add_action( 'wp_set_comment_status', array(&$this, 'flush_widget_cache') );
    596596    }
    597    
     597
    598598    function recent_comments_style() { ?>
    599599    <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
     
    607607    function widget( $args, $instance ) {
    608608        global $wpdb, $comments, $comment;
    609        
     609
    610610        extract($args, EXTR_SKIP);
    611611        $title = empty($instance['title']) ? __('Recent Comments') : apply_filters('widget_title', $instance['title']);
     
    616616        else if ( $number > 15 )
    617617            $number = 15;
    618    
     618
    619619        if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
    620620            $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 15");
    621621            wp_cache_add( 'recent_comments', $comments, 'widget' );
    622622        }
    623        
     623
    624624        $comments = array_slice( (array) $comments, 0, $number );
    625625?>
     
    665665
    666666/**
    667  * Display RSS widget.
    668  *
    669  * Allows for multiple widgets to be displayed.
    670  *
    671  * @since 2.2.0
    672  *
    673  * @param array $args Widget arguments.
    674  * @param int $number Widget number.
    675  */
    676 function wp_widget_rss($args, $widget_args = 1) {
    677     extract($args, EXTR_SKIP);
    678     if ( is_numeric($widget_args) )
    679         $widget_args = array( 'number' => $widget_args );
    680     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    681     extract($widget_args, EXTR_SKIP);
    682 
    683     $options = get_option('widget_rss');
    684 
    685     if ( !isset($options[$number]) )
    686         return;
    687 
    688     if ( isset($options[$number]['error']) && $options[$number]['error'] )
    689         return;
    690 
    691     $url = $options[$number]['url'];
    692     while ( stristr($url, 'http') != $url )
    693         $url = substr($url, 1);
    694     if ( empty($url) )
    695         return;
    696 
    697     $rss = fetch_feed($url);
    698     $title = $options[$number]['title'];
    699     $desc = '';
    700     $link = '';
    701     if ( ! is_wp_error($rss) ) {
    702         $desc = attribute_escape(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
     667 * RSS widget class
     668 *
     669 * @since 2.8.0
     670 */
     671class WP_Widget_RSS extends WP_Widget {
     672
     673    function WP_Widget_RSS() {
     674        $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed') );
     675        $control_ops = array( 'width' => 400, 'height' => 200 );
     676        $this->WP_Widget( 'rss', __('RSS'), $widget_ops, $control_ops );
     677    }
     678
     679    function widget($args, $instance) {
     680
     681        if ( isset($instance['error']) && $instance['error'] )
     682            return;
     683
     684        extract($args, EXTR_SKIP);
     685
     686        $url = $instance['url'];
     687        while ( stristr($url, 'http') != $url )
     688            $url = substr($url, 1);
     689
     690        if ( empty($url) )
     691            return;
     692
     693        $rss = fetch_feed($url);
     694        $title = $instance['title'];
     695        $desc = '';
     696        $link = '';
     697
     698        if ( ! is_wp_error($rss) ) {
     699            $desc = attribute_escape(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
     700            if ( empty($title) )
     701                $title = htmlentities(strip_tags($rss->get_title()));
     702            $link = clean_url(strip_tags($rss->get_permalink()));
     703            while ( stristr($link, 'http') != $link )
     704                $link = substr($link, 1);
     705        }
     706
    703707        if ( empty($title) )
    704             $title = htmlentities(strip_tags($rss->get_title()));
    705         $link = clean_url(strip_tags($rss->get_permalink()));
    706         while ( stristr($link, 'http') != $link )
    707             $link = substr($link, 1);
    708     }
    709     if ( empty($title) )
    710         $title = $desc;
    711     if ( empty($title) )
    712         $title = __('Unknown Feed');
    713     $title = apply_filters('widget_title', $title );
    714     $url = clean_url(strip_tags($url));
    715     $icon = includes_url('images/rss.png');
    716     $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('Syndicate this content')) ."'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
    717 
    718     echo $before_widget;
    719     echo $before_title . $title . $after_title;
    720 
    721     wp_widget_rss_output( $rss, $options[$number] );
    722 
    723     echo $after_widget;
     708            $title = empty($desc) ? __('Unknown Feed') : $desc;
     709
     710        $title = apply_filters('widget_title', $title );
     711        $url = clean_url(strip_tags($url));
     712        $icon = includes_url('images/rss.png');
     713        $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('Syndicate this content')) ."'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
     714
     715        echo $before_widget;
     716        echo $before_title . $title . $after_title;
     717        wp_widget_rss_output( $rss, $instance );
     718        echo $after_widget;
     719    }
     720
     721    function update($new_instance, $old_instance) {
     722        $testurl = $new_instance['url'] != $old_instance['url'];
     723        return wp_widget_rss_process( $new_instance, $testurl );
     724    }
     725
     726    function form($instance) {
     727
     728        if ( empty($instance) )
     729            $instance = array( 'number' => '__i__', 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
     730        else
     731            $instance['number'] = $this->number;
     732
     733        wp_widget_rss_form( $instance );
     734    }
    724735}
    725736
     
    743754
    744755    if ( is_wp_error($rss) ) {
    745         if ( is_admin() || current_user_can('manage_options') ) {
    746             echo '<p>';
    747             printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
    748             echo '</p>';
    749         }
     756        if ( is_admin() || current_user_can('manage_options') )
     757            echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
     758
    750759        return;
    751760    }
     
    815824}
    816825
    817 /**
    818  * Display and process RSS widget control form.
    819  *
    820  * @since 2.2.0
    821  *
    822  * @param int $widget_args Widget number.
    823  */
    824 function wp_widget_rss_control($widget_args) {
    825     global $wp_registered_widgets;
    826     static $updated = false;
    827 
    828     if ( is_numeric($widget_args) )
    829         $widget_args = array( 'number' => $widget_args );
    830     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    831     extract($widget_args, EXTR_SKIP);
    832 
    833     $options = get_option('widget_rss');
    834     if ( !is_array($options) )
    835         $options = array();
    836 
    837     $urls = array();
    838     foreach ( (array) $options as $option )
    839         if ( isset($option['url']) )
    840             $urls[$option['url']] = true;
    841 
    842     if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
    843         $sidebar = (string) $_POST['sidebar'];
    844 
    845         $sidebars_widgets = wp_get_sidebars_widgets();
    846         if ( isset($sidebars_widgets[$sidebar]) )
    847             $this_sidebar =& $sidebars_widgets[$sidebar];
    848         else
    849             $this_sidebar = array();
    850 
    851         foreach ( (array) $this_sidebar as $_widget_id ) {
    852             if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    853                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    854                 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
    855                     unset($options[$widget_number]);
    856             }
    857         }
    858 
    859         foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
    860             if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
    861                 continue;
    862             $widget_rss = stripslashes_deep( $widget_rss );
    863             $url = sanitize_url(strip_tags($widget_rss['url']));
    864             $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
    865         }
    866 
    867         update_option('widget_rss', $options);
    868         $updated = true;
    869     }
    870 
    871     if ( -1 == $number ) {
    872         $title = '';
    873         $url = '';
    874         $items = 10;
    875         $error = false;
    876         $number = '__i__';
    877         $show_summary = 0;
    878         $show_author = 0;
    879         $show_date = 0;
    880     } else {
    881         extract( (array) $options[$number] );
    882     }
    883 
    884     wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
    885 }
     826
    886827
    887828/**
     
    914855    $show_date      = (int) $show_date;
    915856
    916     if ( !empty($error) ) {
    917         $message = sprintf( __('Error in RSS Widget: %s'), $error);
    918         echo "<div class='error'><p>$message</p></div>";
    919         echo "<p class='hide-if-no-js'><strong>$message</strong></p>";
    920     }
     857    if ( !empty($error) )
     858        echo '<p class="widget-error"><strong>' . sprintf( __('RSS Error: %s'), $error) . '</strong></p>';
    921859
    922860    if ( $inputs['url'] ) :
     
    965903        </label>
    966904    </p>
    967     <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
    968905<?php
    969906    endif;
     
    1020957}
    1021958
    1022 /**
    1023  * Register RSS widget to allow multiple RSS widgets on startup.
    1024  *
    1025  * @since 2.2.0
    1026  */
    1027 function wp_widget_rss_register() {
    1028     $options = get_option('widget_rss');
    1029     if ( !is_array($options) )
    1030         $options = array();
    1031 
    1032     $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
    1033     $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
    1034     $name = __('RSS');
    1035 
    1036     $id = false;
    1037     foreach ( (array) array_keys($options) as $o ) {
    1038         // Old widgets can have null values for some reason
    1039         if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
    1040             continue;
    1041         $id = "rss-$o"; // Never never never translate an id
    1042         wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
    1043         wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
    1044     }
    1045 
    1046     // If there are none, we register the widget's existance with a generic template
    1047     if ( !$id ) {
    1048         wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
    1049         wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
    1050     }
    1051 }
     959
    1052960
    1053961/**
     
    11261034
    11271035    register_widget('WP_Widget_Recent_Posts');
    1128    
     1036
    11291037    register_widget('WP_Widget_Recent_Comments');
     1038
     1039    register_widget('WP_Widget_RSS');
    11301040
    11311041    $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
     
    11331043    wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
    11341044
    1135     wp_widget_rss_register();
    1136 
    11371045    do_action('widgets_init');
    11381046}
Note: See TracChangeset for help on using the changeset viewer.