Simple Image Widget - Version 3.0

Version Description

  • Complete rewrite with new media manager support.
Download this release

Release Info

Developer blazersix
Plugin Icon 128x128 Simple Image Widget
Version 3.0
Comparing to
See all releases

Code changes from version 2.1 to 3.0

class-simple-image-widget.php ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Image widget class.
4
+ *
5
+ * @package SimpleImageWidget
6
+ *
7
+ * @since 3.0.0
8
+ */
9
+ class Simple_Image_Widget extends WP_Widget {
10
+ /**
11
+ * Setup widget options.
12
+ *
13
+ * Allows child classes to overload the defaults.
14
+ *
15
+ * @since 3.0.0
16
+ * @see WP_Widget::construct()
17
+ */
18
+ function __construct( $id_base = false, $name = false, $widget_options = array(), $control_options = array() ) {
19
+ $id_base = ( $id_base ) ? $id_base : 'simpleimage'; // Legacy ID.
20
+ $name = ( $name ) ? $name : __( 'Image', 'simple-image-widget' );
21
+
22
+ $widget_options = wp_parse_args( $widget_options, array(
23
+ 'classname' => 'widget_simpleimage', // Legacy class name.
24
+ 'description' => __( 'Display an image', 'simple-image-widget' ),
25
+ ) );
26
+
27
+ $control_options = wp_parse_args( $control_options, array(
28
+ 'width' => 300
29
+ ) );
30
+
31
+ parent::__construct( $id_base, $name, $widget_options, $control_options );
32
+
33
+ // Flush widget group cache when an attachment is saved, deleted, or the theme is switched.
34
+ add_action( 'save_post', array( $this, 'flush_group_cache' ) );
35
+ add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) );
36
+ add_action( 'switch_theme', array( $this, 'flush_group_cache' ) );
37
+ }
38
+
39
+ /**
40
+ * Default widget front end display method.
41
+ *
42
+ * Filters the instance data, fetches the output, displays it, then caches
43
+ * it. Overload or filter the render() method to modify output.
44
+ *
45
+ * @since 3.0.0
46
+ */
47
+ function widget( $args, $instance ) {
48
+ $cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
49
+
50
+ if ( isset( $cache[ $this->id ] ) ) {
51
+ echo $cache[ $this->id ];
52
+ return;
53
+ }
54
+
55
+ // Copy the original title so it can be passed to hooks.
56
+ $instance['title_raw'] = $instance['title'];
57
+ $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
58
+
59
+ // Copy the original text so it can be passed to hooks.
60
+ $instance['text_raw'] = $instance['text'];
61
+ $instance['text'] = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance, $this->id_base );
62
+
63
+ // Start building the output.
64
+ $output = '';
65
+
66
+ // Make sure the image ID is a valid attachment.
67
+ if ( ! empty( $instance['image_id'] ) ) {
68
+ $image = get_post( $instance['image_id'] );
69
+ if ( ! $image || 'attachment' != get_post_type( $image ) ) {
70
+ $output = '<!-- Image Widget Error: Invalid Attachment ID -->';
71
+ }
72
+ }
73
+
74
+ if ( empty( $output ) ) {
75
+ $output = $this->render( $args, $instance );
76
+ }
77
+
78
+ echo $output;
79
+
80
+ $cache[ $this->id ] = $output;
81
+ wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
82
+ }
83
+
84
+ /**
85
+ * Generate the widget output.
86
+ *
87
+ * This is typically done in the widget() method, but moving it to a
88
+ * separate method allows for the routine to be easily overloaded by a
89
+ * class extending this one without having to reimplement all the caching
90
+ * and filtering, or resorting to adding a filter, calling the parent
91
+ * method, then removing the filter.
92
+ *
93
+ * @since 3.0.0
94
+ */
95
+ function render( $args, $instance ) {
96
+ $link_open = '';
97
+ $link_close = '';
98
+ if ( ! empty ( $instance['link'] ) ) {
99
+ $link_open = '<a href="' . esc_url( $instance['link'] ) . '">';
100
+ $link_close = '</a>';
101
+ }
102
+
103
+ $output = $args['before_widget'];
104
+
105
+ // Allow custom output to override the default HTML.
106
+ if ( $inside = apply_filters( 'simple_image_widget_output', '', $args, $instance, $this->id_base ) ) {
107
+ $output .= $inside;
108
+ } else {
109
+ $output .= ( empty( $instance['title'] ) ) ? '' : $args['before_title']. $instance['title'] . $args['after_title'];
110
+
111
+ // Add the image.
112
+ if ( ! empty( $instance['image_id'] ) ) {
113
+ $image_size = ( ! empty( $instance['image_size'] ) ) ? $instance['image_size'] : apply_filters( 'simple_image_widget_output_default_size', 'medium', $this->id_base );
114
+
115
+ $output .= sprintf( '<p>%s%s%s</p>',
116
+ $link_open,
117
+ wp_get_attachment_image( $instance['image_id'], $image_size ),
118
+ $link_close
119
+ );
120
+ } elseif ( ! empty( $instance['image'] ) ) {
121
+ // Legacy output.
122
+ $output .= sprintf( '%s<img src="%s" alt="%s">%s',
123
+ $link_before,
124
+ esc_url( $instance['image'] ),
125
+ ( empty( $instance['alt'] ) ) ? '' : esc_attr( $instance['alt'] ),
126
+ $link_after
127
+ );
128
+ }
129
+
130
+ // Add the text.
131
+ if ( ! empty( $instance['text'] ) ) {
132
+ $output .= apply_filters( 'the_content', $instance['text'] );
133
+ }
134
+
135
+ // Add a more link.
136
+ if ( ! empty( $link_open ) && ! empty( $instance['link_text'] ) ) {
137
+ $output .= '<p class="more">' . $link_open . $instance['link_text'] . $link_close . '</p>';
138
+ }
139
+ }
140
+
141
+ $output .= $args['after_widget'];
142
+
143
+ return $output;
144
+ }
145
+
146
+ /**
147
+ * Form for modifying widget settings.
148
+ *
149
+ * @since 3.0.0
150
+ */
151
+ function form( $instance ) {
152
+ $instance = wp_parse_args( (array) $instance, array(
153
+ 'alt' => '', // Legacy.
154
+ 'image' => '', // Legacy URL field.
155
+ 'image_id' => '',
156
+ 'image_size' => 'full',
157
+ 'link' => '',
158
+ 'link_text' => '',
159
+ 'title' => '',
160
+ 'text' => '',
161
+ ) );
162
+
163
+ $instance['image_id'] = absint( $instance['image_id'] );
164
+ $instance['title'] = wp_strip_all_tags( $instance['title'] );
165
+
166
+ $button_class = array( 'button', 'button-hero', 'simple-image-widget-control-choose' );
167
+ $image_id = $instance['image_id'];
168
+
169
+ // The order of fields can be modified, new fields can be registered, or existing fields can be removed here.
170
+ $fields = (array) apply_filters( 'simple_image_widget_fields', $this->form_fields(), $this->id_base );
171
+ ?>
172
+
173
+ <div class="simple-image-widget-form">
174
+
175
+ <?php do_action( 'simple_image_widget_form_before', $instance, $this->id_base ); ?>
176
+
177
+ <p>
178
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'simple-image-widget' ); ?></label>
179
+ <input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat">
180
+ </p>
181
+
182
+ <?php if ( ! is_simple_image_widget_legacy() ) : ?>
183
+ <p class="simple-image-widget-control<?php echo ( $image_id ) ? ' has-image' : ''; ?>"
184
+ data-title="<?php esc_attr_e( 'Choose an Image for the Widget', 'simple-image-widget' ); ?>"
185
+ data-update-text="<?php esc_attr_e( 'Update Image', 'simple-image-widget' ); ?>"
186
+ data-target=".image-id">
187
+ <?php
188
+ if ( $image_id ) {
189
+ echo wp_get_attachment_image( $image_id, 'medium', false );
190
+ unset( $button_class[ array_search( 'button-hero', $button_class ) ] );
191
+ }
192
+ ?>
193
+ <input type="hidden" name="<?php echo $this->get_field_name( 'image_id' ); ?>" id="<?php echo $this->get_field_id( 'image_id' ); ?>" value="<?php echo $image_id; ?>" class="image-id simple-image-widget-control-target">
194
+ <a href="#" class="<?php echo join( ' ', $button_class ); ?>"><?php _e( 'Choose an Image', 'simple-image-widget' ); ?></a>
195
+ </p>
196
+ <?php endif; ?>
197
+
198
+ <?php if ( is_simple_image_widget_legacy() || ! empty( $instance['image'] ) ) : ?>
199
+ <div class="simple-image-widget-legacy-fields">
200
+ <?php if ( ! is_simple_image_widget_legacy() ) : ?>
201
+ <p>
202
+ <em><?php _e( 'These fields are here to maintain your data from an earlier version.', 'simple-image-widget' ); ?></em>
203
+ </p>
204
+ <p>
205
+ <em><?php _e( 'Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget' ); ?></em>
206
+ </p>
207
+ <?php endif; ?>
208
+
209
+ <p>
210
+ <label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e( 'Image URL:', 'simple-image-widget' ); ?></label>
211
+ <input type="text" name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" value="<?php echo esc_url( $instance['image'] ); ?>" class="widefat">
212
+ </p>
213
+ <p>
214
+ <label for="<?php echo $this->get_field_id( 'alt' ); ?>"><?php _e( 'Alternate Text:', 'simple-image-widget' ); ?></label>
215
+ <input type="text" name="<?php echo $this->get_field_name( 'alt' ); ?>" id="<?php echo $this->get_field_id( 'alt' ); ?>" value="<?php echo esc_attr( $instance['alt'] ); ?>" class="widefat">
216
+ </p>
217
+ </div>
218
+ <?php endif; ?>
219
+
220
+ <?php
221
+ if ( ! empty( $fields ) ) {
222
+ foreach ( $fields as $field ) {
223
+ switch ( $field ) {
224
+ case 'image_size' :
225
+ $sizes = $this->get_image_sizes( $image_id );
226
+ ?>
227
+ <p>
228
+ <label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
229
+ <select name="<?php echo $this->get_field_name( 'image_size' ); ?>" id="<?php echo $this->get_field_id( 'image_size' ); ?>" class="widefat image-size"<?php echo ( sizeof( $sizes ) < 2 ) ? ' disabled="disabled"' : ''; ?>>
230
+ <?php
231
+ foreach ( $sizes as $id => $label ) {
232
+ printf( '<option value="%s"%s>%s</option>',
233
+ esc_attr( $id ),
234
+ selected( $instance['image_size'], $id, false ),
235
+ esc_html( $label )
236
+ );
237
+ }
238
+ ?>
239
+ </select>
240
+ </p>
241
+ <?php
242
+ break;
243
+
244
+ case 'link' :
245
+ ?>
246
+ <p>
247
+ <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
248
+ <input type="text" name="<?php echo $this->get_field_name( 'link' ); ?>" id="<?php echo $this->get_field_id( 'link' ); ?>" value="<?php echo esc_url( $instance['link'] ); ?>" class="widefat">
249
+ </p>
250
+ <?php
251
+ break;
252
+
253
+ case 'link_text' :
254
+ ?>
255
+ <p>
256
+ <label for="<?php echo $this->get_field_id( 'link_text' ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
257
+ <input type="text" name="<?php echo $this->get_field_name( 'link_text' ); ?>" id="<?php echo $this->get_field_id( 'link_text' ); ?>" value="<?php echo esc_attr( $instance['link_text'] ); ?>" class="widefat">
258
+ </p>
259
+ <?php
260
+ break;
261
+
262
+ case 'text' :
263
+ ?>
264
+ <p>
265
+ <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
266
+ <textarea name="<?php echo $this->get_field_name( 'text' ); ?>" id="<?php echo $this->get_field_id( 'text' ); ?>" rows="4" class="widefat"><?php echo esc_textarea( $instance['text'] ); ?></textarea>
267
+ </p>
268
+ <?php
269
+ break;
270
+
271
+ default :
272
+ // Custom fields can be added using this action.
273
+ do_action( 'simple_image_widget_field-' . sanitize_key( $field ), $instance, $this );
274
+ }
275
+ }
276
+ }
277
+
278
+ do_action( 'simple_image_widget_form_after', $instance, $this->id_base );
279
+ ?>
280
+
281
+ </div>
282
+ <?php
283
+ }
284
+
285
+ /**
286
+ * The list of extra fields that should be shown in the widget form.
287
+ *
288
+ * Can be easily overloaded by a child class.
289
+ *
290
+ * @since 3.0.0
291
+ */
292
+ function form_fields() {
293
+ $fields = array( 'link', 'link_text', 'text' );
294
+
295
+ // Don't show the image size field for users with older WordPress versions.
296
+ if ( ! is_simple_image_widget_legacy() ) {
297
+ array_unshift( $fields, 'image_size' );
298
+ }
299
+
300
+ return $fields;
301
+ }
302
+
303
+ /**
304
+ * Save widget settings.
305
+ *
306
+ * @since 3.0.0
307
+ */
308
+ function update( $new_instance, $old_instance ) {
309
+ $instance = wp_parse_args( $new_instance, $old_instance );
310
+
311
+ $instance = apply_filters( 'simple_image_widget_instance', $instance, $new_instance, $old_instance, $this->id_base );
312
+
313
+ $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
314
+ $instance['image_id'] = absint( $new_instance['image_id'] );
315
+ $instance['link'] = esc_url_raw( $new_instance['link'] );
316
+ $instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
317
+ $instance['text'] = wp_kses_data( $new_instance['text'] );
318
+
319
+ $instance['image'] = esc_url_raw( $new_instance['image'] ); // Legacy image URL.
320
+ if ( empty( $instance['image'] ) ) {
321
+ unset( $instance['image'] );
322
+ }
323
+
324
+ $instance['alt'] = wp_strip_all_tags( $instance['alt'] ); // Legacy alt text.
325
+ if ( empty( $instance['alt'] ) ) {
326
+ unset( $instance['alt'] );
327
+ }
328
+
329
+ $this->flush_widget_cache();
330
+
331
+ return $instance;
332
+ }
333
+
334
+ /**
335
+ * Get the various sizes of an images.
336
+ *
337
+ * @since 3.0.0
338
+ *
339
+ * @param int $image_id Image attachment ID.
340
+ * @return array List of image size keys and their localized labels.
341
+ */
342
+ function get_image_sizes( $image_id ) {
343
+ $sizes = array( 'full' => __( 'Full Size', 'simple-image-widget' ) );
344
+
345
+ $imagedata = wp_get_attachment_metadata( $image_id );
346
+ if ( isset( $imagedata['sizes'] ) ) {
347
+ $size_names = simple_image_widget_Loader::get_image_size_names();
348
+
349
+ $sizes['full'] .= ( isset( $imagedata['width'] ) && isset( $imagedata['height'] ) ) ? sprintf( ' (%d&times;%d)', $imagedata['width'], $imagedata['height'] ) : '';
350
+
351
+ foreach( $imagedata['sizes'] as $_size => $data ) {
352
+ $label = ( isset( $size_names[ $_size ] ) ) ? $size_names[ $_size ] : ucwords( $_size );
353
+ $label .= sprintf( ' (%d&times;%d)', $data['width'], $data['height'] );
354
+
355
+ $sizes[ $_size ] = $label;
356
+ }
357
+ }
358
+
359
+ return $sizes;
360
+ }
361
+
362
+ /**
363
+ * Remove a single image widget from the cache.
364
+ *
365
+ * @since 3.0.0
366
+ */
367
+ function flush_widget_cache() {
368
+ $cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
369
+
370
+ if ( isset( $cache[ $this->id ] ) ) {
371
+ unset( $cache[ $this->id ] );
372
+ }
373
+
374
+ wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
375
+ }
376
+
377
+ /**
378
+ * Flush the cache for all image widgets.
379
+ *
380
+ * @since 3.0.0
381
+ */
382
+ function flush_group_cache( $post_id = null ) {
383
+ if ( 'save_post' == current_filter() && 'attachment' != get_post_type( $post_id ) ) {
384
+ return;
385
+ }
386
+
387
+ wp_cache_delete( 'simple_image_widget', 'widget' );
388
+ }
389
+ }
js/simple-image-widget.js ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var SimpleImageWidget;
2
+
3
+ (function($) {
4
+ SimpleImageWidget.updateSizeDropdownOptions = function( field, sizes ) {
5
+ var currentValue = field.val(),
6
+ options;
7
+
8
+ if ( sizes ) {
9
+ $.each( sizes, function( key, size ) {
10
+ var name;
11
+
12
+ if ( key in SimpleImageWidget.imageSizeNames ) {
13
+ name = SimpleImageWidget.imageSizeNames[ key ];
14
+ }
15
+
16
+ options += '<option value="' + key + '">' + name + ' (' + size.width + '&times;' + size.height + ')</option>';
17
+ });
18
+ }
19
+
20
+ if ( ! options ) {
21
+ name = SimpleImageWidget.imageSizeNames['full'] || SimpleImageWidget.fullSizeLabel;
22
+ options = '<option value="full">' + name + '</option>';
23
+ }
24
+
25
+ // Try to maintain the previously selected size if it still exists.
26
+ field.html( options ).val( currentValue ).removeAttr('disabled');
27
+ };
28
+ })(jQuery);
29
+
30
+ /**
31
+ * Media control frame popup.
32
+ */
33
+ jQuery(function($) {
34
+ var Attachment = wp.media.model.Attachment,
35
+ $control, $controlTarget, mediaControl;
36
+
37
+ mediaControl = {
38
+ // Initialize a new media manager or return an existing frame.
39
+ // @see wp.media.featuredImage.frame()
40
+ frame: function() {
41
+ if ( this._frame )
42
+ return this._frame;
43
+
44
+ this._frame = wp.media({
45
+ title: $control.data('title') || SimpleImageWidget.frameTitle,
46
+ library: {
47
+ type: $control.data('media-type') || 'image'
48
+ },
49
+ button: {
50
+ text: $control.data('update-text') || SimpleImageWidget.frameUpdateText
51
+ },
52
+ multiple: $control.data( 'select-multiple' ) || false
53
+ });
54
+
55
+ this._frame.on( 'open', this.updateLibrarySelection ).state('library').on( 'select', this.select );
56
+
57
+ return this._frame;
58
+ },
59
+
60
+ // Update the control when an image is selected from the media library.
61
+ select: function() {
62
+ var selection = this.get('selection'),
63
+ returnProperty = $control.data('return-property') || 'id';
64
+
65
+ // Insert the selected attachment ids into the target element.
66
+ if ( $controlTarget.length ) {
67
+ $controlTarget.val( selection.pluck( returnProperty ) );
68
+ }
69
+
70
+ // Trigger an event on the control to allow custom updates.
71
+ $control.trigger( 'selectionChange.simpleimagewidget', [ selection ] );
72
+ },
73
+
74
+ // Update the selected image in the media library based on the image in the control.
75
+ updateLibrarySelection: function() {
76
+ var selection = this.get('library').get('selection'),
77
+ attachment, selectedIds;
78
+
79
+ if ( $controlTarget.length ) {
80
+ selectedIds = $controlTarget.val();
81
+ if ( selectedIds && '' !== selectedIds && -1 !== selectedIds && '0' !== selectedIds ) {
82
+ attachment = Attachment.get( selectedIds );
83
+ attachment.fetch();
84
+ }
85
+ }
86
+
87
+ selection.reset( attachment ? [ attachment ] : [] );
88
+ },
89
+
90
+ init: function() {
91
+ $('#wpbody').on('click', '.simple-image-widget-control-choose', function(e) {
92
+ var targetSelector;
93
+
94
+ e.preventDefault();
95
+
96
+ $control = $(this).closest('.simple-image-widget-control');
97
+
98
+ targetSelector = $control.data('target') || '.simple-image-widget-control-target';
99
+ if ( 0 === targetSelector.indexOf('#') ) {
100
+ // Context doesn't matter if the selector is an ID.
101
+ $controlTarget = $( targetSelector );
102
+ } else {
103
+ // Search for other selectors within the context of the control.
104
+ $controlTarget = $control.find( targetSelector );
105
+ }
106
+
107
+ mediaControl.frame().open();
108
+ });
109
+ }
110
+ };
111
+
112
+ mediaControl.init();
113
+ });
languages/simple-image-widget.pot ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: Simple Image Widget\n"
4
+ "POT-Creation-Date: 2013-01-11 14:34-0800\n"
5
+ "PO-Revision-Date: 2013-01-11 14:38-0800\n"
6
+ "Last-Translator: Brady Vercher <brady@blazersix.com>\n"
7
+ "Language-Team: \n"
8
+ "MIME-Version: 1.0\n"
9
+ "Content-Type: text/plain; charset=UTF-8\n"
10
+ "Content-Transfer-Encoding: 8bit\n"
11
+ "X-Generator: Poedit 1.5.4\n"
12
+ "X-Poedit-KeywordsList: _e;__;esc_attr_e;esc_attr__;esc_html_e;esc_html__\n"
13
+ "X-Poedit-Basepath: ../\n"
14
+ "X-Poedit-SearchPath-0: .\n"
15
+
16
+ #: class-simple-image-widget.php:20
17
+ msgid "Image"
18
+ msgstr ""
19
+
20
+ #: class-simple-image-widget.php:24
21
+ msgid "Display an image"
22
+ msgstr ""
23
+
24
+ #: class-simple-image-widget.php:178
25
+ msgid "Title:"
26
+ msgstr ""
27
+
28
+ #: class-simple-image-widget.php:184
29
+ msgid "Choose an Image for the Widget"
30
+ msgstr ""
31
+
32
+ #: class-simple-image-widget.php:185
33
+ msgid "Update Image"
34
+ msgstr ""
35
+
36
+ #: class-simple-image-widget.php:194
37
+ msgid "Choose an Image"
38
+ msgstr ""
39
+
40
+ #: class-simple-image-widget.php:202
41
+ msgid "These fields are here to maintain your data from an earlier version."
42
+ msgstr ""
43
+
44
+ #: class-simple-image-widget.php:205
45
+ msgid ""
46
+ "Select an image, then clear these values, and they will disappear when you "
47
+ "save the widget."
48
+ msgstr ""
49
+
50
+ #: class-simple-image-widget.php:210
51
+ msgid "Image URL:"
52
+ msgstr ""
53
+
54
+ #: class-simple-image-widget.php:214
55
+ msgid "Alternate Text:"
56
+ msgstr ""
57
+
58
+ #: class-simple-image-widget.php:228
59
+ msgid "Size:"
60
+ msgstr ""
61
+
62
+ #: class-simple-image-widget.php:247
63
+ msgid "Link:"
64
+ msgstr ""
65
+
66
+ #: class-simple-image-widget.php:256
67
+ msgid "Link Text:"
68
+ msgstr ""
69
+
70
+ #: class-simple-image-widget.php:265
71
+ msgid "Text:"
72
+ msgstr ""
73
+
74
+ #: class-simple-image-widget.php:343 simple-image-widget.php:85
75
+ #: simple-image-widget.php:186
76
+ msgid "Full Size"
77
+ msgstr ""
78
+
79
+ #: simple-image-widget.php:83
80
+ msgid "Choose an Attachment"
81
+ msgstr ""
82
+
83
+ #: simple-image-widget.php:84
84
+ msgid "Update Attachment"
85
+ msgstr ""
86
+
87
+ #: simple-image-widget.php:183
88
+ msgid "Thumbnail"
89
+ msgstr ""
90
+
91
+ #: simple-image-widget.php:184
92
+ msgid "Medium"
93
+ msgstr ""
94
+
95
+ #: simple-image-widget.php:185
96
+ msgid "Large"
97
+ msgstr ""
readme.txt CHANGED
@@ -1,57 +1,36 @@
1
- === Simple Image Widget ===
2
- Contributors: vickio
3
- Donate link: http://vickio.net
4
- Tags: image, sidebar, widget, photo, picture
5
- Requires at least: 2.8
6
- Tested up to: 3.2.1
7
- Stable tag: 2.1
8
-
9
- The simple way to place images in your sidebars.
10
-
11
- == Description ==
12
-
13
- Using this widget you can easily place an image in the sidebar. You can also specify a URL to link to when clicking on the image. It supports multiple instances, so you can use it multiple times in multiple sidebars.
14
-
15
- Once the plugin is enabled, the widget will be available in your widgets list as "Simple Image". You can add this widget to sidebars as many times as you need. The control interface allows you to specify the following options for each instance of the widget:
16
-
17
- * Image URL: The full URL to the image file
18
- * Alternate Text: Shown by the browser if the image cannot be displayed, or if image loading is disabled
19
- * Title: Displayed in a tooltip when the mouse cursor is held over the image for a few seconds
20
- * Link URL: URL to open when the image is clicked on (optional)
21
- * Open link in new window: If this is enabled, the link will open in a new browser window
22
-
23
- == Installation ==
24
-
25
- 1. Copy or upload the `simple-image-widget` folder to your `/wp-content/plugins/` directory
26
- 1. Activate the plugin on the Plugins page
27
- 1. Add the "Simple Image" widget to a sidebar in Appearance -> Widgets
28
-
29
- == Frequently Asked Questions ==
30
-
31
- = How do I upload an image? =
32
-
33
- The Simple Image widget does not provide a mechanism for uploading images or files. You can however easily upload an image by selecting Media -> Add New. After uploading an image, copy its 'File URL' for use in the Simple Image widget 'Image URL' field.
34
-
35
- = How many images can be displayed? =
36
-
37
- Each instance of the widget can only display one image, but you can create as many instances as you need.
38
-
39
- = How do I remove the border from around a link image? =
40
-
41
- In your theme's CSS file, add this code:
42
-
43
- `.simpleimage img { border: 0px; }`
44
-
45
- = How do I change the alignment of the images? =
46
-
47
- This can also be done with CSS:
48
-
49
- `.simpleimage { text-align: center; }`
50
-
51
- = How do I edit my theme's CSS file? =
52
-
53
- In Wordpress, select Appearance -> Editor, then in the file list on the right, select Stylesheet (style.css) under Styles. Add your custom CSS code to the bottom of this file.
54
-
55
- == Screenshots ==
56
-
57
- 1. Simple Image Widget control interface
1
+ === Simple Image Widget ===
2
+ Contributors: blazersix, bradyvercher
3
+ Tags: image widget, widget, media, media manager, sidebar, image, photo, picture
4
+ Requires at least: 3.3
5
+ Tested up to: 3.5
6
+ Stable tag: 3.0
7
+ License: GPL-2.0+
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ A simple image widget utilizing the new WordPress media manager.
11
+
12
+ == Description ==
13
+
14
+ Simple Image Widget provides the absolute easiest method to quicky add an image to a sidebar or any other widget area. Despite its simplicty, it can be extended by developers via the various built in hooks to create additional image-based widgets.
15
+
16
+ Contribute to [development on GitHub](http://github.com/blazersix/simple-image-widget).
17
+
18
+ == Installation ==
19
+
20
+ Installation is just like installing most other plugins. [Check out the codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins) if you have any questions.
21
+
22
+ == Screenshots ==
23
+
24
+ 1. A new image widget.
25
+ 2. The widget after selecting an image.
26
+
27
+ == Notes ==
28
+
29
+ Development and maintenance of this plugin was taken over by Blazer Six starting with version 3.0.
30
+
31
+ Read about the original thought behind creating this widget and ways it can be extended in [this blog post](http://www.blazersix.com/blog/wordpress-image-widget/).
32
+
33
+ == Changelog ==
34
+
35
+ = 3.0 =
36
+ * Complete rewrite with new media manager support.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
screenshot-1.png CHANGED
Binary file
screenshot-2.png ADDED
Binary file
simple-image-widget.php ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Plugin Name: Simple Image Widget
4
+ * Plugin URI: https://wordpress.org/extend/plugins/simple-image-widget/
5
+ * Description: A simple image widget utilizing the new WordPress media manager.
6
+ * Version: 3.0
7
+ * Author: Blazer Six
8
+ * Author URI: http://www.blazersix.com/
9
+ * License: GPL-2.0+
10
+ * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+ * Text Domain: simple-image-widget
12
+ * Domain Path: /languages/
13
+ *
14
+ * @package SimpleImageWidget
15
+ * @author Brady Vercher <brady@blazersix.com>
16
+ * @copyright Copyright (c) 2012, Blazer Six, Inc.
17
+ * @license GPL-2.0+
18
+ */
19
+
20
+ /**
21
+ * Include the image widget class early to make it easy to extend.
22
+ */
23
+ require_once( plugin_dir_path( __FILE__ ) . 'class-simple-image-widget.php' );
24
+
25
+ /**
26
+ * Load the plugin when plugins are loaded.
27
+ */
28
+ add_action( 'plugins_loaded', array( 'Simple_Image_Widget_Loader', 'load' ) );
29
+
30
+ /**
31
+ * The main plugin class for loading the widget and attaching necessary hooks.
32
+ *
33
+ * @since 3.0.0
34
+ */
35
+ class Simple_Image_Widget_Loader {
36
+ /**
37
+ * Setup functionality needed by the widget.
38
+ *
39
+ * @since 3.0.0
40
+ */
41
+ public static function load() {
42
+ add_action( 'init', array( __CLASS__, 'l10n' ) );
43
+ add_action( 'widgets_init', array( __CLASS__, 'register_widget' ) );
44
+
45
+ if ( is_simple_image_widget_legacy() ) {
46
+ return;
47
+ }
48
+
49
+ add_action( 'init', array( __CLASS__, 'init' ) );
50
+ add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_scripts' ) );
51
+ add_action( 'admin_head-widgets.php', array( __CLASS__, 'admin_head_widgets' ) );
52
+ add_action( 'admin_footer-widgets.php', array( __CLASS__, 'admin_footer_widgets' ) );
53
+ }
54
+
55
+ /**
56
+ * Plugin localization support.
57
+ *
58
+ * @since 3.0.0
59
+ */
60
+ public static function l10n() {
61
+ // The "plugin_locale" filter is also used in load_plugin_textdomain()
62
+ $locale = apply_filters( 'plugin_locale', get_locale(), 'simple-image-widget' );
63
+ load_textdomain( 'simple-image-widget', WP_LANG_DIR . '/simple-image-widget/simple-image-widget-' . $locale . '.mo' );
64
+ load_plugin_textdomain( 'simple-image-widget', false, dirname( plugin_basename( __FILE__ ) ) . 'languages/' );
65
+ }
66
+
67
+ /**
68
+ * Register and localize generic script libraries.
69
+ *
70
+ * A preliminary attempt has been made to abstract the
71
+ * 'simple-image-widget-control' script a bit in order to allow it to be
72
+ * re-used anywhere a similiar media selection feature is needed.
73
+ *
74
+ * Custom image size labels need to be added using the
75
+ * 'image_size_names_choose' filter.
76
+ *
77
+ * @since 3.0.0
78
+ */
79
+ public static function init() {
80
+ wp_register_script( 'simple-image-widget', plugin_dir_url( __FILE__ ) . 'js/simple-image-widget.js', array( 'media-upload', 'media-views' ) );
81
+
82
+ wp_localize_script( 'simple-image-widget', 'SimpleImageWidget', array(
83
+ 'frameTitle' => __( 'Choose an Attachment', 'simple-image-widget' ),
84
+ 'frameUpdateText' => __( 'Update Attachment', 'simple-image-widget' ),
85
+ 'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
86
+ 'imageSizeNames' => self::get_image_size_names()
87
+ ) );
88
+ }
89
+
90
+ /**
91
+ * Register the image widget.
92
+ *
93
+ * @since 3.0.0
94
+ */
95
+ public static function register_widget() {
96
+ register_widget( 'Simple_Image_Widget' );
97
+ }
98
+
99
+ /**
100
+ * Enqueue scripts needed for selecting media.
101
+ *
102
+ * @since 3.0.0
103
+ */
104
+ public static function admin_scripts( $hook_suffix ) {
105
+ if ( 'widgets.php' == $hook_suffix ) {
106
+ wp_enqueue_media();
107
+ wp_enqueue_script( 'simple-image-widget' );
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Output CSS for styling the image widget in the dashboard.
113
+ *
114
+ * @since 3.0.0
115
+ */
116
+ public static function admin_head_widgets() {
117
+ ?>
118
+ <style type="text/css">
119
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control { padding: 20px 0; text-align: center; border: 1px dashed #aaa;}
120
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control.has-image { padding: 10px; text-align: left; border: 1px dashed #aaa;}
121
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control img { display: block; margin-bottom: 10px; max-width: 100%; height: auto;}
122
+
123
+ .simple-image-widget-legacy-fields { margin-bottom: 1em; padding: 10px; background-color: #e0e0e0; border-radius: 3px;}
124
+ .simple-image-widget-legacy-fields p:last-child { margin-bottom: 0;}
125
+ </style>
126
+ <?php
127
+ }
128
+
129
+ /**
130
+ * Output custom handler for when an image is selected in the media manager.
131
+ *
132
+ * @since 3.0.0
133
+ */
134
+ public static function admin_footer_widgets() {
135
+ ?>
136
+ <script type="text/javascript">
137
+ jQuery(function($) {
138
+ $('#wpbody').on('selectionChange.simpleimagewidget', '.simple-image-widget-control', function( e, selection ) {
139
+ var $control = $( e.target ),
140
+ $sizeField = $control.closest('.simple-image-widget-form').find('select.image-size'),
141
+ model = selection.first(),
142
+ sizes = model.get('sizes'),
143
+ size, image;
144
+
145
+ if ( sizes ) {
146
+ // The image size to display in the widget.
147
+ size = sizes['post-thumbnail'] || sizes.medium;
148
+ }
149
+
150
+ if ( $sizeField.length ) {
151
+ // Builds the option elements for the size dropdown.
152
+ SimpleImageWidget.updateSizeDropdownOptions( $sizeField, sizes );
153
+ }
154
+
155
+ size = size || model.toJSON();
156
+
157
+ image = $( '<img />', { src: size.url, width: size.width } );
158
+
159
+ $control.find('img').remove().end()
160
+ .prepend( image )
161
+ .addClass('has-image')
162
+ .find('a.simple-image-widget-control-choose').removeClass('button-hero');
163
+ });
164
+ });
165
+ </script>
166
+ <?php
167
+ }
168
+
169
+ /**
170
+ * Get localized image size names.
171
+ *
172
+ * The 'image_size_names_choose' filter exists in core and should be
173
+ * hooked by plugin authors to provide localized labels for custom image
174
+ * sizes added using add_image_size().
175
+ *
176
+ * @see image_size_input_fields()
177
+ * @see http://core.trac.wordpress.org/ticket/20663
178
+ *
179
+ * @since 3.0.0
180
+ */
181
+ public static function get_image_size_names() {
182
+ return apply_filters( 'image_size_names_choose', array(
183
+ 'thumbnail' => __( 'Thumbnail', 'simple-image-widget' ),
184
+ 'medium' => __( 'Medium', 'simple-image-widget' ),
185
+ 'large' => __( 'Large', 'simple-image-widget' ),
186
+ 'full' => __( 'Full Size', 'simple-image-widget' )
187
+ ) );
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Check to see if the current version of WordPress supports the new media manager.
193
+ */
194
+ function is_simple_image_widget_legacy() {
195
+ return version_compare( get_bloginfo( 'version' ), '3.4.2', '<=' );
196
+ }