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