class-wp-widget-rss.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(
  28. 'width' => 400,
  29. 'height' => 200,
  30. );
  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. *
  38. * @param array $args Display arguments including 'before_title', 'after_title',
  39. * 'before_widget', and 'after_widget'.
  40. * @param array $instance Settings for the current RSS widget instance.
  41. */
  42. public function widget( $args, $instance ) {
  43. if ( isset( $instance['error'] ) && $instance['error'] ) {
  44. return;
  45. }
  46. $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
  47. while ( stristr( $url, 'http' ) != $url ) {
  48. $url = substr( $url, 1 );
  49. }
  50. if ( empty( $url ) ) {
  51. return;
  52. }
  53. // self-url destruction sequence
  54. if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) ) {
  55. return;
  56. }
  57. $rss = fetch_feed( $url );
  58. $title = $instance['title'];
  59. $desc = '';
  60. $link = '';
  61. if ( ! is_wp_error( $rss ) ) {
  62. $desc = esc_attr( strip_tags( html_entity_decode( $rss->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) );
  63. if ( empty( $title ) ) {
  64. $title = strip_tags( $rss->get_title() );
  65. }
  66. $link = strip_tags( $rss->get_permalink() );
  67. while ( stristr( $link, 'http' ) != $link ) {
  68. $link = substr( $link, 1 );
  69. }
  70. }
  71. if ( empty( $title ) ) {
  72. $title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' );
  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. $url = strip_tags( $url );
  77. $icon = includes_url( 'images/rss.png' );
  78. if ( $title ) {
  79. $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>';
  80. }
  81. echo $args['before_widget'];
  82. if ( $title ) {
  83. echo $args['before_title'] . $title . $args['after_title'];
  84. }
  85. wp_widget_rss_output( $rss, $instance );
  86. echo $args['after_widget'];
  87. if ( ! is_wp_error( $rss ) ) {
  88. $rss->__destruct();
  89. }
  90. unset( $rss );
  91. }
  92. /**
  93. * Handles updating settings for the current RSS widget instance.
  94. *
  95. * @since 2.8.0
  96. *
  97. * @param array $new_instance New settings for this instance as input by the user via
  98. * WP_Widget::form().
  99. * @param array $old_instance Old settings for this instance.
  100. * @return array Updated settings to save.
  101. */
  102. public function update( $new_instance, $old_instance ) {
  103. $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) );
  104. return wp_widget_rss_process( $new_instance, $testurl );
  105. }
  106. /**
  107. * Outputs the settings form for the RSS widget.
  108. *
  109. * @since 2.8.0
  110. *
  111. * @param array $instance Current settings.
  112. */
  113. public function form( $instance ) {
  114. if ( empty( $instance ) ) {
  115. $instance = array(
  116. 'title' => '',
  117. 'url' => '',
  118. 'items' => 10,
  119. 'error' => false,
  120. 'show_summary' => 0,
  121. 'show_author' => 0,
  122. 'show_date' => 0,
  123. );
  124. }
  125. $instance['number'] = $this->number;
  126. wp_widget_rss_form( $instance );
  127. }
  128. }