class-wp-widget-rss.php 3.6 KB

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