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