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