PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/rsslinks-widget.php

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