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