PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.4.12
Image Widget v4.4.12
trunk 1.0 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2 3.2.1 3.2.10 3.2.11 3.2.2 3.2.3 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9
image-widget / image-widget.php
image-widget Last commit date
lang 1 year ago lib 1 year ago resources 2 months ago views 1 year ago .gitignore 1 year ago image-widget.php 2 months ago index.php 1 year ago readme.txt 2 months ago
image-widget.php
651 lines
1 <?php
2 /*
3 Plugin Name: Image Widget
4 Plugin URI: https://wordpress.org/plugins/image-widget/
5 Description: A simple image widget that uses the native WordPress media manager to add image widgets to your site.
6 Author: The Events Calendar
7 Version: 4.4.12
8 Author URI: https://evnt.is/1aor
9 Text Domain: image-widget
10 Domain Path: /lang
11 */
12
13 // Block direct requests
14 if ( ! defined( 'ABSPATH' ) ) {
15 die( '-1' );
16 }
17
18 // Load the widget on widgets_init
19 function tribe_load_image_widget() {
20 register_widget( 'Tribe_Image_Widget' );
21 }
22 add_action( 'widgets_init', 'tribe_load_image_widget' );
23
24 class Tribe_Image_Widget extends WP_Widget {
25
26 const VERSION = '4.4.12';
27
28 const CUSTOM_IMAGE_SIZE_SLUG = 'tribe_image_widget_custom';
29
30 const VERSION_KEY = '_image_widget_version';
31
32 /**
33 * Tribe Image Widget constructor
34 */
35 public function __construct() {
36 load_plugin_textdomain( 'image-widget', false, trailingslashit( basename( dirname( __FILE__ ) ) ) . 'lang/' );
37
38 $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image-widget' ) );
39 $control_ops = array( 'id_base' => 'widget_sp_image' );
40
41 parent::__construct( 'widget_sp_image', __( 'Image Widget', 'image-widget' ), $widget_ops, $control_ops );
42
43 if ( $this->use_old_uploader() ) {
44 require_once( 'lib/ImageWidgetDeprecated.php' );
45 new ImageWidgetDeprecated( $this );
46 } else {
47 add_action( 'sidebar_admin_setup', array( $this, 'admin_setup' ) );
48 }
49
50 // fire admin_setup if we are in the customizer
51 add_action( 'admin_enqueue_scripts', array( $this, 'maybe_admin_setup' ) );
52 }
53
54 /**
55 * Test to see if this version of WordPress supports the new image manager.
56 *
57 * @return bool True if the current version of WordPress does NOT support the current image management tech.
58 */
59 private function use_old_uploader() {
60 if ( defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
61 return true;
62 }
63
64 return ! function_exists( 'wp_enqueue_media' );
65 }
66
67 /**
68 * Enqueue all the javascript and CSS.
69 */
70 public function admin_setup() {
71 wp_enqueue_media();
72
73 wp_enqueue_style( 'tribe-image-widget', plugins_url( 'resources/css/admin.css', __FILE__ ), array(), self::VERSION );
74 wp_enqueue_script( 'tribe-image-widget', plugins_url( 'resources/js/image-widget.js', __FILE__ ), array( 'jquery', 'media-upload', 'media-views' ), self::VERSION );
75
76 wp_localize_script( 'tribe-image-widget', 'TribeImageWidget', array(
77 'frame_title' => __( 'Select an Image', 'image-widget' ),
78 'button_title' => __( 'Insert Into Widget', 'image-widget' ),
79 ) );
80 }
81
82 /**
83 * Trigger the enqueueing of admin scripts and styles on widget admin page and in the "Customizer" view.
84 */
85 public function maybe_admin_setup() {
86 $screen = get_current_screen();
87
88 if ( 'customize' !== $screen->base ) {
89 return;
90 }
91
92 $this->admin_setup();
93 }
94
95 /**
96 * Widget frontend output
97 *
98 * @param array $args
99 * @param array $instance
100 */
101 public function widget( $args, $instance ) {
102 extract( $args );
103
104 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
105
106 if ( ! empty( $instance['imageurl'] ) || ! empty( $instance['attachment_id'] ) ) {
107
108 /**
109 * Filters the title of the widget.
110 *
111 * @link https://developer.wordpress.org/reference/hooks/widget_title/
112 *
113 * @param string $title The widget title.
114 * @param array $instance The array of widget options.
115 * @param mixed $id_base The widget ID.
116 */
117 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
118
119 /**
120 * Filters the text of the widget.
121 *
122 * @link https://developer.wordpress.org/reference/hooks/widget_text/
123 *
124 * @param string $description The widget description.
125 * @param array $args The array of widget arguments.
126 * @param array $instance The array of widget options.
127 */
128 $instance['description'] = apply_filters( 'widget_text', $instance['description'], $args, $instance );
129
130 /**
131 * Filters the link of the image widget.
132 *
133 * @since 4.0.0
134 *
135 * @param string $link The widget image link.
136 * @param array $args The array of widget arguments.
137 * @param array $instance The array of widget options.
138 */
139 $instance['link'] = apply_filters( 'image_widget_image_link', esc_url( $instance['link'] ), $args, $instance );
140
141 /**
142 * Filters the link id of the image widget.
143 *
144 * @since 4.0.0
145 *
146 * @param string $linkid The widget image link ID.
147 * @param array $args The array of widget arguments.
148 * @param array $instance The array of widget options.
149 */
150 $instance['linkid'] = apply_filters( 'image_widget_image_link_id', esc_attr( $instance['linkid'] ), $args, $instance );
151
152 /**
153 * Filters the link title of the image widget.
154 *
155 * @since 4.0.0
156 *
157 * @param string $linktitle The widget image link title.
158 * @param array $args The array of widget arguments.
159 * @param array $instance The array of widget options.
160 */
161 $instance['linktitle'] = apply_filters( 'image_widget_image_link_title', esc_attr( $instance['linktitle'] ), $args, $instance );
162
163 /**
164 * Filters the link target of the image widget.
165 *
166 * @since 4.0.0
167 *
168 * @param string $linktarget The widget image link target.
169 * @param array $args The array of widget arguments.
170 * @param array $instance The array of widget options.
171 */
172 $instance['linktarget'] = apply_filters( 'image_widget_image_link_target', esc_attr( $instance['linktarget'] ), $args, $instance );
173
174 /**
175 * Filters the width of the image in the widget.
176 *
177 * @since 4.0.0
178 *
179 * @param string $width The width of the widget image.
180 * @param array $args The array of widget arguments.
181 * @param array $instance The array of widget options.
182 */
183 $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance );
184
185 /**
186 * Filters the height of the image in the widget.
187 *
188 * @since 4.0.0
189 *
190 * @param string $height The height of the widget image.
191 * @param array $args The array of widget arguments.
192 * @param array $instance The array of widget options.
193 */
194 $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance );
195
196 /**
197 * Filters the max width of the image in the widget.
198 *
199 * @since 4.0.0
200 *
201 * @param string $maxwidth The max width of the widget image.
202 * @param array $args The array of widget arguments.
203 * @param array $instance The array of widget options.
204 */
205 $instance['maxwidth'] = apply_filters( 'image_widget_image_maxwidth', esc_attr( $instance['maxwidth'] ), $args, $instance );
206
207 /**
208 * Filters the max height of the image in the widget.
209 *
210 * @since 4.0.0
211 *
212 * @param string $maxheight The max height of the widget image.
213 * @param array $args The array of widget arguments.
214 * @param array $instance The array of widget options.
215 */
216 $instance['maxheight'] = apply_filters( 'image_widget_image_maxheight', esc_attr( $instance['maxheight'] ), $args, $instance );
217
218 /**
219 * Filters the alignment of the image in the widget.
220 *
221 * @since 4.0.0
222 *
223 * @param string $align The alignment of the widget image.
224 * @param array $args The array of widget arguments.
225 * @param array $instance The array of widget options.
226 */
227 $instance['align'] = apply_filters( 'image_widget_image_align', esc_attr( $instance['align'] ), $args, $instance );
228
229 /**
230 * Filters the alt text of the image in the widget.
231 *
232 * @since 4.0.0
233 *
234 * @param string $alt The alt text of the widget image.
235 * @param array $args The array of widget arguments.
236 * @param array $instance The array of widget options.
237 */
238 $instance['alt'] = apply_filters( 'image_widget_image_alt', esc_attr( $instance['alt'] ), $args, $instance );
239
240 /**
241 * Filters the rel attribute of the image in the widget.
242 *
243 * @since 4.0.0
244 *
245 * @param string $rel The rel attribute of the widget image.
246 * @param array $args The array of widget arguments.
247 * @param array $instance The array of widget options.
248 */
249 $instance['rel'] = apply_filters( 'image_widget_image_rel', esc_attr( $instance['rel'] ), $args, $instance );
250
251 /**
252 * Checks if the IMAGE_WIDGET_COMPATIBILITY_TEST constant is defined.
253 */
254 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
255
256 /**
257 * Assigns the attachment_id field in the instance array a value based on condition.
258 */
259 $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image'];
260
261 /**
262 * Filters the attachment ID of the image in the widget.
263 *
264 * @since 4.0.0
265 *
266 * @param int $attachment_id The attachment ID of the widget image.
267 * @param array $args The array of widget arguments.
268 * @param array $instance The array of widget options.
269 */
270 $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance );
271
272 /**
273 * Filters the size of the image in the widget.
274 *
275 * @since 4.0.0
276 *
277 * @param string $size The size of the widget image.
278 * @param array $args The array of widget arguments.
279 * @param array $instance The array of widget options.
280 */
281 $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance );
282 }
283
284 /**
285 * Filters the URL of the image in the widget.
286 *
287 * @since 4.0.0
288 *
289 * @param string $imageurl The URL of the widget image.
290 * @param array $args The array of widget arguments.
291 * @param array $instance The array of widget options.
292 */
293 $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance );
294
295 // No longer using extracted vars. This is here for backwards compatibility.
296 extract( $instance );
297
298 include( $this->getTemplateHierarchy( 'widget' ) );
299 }
300 }
301
302 /**
303 * Update widget options
304 *
305 * @param object $new_instance Widget Instance
306 * @param object $old_instance Widget Instance
307 * @return object
308 */
309 public function update( $new_instance, $old_instance ) {
310
311 $instance = $old_instance;
312 $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
313
314 $instance['title'] = strip_tags( $new_instance['title'] );
315
316 if ( current_user_can( 'unfiltered_html' ) ) {
317 $instance['description'] = $new_instance['description'];
318 } else {
319 $instance['description'] = wp_kses_post( $new_instance['description'] );
320 }
321
322 /**
323 * Make the description filterable; especially useful for working with allowing/disallowing HTML and other content.
324 *
325 * @since 4.4.6
326 *
327 * @param $description The current value of $instance['description'].
328 * @param $new_instance The new instance of the widget, i.e. the new values being saved for it.
329 * @param $old_instance The pre-existing instance of the widget, i.e. the values that are potentially being updated.
330 */
331 $instance['description'] = apply_filters( 'tribe_image_widget_instance_description', $instance['description'], $new_instance, $old_instance );
332
333 $instance['link'] = $new_instance['link'];
334 $instance['linktitle'] = $new_instance['linktitle'];
335 $instance['linkid'] = $new_instance['linkid'];
336 $instance['linktarget'] = $new_instance['linktarget'];
337 $instance['width'] = abs( $new_instance['width'] );
338 $instance['height'] = abs( $new_instance['height'] );
339
340 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
341 $instance['size'] = $new_instance['size'];
342 }
343
344 $instance['align'] = $new_instance['align'];
345 $instance['alt'] = $new_instance['alt'];
346 $instance['rel'] = $new_instance['rel'];
347
348 // Reverse compatibility with $image, now called $attachement_id
349 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) {
350 $instance['attachment_id'] = abs( $new_instance['attachment_id'] );
351 } elseif ( $new_instance['image'] > 0 ) {
352 $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] );
353 if ( class_exists( 'ImageWidgetDeprecated' ) ) {
354 $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now
355 }
356 }
357
358 $instance['imageurl'] = $new_instance['imageurl']; // deprecated
359 $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance );
360
361 return $instance;
362 }
363
364 /**
365 * Form UI
366 *
367 * @param object $instance Widget Instance
368 */
369 public function form( $instance ) {
370 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
371 if ( $this->use_old_uploader() ) {
372 include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) );
373 } else {
374 include( $this->getTemplateHierarchy( 'widget-admin' ) );
375 }
376 }
377
378 /**
379 * Render an array of default values.
380 *
381 * @return array default values
382 */
383 private static function get_defaults() {
384
385 $defaults = array(
386 'title' => '',
387 'description' => '',
388 'link' => '',
389 'linkid' => '',
390 'linktitle' => '',
391 'linktarget' => '',
392 'width' => 0,
393 'height' => 0,
394 'maxwidth' => '100%',
395 'maxheight' => '',
396 'image' => 0, // reverse compatible - now attachement_id
397 'imageurl' => '', // reverse compatible.
398 'align' => 'none',
399 'alt' => '',
400 'rel' => '',
401 );
402
403 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
404 $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
405 $defaults['attachment_id'] = 0;
406 }
407 /**
408 * Allow users to customize the default values of various Image Widget options.
409 *
410 * @since 4.4.7
411 *
412 * @param array $defaults The array of default option values.
413 */
414 return apply_filters( 'image_widget_option_defaults', $defaults );
415 }
416
417 /**
418 * Render the image html output.
419 *
420 * @param array $instance
421 * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
422 * @return string image html
423 */
424 private function get_image_html( $instance, $include_link = true ) {
425
426 // Backwards compatible image display.
427 if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
428 $instance['attachment_id'] = $instance['image'];
429 }
430
431 $output = '';
432
433 if ( $include_link && ! empty( $instance['link'] ) ) {
434
435 $attr = array(
436 'href' => $instance['link'],
437 'id' => $instance['linkid'],
438 'target' => $instance['linktarget'],
439 'class' => $this->widget_options['classname'] . '-image-link',
440 'title' => ( ! empty( $instance['linktitle'] ) ) ? $instance['linktitle'] : $instance['title'],
441 'rel' => $instance['rel'],
442 );
443
444 $attr = apply_filters( 'image_widget_link_attributes', $attr, $instance );
445 $attr = array_map( 'esc_attr', $attr );
446
447 $output = '<a';
448
449 foreach ( $attr as $name => $value ) {
450 if ( ! empty( $value ) ) {
451 $output .= sprintf( ' %s="%s"', $name, $value );
452 }
453 }
454 $output .= '>';
455 }
456
457 $size = $this->get_image_size( $instance );
458
459 if ( is_array( $size ) ) {
460 $instance['width'] = $size[0];
461 $instance['height'] = $size[1];
462 } elseif ( ! empty( $instance['attachment_id'] ) ) {
463 //$instance['width'] = $instance['height'] = 0;
464 $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
465 if ( $image_details ) {
466 $instance['imageurl'] = $image_details[0];
467 $instance['width'] = $image_details[1];
468 $instance['height'] = $image_details[2];
469 }
470
471 $image_srcset = function_exists( 'wp_get_attachment_image_srcset' )
472 ? wp_get_attachment_image_srcset( $instance['attachment_id'], $size )
473 : false;
474 if ( $image_srcset ) {
475 $instance['srcset'] = $image_srcset;
476
477 $image_sizes = function_exists( 'wp_get_attachment_image_sizes' )
478 ? wp_get_attachment_image_sizes( $instance['attachment_id'], $size )
479 : false;
480 if ( $image_sizes ) {
481 $instance['sizes'] = $image_sizes;
482 }
483 }
484 }
485
486 $instance['width'] = abs( $instance['width'] );
487 $instance['height'] = abs( $instance['height'] );
488
489 $attr = array();
490
491 if ( ! empty( $instance['alt'] ) ) {
492 $attr['alt'] = $instance['alt'];
493 } elseif ( ! empty( $instance['title'] ) ) {
494 $attr['alt'] = $instance['title'];
495 }
496
497 if ( is_array( $size ) ) {
498 $attr['class'] = 'attachment-' . join( 'x', $size );
499 } else {
500 $attr['class'] = 'attachment-' . $size;
501 }
502
503 $attr['style'] = '';
504 if ( ! empty( $instance['maxwidth'] ) ) {
505 $attr['style'] .= "max-width: {$instance['maxwidth']};";
506 }
507
508 if ( ! empty( $instance['maxheight'] ) ) {
509 $attr['style'] .= "max-height: {$instance['maxheight']};";
510 }
511
512 if ( ! empty( $instance['align'] ) && $instance['align'] != 'none' ) {
513 $attr['class'] .= " align{$instance['align']}";
514 }
515
516 if ( ! empty( $instance['srcset'] ) ) {
517 $attr['srcset'] = $instance['srcset'];
518 }
519
520 if ( ! empty( $instance['sizes'] ) ) {
521 $attr['sizes'] = $instance['sizes'];
522 }
523
524 /**
525 * Allow filtering of the image attributes used on the front-end.
526 *
527 * @param array $attr Image attributes to be filtered.
528 * @param array $instance The current widget instance.
529 */
530 $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
531
532 // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
533 if ( ! empty( $instance['imageurl'] ) ) {
534 // If all we have is an image src url we can still render an image.
535 $attr['src'] = esc_url( $instance['imageurl'] );
536 $attr = array_map( 'esc_attr', $attr );
537 $hwstring = image_hwstring( $instance['width'], $instance['height'] );
538
539 $output .= rtrim( "<img $hwstring" );
540
541 foreach ( $attr as $name => $value ) {
542 $output .= sprintf( ' %s="%s"', $name, $value );
543 }
544
545 $output .= ' />';
546
547 } elseif ( abs( $instance['attachment_id'] ) > 0 ) {
548 $output .= wp_get_attachment_image( $instance['attachment_id'], $size, false, $attr );
549 }
550
551 if ( $include_link && ! empty( $instance['link'] ) ) {
552 $output .= '</a>';
553 }
554
555 return $output;
556 }
557
558 /**
559 * Get all possible image sizes to choose from
560 *
561 * @return array
562 */
563 private function possible_image_sizes() {
564 $registered = get_intermediate_image_sizes();
565 // label other sizes with their image size "ID"
566 $registered = array_combine( $registered, $registered );
567
568 $possible_sizes = array_merge( $registered, array(
569 'full' => __( 'Full Size', 'image-widget' ),
570 'thumbnail' => __( 'Thumbnail', 'image-widget' ),
571 'medium' => __( 'Medium', 'image-widget' ),
572 'large' => __( 'Large', 'image-widget' ),
573 self::CUSTOM_IMAGE_SIZE_SLUG => __( 'Custom', 'image-widget' ),
574 ) );
575
576 /**
577 * Allow filtering the image sizes available for use in the Image Widget dropdown
578 *
579 * @param array $possible_sizes The array of available sizes.
580 */
581 return (array) apply_filters( 'image_size_names_choose', $possible_sizes );
582 }
583
584 /**
585 * Assesses the image size in case it has not been set or in case there is a mismatch.
586 *
587 * @param $instance
588 * @return array|string
589 */
590 private function get_image_size( $instance ) {
591 if ( ! empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
592 $size = $instance['size'];
593 } elseif ( isset( $instance['width'] ) && is_numeric( $instance['width'] ) && isset( $instance['height'] ) && is_numeric( $instance['height'] ) ) {
594 //$size = array(abs($instance['width']),abs($instance['height']));
595 $size = array( $instance['width'], $instance['height'] );
596 } else {
597 $size = 'full';
598 }
599
600 return $size;
601 }
602
603 /**
604 * Establish the aspect ratio of the image.
605 *
606 * @param $instance
607 * @return float|number
608 */
609 private function get_image_aspect_ratio( $instance ) {
610 if ( ! empty( $instance['aspect_ratio'] ) ) {
611 return abs( $instance['aspect_ratio'] );
612 } else {
613 $attachment_id = ( ! empty( $instance['attachment_id'] ) ) ? $instance['attachment_id'] : $instance['image'];
614 if ( ! empty( $attachment_id ) ) {
615 $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
616 if ( $image_details ) {
617 return ( $image_details[1] / $image_details[2] );
618 }
619 }
620 }
621 }
622
623 /**
624 * Loads theme files in appropriate hierarchy: 1) child theme,
625 * 2) parent template, 3) plugin resources. will look in the image-widget/
626 * directory in a theme and the views/ directory in the plugin
627 *
628 * @param string $template template file to search for
629 * @return template path
630 */
631
632 public function getTemplateHierarchy( $template ) {
633 // whether or not .php was added
634 $template_slug = rtrim( $template, '.php' );
635 $template = $template_slug . '.php';
636
637 if ( $theme_file = locate_template( array( 'image-widget/' . $template ) ) ) {
638 $file = $theme_file;
639 } else {
640 $file = 'views/' . $template;
641 }
642
643 /**
644 * Allow filtering of currently-retrieved Image Widget template file's path.
645 *
646 * @param string $file The retrieved template file's path.
647 */
648 return apply_filters( 'sp_template_image-widget_' . $template, $file );
649 }
650 }
651