/wp-includes/widgets/class-wp-widget-rss.php

  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. * @access public 
  24. */ 
  25. public function __construct() { 
  26. $widget_ops = array( 
  27. 'description' => __( 'Entries from any RSS or Atom feed.' ),  
  28. 'customize_selective_refresh' => true,  
  29. ); 
  30. $control_ops = array( 'width' => 400, 'height' => 200 ); 
  31. parent::__construct( 'rss', __( 'RSS' ), $widget_ops, $control_ops ); 
  32.  
  33. /** 
  34. * Outputs the content for the current RSS widget instance. 
  35. * 
  36. * @since 2.8.0 
  37. * @access public 
  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 ( 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() ) ) ) 
  56. return; 
  57.  
  58. $rss = fetch_feed($url); 
  59. $title = $instance['title']; 
  60. $desc = ''; 
  61. $link = ''; 
  62.  
  63. if ( ! is_wp_error($rss) ) { 
  64. $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset')))); 
  65. if ( empty($title) ) 
  66. $title = strip_tags( $rss->get_title() ); 
  67. $link = strip_tags( $rss->get_permalink() ); 
  68. while ( stristr($link, 'http') != $link ) 
  69. $link = substr($link, 1); 
  70.  
  71. if ( empty($title) ) 
  72. $title = empty($desc) ? __('Unknown Feed') : $desc; 
  73.  
  74. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  75. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 
  76.  
  77. $url = strip_tags( $url ); 
  78. $icon = includes_url( 'images/rss.png' ); 
  79. if ( $title ) 
  80. $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>'; 
  81.  
  82. echo $args['before_widget']; 
  83. if ( $title ) { 
  84. echo $args['before_title'] . $title . $args['after_title']; 
  85. wp_widget_rss_output( $rss, $instance ); 
  86. echo $args['after_widget']; 
  87.  
  88. if ( ! is_wp_error($rss) ) 
  89. $rss->__destruct(); 
  90. unset($rss); 
  91.  
  92. /** 
  93. * Handles updating settings for the current RSS widget instance. 
  94. * 
  95. * @since 2.8.0 
  96. * @access public 
  97. * 
  98. * @param array $new_instance New settings for this instance as input by the user via 
  99. * WP_Widget::form(). 
  100. * @param array $old_instance Old settings for this instance. 
  101. * @return array Updated settings to save. 
  102. */ 
  103. public function update( $new_instance, $old_instance ) { 
  104. $testurl = ( isset( $new_instance['url'] ) && ( !isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) ); 
  105. return wp_widget_rss_process( $new_instance, $testurl ); 
  106.  
  107. /** 
  108. * Outputs the settings form for the RSS widget. 
  109. * 
  110. * @since 2.8.0 
  111. * @access public 
  112. * 
  113. * @param array $instance Current settings. 
  114. */ 
  115. public function form( $instance ) { 
  116. if ( empty( $instance ) ) { 
  117. $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 ); 
  118. $instance['number'] = $this->number; 
  119.  
  120. wp_widget_rss_form( $instance ); 
.