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