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