WordPress.org

Make WordPress Core

Changeset 11093


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

Move RSS widget to WP_Widget, see #8441

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/widgets.dev.js

    r10926 r11093  
    1616        });
    1717        this.addEvents();
     18        $('.widget-error').parents('.widget').find('a.widget-action').click();
    1819
    1920        $('#widget-list .widget').draggable({
  • trunk/wp-admin/js/widgets.js

    r10926 r11093  
    1 var wpWidgets;(function(a){wpWidgets={init:function(){var b;a("h3.sidebar-name").click(function(){var d=a(this).siblings(".widgets-sortables");if(d.is(":visible")){d.hide().sortable("disable")}else{d.show().sortable("enable").sortable("refresh")}a(this).siblings("#widget-list").toggle()});this.addEvents();a("#widget-list .widget").draggable({connectToSortable:".widgets-sortables",handle:".widget-title",distance:2,helper:"clone",zIndex:5,start:function(){wpWidgets.fixWebkit(1)},stop:function(d,c){if(b){a(b).hide()}b="";wpWidgets.fixWebkit()}});a(".widgets-sortables").sortable({placeholder:"widget-placeholder",connectWith:".widgets-sortables",items:".widget",handle:".widget-title",cursor:"move",distance:2,opacity:0.65,start:function(d,c){wpWidgets.fixWebkit(1);c.item.find(".widget-inside").hide();c.item.css({marginLeft:"",width:""})},stop:function(f,c){var d=c.item.find("input.add_new").val(),i=c.item.find("input.multi_number").val(),h=c.item.attr("id"),g=a(this).parent().attr("id");c.item.css({marginLeft:"",width:""});if(d){if("multi"==d){c.item.html(c.item.html().replace(/<[^<>]+>/g,function(e){return e.replace(/__i__/g,i)}));c.item.attr("id",h.replace(/__i__/g,i));i++;a("li#"+h).find("input.multi_number").val(i)}else{if("single"==d){c.item.attr("id","new-"+h);b="li#"+h}}wpWidgets.addEvents(c.item);wpWidgets.save(c.item.find("form").serializeArray(),g,0,0);c.item.find("input.add_new").val("");c.item.find("a.widget-action").click()}wpWidgets.saveOrder(g);wpWidgets.fixWebkit()},receive:function(d,c){if(!a(this).is(":visible")){a(this).sortable("cancel")}}}).not(":visible").sortable("disable")},saveOrder:function(c){a("#"+c+" .ajax-feedback").css("visibility","visible");var b={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};a(".widgets-sortables").each(function(){b["sidebars["+a(this).parent().attr("id")+"]"]=a(this).sortable("toArray").join(",")});a.post(ajaxurl,b,function(){a(".ajax-feedback").css("visibility","hidden")})},save:function(e,f,c,d){a("#"+f+" .ajax-feedback").css("visibility","visible");var b={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:f};if(c){b.delete_widget=1}a.map(e,function(h,g){b[h.name]=h.value});a.post(ajaxurl,b,function(g){var h;a(".ajax-feedback").css("visibility","hidden");if(!d){return}if(c){a(d).parents("li.widget").slideUp("normal",function(){a(this).remove()});if(!b.widget_number){h=b["widget-id"];a("#available-widgets .widget-id").each(function(){if(a(this).val()==h){a(this).parents("li.widget").show()}})}}else{a(d).parents(".widget-inside").slideUp("normal",function(){a(this).parents("li.widget").css({width:"",marginLeft:""})})}})},fixWebkit:function(b){b=b?"none":"";a("body").css({WebkitUserSelect:b,KhtmlUserSelect:b})},addEvents:function(b){b=b||document;a("a.widget-action",b).click(function(){var d=parseInt(a(this).parents(".widget").find(".widget-width").val(),10),e={},c=a(this).parents(".widget-top").siblings(".widget-inside");if(c.is(":hidden")){if(d>270&&c.parents(".widgets-sortables").length){e.width=d+30+"px";if(c.parents(".widget-liquid-right").length){e.marginLeft=270-d+"px"}c.parents(".widget").css(e)}c.slideDown("normal")}else{c.slideUp("normal",function(){c.parents(".widget").css({width:"",marginLeft:""})})}return false});a(".widget-control-save",b).click(function(){wpWidgets.save(a(this).parents("form").serializeArray(),a(this).parents(".widgets-holder-wrap").attr("id"),0,this);return false});a(".widget-control-remove",b).click(function(){wpWidgets.save(a(this).parents("form").serializeArray(),a(this).parents(".widgets-holder-wrap").attr("id"),1,this);return false})}};a(document).ready(function(){wpWidgets.init()})})(jQuery);
     1var wpWidgets;(function(a){wpWidgets={init:function(){var b;a("h3.sidebar-name").click(function(){var d=a(this).siblings(".widgets-sortables");if(d.is(":visible")){d.hide().sortable("disable")}else{d.show().sortable("enable").sortable("refresh")}a(this).siblings("#widget-list").toggle()});this.addEvents();a(".widget-error").parents(".widget").find("a.widget-action").click();a("#widget-list .widget").draggable({connectToSortable:".widgets-sortables",handle:".widget-title",distance:2,helper:"clone",zIndex:5,start:function(){wpWidgets.fixWebkit(1)},stop:function(d,c){if(b){a(b).hide()}b="";wpWidgets.fixWebkit()}});a(".widgets-sortables").sortable({placeholder:"widget-placeholder",connectWith:".widgets-sortables",items:".widget",handle:".widget-title",cursor:"move",distance:2,opacity:0.65,start:function(d,c){wpWidgets.fixWebkit(1);c.item.find(".widget-inside").hide();c.item.css({marginLeft:"",width:""})},stop:function(f,c){var d=c.item.find("input.add_new").val(),i=c.item.find("input.multi_number").val(),h=c.item.attr("id"),g=a(this).parent().attr("id");c.item.css({marginLeft:"",width:""});if(d){if("multi"==d){c.item.html(c.item.html().replace(/<[^<>]+>/g,function(e){return e.replace(/__i__/g,i)}));c.item.attr("id",h.replace(/__i__/g,i));i++;a("li#"+h).find("input.multi_number").val(i)}else{if("single"==d){c.item.attr("id","new-"+h);b="li#"+h}}wpWidgets.addEvents(c.item);wpWidgets.save(c.item.find("form").serializeArray(),g,0,0);c.item.find("input.add_new").val("");c.item.find("a.widget-action").click()}wpWidgets.saveOrder(g);wpWidgets.fixWebkit()},receive:function(d,c){if(!a(this).is(":visible")){a(this).sortable("cancel")}}}).not(":visible").sortable("disable")},saveOrder:function(c){a("#"+c+" .ajax-feedback").css("visibility","visible");var b={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};a(".widgets-sortables").each(function(){b["sidebars["+a(this).parent().attr("id")+"]"]=a(this).sortable("toArray").join(",")});a.post(ajaxurl,b,function(){a(".ajax-feedback").css("visibility","hidden")})},save:function(e,f,c,d){a("#"+f+" .ajax-feedback").css("visibility","visible");var b={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:f};if(c){b.delete_widget=1}a.map(e,function(h,g){b[h.name]=h.value});a.post(ajaxurl,b,function(g){var h;a(".ajax-feedback").css("visibility","hidden");if(!d){return}if(c){a(d).parents("li.widget").slideUp("normal",function(){a(this).remove()});if(!b.widget_number){h=b["widget-id"];a("#available-widgets .widget-id").each(function(){if(a(this).val()==h){a(this).parents("li.widget").show()}})}}else{a(d).parents(".widget-inside").slideUp("normal",function(){a(this).parents("li.widget").css({width:"",marginLeft:""})})}})},fixWebkit:function(b){b=b?"none":"";a("body").css({WebkitUserSelect:b,KhtmlUserSelect:b})},addEvents:function(b){b=b||document;a("a.widget-action",b).click(function(){var d=parseInt(a(this).parents(".widget").find(".widget-width").val(),10),e={},c=a(this).parents(".widget-top").siblings(".widget-inside");if(c.is(":hidden")){if(d>270&&c.parents(".widgets-sortables").length){e.width=d+30+"px";if(c.parents(".widget-liquid-right").length){e.marginLeft=270-d+"px"}c.parents(".widget").css(e)}c.slideDown("normal")}else{c.slideUp("normal",function(){c.parents(".widget").css({width:"",marginLeft:""})})}return false});a(".widget-control-save",b).click(function(){wpWidgets.save(a(this).parents("form").serializeArray(),a(this).parents(".widgets-holder-wrap").attr("id"),0,this);return false});a(".widget-control-remove",b).click(function(){wpWidgets.save(a(this).parents("form").serializeArray(),a(this).parents(".widgets-holder-wrap").attr("id"),1,this);return false})}};a(document).ready(function(){wpWidgets.init()})})(jQuery);
  • 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}
  • trunk/wp-includes/script-loader.php

    r11078 r11093  
    331331        $scripts->add_data( 'media-upload', 'group', 1 );
    332332
    333         $scripts->add( 'admin-widgets', "/wp-admin/js/widgets$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable' ), '20090413a' );
     333        $scripts->add( 'admin-widgets', "/wp-admin/js/widgets$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable' ), '20090426' );
    334334        $scripts->add_data( 'admin-widgets', 'group', 1 );
    335335
Note: See TracChangeset for help on using the changeset viewer.