Geen omschrijving

class-wp-widget-rss.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 'show_instance_in_rest' => true,
  27. );
  28. $control_ops = array(
  29. 'width' => 400,
  30. 'height' => 200,
  31. );
  32. parent::__construct( 'rss', __( 'RSS' ), $widget_ops, $control_ops );
  33. }
  34. /**
  35. * Outputs the content for the current RSS widget instance.
  36. *
  37. * @since 2.8.0
  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 ( ! empty( $url ) && 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() ), true ) ) {
  56. return;
  57. }
  58. $rss = fetch_feed( $url );
  59. $title = $instance['title'];
  60. $desc = '';
  61. $link = '';
  62. if ( ! is_wp_error( $rss ) ) {
  63. $desc = esc_attr( strip_tags( html_entity_decode( $rss->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) );
  64. if ( empty( $title ) ) {
  65. $title = strip_tags( $rss->get_title() );
  66. }
  67. $link = strip_tags( $rss->get_permalink() );
  68. while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
  69. $link = substr( $link, 1 );
  70. }
  71. }
  72. if ( empty( $title ) ) {
  73. $title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' );
  74. }
  75. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  76. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  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. }
  86. $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  87. /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  88. $format = apply_filters( 'navigation_widgets_format', $format );
  89. if ( 'html5' === $format ) {
  90. // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  91. $title = trim( strip_tags( $title ) );
  92. $aria_label = $title ? $title : __( 'RSS Feed' );
  93. echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
  94. }
  95. wp_widget_rss_output( $rss, $instance );
  96. if ( 'html5' === $format ) {
  97. echo '</nav>';
  98. }
  99. echo $args['after_widget'];
  100. if ( ! is_wp_error( $rss ) ) {
  101. $rss->__destruct();
  102. }
  103. unset( $rss );
  104. }
  105. /**
  106. * Handles updating settings for the current RSS widget instance.
  107. *
  108. * @since 2.8.0
  109. *
  110. * @param array $new_instance New settings for this instance as input by the user via
  111. * WP_Widget::form().
  112. * @param array $old_instance Old settings for this instance.
  113. * @return array Updated settings to save.
  114. */
  115. public function update( $new_instance, $old_instance ) {
  116. $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] !== $old_instance['url'] ) ) );
  117. return wp_widget_rss_process( $new_instance, $testurl );
  118. }
  119. /**
  120. * Outputs the settings form for the RSS widget.
  121. *
  122. * @since 2.8.0
  123. *
  124. * @param array $instance Current settings.
  125. */
  126. public function form( $instance ) {
  127. if ( empty( $instance ) ) {
  128. $instance = array(
  129. 'title' => '',
  130. 'url' => '',
  131. 'items' => 10,
  132. 'error' => false,
  133. 'show_summary' => 0,
  134. 'show_author' => 0,
  135. 'show_date' => 0,
  136. );
  137. }
  138. $instance['number'] = $this->number;
  139. wp_widget_rss_form( $instance );
  140. }
  141. }