| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* RSS Links Widget |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Register the widget. |
| 10 |
*/ |
| 11 |
function jetpack_rss_links_widget_init() { |
| 12 |
register_widget( Jetpack_RSS_Links_Widget::class ); |
| 13 |
} |
| 14 |
add_action( 'widgets_init', 'jetpack_rss_links_widget_init' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* RSS Links Widget class. |
| 18 |
*/ |
| 19 |
class Jetpack_RSS_Links_Widget extends WP_Widget { |
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$widget_ops = array( |
| 25 |
'classname' => 'widget_rss_links', |
| 26 |
'description' => __( "Links to your blog's RSS feeds", 'jetpack' ), |
| 27 |
'customize_selective_refresh' => true, |
| 28 |
); |
| 29 |
parent::__construct( |
| 30 |
'rss_links', |
| 31 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 32 |
apply_filters( 'jetpack_widget_name', __( 'RSS Links', 'jetpack' ) ), |
| 33 |
$widget_ops |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Display the widget. |
| 39 |
* |
| 40 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 41 |
* @param array $instance The settings for the particular instance of the widget. |
| 42 |
*/ |
| 43 |
public function widget( $args, $instance ) { |
| 44 |
$instance = wp_parse_args( (array) $instance, $this->defaults() ); |
| 45 |
|
| 46 |
$before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : ''; |
| 47 |
$before_title = isset( $args['before_title'] ) ? $args['before_title'] : ''; |
| 48 |
$after_title = isset( $args['after_title'] ) ? $args['after_title'] : ''; |
| 49 |
$after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : ''; |
| 50 |
|
| 51 |
/** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 52 |
$title = apply_filters( 'widget_title', $instance['title'] ); |
| 53 |
echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 54 |
|
| 55 |
if ( $title ) { |
| 56 |
echo $before_title . $title . $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 57 |
} |
| 58 |
|
| 59 |
if ( 'text' === $instance['format'] ) { |
| 60 |
echo '<ul>'; |
| 61 |
} |
| 62 |
|
| 63 |
if ( 'posts' === $instance['display'] ) { |
| 64 |
$this->rss_link( 'posts', $instance ); |
| 65 |
} elseif ( 'comments' === $instance['display'] ) { |
| 66 |
$this->rss_link( 'comments', $instance ); |
| 67 |
} elseif ( 'posts-comments' === $instance['display'] ) { |
| 68 |
$this->rss_link( 'posts', $instance ); |
| 69 |
$this->rss_link( 'comments', $instance ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( 'text' === $instance['format'] ) { |
| 73 |
echo '</ul>'; |
| 74 |
} |
| 75 |
|
| 76 |
echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 77 |
|
| 78 |
/** This action is documented in modules/widgets/gravatar-profile.php */ |
| 79 |
do_action( 'jetpack_stats_extra', 'widget_view', 'rss-links' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Return an associative array of default values |
| 84 |
* These values are used in new widgets as well as when sanitizing input. |
| 85 |
* |
| 86 |
* @return array Array of default values for the Widget's options |
| 87 |
*/ |
| 88 |
public function defaults() { |
| 89 |
return array( |
| 90 |
'title' => '', |
| 91 |
'display' => 'posts-comments', |
| 92 |
'format' => 'text', |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Sanitize widget form values as they are saved. |
| 98 |
* |
| 99 |
* @see WP_Widget::update() |
| 100 |
* |
| 101 |
* @param array $new_instance Values just sent to be saved. |
| 102 |
* @param array $old_instance Previously saved values from database. |
| 103 |
* |
| 104 |
* @return array Updated safe values to be saved. |
| 105 |
*/ |
| 106 |
public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 107 |
$instance = $old_instance; |
| 108 |
|
| 109 |
$instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] ); |
| 110 |
$instance['display'] = $new_instance['display']; |
| 111 |
$instance['format'] = $new_instance['format']; |
| 112 |
$instance['imagesize'] = $new_instance['imagesize']; |
| 113 |
$instance['imagecolor'] = $new_instance['imagecolor']; |
| 114 |
|
| 115 |
return $instance; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Back end widget form. |
| 120 |
* |
| 121 |
* @see WP_Widget::form() |
| 122 |
* |
| 123 |
* @param array $instance Previously saved values from database. |
| 124 |
* |
| 125 |
* @return string|void |
| 126 |
*/ |
| 127 |
public function form( $instance ) { |
| 128 |
$instance = wp_parse_args( (array) $instance, $this->defaults() ); |
| 129 |
|
| 130 |
$title = stripslashes( $instance['title'] ); |
| 131 |
$display = $instance['display']; |
| 132 |
$format = $instance['format']; |
| 133 |
$image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0; |
| 134 |
$image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red'; |
| 135 |
|
| 136 |
echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . ' |
| 137 |
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $title ) . '" /> |
| 138 |
</label></p>'; |
| 139 |
|
| 140 |
$displays = array( |
| 141 |
'posts' => __( 'Posts', 'jetpack' ), |
| 142 |
'comments' => __( 'Comments', 'jetpack' ), |
| 143 |
'posts-comments' => __( 'Posts & Comments', 'jetpack' ), |
| 144 |
); |
| 145 |
echo '<p><label for="' . esc_attr( $this->get_field_id( 'display' ) ) . '">' . esc_html__( 'Feed(s) to Display:', 'jetpack' ) . ' |
| 146 |
<select class="widefat" id="' . esc_attr( $this->get_field_id( 'display' ) ) . '" name="' . esc_attr( $this->get_field_name( 'display' ) ) . '">'; |
| 147 |
foreach ( $displays as $display_option => $label ) { |
| 148 |
echo '<option value="' . esc_attr( $display_option ) . '"'; |
| 149 |
if ( $display_option === $display ) { |
| 150 |
echo ' selected="selected"'; |
| 151 |
} |
| 152 |
echo '>' . esc_html( $label ) . '</option>' . "\n"; |
| 153 |
} |
| 154 |
echo '</select></label></p>'; |
| 155 |
|
| 156 |
$formats = array( |
| 157 |
'text' => __( 'Text Link', 'jetpack' ), |
| 158 |
'image' => __( 'Image Link', 'jetpack' ), |
| 159 |
'text-image' => __( 'Text & Image Links', 'jetpack' ), |
| 160 |
); |
| 161 |
echo '<p><label for="' . esc_attr( $this->get_field_id( 'format' ) ) . '">' . esc_html_x( 'Format:', 'Noun', 'jetpack' ) . ' |
| 162 |
<select class="widefat" id="' . esc_attr( $this->get_field_id( 'format' ) ) . '" name="' . esc_attr( $this->get_field_name( 'format' ) ) . '" onchange="if ( this.value == \'text\' ) jQuery( \'#' . esc_js( $this->get_field_id( 'image-settings' ) ) . '\' ).fadeOut(); else jQuery( \'#' . esc_js( $this->get_field_id( 'image-settings' ) ) . '\' ).fadeIn();">'; |
| 163 |
foreach ( $formats as $format_option => $label ) { |
| 164 |
echo '<option value="' . esc_attr( $format_option ) . '"'; |
| 165 |
if ( $format_option === $format ) { |
| 166 |
echo ' selected="selected"'; |
| 167 |
} |
| 168 |
echo '>' . esc_html( $label ) . '</option>' . "\n"; |
| 169 |
} |
| 170 |
echo '</select></label></p>'; |
| 171 |
|
| 172 |
echo '<div id="' . esc_attr( $this->get_field_id( 'image-settings' ) ) . '"'; |
| 173 |
if ( 'text' === $format ) { |
| 174 |
echo ' style="display: none;"'; |
| 175 |
} |
| 176 |
echo '><h3>' . esc_html__( 'Image Settings:', 'jetpack' ) . '</h3>'; |
| 177 |
|
| 178 |
$sizes = array( |
| 179 |
'small' => __( 'Small', 'jetpack' ), |
| 180 |
'medium' => __( 'Medium', 'jetpack' ), |
| 181 |
'large' => __( 'Large', 'jetpack' ), |
| 182 |
); |
| 183 |
echo '<p><label for="' . esc_attr( $this->get_field_id( 'imagesize' ) ) . '">' . esc_html__( 'Image Size:', 'jetpack' ) . ' |
| 184 |
<select class="widefat" id="' . esc_attr( $this->get_field_id( 'imagesize' ) ) . '" name="' . esc_attr( $this->get_field_name( 'imagesize' ) ) . '">'; |
| 185 |
foreach ( $sizes as $size => $label ) { |
| 186 |
echo '<option value="' . esc_attr( $size ) . '"'; |
| 187 |
if ( $size === $image_size ) { |
| 188 |
echo ' selected="selected"'; |
| 189 |
} |
| 190 |
echo '>' . esc_html( $label ) . '</option>' . "\n"; |
| 191 |
} |
| 192 |
echo '</select></label></p>'; |
| 193 |
|
| 194 |
$colors = array( |
| 195 |
'red' => __( 'Red', 'jetpack' ), |
| 196 |
'orange' => __( 'Orange', 'jetpack' ), |
| 197 |
'green' => __( 'Green', 'jetpack' ), |
| 198 |
'blue' => __( 'Blue', 'jetpack' ), |
| 199 |
'purple' => __( 'Purple', 'jetpack' ), |
| 200 |
'pink' => __( 'Pink', 'jetpack' ), |
| 201 |
'silver' => __( 'Silver', 'jetpack' ), |
| 202 |
); |
| 203 |
echo '<p><label for="' . esc_attr( $this->get_field_id( 'imagecolor' ) ) . '">' . esc_html__( 'Image Color:', 'jetpack' ) . ' |
| 204 |
<select class="widefat" id="' . esc_attr( $this->get_field_id( 'imagecolor' ) ) . '" name="' . esc_attr( $this->get_field_name( 'imagecolor' ) ) . '">'; |
| 205 |
foreach ( $colors as $color => $label ) { |
| 206 |
echo '<option value="' . esc_attr( $color ) . '"'; |
| 207 |
if ( $color === $image_color ) { |
| 208 |
echo ' selected="selected"'; |
| 209 |
} |
| 210 |
echo '>' . esc_html( $label ) . '</option>' . "\n"; |
| 211 |
} |
| 212 |
echo '</select></label></p></div>'; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Output a link with a link to the feed. |
| 217 |
* |
| 218 |
* @param string $type Widget type (posts or comments). |
| 219 |
* @param array $args Widget arguments. |
| 220 |
*/ |
| 221 |
private function rss_link( $type, $args ) { |
| 222 |
if ( 'posts' === $type ) { |
| 223 |
$subscribe_to = esc_html__( 'Subscribe to posts', 'jetpack' ); |
| 224 |
$link_text = esc_html__( 'RSS - Posts', 'jetpack' ); |
| 225 |
$rss_type = 'rss2_url'; |
| 226 |
} elseif ( 'comments' === $type ) { |
| 227 |
$subscribe_to = esc_html__( 'Subscribe to comments', 'jetpack' ); |
| 228 |
$link_text = esc_html__( 'RSS - Comments', 'jetpack' ); |
| 229 |
$rss_type = 'comments_rss2_url'; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Filters the target link attribute for the RSS link in the RSS widget. |
| 234 |
* |
| 235 |
* @module widgets |
| 236 |
* |
| 237 |
* @since 3.4.0 |
| 238 |
* |
| 239 |
* @param bool false Control whether the link should open in a new tab. Default to false. |
| 240 |
*/ |
| 241 |
if ( apply_filters( 'jetpack_rsslinks_widget_target_blank', false ) ) { |
| 242 |
$link_target = '_blank'; |
| 243 |
} else { |
| 244 |
$link_target = '_self'; |
| 245 |
} |
| 246 |
|
| 247 |
$format = $args['format']; |
| 248 |
if ( 'image' === $format ) { |
| 249 |
$link_contents = $this->get_image_tag( $args ); |
| 250 |
} elseif ( 'text-image' === $format ) { |
| 251 |
$link_contents = sprintf( |
| 252 |
'%1$s %2$s', |
| 253 |
$this->get_image_tag( $args ), |
| 254 |
$link_text |
| 255 |
); |
| 256 |
} elseif ( 'text' === $format ) { |
| 257 |
$link_contents = $link_text; |
| 258 |
} |
| 259 |
|
| 260 |
printf( |
| 261 |
'%1$s<a target="%3$s" href="%4$s" title="%5$s">%6$s</a>%2$s', |
| 262 |
'text' === $format ? '<li>' : '<p>', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 263 |
'text' === $format ? '</li>' : '</p>', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 264 |
esc_attr( $link_target ), |
| 265 |
esc_url( get_bloginfo( $rss_type ) ), |
| 266 |
esc_attr( $subscribe_to ), |
| 267 |
$link_contents // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we are escaping this above. |
| 268 |
); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Return an image tag for the RSS icon. |
| 273 |
* |
| 274 |
* @param array $args Widget arguments. |
| 275 |
*/ |
| 276 |
private function get_image_tag( $args ) { |
| 277 |
$image_path = sprintf( |
| 278 |
'images/rss/%1$s-%2$s.png', |
| 279 |
$args['imagecolor'], |
| 280 |
$args['imagesize'] |
| 281 |
); |
| 282 |
|
| 283 |
/** |
| 284 |
* Filters the image used as RSS icon in the RSS widget. |
| 285 |
* |
| 286 |
* @module widgets |
| 287 |
* |
| 288 |
* @since 3.6.0 |
| 289 |
* |
| 290 |
* @param string $var URL of RSS Widget icon. |
| 291 |
*/ |
| 292 |
$image = apply_filters( |
| 293 |
'jetpack_rss_widget_icon', |
| 294 |
plugins_url( $image_path, dirname( __DIR__ ) ) |
| 295 |
); |
| 296 |
|
| 297 |
return sprintf( |
| 298 |
'<img src="%1$s" alt="%2$s" />', |
| 299 |
esc_url( $image ), |
| 300 |
esc_attr__( 'RSS feed', 'jetpack' ) |
| 301 |
); |
| 302 |
} |
| 303 |
} // Class Jetpack_RSS_Links_Widget |
| 304 |
|