PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.3
Image Widget v4.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
446 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.
6 Author: Modern Tribe, Inc.
7 Version: 4.3
8 Author URI: http://m.tri.be/26
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.3';
30
31 const CUSTOM_IMAGE_SIZE_SLUG = 'tribe_image_widget_custom';
32
33 /**
34 * Tribe Image Widget constructor
35 *
36 * @author Modern Tribe, Inc.
37 */
38 public function __construct() {
39 load_plugin_textdomain( 'image-widget', false, trailingslashit( basename( dirname( __FILE__ ) ) ) . 'lang/' );
40 $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image-widget' ) );
41 $control_ops = array( 'id_base' => 'widget_sp_image' );
42 parent::__construct( 'widget_sp_image', __( 'Image Widget', 'image-widget' ), $widget_ops, $control_ops );
43
44 if ( $this->use_old_uploader() ) {
45 require_once( 'lib/ImageWidgetDeprecated.php' );
46 new ImageWidgetDeprecated( $this );
47 } else {
48 add_action( 'sidebar_admin_setup', array( $this, 'admin_setup' ) );
49 }
50 add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
51
52 add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
53
54 if ( ! defined( 'I_HAVE_SUPPORTED_THE_IMAGE_WIDGET' ) )
55 add_action( 'admin_notices', array( $this, 'post_upgrade_nag' ) );
56
57 add_action( 'network_admin_notices', array( $this, 'post_upgrade_nag' ) );
58 }
59
60 /**
61 * Test to see if this version of WordPress supports the new image manager.
62 * @return bool true if the current version of WordPress does NOT support the current image management tech.
63 */
64 private function use_old_uploader() {
65 if ( defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) return true;
66 return ! function_exists( 'wp_enqueue_media' );
67 }
68
69 /**
70 * Enqueue all the javascript.
71 */
72 public function admin_setup() {
73 wp_enqueue_media();
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 * Widget frontend output
84 *
85 * @param array $args
86 * @param array $instance
87 * @author Modern Tribe, Inc.
88 */
89 public function widget( $args, $instance ) {
90 extract( $args );
91 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
92 if ( ! empty( $instance['imageurl'] ) || ! empty( $instance['attachment_id'] ) ) {
93
94 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );
95 $instance['description'] = apply_filters( 'widget_text', $instance['description'], $args, $instance );
96 $instance['link'] = apply_filters( 'image_widget_image_link', esc_url( $instance['link'] ), $args, $instance );
97 $instance['linkid'] = apply_filters( 'image_widget_image_link_id', esc_attr( $instance['linkid'] ), $args, $instance );
98 $instance['linktarget'] = apply_filters( 'image_widget_image_link_target', esc_attr( $instance['linktarget'] ), $args, $instance );
99 $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance );
100 $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance );
101 $instance['maxwidth'] = apply_filters( 'image_widget_image_maxwidth', esc_attr( $instance['maxwidth'] ), $args, $instance );
102 $instance['maxheight'] = apply_filters( 'image_widget_image_maxheight', esc_attr( $instance['maxheight'] ), $args, $instance );
103 $instance['align'] = apply_filters( 'image_widget_image_align', esc_attr( $instance['align'] ), $args, $instance );
104 $instance['alt'] = apply_filters( 'image_widget_image_alt', esc_attr( $instance['alt'] ), $args, $instance );
105 $instance['rel'] = apply_filters( 'image_widget_image_rel', esc_attr( $instance['rel'] ), $args, $instance );
106
107 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
108 $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image'];
109 $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance );
110 $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance );
111 }
112 $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance );
113
114 // No longer using extracted vars. This is here for backwards compatibility.
115 extract( $instance );
116
117 include( $this->getTemplateHierarchy( 'widget' ) );
118 }
119 }
120
121 /**
122 * Update widget options
123 *
124 * @param object $new_instance Widget Instance
125 * @param object $old_instance Widget Instance
126 * @return object
127 * @author Modern Tribe, Inc.
128 */
129 public function update( $new_instance, $old_instance ) {
130 $instance = $old_instance;
131 $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
132 $instance['title'] = strip_tags( $new_instance['title'] );
133 if ( current_user_can( 'unfiltered_html' ) ) {
134 $instance['description'] = $new_instance['description'];
135 } else {
136 $instance['description'] = wp_filter_post_kses( $new_instance['description'] );
137 }
138 $instance['link'] = $new_instance['link'];
139 $instance['linkid'] = $new_instance['linkid'];
140 $instance['linktarget'] = $new_instance['linktarget'];
141 $instance['width'] = abs( $new_instance['width'] );
142 $instance['height'] = abs( $new_instance['height'] );
143 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
144 $instance['size'] = $new_instance['size'];
145 }
146 $instance['align'] = $new_instance['align'];
147 $instance['alt'] = $new_instance['alt'];
148 $instance['rel'] = $new_instance['rel'];
149
150 // Reverse compatibility with $image, now called $attachement_id
151 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) {
152 $instance['attachment_id'] = abs( $new_instance['attachment_id'] );
153 } elseif ( $new_instance['image'] > 0 ) {
154 $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] );
155 if ( class_exists( 'ImageWidgetDeprecated' ) ) {
156 $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now
157 }
158 }
159 $instance['imageurl'] = $new_instance['imageurl']; // deprecated
160
161 $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance );
162
163 return $instance;
164 }
165
166 /**
167 * Form UI
168 *
169 * @param object $instance Widget Instance
170 * @author Modern Tribe, Inc.
171 */
172 public function form( $instance ) {
173 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
174 if ( $this->use_old_uploader() ) {
175 include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) );
176 } else {
177 include( $this->getTemplateHierarchy( 'widget-admin' ) );
178 }
179 }
180
181 /**
182 * Admin header css
183 *
184 * @author Modern Tribe, Inc.
185 */
186 public function admin_head() {
187 ?>
188 <style type="text/css">
189 .uploader input.button {
190 width: 100%;
191 height: 34px;
192 line-height: 33px;
193 margin-top: 15px;
194 }
195 .tribe_preview .aligncenter {
196 display: block;
197 margin-left: auto !important;
198 margin-right: auto !important;
199 }
200 .tribe_preview {
201 overflow: hidden;
202 max-height: 300px;
203 }
204 .tribe_preview img {
205 margin: 10px 0;
206 height: auto;
207 }
208 </style>
209 <?php
210 }
211
212 /**
213 * Render an array of default values.
214 *
215 * @return array default values
216 */
217 private static function get_defaults() {
218
219 $defaults = array(
220 'title' => '',
221 'description' => '',
222 'link' => '',
223 'linkid' => '',
224 'linktarget' => '',
225 'width' => 0,
226 'height' => 0,
227 'maxwidth' => '100%',
228 'maxheight' => '',
229 'image' => 0, // reverse compatible - now attachement_id
230 'imageurl' => '', // reverse compatible.
231 'align' => 'none',
232 'alt' => '',
233 'rel' => '',
234 );
235
236 if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
237 $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
238 $defaults['attachment_id'] = 0;
239 }
240
241 return $defaults;
242 }
243
244 /**
245 * Render the image html output.
246 *
247 * @param array $instance
248 * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
249 * @return string image html
250 */
251 private function get_image_html( $instance, $include_link = true ) {
252
253 // Backwards compatible image display.
254 if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
255 $instance['attachment_id'] = $instance['image'];
256 }
257
258 $output = '';
259
260 if ( $include_link && ! empty( $instance['link'] ) ) {
261 $attr = array(
262 'href' => $instance['link'],
263 'id' => $instance['linkid'],
264 'target' => $instance['linktarget'],
265 'class' => $this->widget_options['classname'] . '-image-link',
266 'title' => ( ! empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
267 'rel' => $instance['rel'],
268 );
269 $attr = apply_filters( 'image_widget_link_attributes', $attr, $instance );
270 $attr = array_map( 'esc_attr', $attr );
271 $output = '<a';
272 foreach ( $attr as $name => $value ) {
273 $output .= sprintf( ' %s="%s"', $name, $value );
274 }
275 $output .= '>';
276 }
277
278 $size = $this->get_image_size( $instance );
279 if ( is_array( $size ) ) {
280 $instance['width'] = $size[0];
281 $instance['height'] = $size[1];
282 } elseif ( ! empty( $instance['attachment_id'] ) ) {
283 //$instance['width'] = $instance['height'] = 0;
284 $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
285 if ( $image_details ) {
286 $instance['imageurl'] = $image_details[0];
287 $instance['width'] = $image_details[1];
288 $instance['height'] = $image_details[2];
289 }
290 }
291 $instance['width'] = abs( $instance['width'] );
292 $instance['height'] = abs( $instance['height'] );
293
294 $attr = array();
295 $attr['alt'] = ( ! empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'];
296 if ( is_array( $size ) ) {
297 $attr['class'] = 'attachment-' . join( 'x', $size );
298 } else {
299 $attr['class'] = 'attachment-' . $size;
300 }
301 $attr['style'] = '';
302 if ( ! empty( $instance['maxwidth'] ) ) {
303 $attr['style'] .= "max-width: {$instance['maxwidth']};";
304 }
305 if ( ! empty( $instance['maxheight'] ) ) {
306 $attr['style'] .= "max-height: {$instance['maxheight']};";
307 }
308 if ( ! empty( $instance['align'] ) && $instance['align'] != 'none' ) {
309 $attr['class'] .= " align{$instance['align']}";
310 }
311 $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
312
313 // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
314 if ( ! empty( $instance['imageurl'] ) ) {
315 // If all we have is an image src url we can still render an image.
316 $attr['src'] = $instance['imageurl'];
317 $attr = array_map( 'esc_attr', $attr );
318 $hwstring = image_hwstring( $instance['width'], $instance['height'] );
319 $output .= rtrim( "<img $hwstring" );
320 foreach ( $attr as $name => $value ) {
321 $output .= sprintf( ' %s="%s"', $name, $value );
322 }
323 $output .= ' />';
324 } elseif ( abs( $instance['attachment_id'] ) > 0 ) {
325 $output .= wp_get_attachment_image( $instance['attachment_id'], $size, false, $attr );
326 }
327
328 if ( $include_link && ! empty( $instance['link'] ) ) {
329 $output .= '</a>';
330 }
331
332 return $output;
333 }
334
335 /**
336 * Get all possible image sizes to choose from
337 *
338 * @return array
339 */
340 private function possible_image_sizes() {
341 $registered = get_intermediate_image_sizes();
342 // label other sizes with their image size "ID"
343 $registered = array_combine( $registered, $registered );
344
345 $possible_sizes = array_merge( $registered, array(
346 'full' => __( 'Full Size', 'image-widget' ),
347 'thumbnail' => __( 'Thumbnail', 'image-widget' ),
348 'medium' => __( 'Medium', 'image-widget' ),
349 'large' => __( 'Large', 'image-widget' ),
350 self::CUSTOM_IMAGE_SIZE_SLUG => __( 'Custom', 'image-widget' ),
351 ) );
352
353 return (array) apply_filters( 'image_size_names_choose', $possible_sizes );
354 }
355
356 /**
357 * Assesses the image size in case it has not been set or in case there is a mismatch.
358 *
359 * @param $instance
360 * @return array|string
361 */
362 private function get_image_size( $instance ) {
363 if ( ! empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
364 $size = $instance['size'];
365 } elseif ( isset( $instance['width'] ) && is_numeric( $instance['width'] ) && isset( $instance['height'] ) && is_numeric( $instance['height'] ) ) {
366 //$size = array(abs($instance['width']),abs($instance['height']));
367 $size = array( $instance['width'], $instance['height'] );
368 } else {
369 $size = 'full';
370 }
371 return $size;
372 }
373
374 /**
375 * Establish the aspect ratio of the image.
376 *
377 * @param $instance
378 * @return float|number
379 */
380 private function get_image_aspect_ratio( $instance ) {
381 if ( ! empty( $instance['aspect_ratio'] ) ) {
382 return abs( $instance['aspect_ratio'] );
383 } else {
384 $attachment_id = ( ! empty( $instance['attachment_id'] ) ) ? $instance['attachment_id'] : $instance['image'];
385 if ( ! empty( $attachment_id ) ) {
386 $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
387 if ( $image_details ) {
388 return ( $image_details[1] / $image_details[2] );
389 }
390 }
391 }
392 }
393
394 /**
395 * Loads theme files in appropriate hierarchy: 1) child theme,
396 * 2) parent template, 3) plugin resources. will look in the image-widget/
397 * directory in a theme and the views/ directory in the plugin
398 *
399 * @param string $template template file to search for
400 * @return template path
401 * @author Modern Tribe, Inc. (Matt Wiebe)
402 **/
403
404 public function getTemplateHierarchy( $template ) {
405 // whether or not .php was added
406 $template_slug = rtrim( $template, '.php' );
407 $template = $template_slug . '.php';
408
409 if ( $theme_file = locate_template( array( 'image-widget/' . $template ) ) ) {
410 $file = $theme_file;
411 } else {
412 $file = 'views/' . $template;
413 }
414 return apply_filters( 'sp_template_image-widget_' . $template, $file );
415 }
416
417 /**
418 * Display a thank you nag when the plugin has been upgraded.
419 */
420 public function post_upgrade_nag() {
421 if ( ! current_user_can( 'install_plugins' ) ) return;
422
423 $version_key = '_image_widget_version';
424 if ( get_site_option( $version_key ) == self::VERSION ) return;
425
426 $msg = sprintf( __( 'Thanks for using the Image Widget! If you like this plugin, please consider <a href="%s" target="_blank">rating it</a> and maybe even check out our premium plugins including our <a href="%s" target="_blank">Events Calendar Pro</a>!', 'image-widget' ), 'http://wordpress.org/plugins/image-widget/?source=image-widget&pos=nag', 'https://theeventscalendar.com/product/wordpress-events-calendar-pro/?source=image-widget&pos=nag' );
427 echo "<div class='update-nag'>$msg</div>";
428
429 update_site_option( $version_key, self::VERSION );
430 }
431
432 /**
433 * Display an informational section in the plugin admin ui.
434 * @param $meta
435 * @param $file
436 *
437 * @return array
438 */
439 public function plugin_row_meta( $meta, $file ) {
440 if ( $file == plugin_basename( dirname( __FILE__ ) . '/image-widget.php' ) ) {
441 $meta[] = '<span class="tribe-test">' . sprintf( __( 'Check out our other <a href="%s" target="_blank">plugins</a> including our <a href="%s" target="_blank">Events Calendar Pro</a>!', 'image-widget' ), 'https://theeventscalendar.com/products/?source=image-widget&pos=pluginlist', 'https://theeventscalendar.com/product/wordpress-events-calendar-pro/?source=image-widget&pos=pluginlist' ) . '</span>';
442 }
443 return $meta;
444 }
445 }
446