PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.4.3
Jetpack – WP Security, Backup, Speed, & Growth v7.4.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / image-widget.php

image-widget.php in Jetpack – WP Security, Backup, Speed, & Growth 7.4.3, at modules/widgets/image-widget.php

275 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: Image Widget
4 * Module Description: Easily add images to your theme's sidebar.
5 * Sort Order: 20
6 * First Introduced: 1.2
7 */
8
9 /**
10 * Register the widget for use in Appearance -> Widgets
11 */
12 add_action( 'widgets_init', 'jetpack_image_widget_init', 11 );
13 function jetpack_image_widget_init() {
14 if ( class_exists( 'WP_Widget_Media_Image' ) && Jetpack_Options::get_option( 'image_widget_migration' ) ) {
15 return;
16 }
17 register_widget( 'Jetpack_Image_Widget' );
18 }
19
20 class Jetpack_Image_Widget extends WP_Widget {
21 /**
22 * Register widget with WordPress.
23 */
24 public function __construct() {
25 parent::__construct(
26 'image',
27 /** This filter is documented in modules/widgets/facebook-likebox.php */
28 apply_filters( 'jetpack_widget_name', esc_html__( 'Image', 'jetpack' ) ),
29 array(
30 'classname' => 'widget_image',
31 'description' => __( 'Display an image in your sidebar', 'jetpack' ),
32 'customize_selective_refresh' => true,
33 )
34 );
35
36 if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
37 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
38 }
39 }
40
41 /**
42 * Loads file for front-end widget style.
43 *
44 * @uses wp_enqueue_style(), plugins_url()
45 */
46 public function enqueue_style() {
47 wp_enqueue_style( 'jetpack_image_widget', plugins_url( 'image-widget/style.css', __FILE__ ), array(), '20140808' );
48 }
49
50 /**
51 * Front-end display of widget.
52 *
53 * @see WP_Widget::widget()
54 *
55 * @param array $args Widget arguments.
56 * @param array $instance Saved values from database.
57 */
58 public function widget( $args, $instance ) {
59 echo $args['before_widget'];
60
61 $instance = wp_parse_args(
62 $instance, array(
63 'title' => '',
64 'img_url' => '',
65 )
66 );
67
68 /** This filter is documented in core/src/wp-includes/default-widgets.php */
69 $title = apply_filters( 'widget_title', $instance['title'] );
70
71 if ( $title ) {
72 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
73 }
74
75 if ( '' != $instance['img_url'] ) {
76
77 $output = '<img src="' . esc_url( $instance['img_url'] ) . '" ';
78
79 if ( '' != $instance['alt_text'] ) {
80 $output .= 'alt="' . esc_attr( $instance['alt_text'] ) . '" ';
81 }
82 if ( '' != $instance['img_title'] ) {
83 $output .= 'title="' . esc_attr( $instance['img_title'] ) . '" ';
84 }
85 if ( '' == $instance['caption'] ) {
86 $output .= 'class="align' . esc_attr( $instance['align'] ) . '" ';
87 }
88 if ( '' != $instance['img_width'] ) {
89 $output .= 'width="' . esc_attr( $instance['img_width'] ) . '" ';
90 }
91 if ( '' != $instance['img_height'] ) {
92 $output .= 'height="' . esc_attr( $instance['img_height'] ) . '" ';
93 }
94 $output .= '/>';
95
96 if ( class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
97 $output = Jetpack_Photon::filter_the_content( $output );
98 }
99
100 if ( '' != $instance['link'] ) {
101 $target = ! empty( $instance['link_target_blank'] )
102 ? 'target="_blank"'
103 : '';
104 $output = '<a ' . $target . ' href="' . esc_url( $instance['link'] ) . '">' . $output . '</a>';
105 }
106 if ( '' != $instance['caption'] ) {
107 /** This filter is documented in core/src/wp-includes/default-widgets.php */
108 $caption = apply_filters( 'widget_text', $instance['caption'] );
109 $img_width = ( ! empty( $instance['img_width'] ) ? 'style="width: ' . esc_attr( $instance['img_width'] ) . 'px"' : '' );
110 $output = '<figure ' . $img_width . ' class="wp-caption align' . esc_attr( $instance['align'] ) . '">
111 ' . $output . '
112 <figcaption class="wp-caption-text">' . $caption . '</figcaption>
113 </figure>'; // wp_kses_post caption on update
114 }
115 echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>';
116 } else {
117 if ( current_user_can( 'edit_theme_options' ) ) {
118 echo '<p>' . sprintf( __( 'Image missing or invalid URL. Please check the Image widget URL in your <a href="%s">widget settings</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>';
119 }
120 }
121
122 echo "\n" . $args['after_widget'];
123
124 /** This action is documented in modules/widgets/gravatar-profile.php */
125 do_action( 'jetpack_stats_extra', 'widget_view', 'image' );
126 }
127
128 /**
129 * Sanitize widget form values as they are saved.
130 *
131 * @see WP_Widget::update()
132 *
133 * @param array $new_instance Values just sent to be saved.
134 * @param array $old_instance Previously saved values from database.
135 *
136 * @return array Updated safe values to be saved.
137 */
138 public function update( $new_instance, $old_instance ) {
139 $allowed_caption_html = array(
140 'a' => array(
141 'href' => array(),
142 'title' => array(),
143 ),
144 'b' => array(),
145 'em' => array(),
146 'i' => array(),
147 'p' => array(),
148 'strong' => array(),
149 );
150
151 $instance = $old_instance;
152
153 $instance['title'] = strip_tags( $new_instance['title'] );
154 $instance['img_url'] = esc_url( trim( $new_instance['img_url'] ) );
155 $instance['alt_text'] = strip_tags( $new_instance['alt_text'] );
156 $instance['img_title'] = strip_tags( $new_instance['img_title'] );
157 $instance['caption'] = wp_kses( stripslashes( $new_instance['caption'] ), $allowed_caption_html );
158 $instance['align'] = $new_instance['align'];
159 $instance['link'] = esc_url( trim( $new_instance['link'] ) );
160 $instance['link_target_blank'] = isset( $new_instance['link_target_blank'] ) ? (bool) $new_instance['link_target_blank'] : false;
161
162 $new_img_width = absint( $new_instance['img_width'] );
163 $new_img_height = absint( $new_instance['img_height'] );
164
165 if ( ! empty( $instance['img_url'] ) && '' == $new_img_width && '' == $new_img_height ) {
166 // Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout
167 $tmp_file = download_url( $instance['img_url'], 10 );
168 if ( ! is_wp_error( $tmp_file ) ) {
169 $size = getimagesize( $tmp_file );
170
171 $width = $size[0];
172 $instance['img_width'] = absint( $width );
173
174 $height = $size[1];
175 $instance['img_height'] = absint( $height );
176
177 unlink( $tmp_file );
178 } else {
179 $instance['img_width'] = $new_img_width;
180 $instance['img_height'] = $new_img_height;
181 }
182 } else {
183 $instance['img_width'] = $new_img_width;
184 $instance['img_height'] = $new_img_height;
185 }
186
187 return $instance;
188 }
189
190 /**
191 * Back end widget form.
192 *
193 * @see WP_Widget::form()
194 *
195 * @param array $instance Previously saved values from database.
196 */
197 public function form( $instance ) {
198 // Defaults
199 $instance = wp_parse_args(
200 (array) $instance, array(
201 'title' => '',
202 'img_url' => '',
203 'alt_text' => '',
204 'img_title' => '',
205 'caption' => '',
206 'align' => 'none',
207 'img_width' => '',
208 'img_height' => '',
209 'link' => '',
210 'link_target_blank' => false,
211 )
212 );
213
214 $title = esc_attr( $instance['title'] );
215 $img_url = esc_url( $instance['img_url'], null, 'display' );
216 $alt_text = esc_attr( $instance['alt_text'] );
217 $img_title = esc_attr( $instance['img_title'] );
218 $caption = esc_textarea( $instance['caption'] );
219 $align = esc_attr( $instance['align'] );
220 $img_width = esc_attr( $instance['img_width'] );
221 $img_height = esc_attr( $instance['img_height'] );
222 $link_target_blank = checked( $instance['link_target_blank'], true, false );
223
224 $link = esc_url( $instance['link'], null, 'display' );
225
226 echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . '
227 <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" />
228 </label></p>
229 <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . '
230 <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" />
231 </label></p>
232 <p><label for="' . $this->get_field_id( 'alt_text' ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a>
233 <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" />
234 </label></p>
235 <p><label for="' . $this->get_field_id( 'img_title' ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a>
236 <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" />
237 </label></p>
238 <p><label for="' . $this->get_field_id( 'caption' ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a>
239 <textarea class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" rows="2" cols="20">' . $caption . '</textarea>
240 </label></p>';
241
242 $alignments = array(
243 'none' => __( 'None', 'jetpack' ),
244 'left' => __( 'Left', 'jetpack' ),
245 'center' => __( 'Center', 'jetpack' ),
246 'right' => __( 'Right', 'jetpack' ),
247 );
248 echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . '
249 <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">';
250 foreach ( $alignments as $alignment => $alignment_name ) {
251 echo '<option value="' . esc_attr( $alignment ) . '" ';
252 if ( $alignment == $align ) {
253 echo 'selected="selected" ';
254 }
255 echo '>' . esc_html( $alignment_name ) . "</option>\n";
256 }
257 echo '</select></label></p>';
258
259 echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width in pixels:', 'jetpack' ) . '
260 <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" />
261 </label>
262 <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height in pixels:', 'jetpack' ) . '
263 <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" />
264 </label><br />
265 <small>' . esc_html__( 'If empty, we will attempt to determine the image size.', 'jetpack' ) . '</small></p>
266 <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . '
267 <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" />
268 </label>
269 <label for="' . $this->get_field_id( 'link_target_blank' ) . '">
270 <input type="checkbox" name="' . $this->get_field_name( 'link_target_blank' ) . '" id="' . $this->get_field_id( 'link_target_blank' ) . '" value="1"' . $link_target_blank . '/>
271 ' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . '
272 </label></p>';
273 }
274 } // Class Jetpack_Image_Widget
275