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