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