rsslinks-widget.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. function widget( $args, $instance ) {
  23. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  24. extract( $args );
  25. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  26. $title = apply_filters( 'widget_title', $instance['title'] );
  27. echo $before_widget;
  28. if ( $title )
  29. echo $before_title . stripslashes( $title ) . $after_title;
  30. if ( 'text' == $instance['format'] ) echo '<ul>';
  31. if ( 'posts' == $instance['display'] ) {
  32. $this->_rss_link( 'posts', $instance);
  33. } elseif ( 'comments' == $instance['display'] ) {
  34. $this->_rss_link( 'comments', $instance);
  35. } elseif ( 'posts-comments' == $instance['display'] ) {
  36. $this->_rss_link( 'posts', $instance );
  37. $this->_rss_link( 'comments', $instance );
  38. }
  39. if ( 'text' == $instance['format'] ) echo '</ul>';
  40. echo "\n" . $after_widget;
  41. /** This action is documented in modules/widgets/gravatar-profile.php */
  42. do_action( 'jetpack_stats_extra', 'widget_view', 'rss-links' );
  43. }
  44. /**
  45. * Return an associative array of default values
  46. * These values are used in new widgets as well as when sanitizing input.
  47. *
  48. * @return array Array of default values for the Widget's options
  49. */
  50. function defaults() {
  51. return array(
  52. 'title' => '',
  53. 'display' => 'posts-comments',
  54. 'format' => 'text'
  55. );
  56. }
  57. function update( $new_instance, $old_instance ) {
  58. $instance = $old_instance;
  59. $instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] );
  60. $instance['display'] = $new_instance['display'];
  61. $instance['format'] = $new_instance['format'];
  62. $instance['imagesize'] = $new_instance['imagesize'];
  63. $instance['imagecolor'] = $new_instance['imagecolor'];
  64. return $instance;
  65. }
  66. function form( $instance ) {
  67. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  68. $title = stripslashes( $instance['title'] );
  69. $display = $instance['display'];
  70. $format = $instance['format'];
  71. $image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0 ;
  72. $image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red';
  73. echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
  74. <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" />
  75. </label></p>';
  76. $displays = array(
  77. 'posts' => __( 'Posts', 'jetpack' ),
  78. 'comments' => __( 'Comments', 'jetpack' ),
  79. 'posts-comments' => __( 'Posts & Comments', 'jetpack' )
  80. );
  81. echo '<p><label for="' . $this->get_field_id( 'display' ) . '">' . esc_html__( 'Feed(s) to Display:', 'jetpack' ) . '
  82. <select class="widefat" id="' . $this->get_field_id( 'display' ) . '" name="' . $this->get_field_name( 'display' ) . '">';
  83. foreach ( $displays as $display_option => $label ) {
  84. echo '<option value="' . esc_attr( $display_option ) . '"';
  85. if ( $display_option == $display ) echo ' selected="selected"';
  86. echo '>' . esc_html( $label ) . '</option>' . "\n";
  87. }
  88. echo '</select></label></p>';
  89. $formats = array(
  90. 'text' => __( 'Text Link', 'jetpack' ),
  91. 'image' => __( 'Image Link', 'jetpack' ),
  92. 'text-image' => __( 'Text & Image Links', 'jetpack' )
  93. );
  94. echo '<p><label for="' . $this->get_field_id( 'format' ) . '">' . _x( 'Format:', 'Noun', 'jetpack' ) . '
  95. <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();">';
  96. foreach ( $formats as $format_option => $label ) {
  97. echo '<option value="' . esc_attr( $format_option ) . '"';
  98. if ( $format_option == $format ) echo ' selected="selected"';
  99. echo '>' . esc_html( $label ) . '</option>' . "\n";
  100. }
  101. echo '</select></label></p>';
  102. echo '<div id="' . $this->get_field_id( 'image-settings' ) . '"';
  103. if ( 'text' == $format ) echo ' style="display: none;"';
  104. echo '><h3>' . esc_html__( 'Image Settings:', 'jetpack' ) . '</h3>';
  105. $sizes = array(
  106. 'small' => __( 'Small', 'jetpack' ),
  107. 'medium' => __( 'Medium', 'jetpack' ),
  108. 'large' => __( 'Large', 'jetpack' )
  109. );
  110. echo '<p><label for="' . $this->get_field_id( 'imagesize' ) . '">' . esc_html__( 'Image Size:', 'jetpack' ) . '
  111. <select class="widefat" id="' . $this->get_field_id( 'imagesize' ) . '" name="' . $this->get_field_name( 'imagesize' ) . '">';
  112. foreach ( $sizes as $size => $label ) {
  113. echo '<option value="' . esc_attr( $size) . '"';
  114. if ( $size == $image_size ) echo ' selected="selected"';
  115. echo '>' . esc_html( $label ) . '</option>' . "\n";
  116. }
  117. echo '</select></label></p>';
  118. $colors = array(
  119. 'red' => __( 'Red', 'jetpack' ),
  120. 'orange' => __( 'Orange', 'jetpack' ),
  121. 'green' => __( 'Green', 'jetpack' ),
  122. 'blue' => __( 'Blue', 'jetpack' ),
  123. 'purple' => __( 'Purple', 'jetpack' ),
  124. 'pink' => __( 'Pink', 'jetpack' ),
  125. 'silver' => __( 'Silver', 'jetpack' ),
  126. );
  127. echo '<p><label for="' . $this->get_field_id( 'imagecolor' ) . '">' . esc_html__( 'Image Color:', 'jetpack' ) . '
  128. <select class="widefat" id="' . $this->get_field_id( 'imagecolor' ) . '" name="' . $this->get_field_name( 'imagecolor' ) . '">';
  129. foreach ( $colors as $color => $label ) {
  130. echo '<option value="' . esc_attr( $color) . '"';
  131. if ( $color == $image_color ) echo ' selected="selected"';
  132. echo '>' . esc_html( $label ) . '</option>' . "\n";
  133. }
  134. echo '</select></label></p></div>';
  135. }
  136. function _rss_link( $type = 'posts', $args ) {
  137. if ( 'posts' == $type ) {
  138. $type_text = __( 'Posts', 'jetpack' );
  139. $rss_type = 'rss2_url';
  140. } elseif ( 'comments' == $type ) {
  141. $type_text = __( 'Comments', 'jetpack' );
  142. $rss_type = 'comments_rss2_url';
  143. }
  144. $subscribe_to = sprintf( __( 'Subscribe to %s', 'jetpack' ), $type_text );
  145. $link_item = '';
  146. $format = $args['format'];
  147. /**
  148. * Filters the target link attribute for the RSS link in the RSS widget.
  149. *
  150. * @module widgets
  151. *
  152. * @since 3.4.0
  153. *
  154. * @param bool false Control whether the link should open in a new tab. Default to false.
  155. */
  156. if ( apply_filters( 'jetpack_rsslinks_widget_target_blank', false ) ) {
  157. $link_target = '_blank';
  158. } else {
  159. $link_target = '_self';
  160. }
  161. if ( 'image' == $format || 'text-image' == $format ) {
  162. /**
  163. * Filters the image used as RSS icon in the RSS widget.
  164. *
  165. * @module widgets
  166. *
  167. * @since 3.6.0
  168. *
  169. * @param string $var URL of RSS Widget icon.
  170. */
  171. $link_image = apply_filters( 'jetpack_rss_widget_icon', plugins_url( 'images/rss/' . $args['imagecolor'] . '-' . $args['imagesize'] . '.png', dirname( dirname( __FILE__ ) ) ) );
  172. $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>';
  173. }
  174. if ( 'text-image' == $format ) {
  175. $link_item .= '&nbsp;<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ). '</a>';
  176. }
  177. if ( 'text' == $format ) {
  178. $link_item = '<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ). '</a>';
  179. }
  180. if ( 'text' == $format )
  181. echo '<li>';
  182. else
  183. echo '<p>';
  184. echo $link_item;
  185. if ( 'text' == $format )
  186. echo '</li>';
  187. else
  188. echo '</p>';
  189. }
  190. } // Class Jetpack_RSS_Links_Widget
  191. function jetpack_rss_links_widget_init() {
  192. register_widget( 'Jetpack_RSS_Links_Widget' );
  193. }
  194. add_action( 'widgets_init', 'jetpack_rss_links_widget_init' );