PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.4.4
Jetpack – WP Security, Backup, Speed, & Growth v6.4.4
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 6.4.4, at modules/widgets/image-widget.php

259 lines 11.0 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( $instance, array(
62 'title' => '',
63 'img_url' => ''
64 ) );
65
66 /** This filter is documented in core/src/wp-includes/default-widgets.php */
67 $title = apply_filters( 'widget_title', $instance['title'] );
68
69 if ( $title ) {
70 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
71 }
72
73 if ( '' != $instance['img_url'] ) {
74
75 $output = '<img src="' . esc_url( $instance['img_url'] ) . '" ';
76
77 if ( '' != $instance['alt_text'] ) {
78 $output .= 'alt="' . esc_attr( $instance['alt_text'] ) .'" ';
79 }
80 if ( '' != $instance['img_title'] ) {
81 $output .= 'title="' . esc_attr( $instance['img_title'] ) .'" ';
82 }
83 if ( '' == $instance['caption'] ) {
84 $output .= 'class="align' . esc_attr( $instance['align'] ) . '" ';
85 }
86 if ( '' != $instance['img_width'] ) {
87 $output .= 'width="' . esc_attr( $instance['img_width'] ) .'" ';
88 }
89 if ( '' != $instance['img_height'] ) {
90 $output .= 'height="' . esc_attr( $instance['img_height'] ) .'" ';
91 }
92 $output .= '/>';
93
94 if ( class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
95 $output = Jetpack_Photon::filter_the_content( $output );
96 }
97
98 if ( '' != $instance['link'] ) {
99 $target = ! empty( $instance['link_target_blank'] )
100 ? 'target="_blank"'
101 : '';
102 $output = '<a ' . $target . ' href="' . esc_url( $instance['link'] ) . '">' . $output . '</a>';
103 }
104 if ( '' != $instance['caption'] ) {
105 /** This filter is documented in core/src/wp-includes/default-widgets.php */
106 $caption = apply_filters( 'widget_text', $instance['caption'] );
107 $img_width = ( ! empty( $instance['img_width'] ) ? 'style="width: ' . esc_attr( $instance['img_width'] ) .'px"' : '' );
108 $output = '<figure ' . $img_width .' class="wp-caption align' . esc_attr( $instance['align'] ) . '">
109 ' . $output . '
110 <figcaption class="wp-caption-text">' . $caption . '</figcaption>
111 </figure>'; // wp_kses_post caption on update
112 }
113 echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>';
114 } else {
115 if ( current_user_can( 'edit_theme_options' ) ) {
116 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>';
117 }
118 }
119
120 echo "\n" . $args['after_widget'];
121
122 /** This action is documented in modules/widgets/gravatar-profile.php */
123 do_action( 'jetpack_stats_extra', 'widget_view', 'image' );
124 }
125
126 /**
127 * Sanitize widget form values as they are saved.
128 *
129 * @see WP_Widget::update()
130 *
131 * @param array $new_instance Values just sent to be saved.
132 * @param array $old_instance Previously saved values from database.
133 *
134 * @return array Updated safe values to be saved.
135 */
136 public function update( $new_instance, $old_instance ) {
137 $allowed_caption_html = array(
138 'a' => array(
139 'href' => array(),
140 'title' => array(),
141 ),
142 'b' => array(),
143 'em' => array(),
144 'i' => array(),
145 'p' => array(),
146 'strong' => array()
147 );
148
149 $instance = $old_instance;
150
151 $instance['title'] = strip_tags( $new_instance['title'] );
152 $instance['img_url'] = esc_url( trim( $new_instance['img_url'] ) );
153 $instance['alt_text'] = strip_tags( $new_instance['alt_text'] );
154 $instance['img_title'] = strip_tags( $new_instance['img_title'] );
155 $instance['caption'] = wp_kses( stripslashes($new_instance['caption'] ), $allowed_caption_html );
156 $instance['align'] = $new_instance['align'];
157 $instance['link'] = esc_url( trim( $new_instance['link'] ) );
158 $instance['link_target_blank'] = isset( $new_instance['link_target_blank'] ) ? (bool) $new_instance['link_target_blank'] : false;
159
160 $new_img_width = absint( $new_instance['img_width'] );
161 $new_img_height = absint( $new_instance['img_height'] );
162
163 if ( ! empty( $instance['img_url'] ) && '' == $new_img_width && '' == $new_img_height ) {
164 // Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout
165 $tmp_file = download_url( $instance['img_url'], 10 );
166 if ( ! is_wp_error( $tmp_file ) ) {
167 $size = getimagesize( $tmp_file );
168
169 $width = $size[0];
170 $instance['img_width'] = absint( $width );
171
172 $height = $size[1];
173 $instance['img_height'] = absint( $height );
174
175 unlink( $tmp_file );
176 } else {
177 $instance['img_width'] = $new_img_width;
178 $instance['img_height'] = $new_img_height;
179 }
180 } else {
181 $instance['img_width'] = $new_img_width;
182 $instance['img_height'] = $new_img_height;
183 }
184
185 return $instance;
186 }
187
188 /**
189 * Back end widget form.
190 *
191 * @see WP_Widget::form()
192 *
193 * @param array $instance Previously saved values from database.
194 */
195 public function form( $instance ) {
196 // Defaults
197 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'img_url' => '', 'alt_text' => '', 'img_title' => '', 'caption' => '', 'align' => 'none', 'img_width' => '', 'img_height' => '', 'link' => '', 'link_target_blank' => false ) );
198
199 $title = esc_attr( $instance['title'] );
200 $img_url = esc_url( $instance['img_url'], null, 'display' );
201 $alt_text = esc_attr( $instance['alt_text'] );
202 $img_title = esc_attr( $instance['img_title'] );
203 $caption = esc_textarea( $instance['caption'] );
204 $align = esc_attr( $instance['align'] );
205 $img_width = esc_attr( $instance['img_width'] );
206 $img_height = esc_attr( $instance['img_height'] );
207 $link_target_blank = checked( $instance['link_target_blank'], true, false );
208
209 $link = esc_url( $instance['link'], null, 'display' );
210
211 echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . '
212 <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" />
213 </label></p>
214 <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . '
215 <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" />
216 </label></p>
217 <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>
218 <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" />
219 </label></p>
220 <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>
221 <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" />
222 </label></p>
223 <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>
224 <textarea class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" rows="2" cols="20">' . $caption . '</textarea>
225 </label></p>';
226
227 $alignments = array(
228 'none' => __( 'None', 'jetpack' ),
229 'left' => __( 'Left', 'jetpack' ),
230 'center' => __( 'Center', 'jetpack' ),
231 'right' => __( 'Right', 'jetpack' ),
232 );
233 echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . '
234 <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">';
235 foreach ( $alignments as $alignment => $alignment_name ) {
236 echo '<option value="' . esc_attr( $alignment ) . '" ';
237 if ( $alignment == $align )
238 echo 'selected="selected" ';
239 echo '>' . esc_html($alignment_name) . "</option>\n";
240 }
241 echo '</select></label></p>';
242
243 echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width in pixels:', 'jetpack' ) . '
244 <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" />
245 </label>
246 <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height in pixels:', 'jetpack' ) . '
247 <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" />
248 </label><br />
249 <small>' . esc_html__( "If empty, we will attempt to determine the image size.", 'jetpack' ) . '</small></p>
250 <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . '
251 <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" />
252 </label>
253 <label for="' . $this->get_field_id( 'link_target_blank' ) . '">
254 <input type="checkbox" name="' . $this->get_field_name( 'link_target_blank' ) . '" id="' . $this->get_field_id( 'link_target_blank' ) . '" value="1"' . $link_target_blank . '/>
255 ' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . '
256 </label></p>';
257 }
258 } // Class Jetpack_Image_Widget
259