PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.1
Image Widget v4.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 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
414 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 // 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.1';
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 margin-top: 15px;
186 }
187 .tribe_preview .aligncenter {
188 display: block;
189 margin-left: auto !important;
190 margin-right: auto !important;
191 }
192 .tribe_preview {
193 overflow: hidden;
194 max-height: 300px;
195 }
196 .tribe_preview img {
197 margin: 10px 0;
198 height: auto;
199 }
200 </style>
201 <?php
202 }
203
204 /**
205 * Render an array of default values.
206 *
207 * @return array default values
208 */
209 private static function get_defaults() {
210
211 $defaults = array(
212 'title' => '',
213 'description' => '',
214 'link' => '',
215 'linktarget' => '',
216 'width' => 0,
217 'height' => 0,
218 'maxwidth' => '100%',
219 'maxheight' => '',
220 'image' => 0, // reverse compatible - now attachement_id
221 'imageurl' => '', // reverse compatible.
222 'align' => 'none',
223 'alt' => '',
224 );
225
226 if ( !defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) {
227 $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG;
228 $defaults['attachment_id'] = 0;
229 }
230
231 return $defaults;
232 }
233
234 /**
235 * Render the image html output.
236 *
237 * @param array $instance
238 * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored.
239 * @return string image html
240 */
241 private function get_image_html( $instance, $include_link = true ) {
242
243 // Backwards compatible image display.
244 if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
245 $instance['attachment_id'] = $instance['image'];
246 }
247
248 $output = '';
249
250 if ( $include_link && !empty( $instance['link'] ) ) {
251 $attr = array(
252 'href' => $instance['link'],
253 'target' => $instance['linktarget'],
254 'class' => $this->widget_options['classname'].'-image-link',
255 'title' => ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
256 );
257 $attr = apply_filters('image_widget_link_attributes', $attr, $instance );
258 $attr = array_map( 'esc_attr', $attr );
259 $output = '<a';
260 foreach ( $attr as $name => $value ) {
261 $output .= sprintf( ' %s="%s"', $name, $value );
262 }
263 $output .= '>';
264 }
265
266 $size = $this->get_image_size( $instance );
267 if ( is_array( $size ) ) {
268 $instance['width'] = $size[0];
269 $instance['height'] = $size[1];
270 } elseif ( !empty( $instance['attachment_id'] ) ) {
271 //$instance['width'] = $instance['height'] = 0;
272 $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
273 if ($image_details) {
274 $instance['imageurl'] = $image_details[0];
275 $instance['width'] = $image_details[1];
276 $instance['height'] = $image_details[2];
277 }
278 }
279 $instance['width'] = abs( $instance['width'] );
280 $instance['height'] = abs( $instance['height'] );
281
282 $attr = array();
283 $attr['alt'] = ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'];
284 if (is_array($size)) {
285 $attr['class'] = 'attachment-'.join('x',$size);
286 } else {
287 $attr['class'] = 'attachment-'.$size;
288 }
289 $attr['style'] = '';
290 if (!empty($instance['maxwidth'])) {
291 $attr['style'] .= "max-width: {$instance['maxwidth']};";
292 }
293 if (!empty($instance['maxheight'])) {
294 $attr['style'] .= "max-height: {$instance['maxheight']};";
295 }
296 if (!empty($instance['align']) && $instance['align'] != 'none') {
297 $attr['class'] .= " align{$instance['align']}";
298 }
299 $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
300
301 // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
302 if ( !empty( $instance['imageurl'] ) ) {
303 // If all we have is an image src url we can still render an image.
304 $attr['src'] = $instance['imageurl'];
305 $attr = array_map( 'esc_attr', $attr );
306 $hwstring = image_hwstring( $instance['width'], $instance['height'] );
307 $output .= rtrim("<img $hwstring");
308 foreach ( $attr as $name => $value ) {
309 $output .= sprintf( ' %s="%s"', $name, $value );
310 }
311 $output .= ' />';
312 } elseif( abs( $instance['attachment_id'] ) > 0 ) {
313 $output .= wp_get_attachment_image($instance['attachment_id'], $size, false, $attr);
314 }
315
316 if ( $include_link && !empty( $instance['link'] ) ) {
317 $output .= '</a>';
318 }
319
320 return $output;
321 }
322
323 /**
324 * Assesses the image size in case it has not been set or in case there is a mismatch.
325 *
326 * @param $instance
327 * @return array|string
328 */
329 private function get_image_size( $instance ) {
330 if ( !empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) {
331 $size = $instance['size'];
332 } elseif ( isset( $instance['width'] ) && is_numeric($instance['width']) && isset( $instance['height'] ) && is_numeric($instance['height']) ) {
333 //$size = array(abs($instance['width']),abs($instance['height']));
334 $size = array($instance['width'],$instance['height']);
335 } else {
336 $size = 'full';
337 }
338 return $size;
339 }
340
341 /**
342 * Establish the aspect ratio of the image.
343 *
344 * @param $instance
345 * @return float|number
346 */
347 private function get_image_aspect_ratio( $instance ) {
348 if ( !empty( $instance['aspect_ratio'] ) ) {
349 return abs( $instance['aspect_ratio'] );
350 } else {
351 $attachment_id = ( !empty($instance['attachment_id']) ) ? $instance['attachment_id'] : $instance['image'];
352 if ( !empty($attachment_id) ) {
353 $image_details = wp_get_attachment_image_src( $attachment_id, 'full' );
354 if ($image_details) {
355 return ( $image_details[1]/$image_details[2] );
356 }
357 }
358 }
359 }
360
361 /**
362 * Loads theme files in appropriate hierarchy: 1) child theme,
363 * 2) parent template, 3) plugin resources. will look in the image-widget/
364 * directory in a theme and the views/ directory in the plugin
365 *
366 * @param string $template template file to search for
367 * @return template path
368 * @author Modern Tribe, Inc. (Matt Wiebe)
369 **/
370
371 function getTemplateHierarchy($template) {
372 // whether or not .php was added
373 $template_slug = rtrim($template, '.php');
374 $template = $template_slug . '.php';
375
376 if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
377 $file = $theme_file;
378 } else {
379 $file = 'views/' . $template;
380 }
381 return apply_filters( 'sp_template_image-widget_'.$template, $file);
382 }
383
384
385 /**
386 * Display a thank you nag when the plugin has been upgraded.
387 */
388 public function post_upgrade_nag() {
389 if ( !current_user_can('install_plugins') ) return;
390
391 $version_key = '_image_widget_version';
392 if ( get_site_option( $version_key ) == self::VERSION ) return;
393
394 $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');
395 echo "<div class='update-nag'>$msg</div>";
396
397 update_site_option( $version_key, self::VERSION );
398 }
399
400 /**
401 * Display an informational section in the plugin admin ui.
402 * @param $meta
403 * @param $file
404 *
405 * @return array
406 */
407 public function plugin_row_meta( $meta, $file ) {
408 if ( $file == plugin_basename( dirname(__FILE__).'/image-widget.php' ) ) {
409 $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>';
410 }
411 return $meta;
412 }
413 }
414