You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

129 lines
3.7 KiB

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