[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-rss.php (source)

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


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1