| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 if ( $title ) { 90 $feed_url = strip_tags( $url ); 91 $feed_icon = includes_url( 'images/rss.png' ); 92 93 $feed_link = sprintf( 94 '<a class="rsswidget rss-widget-feed" href="%1$s"><img class="rss-widget-icon" style="border:0" width="14" height="14" src="%2$s" alt="%3$s"%4$s /></a> ', 95 esc_url( $feed_url ), 96 esc_url( $feed_icon ), 97 /* translators: %s: RSS feed title. */ 98 esc_attr( sprintf( __( 'RSS feed: %s' ), wp_strip_all_tags( $title ) ) ), 99 ( wp_lazy_loading_enabled( 'img', 'rss_widget_feed_icon' ) ? ' loading="lazy"' : '' ) 100 ); 101 102 /** 103 * Filters the classic RSS widget's feed icon link. 104 * 105 * Themes can remove the icon link by using `add_filter( 'rss_widget_feed_link', '__return_empty_string' );`. 106 * 107 * @since 5.9.0 108 * 109 * @param string|false $feed_link HTML for link to RSS feed. 110 * @param array $instance Array of settings for the current widget. 111 */ 112 $feed_link = apply_filters( 'rss_widget_feed_link', $feed_link, $instance ); 113 114 $title = $feed_link . '<a class="rsswidget rss-widget-title" href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>'; 115 } 116 117 echo $args['before_widget']; 118 if ( $title ) { 119 echo $args['before_title'] . $title . $args['after_title']; 120 } 121 122 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 123 124 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 125 $format = apply_filters( 'navigation_widgets_format', $format ); 126 127 if ( 'html5' === $format ) { 128 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 129 $title = trim( strip_tags( $title ) ); 130 $aria_label = $title ? $title : __( 'RSS Feed' ); 131 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">'; 132 } 133 134 wp_widget_rss_output( $rss, $instance ); 135 136 if ( 'html5' === $format ) { 137 echo '</nav>'; 138 } 139 140 echo $args['after_widget']; 141 142 if ( ! is_wp_error( $rss ) ) { 143 $rss->__destruct(); 144 } 145 unset( $rss ); 146 } 147 148 /** 149 * Handles updating settings for the current RSS widget instance. 150 * 151 * @since 2.8.0 152 * 153 * @param array $new_instance New settings for this instance as input by the user via 154 * WP_Widget::form(). 155 * @param array $old_instance Old settings for this instance. 156 * @return array Updated settings to save. 157 */ 158 public function update( $new_instance, $old_instance ) { 159 $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] !== $old_instance['url'] ) ) ); 160 return wp_widget_rss_process( $new_instance, $testurl ); 161 } 162 163 /** 164 * Outputs the settings form for the RSS widget. 165 * 166 * @since 2.8.0 167 * 168 * @param array $instance Current settings. 169 */ 170 public function form( $instance ) { 171 if ( empty( $instance ) ) { 172 $instance = array( 173 'title' => '', 174 'url' => '', 175 'items' => 10, 176 'error' => false, 177 'show_summary' => 0, 178 'show_author' => 0, 179 'show_date' => 0, 180 ); 181 } 182 $instance['number'] = $this->number; 183 184 wp_widget_rss_form( $instance ); 185 } 186 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jul 29 08:20:18 2026 | Cross-referenced by PHPXref |