PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.4.4
Responsive Lightbox & Gallery v2.4.4
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / class-widgets.php
responsive-lightbox / includes Last commit date
providers 3 years ago class-fast-image.php 3 years ago class-folders-walker.php 7 years ago class-folders.php 3 years ago class-frontend.php 3 years ago class-galleries.php 3 years ago class-multilang.php 3 years ago class-remote-library-api.php 3 years ago class-remote-library.php 3 years ago class-settings.php 3 years ago class-tour.php 3 years ago class-welcome.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-widgets.php
767 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 new Responsive_Lightbox_Widgets();
7
8 /**
9 * Responsive Lightbox Widgets class.
10 *
11 * @class Responsive_Lightbox_Widgets
12 */
13 class Responsive_Lightbox_Widgets {
14
15 /**
16 * Class constructor.
17 *
18 * @return void
19 */
20 public function __construct() {
21 // actions
22 add_action( 'widgets_init', [ $this, 'register_widgets' ] );
23 }
24
25 /**
26 * Register widgets.
27 *
28 * @return void
29 */
30 public function register_widgets() {
31 register_widget( 'Responsive_Lightbox_Gallery_Widget' );
32 register_widget( 'Responsive_Lightbox_Image_Widget' );
33 }
34 }
35
36 /**
37 * Responsive Lightbox Gallery Widget class.
38 *
39 * @class Responsive_Lightbox_Gallery_Widget
40 */
41 class Responsive_Lightbox_Gallery_Widget extends WP_Widget {
42
43 private $rlg_defaults = [];
44 private $rlg_orders = [];
45 private $rlg_order_types = [];
46 private $rlg_image_sizes = [];
47 private $rlg_gallery_types = [];
48 private $rli_allowed_html = [
49 'div' => [
50 'class' => [],
51 'id' => []
52 ],
53 'p' => [],
54 'br' => [],
55 'textarea' => [
56 'id' => [],
57 'class' => [],
58 'name' => []
59 ],
60 'label' => [
61 'for' => []
62 ],
63 'input' => [
64 'type' => [],
65 'class' => [],
66 'id' => [],
67 'name' => [],
68 'value' => [],
69 'min' => []
70 ],
71 'a' => [
72 'href' => [],
73 'class' => [],
74 'title' => []
75 ],
76 'ul' => [
77 'id' => [],
78 'class' => []
79 ],
80 'li' => [
81 'class' => [],
82 'data-attachment_id' => []
83 ],
84 'select' => [
85 'name' => [],
86 'id' => [],
87 'class' => []
88 ],
89 'option' => [
90 'value' => [],
91 'selected' => []
92 ],
93 'img' => [
94 'id' => [],
95 'width' => [],
96 'height' => [],
97 'src' => [],
98 'class' => [],
99 'alt' => [],
100 'decoding' => [],
101 'loading' => [],
102 'srcset' => [],
103 'sizes' => [],
104 'style' => [],
105 'title' => [],
106 'data-*' => [],
107 'aria-describedby' => [],
108 'aria-details' => [],
109 'aria-label' => [],
110 'aria-labelledby' => [],
111 'aria-hidden' => [],
112 'align' => []
113 ]
114 ];
115
116 /**
117 * Class constructor.
118 *
119 * @return void
120 */
121 public function __construct() {
122 parent::__construct(
123 'Responsive_Lightbox_Gallery_Widget',
124 __( 'Gallery', 'responsive-lightbox' ),
125 [
126 'description' => __( 'Displays an image gallery.', 'responsive-lightbox' ),
127 'classname' => 'rl-gallery-widget'
128 ]
129 );
130
131 $this->rlg_defaults = [
132 'title' => __( 'Gallery', 'responsive-lightbox' ),
133 'orderby' => 'menu_order',
134 'order' => 'asc',
135 'columns' => 3,
136 'size' => 'thumbnail',
137 'type' => 'none',
138 'atts' => '',
139 'ids' => ''
140 ];
141
142 $this->rlg_orders = [
143 'menu_order' => __( 'Menu order', 'responsive-lightbox' ),
144 'title' => __( 'Title', 'responsive-lightbox' ),
145 'post_date' => __( 'Image date', 'responsive-lightbox' ),
146 'ID' => __( 'ID', 'responsive-lightbox' ),
147 'rand' => __( 'Random', 'responsive-lightbox' )
148 ];
149
150 $this->rlg_order_types = [
151 'asc' => __( 'Ascending', 'responsive-lightbox' ),
152 'desc' => __( 'Descending', 'responsive-lightbox' )
153 ];
154
155 $gallery_types = apply_filters( 'rl_gallery_types', Responsive_Lightbox()->gallery_types );
156
157 if ( ! empty( $gallery_types ) ) {
158 $this->rlg_gallery_types = array_merge(
159 [
160 'none' => __( 'None', 'responsive-lightbox' ),
161 'default' => __( 'Default', 'responsive-lightbox' )
162 ],
163 $gallery_types
164 );
165 }
166
167 $this->rlg_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() );
168
169 sort( $this->rlg_image_sizes, SORT_STRING );
170 }
171
172 /**
173 * Display widget.
174 *
175 * @param array $args
176 * @param object $instance
177 * @return void
178 */
179 public function widget( $args, $instance ) {
180 $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
181
182 $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title'];
183
184 $atts = [];
185
186 // escape atts
187 if ( $instance['atts'] !== '' ) {
188 $atts_exp = explode( '" ', $instance['atts'] );
189
190 if ( ! empty( $atts_exp ) ) {
191 end( $atts_exp );
192
193 $last = key( $atts_exp );
194
195 reset( $atts_exp );
196
197 foreach ( $atts_exp as $id => $attribute ) {
198 $check = $attribute . ( $last === $id ? '' : '"' );
199
200 if ( preg_match( '/^([a-z0-9_-]+)=\"(.+?)\"$/', $check, $matches ) === 1 )
201 $atts[] = $matches[1] . '="' . esc_attr( $matches[2] ) . '"';
202 }
203 }
204 }
205
206 if ( ! empty( $atts ) )
207 $instance['atts'] = implode( ' ', $atts );
208 else
209 $instance['atts'] = '';
210
211 $html .= do_shortcode( '[gallery link="file" columns="' . (int) $instance['columns'] . '" size="' . esc_attr( $instance['size'] ) . '" ' . ( $instance['type'] !== 'none' ? 'type="' . esc_attr( $instance['type'] ) . '"' : '' ) . ' ids="' . ( ! empty( $instance['ids'] ) ? esc_attr( $instance['ids'] ) : '' ) . '" orderby="' . esc_attr( $instance['orderby'] ) . '" order="' . esc_attr( $instance['order'] ) . '"' . ( $instance['atts'] !== '' ? ' ' . $instance['atts'] : '' ) . ']' );
212 $html .= $args['after_widget'];
213
214 echo wp_kses_post( apply_filters( 'rl_gallery_widget_html', $html, $instance ) );
215 }
216
217 /** Render widget form.
218 *
219 * @param object $instance
220 * @return void
221 */
222 public function form( $instance ) {
223 $attachments = ! empty( $instance['ids'] ) ? array_filter( explode( ',', $instance['ids'] ) ) : [];
224
225 $html = '
226 <div class="rl-gallery-widget-container">
227 <p>
228 <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . ':</label>
229 <input id="' . $this->get_field_id( 'title' ) . '" class="widefat" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( isset( $instance['title'] ) ? $instance['title'] : $this->rlg_defaults['title'] ) . '" />
230 </p>
231 <div id="' . $this->get_field_id( 'gallery' ) . '" class="rl-gallery-widget' . ( ! empty( $attachments ) ? ' has-image' : '' ) . '">
232 <input type="hidden" class="rl-gallery-ids" id="' . $this->get_field_id( 'ids' ) . '" name="' . $this->get_field_name( 'ids' ) . '" value="' . ( ! empty( $instance['ids'] ) ? esc_attr( $instance['ids'] ) : '' ) . '">';
233
234 $html .= '
235 <a href="#" class="rl-gallery-widget-select button button-secondary">' . esc_html__( 'Select images', 'responsive-lightbox' ) . '</a>
236 <div class="rl-gallery-widget-content">
237 <ul id="' . $this->get_field_id( 'gallery-images' ) . '" class="rl-gallery-images">';
238
239 if ( $attachments ) {
240 foreach ( $attachments as $attachment_id ) {
241 if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) )
242 continue;
243
244 $html .= '
245 <li class="rl-gallery-image" data-attachment_id="' . (int) $attachment_id . '">
246 <div class="rl-gallery-inner">' . wp_get_attachment_image( $attachment_id, 'thumbnail' ) . '</div>
247 <div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons dashicons-no" title="' . esc_attr__( 'Delete image', 'responsive-lightbox' ) . '"></a></div>
248 </li>';
249 }
250 }
251
252 $html .= '
253 </ul>
254 </div>
255 </div>
256 <p>';
257
258 if ( ! empty( $this->rlg_gallery_types ) ) {
259 $html .= '
260 <label for="' . $this->get_field_id( 'type' ) . '">' . esc_html__( 'Gallery type', 'responsive-lightbox' ) . ':</label>
261 <select id="' . $this->get_field_id( 'type' ) . '" class="widefat" name="' . $this->get_field_name( 'type' ) . '">';
262
263 foreach ( $this->rlg_gallery_types as $id => $type ) {
264 $html .= '
265 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['type'] ) ? $instance['type'] : $this->rlg_defaults['type'] ), false ) . '>' . esc_html( $type ) . '</option>';
266 }
267
268 $html .= '
269 </select>
270 </p>
271 <p>';
272 }
273
274 $html .= '
275 <label for="' . $this->get_field_id( 'orderby' ) . '">' . esc_html__( 'Order by', 'responsive-lightbox' ) . ':</label>
276 <select id="' . $this->get_field_id( 'orderby' ) . '" class="widefat" name="' . $this->get_field_name( 'orderby' ) . '">';
277
278 foreach ( $this->rlg_orders as $id => $orderby ) {
279 $html .= '
280 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['orderby'] ) ? $instance['orderby'] : $this->rlg_defaults['orderby'] ), false ) . '>' . esc_html( $orderby ) . '</option>';
281 }
282
283 $html .= '
284 </select>
285 </p>
286 <p>
287 <label for="' . $this->get_field_id( 'order' ) . '">' . esc_html__( 'Order', 'responsive-lightbox' ) . ':</label>
288 <select id="' . $this->get_field_id( 'order' ) . '" class="widefat" name="' . $this->get_field_name( 'order' ) . '">';
289
290 foreach ( $this->rlg_order_types as $id => $order ) {
291 $html .= '
292 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->rlg_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>';
293 }
294
295 $html .= '
296 </select>
297 </p>
298 <p>
299 <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Image size', 'responsive-lightbox' ) . ':</label>
300 <select id="' . $this->get_field_id( 'size' ) . '" class="widefat" name="' . $this->get_field_name( 'size' ) . '">';
301
302 foreach ( $this->rlg_image_sizes as $size ) {
303 $html .= '
304 <option value="' . esc_attr( $size ) . '" ' . selected( $size, ( isset( $instance['size'] ) ? $instance['size'] : $this->rlg_defaults['size'] ), false ) . '>' . esc_html( $size ) . '</option>';
305 }
306
307 $html .= '
308 </select>
309 </p>
310 <p>
311 <label for="' . $this->get_field_id( 'columns' ) . '">' . esc_html__( 'Number of columns', 'responsive-lightbox' ) . ':</label>
312 <input id="' . $this->get_field_id( 'columns' ) . '" class="small-text" name="' . $this->get_field_name( 'columns' ) . '" type="number" min="0" value="' . (int) ( isset( $instance['columns'] ) ? $instance['columns'] : $this->rlg_defaults['columns'] ) . '" />
313 </p>
314 <p>
315 <label for="' . $this->get_field_id( 'atts' ) . '">' . esc_html__( 'Custom gallery shortcode attributes', 'responsive-lightbox' ) . ':</label>
316 <br />
317 <textarea id="' . $this->get_field_id( 'atts' ) . '" class="widefat" name="' . $this->get_field_name( 'atts' ) . '">' . esc_textarea( isset( $instance['atts'] ) ? $instance['atts'] : $this->rlg_defaults['atts'] ) . '</textarea>
318 </p>
319 </div>';
320
321 echo wp_kses( $html, $this->rli_allowed_html );
322 }
323
324 /**
325 * Save widget form.
326 *
327 * @param array $new_instance
328 * @param array $old_instance
329 * @return array
330 */
331 public function update( $new_instance, $old_instance ) {
332 // title
333 $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? trim( $new_instance['title'] ) : $this->rlg_defaults['title'] );
334
335 // order by
336 $old_instance['orderby'] = isset( $new_instance['orderby'] ) && array_key_exists( $new_instance['orderby'], $this->rlg_orders ) ? $new_instance['orderby'] : $this->rlg_defaults['orderby'];
337
338 // order
339 $old_instance['order'] = isset( $new_instance['order'] ) && array_key_exists( $new_instance['order'], $this->rlg_order_types ) ? $new_instance['order'] : $this->rlg_defaults['order'];
340
341 // image size
342 $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rlg_image_sizes, true ) ? $new_instance['size'] : $this->rlg_defaults['size'];
343
344 // gallery type
345 $old_instance['type'] = isset( $new_instance['type'] ) && array_key_exists( $new_instance['type'], $this->rlg_gallery_types ) ? $new_instance['type'] : $this->rlg_defaults['type'];
346
347 // number of columns
348 $old_instance['columns'] = isset( $new_instance['columns'] ) ? ( ( $columns = (int) $new_instance['columns'] ) > 0 ? $columns : $this->rlg_defaults['columns'] ) : $this->rlg_defaults['columns'];
349
350 // image ids
351 if ( ! empty( $new_instance['ids'] ) && is_string( $new_instance['ids'] ) ) {
352 // get unique and non empty attachment ids only
353 $old_instance['ids'] = implode( ',', array_unique( array_filter( array_map( 'intval', explode( ',', $new_instance['ids'] ) ) ) ) );
354 } else
355 $old_instance['ids'] = $this->rlg_defaults['ids'];
356
357 // custom attributes
358 $atts = sanitize_textarea_field( preg_replace( '/\s+/', ' ', trim( str_replace( [ "\r\n", "\n\r", "\n", "\r" ], ' ', isset( $new_instance['atts'] ) ? $new_instance['atts'] : $this->rlg_defaults['atts'] ) ) ) );
359
360 $new_atts = [];
361
362 if ( $atts !== '' ) {
363 $atts_exp = explode( '" ', $atts );
364
365 if ( ! empty( $atts_exp ) ) {
366 end( $atts_exp );
367
368 $last = key( $atts_exp );
369
370 reset( $atts_exp );
371
372 foreach ( $atts_exp as $id => $attribute ) {
373 $check = $attribute . ( $last === $id ? '' : '"' );
374
375 if ( preg_match( '/^[a-z0-9_-]+=\"(.+?)\"$/', $check ) === 1 )
376 $new_atts[] = $check;
377 }
378 }
379 }
380
381 if ( ! empty( $new_atts ) )
382 $old_instance['atts'] = implode( ' ', $new_atts );
383 else
384 $old_instance['atts'] = '';
385
386 return $old_instance;
387 }
388 }
389
390 /**
391 * Responsive Lightbox Gallery Widget class.
392 *
393 * @class Responsive_Lightbox_Gallery_Widget
394 */
395 class Responsive_Lightbox_Image_Widget extends WP_Widget {
396
397 private $rli_defaults = [];
398 private $rli_text_positions = [];
399 private $rli_link_to = [];
400 private $rli_aligns = [];
401 private $rli_image_sizes = [];
402 private $rli_allowed_html = [
403 'div' => [
404 'class' => [],
405 'id' => []
406 ],
407 'p' => [
408 'style' => [],
409 'class' => []
410 ],
411 'textarea' => [
412 'id' => [],
413 'class' => [],
414 'name' => [],
415 'rows' => []
416 ],
417 'label' => [
418 'for' => []
419 ],
420 'input' => [
421 'type' => [],
422 'class' => [],
423 'id' => [],
424 'name' => [],
425 'value' => [],
426 'min' => [],
427 'checked' => []
428 ],
429 'a' => [
430 'href' => [],
431 'class' => [],
432 'title' => []
433 ],
434 'select' => [
435 'name' => [],
436 'id' => [],
437 'class' => []
438 ],
439 'option' => [
440 'value' => [],
441 'selected' => []
442 ],
443 'img' => [
444 'id' => [],
445 'width' => [],
446 'height' => [],
447 'src' => [],
448 'class' => [],
449 'alt' => [],
450 'decoding' => [],
451 'loading' => [],
452 'srcset' => [],
453 'sizes' => [],
454 'style' => [],
455 'title' => [],
456 'data-*' => [],
457 'aria-describedby' => [],
458 'aria-details' => [],
459 'aria-label' => [],
460 'aria-labelledby' => [],
461 'aria-hidden' => [],
462 'align' => []
463 ]
464 ];
465
466 /**
467 * Class constructor.
468 *
469 * @return void
470 */
471 public function __construct() {
472 parent::__construct(
473 'Responsive_Lightbox_Image_Widget',
474 __( 'Image', 'responsive-lightbox' ),
475 [
476 'description' => __( 'Displays a single image.', 'responsive-lightbox' ),
477 'classname' => 'rl-image-widget'
478 ]
479 );
480
481 $this->rli_defaults = [
482 'title' => __( 'Image', 'responsive-lightbox' ),
483 'image_id' => 0,
484 'responsive' => true,
485 'size' => 'thumbnail',
486 'link_to' => 'file',
487 'link_custom_url' => '',
488 'image_align' => 'none',
489 'text' => '',
490 'autobr' => false,
491 'text_position' => 'below_image',
492 'text_align' => 'none'
493 ];
494
495 $this->rli_text_positions = [
496 'below_image' => __( 'Below the image', 'responsive-lightbox' ),
497 'above_image' => __( 'Above the image', 'responsive-lightbox' )
498 ];
499
500 $this->rli_link_to = [
501 'none' => __( 'None', 'responsive-lightbox' ),
502 'file' => __( 'Media File', 'responsive-lightbox' ),
503 'post' => __( 'Attachment Page', 'responsive-lightbox' ),
504 'custom' => __( 'Custom URL', 'responsive-lightbox' )
505 ];
506
507 $this->rli_aligns = [
508 'none' => __( 'None', 'responsive-lightbox' ),
509 'left' => __( 'Left', 'responsive-lightbox' ),
510 'center' => __( 'Center', 'responsive-lightbox' ),
511 'right' => __( 'Right', 'responsive-lightbox' ),
512 'justify' => __( 'Justify', 'responsive-lightbox' )
513 ];
514
515 $this->rli_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() );
516
517 sort( $this->rli_image_sizes, SORT_STRING );
518 }
519
520 /**
521 * Display widget.
522 *
523 * @param array $args
524 * @param array $instance
525 * @return void
526 */
527 public function widget( $args, $instance ) {
528 switch ( $instance['link_to'] ) {
529 case 'file':
530 $file = wp_get_attachment_image_src( $instance['image_id'], 'full', false );
531 $href = $file[0];
532 break;
533
534 case 'post':
535 $href = get_permalink( $instance['image_id'] );
536 break;
537
538 case 'custom':
539 $href = $instance['link_custom_url'];
540 break;
541
542 case 'none':
543 default:
544 $href = '';
545 }
546
547 // image align
548 if ( $instance['image_align'] === 'left' )
549 $image_align = 'float: left;';
550 elseif ( $instance['image_align'] === 'center' )
551 $image_align = 'margin-left: auto; margin-right: auto; display: block;';
552 elseif ( $instance['image_align'] === 'right' )
553 $image_align = 'float: right;';
554 else
555 $image_align = '';
556
557 // text align
558 if ( $instance['text_align'] === 'left' )
559 $text_align = 'text-align: left; display: block;';
560 elseif ( $instance['text_align'] === 'center' )
561 $text_align = 'text-align: center; display: block;';
562 elseif ( $instance['text_align'] === 'right' )
563 $text_align = 'text-align: right; display: block;';
564 elseif ( $instance['text_align'] === 'justify' )
565 $text_align = 'text-align: justify; display: block;';
566 else
567 $text_align = '';
568
569 // get image data
570 $image = wp_get_attachment_image_src( $instance['image_id'], $instance['size'], false );
571
572 $width = $instance['responsive'] === false ? $image[1] : '100%';
573 $height = $instance['responsive'] === false ? $image[2] : 'auto';
574 $post = get_post( $instance['image_id'] );
575 $image_title = isset( $post->post_title ) ? $post->post_title : '';
576 $alt = (string) get_post_meta( $instance['image_id'], '_wp_attachment_image_alt', true );
577 $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
578
579 // start output
580 $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title'];
581
582 if ( $instance['autobr'] === true )
583 $escaped_text = wpautop( esc_html( $instance['text'] ) );
584 else
585 $escaped_text = esc_html( $instance['text'] );
586
587 $container_html = '<div class="rl-image-widget-text" style="' . esc_attr( $text_align ) . '">' . $escaped_text . '</div>';
588 $image_html = ( $href !== '' ? '<a href="' . esc_url( $href ) . '" class="rl-image-widget-link">' : '' ) . '<img class="rl-image-widget-image" src="' . esc_url( $image[0] ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" title="' . esc_attr( $image_title ) . '" alt="' . esc_attr( $alt ) . '" style="' . esc_attr( $image_align ) . '" />' . ( $href !== '' ? '</a>' : '' );
589
590 if ( $instance['text_position'] === 'below_image' )
591 $html .= $image_html . $container_html;
592 else
593 $html .= $container_html . $image_html;
594
595 $html .= $args['after_widget'];
596
597 echo wp_kses_post( apply_filters( 'rl_image_widget_html', $html, $instance ) );
598 }
599
600 /** Render widget form.
601 *
602 * @param array $instance
603 * @return void
604 */
605 public function form( $instance ) {
606 $image_id = (int) ( isset( $instance['image_id'] ) ? $instance['image_id'] : $this->rli_defaults['image_id'] );
607 $image = '';
608
609 if ( ! empty( $image_id ) )
610 $image = wp_get_attachment_image( $image_id, 'medium', false );
611
612 if ( ! $image )
613 $image = wp_get_attachment_image( $image_id, 'full', false );
614
615 $html = '
616 <div class="rl-image-widget-container">
617 <p>
618 <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . '</label>
619 <input id="' . $this->get_field_id( 'title' ) . '" class="widefat" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( isset( $instance['title'] ) ? $instance['title'] : $this->rli_defaults['title'] ) . '" />
620 </p>
621 <div class="rl-image-widget' . ( ! empty( $image_id ) ? ' has-image' : '' ) . '">
622 <input class="rl-image-widget-image-id" type="hidden" name="' . $this->get_field_name( 'image_id' ) . '" value="' . (int) $image_id . '" />
623 <a href="#" class="rl-image-widget-select button button-secondary">' . esc_html__( 'Select image', 'responsive-lightbox' ) . '</a>
624 <div class="rl-image-widget-content">';
625
626 if ( ! empty( $image ) )
627 $html .= $image;
628
629 $html .= '
630 </div>
631 </div>
632 <p>
633 <input id="' . $this->get_field_id( 'responsive' ) . '" type="checkbox" name="' . $this->get_field_name( 'responsive' ) . '" value="responsive" ' . checked( true, ( isset( $instance['responsive'] ) ? $instance['responsive'] : $this->rli_defaults['responsive'] ), false ) . ' /> <label for="' . $this->get_field_id( 'responsive' ) . '">' . esc_html__( 'Force responsive', 'responsive-lightbox' ) . '</label>
634 </p>';
635
636 $html .= '
637 <p>
638 <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Size', 'responsive-lightbox' ) . '</label>
639 <select class="rl-image-size-select widefat" id="' . $this->get_field_id( 'size' ) . '" name="' . $this->get_field_name( 'size' ) . '">';
640
641 $size_type = ( isset( $instance['size'] ) ? $instance['size'] : $this->rli_defaults['size'] );
642
643 foreach ( $this->rli_image_sizes as $size ) {
644 $html .= '
645 <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>';
646 }
647
648 $html .= '
649 </select>
650 </p>
651 <p>
652 <label for="' . $this->get_field_id( 'link_to' ) . '">' . esc_html__( 'Link to', 'responsive-lightbox' ) . '</label>
653 <select class="rl-image-link-to widefat" id="' . $this->get_field_id( 'link_to' ) . '" name="' . $this->get_field_name( 'link_to' ) . '">';
654
655 $link_type = ( isset( $instance['link_to'] ) ? $instance['link_to'] : $this->rli_defaults['link_to'] );
656
657 foreach ( $this->rli_link_to as $id => $type ) {
658 $html .= '
659 <option value="' . esc_attr( $id ) . '" ' . selected( $id, $link_type, false ) . '>' . esc_html( $type ) . '</option>';
660 }
661
662 $html .= '
663 </select>
664 </p>
665 <p class="rl-image-link-url"' . ( $link_type === 'custom' ? '' : ' style="display: none;"' ) . '>
666 <label for="' . $this->get_field_id( 'link_custom_url' ) . '">' . esc_html__( 'URL', 'responsive-lightbox' ) . '</label>
667 <input id="' . $this->get_field_id( 'link_custom_url' ) . '" class="widefat" name="' . $this->get_field_name( 'link_custom_url' ) . '" type="text" value="' . esc_attr( isset( $instance['link_custom_url'] ) ? $instance['link_custom_url'] : $this->rli_defaults['link_custom_url'] ) . '" />
668 </p>';
669
670 $html .= '
671 <p>
672 <label for="' . $this->get_field_id( 'image_align' ) . '">' . esc_html__( 'Image align', 'responsive-lightbox' ) . '</label>
673 <select id="' . $this->get_field_id( 'image_align' ) . '" class="widefat" name="' . $this->get_field_name( 'image_align' ) . '">';
674
675 foreach ( $this->rli_aligns as $id => $image_align ) {
676 if ( $id !== 'justify' )
677 $html .= '
678 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['image_align'] ) ? $instance['image_align'] : $this->rli_defaults['image_align'] ), false ) . '>' . esc_html( $image_align ) . '</option>';
679 }
680
681 $html .= '
682 </select>
683 </p>
684 <p>
685 <label for="' . $this->get_field_id( 'text' ) . '">' . esc_html__( 'Text', 'responsive-lightbox' ) . '</label>
686 <textarea id="' . $this->get_field_id( 'text' ) . '" class="widefat" name="' . $this->get_field_name( 'text' ) . '" rows="4">' . esc_html( isset( $instance['text'] ) ? $instance['text'] : $this->rli_defaults['text'] ) . '</textarea>
687 </p>
688 <p>
689 <input id="' . $this->get_field_id( 'autobr' ) . '" type="checkbox" name="' . $this->get_field_name( 'autobr' ) . '" value="autobr" ' . checked( true, ( isset( $instance['autobr'] ) ? $instance['autobr'] : $this->rli_defaults['autobr'] ), false ) . ' /> <label for="' . $this->get_field_id( 'autobr' ) . '">' . esc_html__( 'Automatically add paragraphs', 'responsive-lightbox' ) . '</label>
690 </p>';
691
692 $html .= '
693 <p>
694 <label for="' . $this->get_field_id( 'text_position' ) . '">' . esc_html__( 'Text position', 'responsive-lightbox' ) . '</label>
695 <select id="' . $this->get_field_id( 'text_position' ) . '" class="widefat" name="' . $this->get_field_name( 'text_position' ) . '">';
696
697 foreach ( $this->rli_text_positions as $id => $text_position ) {
698 $html .= '
699 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['text_position'] ) ? $instance['text_position'] : $this->rli_defaults['text_position'] ), false ) . '>' . esc_html( $text_position ) . '</option>';
700 }
701
702 $html .= '
703 </select>
704 </p>
705 <label for="' . $this->get_field_id( 'text_align' ) . '">' . esc_html__( 'Text align', 'responsive-lightbox' ) . '</label>
706 <select id="' . $this->get_field_id( 'text_align' ) . '" class="widefat" name="' . $this->get_field_name( 'text_align' ) . '">';
707
708 foreach ( $this->rli_aligns as $id => $text_align ) {
709 $html .= '
710 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['text_align'] ) ? $instance['text_align'] : $this->rli_defaults['text_align'] ), false ) . '>' . esc_html( $text_align ) . '</option>';
711 }
712
713 $html .= '
714 </select>
715 </div>';
716
717 add_filter( 'safe_style_css', [ $this, 'rli_allow_display_attr' ] );
718
719 echo wp_kses( $html, $this->rli_allowed_html );
720
721 remove_filter( 'safe_style_css', [ $this, 'rli_allow_display_attr' ] );
722 }
723
724 /**
725 * Save widget form.
726 *
727 * @param array $new_instance
728 * @param array $old_instance
729 * @return array
730 */
731 public function update( $new_instance, $old_instance ) {
732 // whitelists
733 $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rli_image_sizes, true ) ? $new_instance['size'] : $this->rli_defaults['size'];
734 $old_instance['link_to'] = isset( $new_instance['link_to'] ) && in_array( $new_instance['link_to'], array_keys( $this->rli_link_to ), true ) ? $new_instance['link_to'] : $this->rli_defaults['link_to'];
735 $old_instance['image_align'] = isset( $new_instance['image_align'] ) && in_array( $new_instance['image_align'], array_keys( $this->rli_aligns ), true ) ? $new_instance['image_align'] : $this->rli_defaults['image_align'];
736 $old_instance['text_position'] = isset( $new_instance['text_position'] ) && in_array( $new_instance['text_position'], array_keys( $this->rli_text_positions ), true ) ? $new_instance['text_position'] : $this->rli_defaults['text_position'];
737 $old_instance['text_align'] = isset( $new_instance['text_align'] ) && in_array( $new_instance['text_align'], array_keys( $this->rli_aligns ), true ) ? $new_instance['text_align'] : $this->rli_defaults['text_align'];
738
739 // booleands
740 $old_instance['responsive'] = ! empty( $new_instance['responsive'] );
741 $old_instance['autobr'] = ! empty( $new_instance['autobr'] );
742
743 // texts
744 $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->rli_defaults['title'] );
745 $old_instance['text'] = isset( $new_instance['text'] ) ? wp_kses_post( $new_instance['text'] ) : $this->rli_defaults['text'];
746
747 // integers
748 $old_instance['image_id'] = isset( $new_instance['image_id'] ) ? (int) $new_instance['image_id'] : $this->rli_defaults['image_id'];
749
750 // urls
751 $old_instance['link_custom_url'] = isset( $new_instance['link_custom_url'] ) ? esc_url( $new_instance['link_custom_url'] ) : $this->rli_defaults['link_custom_url'];
752
753 return $old_instance;
754 }
755
756 /**
757 * Add display property to style safe list.
758 *
759 * @param array $styles
760 * @return array
761 */
762 public function rli_allow_display_attr( $styles ) {
763 $styles[] = 'display';
764
765 return $styles;
766 }
767 }