PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.4.2
Image Widget v4.4.2
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 9 years ago lib 9 years ago resources 9 years ago views 9 years ago image-widget.php 9 years ago index.php 9 years ago readme.txt 9 years ago
image-widget.php
540 lines
1 <?php
2 /*
3 Plugin Name: Image Widget
4 Plugin URI: http://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. <strong><a href="http://m.tri.be/19my">Image Widget Plus</a> - Multiple images, slider and more.</strong>
6 Author: Modern Tribe, Inc.
7 Version: 4.4.2
8 Author URI: http://m.tri.be/iwpdoc
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 /**
25 * Tribe_Image_Widget class
26 **/
27 class Tribe_Image_Widget extends WP_Widget {
28
29 const VERSION = '4.4.2';
30
31 const CUSTOM_IMAGE_SIZE_SLUG = 'tribe_image_widget_custom';
32
33 const VERSION_KEY = '_image_widget_version';
34
35 /**
36 * Tribe Image Widget constructor
37 *
38 * @author Modern Tribe, Inc.
39 */
40 public function __construct() {
41 load_plugin_textdomain( 'image-widget', false, trailingslashit( basename( dirname( __FILE__ ) ) ) . 'lang/' );
42 $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image-widget' ) );
43 $control_ops = array( 'id_base' => 'widget_sp_image' );
44 parent::__construct( 'widget_sp_image', __( 'Image Widget', 'image-widget' ), $widget_ops, $control_ops );
45
46 if ( $this->use_old_uploader() ) {
47 require_once( 'lib/ImageWidgetDeprecated.php' );
48 new ImageWidgetDeprecated( $this );
49 } else {
50 add_action( 'admin_enqueue_scripts', array( $this, 'admin_setup' ) );
51 }
52
53 add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
54
55 if ( ! defined( 'I_HAVE_SUPPORTED_THE_IMAGE_WIDGET' ) )
56 add_action( 'admin_notices', array( $this, 'post_upgrade_nag' ) );
57
58 add_action( 'network_admin_notices', array( $this, 'post_upgrade_nag' ) );
59 add_action( 'wp_ajax_dismissed_image_widget_notice_handler', array( $this, 'ajax_notice_handler' ) );
60 }
61
62 /**
63 * Test to see if this version of WordPress supports the new image manager.
64 * @return bool true if the current version of WordPress does NOT support the current image management tech.
65 */
66 private function use_old_uploader() {
67 if ( defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) return true;
68 return ! function_exists( 'wp_enqueue_media' );
69 }
70
71 /**
72 * Enqueue all the javascript and CSS.
73 */
74 public function admin_setup() {
75
76 // Only load on widget admin page and in the "Customizer" view.
77 $screen = get_current_screen();
78 $should_load = 'customize' == $screen->base || 'widgets' == $screen->base;
79
80 if ( ! $should_load ) {
81 return;
82 }
83
84 wp_enqueue_media();
85
86 wp_enqueue_style( 'tribe-image-widget', plugins_url( 'resources/css/admin.css', __FILE__ ), array(), self::VERSION );
87
88 wp_enqueue_script( 'tribe-image-widget', plugins_url( 'resources/js/image-widget.js', __FILE__ ), array( 'jquery', 'media-upload', 'media-views' ), self::VERSION );
89
90 wp_localize_script( 'tribe-image-widget', 'TribeImageWidget', array(
91 'frame_title' => __( 'Select an Image', 'image-widget' ),
92 'button_title' => __( 'Insert Into Widget', 'image-widget' ),
93 ) );
94 }
95
96 /**
97 * Widget frontend output
98 *
99 * @param array $args
100 * @param array $instance
101 * @author Modern Tribe, Inc.
102 */
103 public function widget( $args, $instance ) {
104 extract( $args );
105 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
106 if ( ! empty( $instance['imageurl'] ) || ! empty( $instance['attachment_id'] ) ) {
107
108 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );
109 $instance['description'] = apply_filters( 'widget_text', $instance['description'], $args, $instance );
110 $instance['link'] = apply_filters( 'image_widget_image_link', esc_url( $instance['link'] ), $args, $instance );
111 $instance['linkid'] = apply_filters( 'image_widget_image_link_id', esc_attr( $instance['linkid'] ), $args, $instance );
112 $instance['linktarget'] = apply_filters( 'image_widget_image_link_target', esc_attr( $instance['linktarget'] ), $args, $instance );
113 $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance );
114 $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance );
115 $instance['maxwidth'] = apply_filters( 'image_widget_image_maxwidth', esc_attr( $instance['maxwidth'] ), $args, $instance );
116 $instance['maxheight'] = apply_filters( 'image_widget_image_maxheight', esc_attr( $instance['maxheight'] ), $args, $instance );
117 $instance['align'] = apply_filters( 'image_widget_image_align', esc_attr( $instance['align'] ), $args, $instance );
118 $instance['alt'] = apply_filters( 'image_widget_image_alt', esc_attr( $instance['alt'] ), $args, $instance );
119 $instance['rel'] = apply_filters( 'image_widget_image_rel', esc_attr( $instance['rel'] ), $args, $instance );
120
121 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
122 $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image'];
123 $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance );
124 $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance );
125 }
126 $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance );
127
128 // No longer using extracted vars. This is here for backwards compatibility.
129 extract( $instance );
130
131 include( $this->getTemplateHierarchy( 'widget' ) );
132 }
133 }
134
135 /**
136 * Update widget options
137 *
138 * @param object $new_instance Widget Instance
139 * @param object $old_instance Widget Instance
140 * @return object
141 * @author Modern Tribe, Inc.
142 */
143 public function update( $new_instance, $old_instance ) {
144 $instance = $old_instance;
145 $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
146 $instance['title'] = strip_tags( $new_instance['title'] );
147 if ( current_user_can( 'unfiltered_html' ) ) {
148 $instance['description'] = $new_instance['description'];
149 } else {
150 $instance['description'] = wp_filter_post_kses( $new_instance['description'] );
151 }
152 $instance['link'] = $new_instance['link'];
153 $instance['linkid'] = $new_instance['linkid'];
154 $instance['linktarget'] = $new_instance['linktarget'];
155 $instance['width'] = abs( $new_instance['width'] );
156 $instance['height'] = abs( $new_instance['height'] );
157 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
158 $instance['size'] = $new_instance['size'];
159 }
160 $instance['align'] = $new_instance['align'];
161 $instance['alt'] = $new_instance['alt'];
162 $instance['rel'] = $new_instance['rel'];
163
164 // Reverse compatibility with $image, now called $attachement_id
165 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) {
166 $instance['attachment_id'] = abs( $new_instance['attachment_id'] );
167 } elseif ( $new_instance['image'] > 0 ) {
168 $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] );
169 if ( class_exists( 'ImageWidgetDeprecated' ) ) {
170 $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now
171 }
172 }
173 $instance['imageurl'] = $new_instance['imageurl']; // deprecated
174
175 $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance );
176
177 return $instance;
178 }
179
180 /**
181 * Form UI
182 *
183 * @param object $instance Widget Instance
184 * @author Modern Tribe, Inc.
185 */
186 public function form( $instance ) {
187 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
188 if ( $this->use_old_uploader() ) {
189 include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) );
190 } else {
191 include( $this->getTemplateHierarchy( 'widget-admin' ) );
192 }
193 }
194
195 /**
196 * Render an array of default values.
197 *
198 * @return array default values
199 */
200 private static function get_defaults() {
201
202 $defaults = array(
203 'title' => '',
204 'description' => '',
205 'link' => '',
206 'linkid' => '',
207 'linktarget' => '',
208 'width' => 0,
209 'height' => 0,
210 'maxwidth' => '100%',
211 'maxheight' => '',
212 'image' => 0, // reverse compatible - now attachement_id
213 'imageurl' => '', // reverse compatible.
214 'align' => 'none',
215 'alt' => '',
216 'rel' => '',
217 );
218
219 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
220 $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
221 $defaults['attachment_id'] = 0;
222 }
223
224 return $defaults;
225 }
226
227 /**
228 * Render the image html output.
229 *
230 * @param array $instance
231 * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
232 * @return string image html
233 */
234 private function get_image_html( $instance, $include_link = true ) {
235
236 // Backwards compatible image display.
237 if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
238 $instance['attachment_id'] = $instance['image'];
239 }
240
241 $output = '';
242
243 if ( $include_link && ! empty( $instance['link'] ) ) {
244 $attr = array(
245 'href' => $instance['link'],
246 'id' => $instance['linkid'],
247 'target' => $instance['linktarget'],
248 'class' => $this->widget_options['classname'] . '-image-link',
249 'title' => ( ! empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
250 'rel' => $instance['rel'],
251 );
252 $attr = apply_filters( 'image_widget_link_attributes', $attr, $instance );
253 $attr = array_map( 'esc_attr', $attr );
254 $output = '<a';
255 foreach ( $attr as $name => $value ) {
256 if ( ! empty( $value ) ) {
257 $output .= sprintf( ' %s="%s"', $name, $value );
258 }
259 }
260 $output .= '>';
261 }
262
263 $size = $this->get_image_size( $instance );
264 if ( is_array( $size ) ) {
265 $instance['width'] = $size[0];
266 $instance['height'] = $size[1];
267 } elseif ( ! empty( $instance['attachment_id'] ) ) {
268 //$instance['width'] = $instance['height'] = 0;
269 $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
270 if ( $image_details ) {
271 $instance['imageurl'] = $image_details[0];
272 $instance['width'] = $image_details[1];
273 $instance['height'] = $image_details[2];
274 }
275
276 $image_srcset = function_exists( 'wp_get_attachment_image_srcset' )
277 ? wp_get_attachment_image_srcset( $instance['attachment_id'], $size )
278 : false;
279 if ( $image_srcset ) {
280 $instance['srcset'] = $image_srcset;
281
282 $image_sizes = function_exists( 'wp_get_attachment_image_sizes' )
283 ? wp_get_attachment_image_sizes( $instance['attachment_id'], $size )
284 : false;
285 if ( $image_sizes ) {
286 $instance['sizes'] = $image_sizes;
287 }
288 }
289 }
290 $instance['width'] = abs( $instance['width'] );
291 $instance['height'] = abs( $instance['height'] );
292
293 $attr = array();
294
295 if ( ! empty( $instance['alt'] ) ) {
296 $attr['alt'] = $instance['alt'];
297 } elseif ( ! empty( $instance['title'] ) ) {
298 $attr['alt'] = $instance['title'];
299 }
300
301 if ( is_array( $size ) ) {
302 $attr['class'] = 'attachment-' . join( 'x', $size );
303 } else {
304 $attr['class'] = 'attachment-' . $size;
305 }
306
307 $attr['style'] = '';
308 if ( ! empty( $instance['maxwidth'] ) ) {
309 $attr['style'] .= "max-width: {$instance['maxwidth']};";
310 }
311
312 if ( ! empty( $instance['maxheight'] ) ) {
313 $attr['style'] .= "max-height: {$instance['maxheight']};";
314 }
315
316 if ( ! empty( $instance['align'] ) && $instance['align'] != 'none' ) {
317 $attr['class'] .= " align{$instance['align']}";
318 }
319
320 if ( ! empty( $instance['srcset'] ) ) {
321 $attr['srcset'] = $instance['srcset'];
322 }
323
324 if ( ! empty( $instance['sizes'] ) ) {
325 $attr['sizes'] = $instance['sizes'];
326 }
327 $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
328
329 // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
330 if ( ! empty( $instance['imageurl'] ) ) {
331 // If all we have is an image src url we can still render an image.
332 $attr['src'] = $instance['imageurl'];
333 $attr = array_map( 'esc_attr', $attr );
334 $hwstring = image_hwstring( $instance['width'], $instance['height'] );
335 $output .= rtrim( "<img $hwstring" );
336 foreach ( $attr as $name => $value ) {
337 $output .= sprintf( ' %s="%s"', $name, $value );
338 }
339 $output .= ' />';
340 } elseif ( abs( $instance['attachment_id'] ) > 0 ) {
341 $output .= wp_get_attachment_image( $instance['attachment_id'], $size, false, $attr );
342 }
343
344 if ( $include_link && ! empty( $instance['link'] ) ) {
345 $output .= '</a>';
346 }
347
348 return $output;
349 }
350
351 /**
352 * Get all possible image sizes to choose from
353 *
354 * @return array
355 */
356 private function possible_image_sizes() {
357 $registered = get_intermediate_image_sizes();
358 // label other sizes with their image size "ID"
359 $registered = array_combine( $registered, $registered );
360
361 $possible_sizes = array_merge( $registered, array(
362 'full' => __( 'Full Size', 'image-widget' ),
363 'thumbnail' => __( 'Thumbnail', 'image-widget' ),
364 'medium' => __( 'Medium', 'image-widget' ),
365 'large' => __( 'Large', 'image-widget' ),
366 self::CUSTOM_IMAGE_SIZE_SLUG => __( 'Custom', 'image-widget' ),
367 ) );
368
369 return (array) apply_filters( 'image_size_names_choose', $possible_sizes );
370 }
371
372 /**
373 * Assesses the image size in case it has not been set or in case there is a mismatch.
374 *
375 * @param $instance
376 * @return array|string
377 */
378 private function get_image_size( $instance ) {
379 if ( ! empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
380 $size = $instance['size'];
381 } elseif ( isset( $instance['width'] ) && is_numeric( $instance['width'] ) && isset( $instance['height'] ) && is_numeric( $instance['height'] ) ) {
382 //$size = array(abs($instance['width']),abs($instance['height']));
383 $size = array( $instance['width'], $instance['height'] );
384 } else {
385 $size = 'full';
386 }
387 return $size;
388 }
389
390 /**
391 * Establish the aspect ratio of the image.
392 *
393 * @param $instance
394 * @return float|number
395 */
396 private function get_image_aspect_ratio( $instance ) {
397 if ( ! empty( $instance['aspect_ratio'] ) ) {
398 return abs( $instance['aspect_ratio'] );
399 } else {
400 $attachment_id = ( ! empty( $instance['attachment_id'] ) ) ? $instance['attachment_id'] : $instance['image'];
401 if ( ! empty( $attachment_id ) ) {
402 $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
403 if ( $image_details ) {
404 return ( $image_details[1] / $image_details[2] );
405 }
406 }
407 }
408 }
409
410 /**
411 * Loads theme files in appropriate hierarchy: 1) child theme,
412 * 2) parent template, 3) plugin resources. will look in the image-widget/
413 * directory in a theme and the views/ directory in the plugin
414 *
415 * @param string $template template file to search for
416 * @return template path
417 * @author Modern Tribe, Inc. (Matt Wiebe)
418 **/
419
420 public function getTemplateHierarchy( $template ) {
421 // whether or not .php was added
422 $template_slug = rtrim( $template, '.php' );
423 $template = $template_slug . '.php';
424
425 if ( $theme_file = locate_template( array( 'image-widget/' . $template ) ) ) {
426 $file = $theme_file;
427 } else {
428 $file = 'views/' . $template;
429 }
430 return apply_filters( 'sp_template_image-widget_' . $template, $file );
431 }
432
433 /**
434 * Display a thank you nag when the plugin has been upgraded.
435 */
436 public function post_upgrade_nag() {
437 if ( ! current_user_can( 'install_plugins' ) ) return;
438
439 global $pagenow;
440 $msg = false;
441 switch ( $pagenow ) {
442 case 'plugins.php' :
443 $msg = $this->upgrade_nag_plugins_admin_msg();
444 break;
445 case 'widgets.php' :
446 $msg = $this->upgrade_nag_widget_admin_msg();
447 break;
448 }
449
450 if ( ! $msg ) return;
451
452 echo $msg;
453 ?><script>
454 jQuery(document).ready(function($){
455 // Dismiss our admin notice
456 $( document ).on( 'click', '.image-widget-notice .notice-dismiss', function () {
457 var key = $( this ).closest( '.image-widget-notice' ).data( 'key' );
458 $.ajax( ajaxurl,
459 {
460 type: 'POST',
461 data: {
462 action: 'dismissed_image_widget_notice_handler',
463 key: key
464 }
465 } );
466 } );
467 } );
468 </script><?php
469 }
470
471 /**
472 * AJAX handler to store the state of dismissible notices.
473 */
474 public function ajax_notice_handler() {
475 if ( empty( $_POST['key'] ) ) return;
476 $key = $this->generate_key( sanitize_text_field( $_POST['key'] ) );
477 update_site_option( $key, self::VERSION );
478 }
479
480 /**
481 * Generate version key for admin notice options
482 *
483 * @param string $key
484 * @return string option key
485 */
486 private function generate_key( $key ) {
487 $option_key = join( '_', array(
488 self::VERSION_KEY,
489 $key,
490 ) );
491 return $option_key;
492 }
493
494 /**
495 * Upgrade nag: Plugins Admin
496 *
497 * @return string alert message.
498 */
499 private function upgrade_nag_plugins_admin_msg() {
500 $key = 'plugin';
501 $option_key = $this->generate_key( $key );
502 if ( get_site_option( $option_key ) == self::VERSION ) return;
503 $msg = sprintf(
504 __( '<p class="dashicons-before dashicons-format-gallery"><strong>Image Widget Plus</strong> - Add lightbox, slideshow, and random image widgets. <strong><a href="%s" target="_blank">Find out how!</a></strong></p>', 'image-widget' ),
505 'http://m.tri.be/19my',
506 'http://m.tri.be/19my'
507 );
508 return "<div class='notice notice-info is-dismissible image-widget-notice' data-key='$key'>$msg</div>";
509 }
510
511 /**
512 * Upgrade nag: Widget Admin
513 *
514 * @return string alert message.
515 */
516 private function upgrade_nag_widget_admin_msg() {
517 $key = 'widget';
518 $option_key = $this->generate_key( $key );
519 if ( get_site_option( $option_key ) == self::VERSION ) return;
520 $msg = sprintf(
521 __( '<p class="dashicons-before dashicons-star-filled"><strong>Image Widget Plus</strong> - Add lightbox, slideshow, and random image widgets. <strong><a href="%s" target="_blank">Find out how!</a></strong></p>', 'image-widget' ),
522 'http://m.tri.be/19mx'
523 );
524 return "<div class='notice notice-info is-dismissible image-widget-notice' data-key='$key'>$msg</div>";
525 }
526
527 /**
528 * Display an informational section in the plugin admin ui.
529 * @param $meta
530 * @param $file
531 *
532 * @return array
533 */
534 public function plugin_row_meta( $meta, $file ) {
535 if ( $file == plugin_basename( dirname( __FILE__ ) . '/image-widget.php' ) ) {
536 $meta[] = '<strong><a href="http://m.tri.be/19ma" target="_blank">' . esc_html__( 'Image Widget Plus', 'image-widget' ) . '</a></strong>';
537 }
538 return $meta;
539 }
540 }