PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.1.2
Image Widget v4.1.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 12 years ago lib 13 years ago resources 13 years ago views 10 years ago image-widget.php 10 years ago readme.txt 10 years ago screenshot-1.png 13 years ago screenshot-2.png 13 years ago screenshot-3.png 13 years ago
image-widget.php
420 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.1.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.1.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
104 if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
105 $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image'];
106 $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance );
107 $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance );
108 }
109 $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance );
110
111 // No longer using extracted vars. This is here for backwards compatibility.
112 extract( $instance );
113
114 include( $this->getTemplateHierarchy( 'widget' ) );
115 }
116 }
117
118 /**
119 * Update widget options
120 *
121 * @param object $new_instance Widget Instance
122 * @param object $old_instance Widget Instance
123 * @return object
124 * @author Modern Tribe, Inc.
125 */
126 public function update( $new_instance, $old_instance ) {
127 $instance = $old_instance;
128 $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
129 $instance['title'] = strip_tags($new_instance['title']);
130 if ( current_user_can('unfiltered_html') ) {
131 $instance['description'] = $new_instance['description'];
132 } else {
133 $instance['description'] = wp_filter_post_kses($new_instance['description']);
134 }
135 $instance['link'] = $new_instance['link'];
136 $instance['linkid'] = $new_instance['linkid'];
137 $instance['linktarget'] = $new_instance['linktarget'];
138 $instance['width'] = abs( $new_instance['width'] );
139 $instance['height'] =abs( $new_instance['height'] );
140 if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
141 $instance['size'] = $new_instance['size'];
142 }
143 $instance['align'] = $new_instance['align'];
144 $instance['alt'] = $new_instance['alt'];
145
146 // Reverse compatibility with $image, now called $attachement_id
147 if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) {
148 $instance['attachment_id'] = abs( $new_instance['attachment_id'] );
149 } elseif ( $new_instance['image'] > 0 ) {
150 $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] );
151 if ( class_exists('ImageWidgetDeprecated') ) {
152 $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now
153 }
154 }
155 $instance['imageurl'] = $new_instance['imageurl']; // deprecated
156
157 $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance );
158
159 return $instance;
160 }
161
162 /**
163 * Form UI
164 *
165 * @param object $instance Widget Instance
166 * @author Modern Tribe, Inc.
167 */
168 public function form( $instance ) {
169 $instance = wp_parse_args( (array) $instance, self::get_defaults() );
170 if ( $this->use_old_uploader() ) {
171 include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) );
172 } else {
173 include( $this->getTemplateHierarchy( 'widget-admin' ) );
174 }
175 }
176
177 /**
178 * Admin header css
179 *
180 * @author Modern Tribe, Inc.
181 */
182 public function admin_head() {
183 ?>
184 <style type="text/css">
185 .uploader input.button {
186 width: 100%;
187 height: 34px;
188 line-height: 33px;
189 margin-top: 15px;
190 }
191 .tribe_preview .aligncenter {
192 display: block;
193 margin-left: auto !important;
194 margin-right: auto !important;
195 }
196 .tribe_preview {
197 overflow: hidden;
198 max-height: 300px;
199 }
200 .tribe_preview img {
201 margin: 10px 0;
202 height: auto;
203 }
204 </style>
205 <?php
206 }
207
208 /**
209 * Render an array of default values.
210 *
211 * @return array default values
212 */
213 private static function get_defaults() {
214
215 $defaults = array(
216 'title' => '',
217 'description' => '',
218 'link' => '',
219 'linkid' => '',
220 'linktarget' => '',
221 'width' => 0,
222 'height' => 0,
223 'maxwidth' => '100%',
224 'maxheight' => '',
225 'image' => 0, // reverse compatible - now attachement_id
226 'imageurl' => '', // reverse compatible.
227 'align' => 'none',
228 'alt' => '',
229 );
230
231 if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
232 $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
233 $defaults['attachment_id'] = 0;
234 }
235
236 return $defaults;
237 }
238
239 /**
240 * Render the image html output.
241 *
242 * @param array $instance
243 * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
244 * @return string image html
245 */
246 private function get_image_html( $instance, $include_link = true ) {
247
248 // Backwards compatible image display.
249 if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
250 $instance['attachment_id'] = $instance['image'];
251 }
252
253 $output = '';
254
255 if ( $include_link && !empty( $instance['link'] ) ) {
256 $attr = array(
257 'href' => $instance['link'],
258 'id' => $instance['linkid'],
259 'target' => $instance['linktarget'],
260 'class' => $this->widget_options['classname'].'-image-link',
261 'title' => ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
262 );
263 $attr = apply_filters('image_widget_link_attributes', $attr, $instance );
264 $attr = array_map( 'esc_attr', $attr );
265 $output = '<a';
266 foreach ( $attr as $name => $value ) {
267 $output .= sprintf( ' %s="%s"', $name, $value );
268 }
269 $output .= '>';
270 }
271
272 $size = $this->get_image_size( $instance );
273 if ( is_array( $size ) ) {
274 $instance['width'] = $size[0];
275 $instance['height'] = $size[1];
276 } elseif ( !empty( $instance['attachment_id'] ) ) {
277 //$instance['width'] = $instance['height'] = 0;
278 $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
279 if ($image_details) {
280 $instance['imageurl'] = $image_details[0];
281 $instance['width'] = $image_details[1];
282 $instance['height'] = $image_details[2];
283 }
284 }
285 $instance['width'] = abs( $instance['width'] );
286 $instance['height'] = abs( $instance['height'] );
287
288 $attr = array();
289 $attr['alt'] = ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'];
290 if (is_array($size)) {
291 $attr['class'] = 'attachment-'.join('x',$size);
292 } else {
293 $attr['class'] = 'attachment-'.$size;
294 }
295 $attr['style'] = '';
296 if (!empty($instance['maxwidth'])) {
297 $attr['style'] .= "max-width: {$instance['maxwidth']};";
298 }
299 if (!empty($instance['maxheight'])) {
300 $attr['style'] .= "max-height: {$instance['maxheight']};";
301 }
302 if (!empty($instance['align']) && $instance['align'] != 'none') {
303 $attr['class'] .= " align{$instance['align']}";
304 }
305 $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
306
307 // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
308 if ( !empty( $instance['imageurl'] ) ) {
309 // If all we have is an image src url we can still render an image.
310 $attr['src'] = $instance['imageurl'];
311 $attr = array_map( 'esc_attr', $attr );
312 $hwstring = image_hwstring( $instance['width'], $instance['height'] );
313 $output .= rtrim("<img $hwstring");
314 foreach ( $attr as $name => $value ) {
315 $output .= sprintf( ' %s="%s"', $name, $value );
316 }
317 $output .= ' />';
318 } elseif( abs( $instance['attachment_id'] ) > 0 ) {
319 $output .= wp_get_attachment_image($instance['attachment_id'], $size, false, $attr);
320 }
321
322 if ( $include_link && !empty( $instance['link'] ) ) {
323 $output .= '</a>';
324 }
325
326 return $output;
327 }
328
329 /**
330 * Assesses the image size in case it has not been set or in case there is a mismatch.
331 *
332 * @param $instance
333 * @return array|string
334 */
335 private function get_image_size( $instance ) {
336 if ( !empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
337 $size = $instance['size'];
338 } elseif ( isset( $instance['width'] ) && is_numeric($instance['width']) && isset( $instance['height'] ) && is_numeric($instance['height']) ) {
339 //$size = array(abs($instance['width']),abs($instance['height']));
340 $size = array($instance['width'],$instance['height']);
341 } else {
342 $size = 'full';
343 }
344 return $size;
345 }
346
347 /**
348 * Establish the aspect ratio of the image.
349 *
350 * @param $instance
351 * @return float|number
352 */
353 private function get_image_aspect_ratio( $instance ) {
354 if ( !empty( $instance['aspect_ratio'] ) ) {
355 return abs( $instance['aspect_ratio'] );
356 } else {
357 $attachment_id = ( !empty($instance['attachment_id']) ) ? $instance['attachment_id'] : $instance['image'];
358 if ( !empty($attachment_id) ) {
359 $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
360 if ($image_details) {
361 return ( $image_details[1]/$image_details[2] );
362 }
363 }
364 }
365 }
366
367 /**
368 * Loads theme files in appropriate hierarchy: 1) child theme,
369 * 2) parent template, 3) plugin resources. will look in the image-widget/
370 * directory in a theme and the views/ directory in the plugin
371 *
372 * @param string $template template file to search for
373 * @return template path
374 * @author Modern Tribe, Inc. (Matt Wiebe)
375 **/
376
377 public function getTemplateHierarchy($template) {
378 // whether or not .php was added
379 $template_slug = rtrim($template, '.php');
380 $template = $template_slug . '.php';
381
382 if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
383 $file = $theme_file;
384 } else {
385 $file = 'views/' . $template;
386 }
387 return apply_filters( 'sp_template_image-widget_'.$template, $file);
388 }
389
390
391 /**
392 * Display a thank you nag when the plugin has been upgraded.
393 */
394 public function post_upgrade_nag() {
395 if ( !current_user_can('install_plugins') ) return;
396
397 $version_key = '_image_widget_version';
398 if ( get_site_option( $version_key ) == self::VERSION ) return;
399
400 $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');
401 echo "<div class='update-nag'>$msg</div>";
402
403 update_site_option( $version_key, self::VERSION );
404 }
405
406 /**
407 * Display an informational section in the plugin admin ui.
408 * @param $meta
409 * @param $file
410 *
411 * @return array
412 */
413 public function plugin_row_meta( $meta, $file ) {
414 if ( $file == plugin_basename( dirname(__FILE__).'/image-widget.php' ) ) {
415 $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>';
416 }
417 return $meta;
418 }
419 }
420