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


Generated: Mon Sep 28 01:00:02 2020 Cross-referenced by PHPXref 0.7.1