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