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