class-wp-widget-rss.php (4567B)
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 'show_instance_in_rest' => true, 29 30 ); 31 $control_ops = array( 32 'width' => 400, 33 'height' => 200, 34 ); 35 parent::__construct( 'rss', __( 'RSS' ), $widget_ops, $control_ops ); 36 } 37 38 /** 39 * Outputs the content for the current RSS widget instance. 40 * 41 * @since 2.8.0 42 * 43 * @param array $args Display arguments including 'before_title', 'after_title', 44 * 'before_widget', and 'after_widget'. 45 * @param array $instance Settings for the current RSS widget instance. 46 */ 47 public function widget( $args, $instance ) { 48 if ( isset( $instance['error'] ) && $instance['error'] ) { 49 return; 50 } 51 52 $url = ! empty( $instance['url'] ) ? $instance['url'] : ''; 53 while ( ! empty( $url ) && stristr( $url, 'http' ) !== $url ) { 54 $url = substr( $url, 1 ); 55 } 56 57 if ( empty( $url ) ) { 58 return; 59 } 60 61 // Self-URL destruction sequence. 62 if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ), true ) ) { 63 return; 64 } 65 66 $rss = fetch_feed( $url ); 67 $title = $instance['title']; 68 $desc = ''; 69 $link = ''; 70 71 if ( ! is_wp_error( $rss ) ) { 72 $desc = esc_attr( strip_tags( html_entity_decode( $rss->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) ); 73 if ( empty( $title ) ) { 74 $title = strip_tags( $rss->get_title() ); 75 } 76 $link = strip_tags( $rss->get_permalink() ); 77 while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) { 78 $link = substr( $link, 1 ); 79 } 80 } 81 82 if ( empty( $title ) ) { 83 $title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' ); 84 } 85 86 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 87 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 88 89 $url = strip_tags( $url ); 90 $icon = includes_url( 'images/rss.png' ); 91 if ( $title ) { 92 $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>'; 93 } 94 95 echo $args['before_widget']; 96 if ( $title ) { 97 echo $args['before_title'] . $title . $args['after_title']; 98 } 99 100 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 101 102 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 103 $format = apply_filters( 'navigation_widgets_format', $format ); 104 105 if ( 'html5' === $format ) { 106 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 107 $title = trim( strip_tags( $title ) ); 108 $aria_label = $title ? $title : __( 'RSS Feed' ); 109 echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">'; 110 } 111 112 wp_widget_rss_output( $rss, $instance ); 113 114 if ( 'html5' === $format ) { 115 echo '</nav>'; 116 } 117 118 echo $args['after_widget']; 119 120 if ( ! is_wp_error( $rss ) ) { 121 $rss->__destruct(); 122 } 123 unset( $rss ); 124 } 125 126 /** 127 * Handles updating settings for the current RSS widget instance. 128 * 129 * @since 2.8.0 130 * 131 * @param array $new_instance New settings for this instance as input by the user via 132 * WP_Widget::form(). 133 * @param array $old_instance Old settings for this instance. 134 * @return array Updated settings to save. 135 */ 136 public function update( $new_instance, $old_instance ) { 137 $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] !== $old_instance['url'] ) ) ); 138 return wp_widget_rss_process( $new_instance, $testurl ); 139 } 140 141 /** 142 * Outputs the settings form for the RSS widget. 143 * 144 * @since 2.8.0 145 * 146 * @param array $instance Current settings. 147 */ 148 public function form( $instance ) { 149 if ( empty( $instance ) ) { 150 $instance = array( 151 'title' => '', 152 'url' => '', 153 'items' => 10, 154 'error' => false, 155 'show_summary' => 0, 156 'show_author' => 0, 157 'show_date' => 0, 158 ); 159 } 160 $instance['number'] = $this->number; 161 162 wp_widget_rss_form( $instance ); 163 } 164 }