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