Keine Beschreibung

rsslinks-widget.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Module Name: RSS Links Widget
  4. * Module Description: Easily add RSS links to your theme's sidebar.
  5. * Sort Order: 20
  6. * First Introduced: 1.2
  7. */
  8. class Jetpack_RSS_Links_Widget extends WP_Widget {
  9. function __construct() {
  10. $widget_ops = array(
  11. 'classname' => 'widget_rss_links',
  12. 'description' => __( "Links to your blog's RSS feeds", 'jetpack' ),
  13. 'customize_selective_refresh' => true,
  14. );
  15. parent::__construct(
  16. 'rss_links',
  17. /** This filter is documented in modules/widgets/facebook-likebox.php */
  18. apply_filters( 'jetpack_widget_name', __( 'RSS Links', 'jetpack' ) ),
  19. $widget_ops
  20. );
  21. }
  22. /**
  23. * Display the widget.
  24. *
  25. * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  26. * @param array $instance The settings for the particular instance of the widget.
  27. */
  28. public function widget( $args, $instance ) {
  29. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  30. $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
  31. $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
  32. $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
  33. $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
  34. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  35. $title = apply_filters( 'widget_title', $instance['title'] );
  36. echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  37. if ( $title ) {
  38. echo $before_title . esc_html( $title ) . $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  39. }
  40. if ( 'text' === $instance['format'] ) {
  41. echo '<ul>';
  42. }
  43. if ( 'posts' === $instance['display'] ) {
  44. $this->_rss_link( 'posts', $instance );
  45. } elseif ( 'comments' === $instance['display'] ) {
  46. $this->_rss_link( 'comments', $instance );
  47. } elseif ( 'posts-comments' === $instance['display'] ) {
  48. $this->_rss_link( 'posts', $instance );
  49. $this->_rss_link( 'comments', $instance );
  50. }
  51. if ( 'text' === $instance['format'] ) {
  52. echo '</ul>';
  53. }
  54. echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  55. /** This action is documented in modules/widgets/gravatar-profile.php */
  56. do_action( 'jetpack_stats_extra', 'widget_view', 'rss-links' );
  57. }
  58. /**
  59. * Return an associative array of default values
  60. * These values are used in new widgets as well as when sanitizing input.
  61. *
  62. * @return array Array of default values for the Widget's options
  63. */
  64. function defaults() {
  65. return array(
  66. 'title' => '',
  67. 'display' => 'posts-comments',
  68. 'format' => 'text',
  69. );
  70. }
  71. function update( $new_instance, $old_instance ) {
  72. $instance = $old_instance;
  73. $instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] );
  74. $instance['display'] = $new_instance['display'];
  75. $instance['format'] = $new_instance['format'];
  76. $instance['imagesize'] = $new_instance['imagesize'];
  77. $instance['imagecolor'] = $new_instance['imagecolor'];
  78. return $instance;
  79. }
  80. function form( $instance ) {
  81. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  82. $title = stripslashes( $instance['title'] );
  83. $display = $instance['display'];
  84. $format = $instance['format'];
  85. $image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0;
  86. $image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red';
  87. echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
  88. <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" />
  89. </label></p>';
  90. $displays = array(
  91. 'posts' => __( 'Posts', 'jetpack' ),
  92. 'comments' => __( 'Comments', 'jetpack' ),
  93. 'posts-comments' => __( 'Posts & Comments', 'jetpack' ),
  94. );
  95. echo '<p><label for="' . $this->get_field_id( 'display' ) . '">' . esc_html__( 'Feed(s) to Display:', 'jetpack' ) . '
  96. <select class="widefat" id="' . $this->get_field_id( 'display' ) . '" name="' . $this->get_field_name( 'display' ) . '">';
  97. foreach ( $displays as $display_option => $label ) {
  98. echo '<option value="' . esc_attr( $display_option ) . '"';
  99. if ( $display_option == $display ) {
  100. echo ' selected="selected"';
  101. }
  102. echo '>' . esc_html( $label ) . '</option>' . "\n";
  103. }
  104. echo '</select></label></p>';
  105. $formats = array(
  106. 'text' => __( 'Text Link', 'jetpack' ),
  107. 'image' => __( 'Image Link', 'jetpack' ),
  108. 'text-image' => __( 'Text & Image Links', 'jetpack' ),
  109. );
  110. echo '<p><label for="' . $this->get_field_id( 'format' ) . '">' . _x( 'Format:', 'Noun', 'jetpack' ) . '
  111. <select class="widefat" id="' . $this->get_field_id( 'format' ) . '" name="' . $this->get_field_name( 'format' ) . '" onchange="if ( this.value == \'text\' ) jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeOut(); else jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeIn();">';
  112. foreach ( $formats as $format_option => $label ) {
  113. echo '<option value="' . esc_attr( $format_option ) . '"';
  114. if ( $format_option == $format ) {
  115. echo ' selected="selected"';
  116. }
  117. echo '>' . esc_html( $label ) . '</option>' . "\n";
  118. }
  119. echo '</select></label></p>';
  120. echo '<div id="' . $this->get_field_id( 'image-settings' ) . '"';
  121. if ( 'text' == $format ) {
  122. echo ' style="display: none;"';
  123. }
  124. echo '><h3>' . esc_html__( 'Image Settings:', 'jetpack' ) . '</h3>';
  125. $sizes = array(
  126. 'small' => __( 'Small', 'jetpack' ),
  127. 'medium' => __( 'Medium', 'jetpack' ),
  128. 'large' => __( 'Large', 'jetpack' ),
  129. );
  130. echo '<p><label for="' . $this->get_field_id( 'imagesize' ) . '">' . esc_html__( 'Image Size:', 'jetpack' ) . '
  131. <select class="widefat" id="' . $this->get_field_id( 'imagesize' ) . '" name="' . $this->get_field_name( 'imagesize' ) . '">';
  132. foreach ( $sizes as $size => $label ) {
  133. echo '<option value="' . esc_attr( $size ) . '"';
  134. if ( $size == $image_size ) {
  135. echo ' selected="selected"';
  136. }
  137. echo '>' . esc_html( $label ) . '</option>' . "\n";
  138. }
  139. echo '</select></label></p>';
  140. $colors = array(
  141. 'red' => __( 'Red', 'jetpack' ),
  142. 'orange' => __( 'Orange', 'jetpack' ),
  143. 'green' => __( 'Green', 'jetpack' ),
  144. 'blue' => __( 'Blue', 'jetpack' ),
  145. 'purple' => __( 'Purple', 'jetpack' ),
  146. 'pink' => __( 'Pink', 'jetpack' ),
  147. 'silver' => __( 'Silver', 'jetpack' ),
  148. );
  149. echo '<p><label for="' . $this->get_field_id( 'imagecolor' ) . '">' . esc_html__( 'Image Color:', 'jetpack' ) . '
  150. <select class="widefat" id="' . $this->get_field_id( 'imagecolor' ) . '" name="' . $this->get_field_name( 'imagecolor' ) . '">';
  151. foreach ( $colors as $color => $label ) {
  152. echo '<option value="' . esc_attr( $color ) . '"';
  153. if ( $color == $image_color ) {
  154. echo ' selected="selected"';
  155. }
  156. echo '>' . esc_html( $label ) . '</option>' . "\n";
  157. }
  158. echo '</select></label></p></div>';
  159. }
  160. function _rss_link( $type, $args ) {
  161. if ( 'posts' == $type ) {
  162. $type_text = __( 'Posts', 'jetpack' );
  163. $rss_type = 'rss2_url';
  164. } elseif ( 'comments' == $type ) {
  165. $type_text = __( 'Comments', 'jetpack' );
  166. $rss_type = 'comments_rss2_url';
  167. }
  168. $subscribe_to = sprintf( __( 'Subscribe to %s', 'jetpack' ), $type_text );
  169. $link_item = '';
  170. $format = $args['format'];
  171. /**
  172. * Filters the target link attribute for the RSS link in the RSS widget.
  173. *
  174. * @module widgets
  175. *
  176. * @since 3.4.0
  177. *
  178. * @param bool false Control whether the link should open in a new tab. Default to false.
  179. */
  180. if ( apply_filters( 'jetpack_rsslinks_widget_target_blank', false ) ) {
  181. $link_target = '_blank';
  182. } else {
  183. $link_target = '_self';
  184. }
  185. if ( 'image' == $format || 'text-image' == $format ) {
  186. /**
  187. * Filters the image used as RSS icon in the RSS widget.
  188. *
  189. * @module widgets
  190. *
  191. * @since 3.6.0
  192. *
  193. * @param string $var URL of RSS Widget icon.
  194. */
  195. $link_image = apply_filters( 'jetpack_rss_widget_icon', plugins_url( 'images/rss/' . $args['imagecolor'] . '-' . $args['imagesize'] . '.png', dirname( dirname( __FILE__ ) ) ) );
  196. $link_item = '<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '"><img src="' . esc_url( $link_image ) . '" alt="RSS Feed" /></a>';
  197. }
  198. if ( 'text-image' == $format ) {
  199. $link_item .= '&nbsp;<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ) . '</a>';
  200. }
  201. if ( 'text' == $format ) {
  202. $link_item = '<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ) . '</a>';
  203. }
  204. if ( 'text' == $format ) {
  205. echo '<li>';
  206. } else {
  207. echo '<p>';
  208. }
  209. echo $link_item;
  210. if ( 'text' == $format ) {
  211. echo '</li>';
  212. } else {
  213. echo '</p>';
  214. }
  215. }
  216. } // Class Jetpack_RSS_Links_Widget
  217. function jetpack_rss_links_widget_init() {
  218. register_widget( 'Jetpack_RSS_Links_Widget' );
  219. }
  220. add_action( 'widgets_init', 'jetpack_rss_links_widget_init' );