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