]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blob - wp-includes/widgets/class-wp-widget-rss.php
169e129e28edbc02e984cf9ebe14bdd15c7b684b
[autoinstallsdev/wordpress.git] / wp-includes / widgets / class-wp-widget-rss.php
1 <?php
2 /**
3  * Widget API: WP_Widget_RSS class
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to implement a RSS widget.
12  *
13  * @since 2.8.0
14  *
15  * @see WP_Widget
16  */
17 class WP_Widget_RSS extends WP_Widget {
18
19         /**
20          * Sets up a new RSS widget instance.
21          *
22          * @since 2.8.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed.') );
27                 $control_ops = array( 'width' => 400, 'height' => 200 );
28                 parent::__construct( 'rss', __('RSS'), $widget_ops, $control_ops );
29         }
30
31         /**
32          * Outputs the content for the current RSS widget instance.
33          *
34          * @since 2.8.0
35          * @access public
36          *
37          * @param array $args     Display arguments including 'before_title', 'after_title',
38          *                        'before_widget', and 'after_widget'.
39          * @param array $instance Settings for the current RSS widget instance.
40          */
41         public function widget( $args, $instance ) {
42                 if ( isset($instance['error']) && $instance['error'] )
43                         return;
44
45                 $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
46                 while ( stristr($url, 'http') != $url )
47                         $url = substr($url, 1);
48
49                 if ( empty($url) )
50                         return;
51
52                 // self-url destruction sequence
53                 if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) )
54                         return;
55
56                 $rss = fetch_feed($url);
57                 $title = $instance['title'];
58                 $desc = '';
59                 $link = '';
60
61                 if ( ! is_wp_error($rss) ) {
62                         $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
63                         if ( empty($title) )
64                                 $title = strip_tags( $rss->get_title() );
65                         $link = strip_tags( $rss->get_permalink() );
66                         while ( stristr($link, 'http') != $link )
67                                 $link = substr($link, 1);
68                 }
69
70                 if ( empty($title) )
71                         $title = empty($desc) ? __('Unknown Feed') : $desc;
72
73                 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
74                 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
75
76                 $url = strip_tags( $url );
77                 $icon = includes_url( 'images/rss.png' );
78                 if ( $title )
79                         $title = '<a class="rsswidget" href="' . esc_url( $url ) . '"><img class="rss-widget-icon" style="border:0" width="14" height="14" src="' . esc_url( $icon ) . '" alt="RSS" /></a> <a class="rsswidget" href="' . esc_url( $link ) . '">"'. esc_html( $title ) .'"</a>';
80
81                 echo $args['before_widget'];
82                 if ( $title ) {
83                         echo $args['before_title'] . $title . $args['after_title'];
84                 }
85                 wp_widget_rss_output( $rss, $instance );
86                 echo $args['after_widget'];
87
88                 if ( ! is_wp_error($rss) )
89                         $rss->__destruct();
90                 unset($rss);
91         }
92
93         /**
94          * Handles updating settings for the current RSS widget instance.
95          *
96          * @since 2.8.0
97          * @access public
98          *
99          * @param array $new_instance New settings for this instance as input by the user via
100          *                            WP_Widget::form().
101          * @param array $old_instance Old settings for this instance.
102          * @return array Updated settings to save.
103          */
104         public function update( $new_instance, $old_instance ) {
105                 $testurl = ( isset( $new_instance['url'] ) && ( !isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) );
106                 return wp_widget_rss_process( $new_instance, $testurl );
107         }
108
109         /**
110          * Outputs the settings form for the RSS widget.
111          *
112          * @since 2.8.0
113          * @access public
114          *
115          * @param array $instance Current settings.
116          */
117         public function form( $instance ) {
118                 if ( empty( $instance ) ) {
119                         $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
120                 }
121                 $instance['number'] = $this->number;
122
123                 wp_widget_rss_form( $instance );
124         }
125 }