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