class-wp-widget-rss.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Core class used to implement a RSS widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_RSS extends WP_Widget {
  17. /**
  18. * Sets up a new RSS widget instance.
  19. *
  20. * @since 2.8.0
  21. */
  22. public function __construct() {
  23. $widget_ops = array(
  24. 'description' => __( 'Entries from any RSS or Atom feed.' ),
  25. 'customize_selective_refresh' => true,
  26. 'show_instance_in_rest' => true,
  27. );
  28. $control_ops = array(
  29. 'width' => 400,
  30. 'height' => 200,
  31. );
  32. parent::__construct( 'rss', __( 'RSS' ), $widget_ops, $control_ops );
  33. }
  34. /**
  35. * Outputs the content for the current RSS widget instance.
  36. *
  37. * @since 2.8.0
  38. *
  39. * @param array $args Display arguments including 'before_title', 'after_title',
  40. * 'before_widget', and 'after_widget'.
  41. * @param array $instance Settings for the current RSS widget instance.
  42. */
  43. public function widget( $args, $instance ) {
  44. if ( isset( $instance['error'] ) && $instance['error'] ) {
  45. return;
  46. }
  47. $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
  48. while ( ! empty( $url ) && stristr( $url, 'http' ) !== $url ) {
  49. $url = substr( $url, 1 );
  50. }
  51. if ( empty( $url ) ) {
  52. return;
  53. }
  54. // Self-URL destruction sequence.
  55. if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ), true ) ) {
  56. return;
  57. }
  58. $rss = fetch_feed( $url );
  59. $title = $instance['title'];
  60. $desc = '';
  61. $link = '';
  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. }
  67. $link = strip_tags( $rss->get_permalink() );
  68. while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
  69. $link = substr( $link, 1 );
  70. }
  71. }
  72. if ( empty( $title ) ) {
  73. $title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' );
  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. if ( $title ) {
  78. $feed_link = '';
  79. $feed_url = strip_tags( $url );
  80. $feed_icon = includes_url( 'images/rss.png' );
  81. $feed_link = sprintf(
  82. '<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> ',
  83. esc_url( $feed_url ),
  84. esc_url( $feed_icon ),
  85. esc_attr__( 'RSS' ),
  86. ( wp_lazy_loading_enabled( 'img', 'rss_widget_feed_icon' ) ? ' loading="lazy"' : '' )
  87. );
  88. /**
  89. * Filters the classic RSS widget's feed icon link.
  90. *
  91. * Themes can remove the icon link by using `add_filter( 'rss_widget_feed_link', '__return_false' );`.
  92. *
  93. * @since 5.9.0
  94. *
  95. * @param string $feed_link HTML for link to RSS feed.
  96. * @param array $instance Array of settings for the current widget.
  97. */
  98. $feed_link = apply_filters( 'rss_widget_feed_link', $feed_link, $instance );
  99. $title = $feed_link . '<a class="rsswidget rss-widget-title" href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>';
  100. }
  101. echo $args['before_widget'];
  102. if ( $title ) {
  103. echo $args['before_title'] . $title . $args['after_title'];
  104. }
  105. $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  106. /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  107. $format = apply_filters( 'navigation_widgets_format', $format );
  108. if ( 'html5' === $format ) {
  109. // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  110. $title = trim( strip_tags( $title ) );
  111. $aria_label = $title ? $title : __( 'RSS Feed' );
  112. echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
  113. }
  114. wp_widget_rss_output( $rss, $instance );
  115. if ( 'html5' === $format ) {
  116. echo '</nav>';
  117. }
  118. echo $args['after_widget'];
  119. if ( ! is_wp_error( $rss ) ) {
  120. $rss->__destruct();
  121. }
  122. unset( $rss );
  123. }
  124. /**
  125. * Handles updating settings for the current RSS widget instance.
  126. *
  127. * @since 2.8.0
  128. *
  129. * @param array $new_instance New settings for this instance as input by the user via
  130. * WP_Widget::form().
  131. * @param array $old_instance Old settings for this instance.
  132. * @return array Updated settings to save.
  133. */
  134. public function update( $new_instance, $old_instance ) {
  135. $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] !== $old_instance['url'] ) ) );
  136. return wp_widget_rss_process( $new_instance, $testurl );
  137. }
  138. /**
  139. * Outputs the settings form for the RSS widget.
  140. *
  141. * @since 2.8.0
  142. *
  143. * @param array $instance Current settings.
  144. */
  145. public function form( $instance ) {
  146. if ( empty( $instance ) ) {
  147. $instance = array(
  148. 'title' => '',
  149. 'url' => '',
  150. 'items' => 10,
  151. 'error' => false,
  152. 'show_summary' => 0,
  153. 'show_author' => 0,
  154. 'show_date' => 0,
  155. );
  156. }
  157. $instance['number'] = $this->number;
  158. wp_widget_rss_form( $instance );
  159. }
  160. }