Simple Image Widget - Version 4.0.0

Version Description

  • New template system to make it easier to override the output.
  • Restructured to make it more intuitive for developers to extend the widget.
  • Moved legacy support into a separate class that hooks into the widget.
  • Works with the Widget Customizer added in WordPress 3.9.
  • Improved compatibility with plugins like Page Builder by SiteOrigin.
Download this release

Release Info

Developer bradyvercher
Plugin Icon 128x128 Simple Image Widget
Version 4.0.0
Comparing to
See all releases

Code changes from version 3.0.4 to 4.0.0

assets/scripts/simple-image-widget.js ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*global _:false, wp:false */
2
+
3
+ window.SimpleImageWidget = window.SimpleImageWidget || {};
4
+
5
+ (function( window, $, _, wp, undefined ) {
6
+ 'use strict';
7
+
8
+ var SimpleImageWidget = window.SimpleImageWidget,
9
+ Attachment = wp.media.model.Attachment,
10
+ frames = [],
11
+ Control, l10n;
12
+
13
+ // Link any localized strings.
14
+ l10n = SimpleImageWidget.l10n = SimpleImageWidget.l10n || {};
15
+
16
+ /**
17
+ * Control module object.
18
+ */
19
+ Control = function( el, options ) {
20
+ var defaults, selector, settings;
21
+
22
+ this.$el = $( el );
23
+
24
+ selector = this.$el.data( 'target' ) || '.simple-image-widget-control-target';
25
+ if ( 0 === selector.indexOf( '#' ) ) {
26
+ this.$target = $( selector );
27
+ } else {
28
+ // Search within the context of the control.
29
+ this.$target = this.$el.find( selector );
30
+ }
31
+
32
+ defaults = {
33
+ frame: {
34
+ id: 'simple-image-widget',
35
+ title: this.$el.data( 'title' ) || l10n.frameTitle,
36
+ updateText: this.$el.data( 'update-text' ) || l10n.frameUpdateText,
37
+ multiple: this.$el.data( 'select-multiple' ) || false
38
+ },
39
+ mediaType: this.$el.data( 'media-type' ) || 'image',
40
+ returnProperty: this.$el.data( 'return-property' ) || 'id'
41
+ };
42
+
43
+ options = options || {};
44
+ options.frame = options.frame || {};
45
+ this.settings = _.extend( {}, defaults, options );
46
+ this.settings.frame = _.extend( {}, defaults.frame, options.frame );
47
+
48
+ /**
49
+ * Initialize a media frame.
50
+ *
51
+ * @returns {wp.media.view.MediaFrame.Select}
52
+ */
53
+ this.frame = function() {
54
+ var frame = frames[ this.settings.frame.id ];
55
+
56
+ if ( frame ) {
57
+ frame.control = this;
58
+ return frame;
59
+ }
60
+
61
+ frame = wp.media({
62
+ title: this.settings.frame.title,
63
+ library: {
64
+ type: this.settings.mediaType
65
+ },
66
+ button: {
67
+ text: this.settings.frame.updateText
68
+ },
69
+ multiple: this.settings.frame.multiple
70
+ });
71
+
72
+ frame.control = this;
73
+ frames[ this.settings.frame.id ] = frame;
74
+
75
+ // Update the selected image in the media library based on the image in the control.
76
+ frame.on( 'open', function() {
77
+ var selection = this.get( 'library' ).get( 'selection' ),
78
+ attachment, ids;
79
+
80
+ if ( frame.control.$target.length ) {
81
+ ids = frame.control.$target.val();
82
+ // @todo Make sure the ids aren't already in the selection.
83
+ if ( ids && '' !== ids && -1 !== ids && '0' !== ids ) {
84
+ attachment = Attachment.get( ids );
85
+ attachment.fetch();
86
+ }
87
+ }
88
+
89
+ selection.reset( attachment ? [ attachment ] : [] );
90
+ });
91
+
92
+ // Update the control when an image is selected from the media library.
93
+ frame.state( 'library' ).on( 'select', function() {
94
+ var selection = this.get( 'selection' );
95
+ frame.control.setAttachments( selection );
96
+ frame.control.$el.trigger( 'selectionChange.simpleimagewidget', [ selection ] );
97
+ });
98
+
99
+ return frame;
100
+ };
101
+
102
+ /**
103
+ * Set the control's attachments.
104
+ *
105
+ * @param {Array} attachments An array of wp.media.model.Attachment objects.
106
+ */
107
+ this.setAttachments = function( attachments ) {
108
+ var prop = this.$el.data( 'return-property' ) || 'id';
109
+
110
+ // Insert the selected attachment ids into the target element.
111
+ if ( this.$target.length ) {
112
+ this.$target.val( attachments.pluck( prop ) ).trigger( 'change' );
113
+ }
114
+ };
115
+ };
116
+
117
+ _.extend( SimpleImageWidget, {
118
+ /**
119
+ * Retrieve a media selection control object.
120
+ *
121
+ * @param {Object} el HTML element.
122
+ *
123
+ * @returns {Control}
124
+ */
125
+ getControl: function( el ) {
126
+ var control, $control;
127
+
128
+ $control = $( el ).closest( '.simple-image-widget-control' );
129
+ control = $control.data( 'media-control' );
130
+
131
+ if ( ! control ) {
132
+ control = new Control( $control );
133
+ $control.data( 'media-control', control );
134
+ }
135
+
136
+ return control;
137
+ },
138
+
139
+ /**
140
+ * Update a dropdown field with size options.
141
+ *
142
+ * @param {Object} field Dropdown field element.
143
+ * @param {Array} sizes
144
+ */
145
+ updateSizeDropdownOptions: function( field, sizes ) {
146
+ var $field = field,
147
+ currentValue, name, options;
148
+
149
+ if ( ! ( $field instanceof $ ) ) {
150
+ $field = $( $field );
151
+ }
152
+
153
+ if ( sizes ) {
154
+ _.each( sizes, function( size, key ) {
155
+ var name = l10n.imageSizeNames[ key ] || '';
156
+ options += '<option value="' + key + '">' + name + ' (' + size.width + '&times;' + size.height + ')</option>';
157
+ });
158
+ }
159
+
160
+ if ( ! options ) {
161
+ name = l10n.imageSizeNames['full'] || l10n.fullSizeLabel;
162
+ options = '<option value="full">' + name + '</option>';
163
+ }
164
+
165
+ // Try to maintain the previously selected size if it still exists.
166
+ currentValue = $field.val();
167
+ $field.html( options ).val( currentValue ).removeAttr( 'disabled' );
168
+ }
169
+ });
170
+
171
+ // Document ready.
172
+ jQuery(function( $ ) {
173
+ var $body = $( 'body' );
174
+
175
+ // Open the media frame when the choose button or image are clicked.
176
+ $body.on( 'click', '.simple-image-widget-control-choose, .simple-image-widget-form img', function( e ) {
177
+ e.preventDefault();
178
+ SimpleImageWidget.getControl( this ).frame().open();
179
+ });
180
+
181
+ // Update the image preview and size dropdown in a widget when an image is selected.
182
+ $body.on( 'selectionChange.simpleimagewidget', '.simple-image-widget-control', function( e, selection ) {
183
+ var $control = $( e.target ),
184
+ $sizeField = $control.closest( '.simple-image-widget-form' ).find( 'select.image-size' ),
185
+ model = selection.first(),
186
+ sizes = model.get( 'sizes' ),
187
+ size, image;
188
+
189
+ if ( sizes ) {
190
+ size = sizes['post-thumbnail'] || sizes.medium;
191
+ }
192
+
193
+ if ( $sizeField.length ) {
194
+ SimpleImageWidget.updateSizeDropdownOptions( $sizeField, sizes );
195
+ }
196
+
197
+ size = size || model.toJSON();
198
+ image = $( '<img />', { src: size.url });
199
+
200
+ $control.find( 'img' ).remove().end()
201
+ .prepend( image )
202
+ .addClass( 'has-image' )
203
+ .find( 'a.simple-image-widget-control-choose' ).removeClass( 'button-hero' );
204
+ });
205
+ });
206
+ })( this, jQuery, _, wp );
assets/styles/simple-image-widget.css ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control {
2
+ border: 1px dashed #aaa;
3
+ padding: 20px 0;
4
+ text-align: center;
5
+ }
6
+
7
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control.has-image {
8
+ border: 1px dashed #aaa;
9
+ padding: 10px;
10
+ text-align: left;
11
+ }
12
+
13
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control img {
14
+ display: block;
15
+ height: auto;
16
+ margin-bottom: 10px;
17
+ max-width: 100%;
18
+ }
19
+
20
+ .simple-image-widget-legacy-fields {
21
+ margin-bottom: 1em;
22
+ padding: 10px;
23
+ background-color: #e0e0e0;
24
+ border-radius: 3px;
25
+ }
26
+
27
+ .simple-image-widget-legacy-fields p:last-child {
28
+ margin-bottom: 0;
29
+ }
class-simple-image-widget.php DELETED
@@ -1,398 +0,0 @@
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
- $instance['link_open'] = '';
97
- $instance['link_close'] = '';
98
- if ( ! empty ( $instance['link'] ) ) {
99
- $target = ( empty( $instance['new_window'] ) ) ? '' : ' target="_blank"';
100
- $instance['link_open'] = '<a href="' . esc_url( $instance['link'] ) . '"' . $target . '>';
101
- $instance['link_close'] = '</a>';
102
- }
103
-
104
- $output = $args['before_widget'];
105
-
106
- // Allow custom output to override the default HTML.
107
- if ( $inside = apply_filters( 'simple_image_widget_output', '', $args, $instance, $this->id_base ) ) {
108
- $output .= $inside;
109
- } else {
110
- $output .= ( empty( $instance['title'] ) ) ? '' : $args['before_title']. $instance['title'] . $args['after_title'];
111
-
112
- // Add the image.
113
- if ( ! empty( $instance['image_id'] ) ) {
114
- $image_size = ( ! empty( $instance['image_size'] ) ) ? $instance['image_size'] : apply_filters( 'simple_image_widget_output_default_size', 'medium', $this->id_base );
115
-
116
- $output .= sprintf( '<p class="simple-image">%s%s%s</p>',
117
- $instance['link_open'],
118
- wp_get_attachment_image( $instance['image_id'], $image_size ),
119
- $instance['link_close']
120
- );
121
- } elseif ( ! empty( $instance['image'] ) ) {
122
- // Legacy output.
123
- $output .= sprintf( '%s<img src="%s" alt="%s">%s',
124
- $instance['link_open'],
125
- esc_url( $instance['image'] ),
126
- ( empty( $instance['alt'] ) ) ? '' : esc_attr( $instance['alt'] ),
127
- $instance['link_close']
128
- );
129
- }
130
-
131
- // Add the text.
132
- if ( ! empty( $instance['text'] ) ) {
133
- $output .= apply_filters( 'the_content', $instance['text'] );
134
- }
135
-
136
- // Add a more link.
137
- if ( ! empty( $instance['link_open'] ) && ! empty( $instance['link_text'] ) ) {
138
- $output .= '<p class="more">' . $instance['link_open'] . $instance['link_text'] . $instance['link_close'] . '</p>';
139
- }
140
- }
141
-
142
- $output .= $args['after_widget'];
143
-
144
- return $output;
145
- }
146
-
147
- /**
148
- * Form for modifying widget settings.
149
- *
150
- * @since 3.0.0
151
- */
152
- function form( $instance ) {
153
- $instance = wp_parse_args( (array) $instance, array(
154
- 'alt' => '', // Legacy.
155
- 'image' => '', // Legacy URL field.
156
- 'image_id' => '',
157
- 'image_size' => 'full',
158
- 'link' => '',
159
- 'link_text' => '',
160
- 'new_window' => '',
161
- 'title' => '',
162
- 'text' => '',
163
- ) );
164
-
165
- $instance['image_id'] = absint( $instance['image_id'] );
166
- $instance['title'] = wp_strip_all_tags( $instance['title'] );
167
-
168
- $button_class = array( 'button', 'button-hero', 'simple-image-widget-control-choose' );
169
- $image_id = $instance['image_id'];
170
-
171
- // The order of fields can be modified, new fields can be registered, or existing fields can be removed here.
172
- $fields = (array) apply_filters( 'simple_image_widget_fields', $this->form_fields(), $this->id_base );
173
- ?>
174
-
175
- <div class="simple-image-widget-form">
176
-
177
- <?php do_action( 'simple_image_widget_form_before', $instance, $this->id_base ); ?>
178
-
179
- <p>
180
- <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'simple-image-widget' ); ?></label>
181
- <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">
182
- </p>
183
-
184
- <?php if ( ! is_simple_image_widget_legacy() ) : ?>
185
- <p class="simple-image-widget-control<?php echo ( $image_id ) ? ' has-image' : ''; ?>"
186
- data-title="<?php esc_attr_e( 'Choose an Image for the Widget', 'simple-image-widget' ); ?>"
187
- data-update-text="<?php esc_attr_e( 'Update Image', 'simple-image-widget' ); ?>"
188
- data-target=".image-id">
189
- <?php
190
- if ( $image_id ) {
191
- echo wp_get_attachment_image( $image_id, 'medium', false );
192
- unset( $button_class[ array_search( 'button-hero', $button_class ) ] );
193
- }
194
- ?>
195
- <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">
196
- <a href="#" class="<?php echo join( ' ', $button_class ); ?>"><?php _e( 'Choose an Image', 'simple-image-widget' ); ?></a>
197
- </p>
198
- <?php endif; ?>
199
-
200
- <?php if ( is_simple_image_widget_legacy() || ! empty( $instance['image'] ) ) : ?>
201
- <div class="simple-image-widget-legacy-fields">
202
- <?php if ( ! is_simple_image_widget_legacy() ) : ?>
203
- <p>
204
- <em><?php _e( 'These fields are here to maintain your data from an earlier version.', 'simple-image-widget' ); ?></em>
205
- </p>
206
- <p>
207
- <em><?php _e( 'Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget' ); ?></em>
208
- </p>
209
- <?php endif; ?>
210
-
211
- <p>
212
- <label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e( 'Image URL:', 'simple-image-widget' ); ?></label>
213
- <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">
214
- </p>
215
- <p>
216
- <label for="<?php echo $this->get_field_id( 'alt' ); ?>"><?php _e( 'Alternate Text:', 'simple-image-widget' ); ?></label>
217
- <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">
218
- </p>
219
- </div>
220
- <?php endif; ?>
221
-
222
- <?php
223
- if ( ! empty( $fields ) ) {
224
- foreach ( $fields as $field ) {
225
- switch ( $field ) {
226
- case 'image_size' :
227
- $sizes = $this->get_image_sizes( $image_id );
228
- ?>
229
- <p>
230
- <label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
231
- <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"' : ''; ?>>
232
- <?php
233
- foreach ( $sizes as $id => $label ) {
234
- printf( '<option value="%s"%s>%s</option>',
235
- esc_attr( $id ),
236
- selected( $instance['image_size'], $id, false ),
237
- esc_html( $label )
238
- );
239
- }
240
- ?>
241
- </select>
242
- </p>
243
- <?php
244
- break;
245
-
246
- case 'link' :
247
- ?>
248
- <p style="margin-bottom: 0.25em">
249
- <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
250
- <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">
251
- </p>
252
- <p style="padding-left: 2px">
253
- <label for="<?php echo $this->get_field_id( 'new_window' ); ?>">
254
- <input type="checkbox" name="<?php echo $this->get_field_name( 'new_window' ); ?>" id="<?php echo $this->get_field_id( 'new_window' ); ?>" <?php checked( $instance['new_window'] ); ?>>
255
- <?php _e( 'Open in new window?', 'simple-image-widget' ); ?>
256
- </label>
257
- </p>
258
- <?php
259
- break;
260
-
261
- case 'link_text' :
262
- ?>
263
- <p>
264
- <label for="<?php echo $this->get_field_id( 'link_text' ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
265
- <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">
266
- </p>
267
- <?php
268
- break;
269
-
270
- case 'text' :
271
- ?>
272
- <p>
273
- <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
274
- <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>
275
- </p>
276
- <?php
277
- break;
278
-
279
- default :
280
- // Custom fields can be added using this action.
281
- do_action( 'simple_image_widget_field-' . sanitize_key( $field ), $instance, $this );
282
- }
283
- }
284
- }
285
-
286
- do_action( 'simple_image_widget_form_after', $instance, $this->id_base );
287
- ?>
288
-
289
- </div>
290
- <?php
291
- }
292
-
293
- /**
294
- * The list of extra fields that should be shown in the widget form.
295
- *
296
- * Can be easily overloaded by a child class.
297
- *
298
- * @since 3.0.0
299
- */
300
- function form_fields() {
301
- $fields = array( 'link', 'link_text', 'text' );
302
-
303
- // Don't show the image size field for users with older WordPress versions.
304
- if ( ! is_simple_image_widget_legacy() ) {
305
- array_unshift( $fields, 'image_size' );
306
- }
307
-
308
- return $fields;
309
- }
310
-
311
- /**
312
- * Save widget settings.
313
- *
314
- * @since 3.0.0
315
- */
316
- function update( $new_instance, $old_instance ) {
317
- $instance = wp_parse_args( $new_instance, $old_instance );
318
-
319
- $instance = apply_filters( 'simple_image_widget_instance', $instance, $new_instance, $old_instance, $this->id_base );
320
-
321
- $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
322
- $instance['image_id'] = absint( $new_instance['image_id'] );
323
- $instance['link'] = esc_url_raw( $new_instance['link'] );
324
- $instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
325
- $instance['new_window'] = isset( $new_instance['new_window'] );
326
- $instance['text'] = wp_kses_data( $new_instance['text'] );
327
-
328
- $instance['image'] = esc_url_raw( $new_instance['image'] ); // Legacy image URL.
329
- if ( empty( $instance['image'] ) ) {
330
- unset( $instance['image'] );
331
- }
332
-
333
- $instance['alt'] = wp_strip_all_tags( $instance['alt'] ); // Legacy alt text.
334
- if ( empty( $instance['alt'] ) ) {
335
- unset( $instance['alt'] );
336
- }
337
-
338
- $this->flush_widget_cache();
339
-
340
- return $instance;
341
- }
342
-
343
- /**
344
- * Get the various sizes of an images.
345
- *
346
- * @since 3.0.0
347
- *
348
- * @param int $image_id Image attachment ID.
349
- * @return array List of image size keys and their localized labels.
350
- */
351
- function get_image_sizes( $image_id ) {
352
- $sizes = array( 'full' => __( 'Full Size', 'simple-image-widget' ) );
353
-
354
- $imagedata = wp_get_attachment_metadata( $image_id );
355
- if ( isset( $imagedata['sizes'] ) ) {
356
- $size_names = Simple_Image_Widget_Loader::get_image_size_names();
357
-
358
- $sizes['full'] .= ( isset( $imagedata['width'] ) && isset( $imagedata['height'] ) ) ? sprintf( ' (%d&times;%d)', $imagedata['width'], $imagedata['height'] ) : '';
359
-
360
- foreach( $imagedata['sizes'] as $_size => $data ) {
361
- $label = ( isset( $size_names[ $_size ] ) ) ? $size_names[ $_size ] : ucwords( $_size );
362
- $label .= sprintf( ' (%d&times;%d)', $data['width'], $data['height'] );
363
-
364
- $sizes[ $_size ] = $label;
365
- }
366
- }
367
-
368
- return $sizes;
369
- }
370
-
371
- /**
372
- * Remove a single image widget from the cache.
373
- *
374
- * @since 3.0.0
375
- */
376
- function flush_widget_cache() {
377
- $cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
378
-
379
- if ( isset( $cache[ $this->id ] ) ) {
380
- unset( $cache[ $this->id ] );
381
- }
382
-
383
- wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
384
- }
385
-
386
- /**
387
- * Flush the cache for all image widgets.
388
- *
389
- * @since 3.0.0
390
- */
391
- function flush_group_cache( $post_id = null ) {
392
- if ( 'save_post' == current_filter() && 'attachment' != get_post_type( $post_id ) ) {
393
- return;
394
- }
395
-
396
- wp_cache_delete( 'simple_image_widget', 'widget' );
397
- }
398
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-simple-image-widget-legacy.php ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Legacy support.
4
+ *
5
+ * @package SimpleImageWidget
6
+ * @copyright Copyright (c) 2014, Blazer Six, Inc.
7
+ * @license GPL-2.0+
8
+ * @since 4.0.0
9
+ */
10
+
11
+ /**
12
+ * Class to add support for features and data from previous versions.
13
+ *
14
+ * @package SimpleImageWidget
15
+ * @since 4.0.0
16
+ */
17
+ class Simple_Image_Widget_Legacy {
18
+ /**
19
+ * Load legacy support.
20
+ *
21
+ * @since 4.0.0
22
+ */
23
+ public function load() {
24
+ add_filter( 'simple_image_widget_output', array( $this, 'output' ), 10, 4 );
25
+ add_filter( 'simple_image_widget_fields', array( $this, 'fields' ), 10, 2 );
26
+ add_action( 'simple_image_widget_field-legacy', array( $this, 'display_fields' ), 10, 2 );
27
+ add_filter( 'simple_image_widget_instance', array( $this, 'sanitize_data' ), 10, 4 );
28
+ }
29
+
30
+ /**
31
+ * Legacy widget output.
32
+ *
33
+ * @since 4.0.0
34
+ *
35
+ * @param string $output HTML output.
36
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
37
+ * @param array $instance The widget instance settings.
38
+ * @param string $id_base Base widget type id.
39
+ * @return string HTML output.
40
+ */
41
+ public function output( $output, $args, $instance, $id_base ) {
42
+ if ( 'simpleimage' != $id_base || ! empty( $instance['image_id'] ) || empty( $instance['image'] ) ) {
43
+ return $output;
44
+ }
45
+
46
+ // Legacy output.
47
+ $output = ( empty( $instance['title'] ) ) ? '' : $args['before_title'] . $instance['title'] . $args['after_title'];
48
+
49
+ // Add the image.
50
+ $output = sprintf(
51
+ '%s<img src="%s" alt="%s">%s',
52
+ $instance['link_open'],
53
+ esc_url( $instance['image'] ),
54
+ ( empty( $instance['alt'] ) ) ? '' : esc_attr( $instance['alt'] ),
55
+ $instance['link_close']
56
+ );
57
+
58
+ // Add the text.
59
+ if ( ! empty( $instance['text'] ) ) {
60
+ $output .= apply_filters( 'the_content', $instance['text'] );
61
+ }
62
+
63
+ // Add a more link.
64
+ if ( ! empty( $instance['link_open'] ) && ! empty( $instance['link_text'] ) ) {
65
+ $output .= '<p class="more">' . $instance['link_open'] . $instance['link_text'] . $instance['link_close'] . '</p>';
66
+ }
67
+
68
+ return $output;
69
+ }
70
+
71
+ /**
72
+ * Remove the image size field for versions of WordPress older than 3.5.
73
+ *
74
+ * @since 4.0.0
75
+ *
76
+ * @param array $fields List of field ids.
77
+ * @param string $id_base Base widget type id.
78
+ * @return array
79
+ */
80
+ public function fields( $fields, $id_base ) {
81
+ if ( 'simpleimage' == $id_base && is_simple_image_widget_legacy() ) {
82
+ $key = array_search( 'image_size', $fields );
83
+ if ( false !== $key ) {
84
+ unset( $fields[ $key ] );
85
+ }
86
+
87
+ // Add a field for the old widget stuff.
88
+ array_unshift( $fields, 'legacy' );
89
+ }
90
+
91
+ return $fields;
92
+ }
93
+
94
+ /**
95
+ * Display legacy fields in the widget edit form.
96
+ *
97
+ * @since 4.0.0
98
+ *
99
+ * @param array $instance The widget instance settings.
100
+ * @param WP_Widget $widget Widget instance.
101
+ */
102
+ public function display_fields( $instance, $widget ) {
103
+ if ( is_simple_image_widget_legacy() || ! empty( $instance['image'] ) ) :
104
+ ?>
105
+ <div class="simple-image-widget-legacy-fields">
106
+ <?php if ( ! is_simple_image_widget_legacy() ) : ?>
107
+ <p>
108
+ <em><?php _e( 'These fields are here to maintain your data from an earlier version.', 'simple-image-widget' ); ?></em>
109
+ </p>
110
+ <p>
111
+ <em><?php _e( 'Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget' ); ?></em>
112
+ </p>
113
+ <?php endif; ?>
114
+
115
+ <p>
116
+ <label for="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>"><?php _e( 'Image URL:', 'simple-image-widget' ); ?></label>
117
+ <input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'image' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>" value="<?php echo esc_url( $instance['image'] ); ?>" class="widefat">
118
+ </p>
119
+ <p>
120
+ <label for="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>"><?php _e( 'Alternate Text:', 'simple-image-widget' ); ?></label>
121
+ <input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'alt' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>" value="<?php echo esc_attr( $instance['alt'] ); ?>" class="widefat">
122
+ </p>
123
+ </div>
124
+ <?php
125
+ endif;
126
+ }
127
+
128
+ /**
129
+ * Sanitize legacy field values.
130
+ *
131
+ * Called in Simple_Image_Widget::update().
132
+ *
133
+ * @since 4.0.0
134
+ *
135
+ * @param array $instance Merged widget settings.
136
+ * @param array $new_instance New widget settings.
137
+ * @param array $old_instance Previous widget settings.
138
+ * @param string $id_base Base widget type id.
139
+ * @return array Sanitized settings.
140
+ */
141
+ public function sanitize_data( $instance, $new_instance, $old_instance, $id_base ) {
142
+ if ( 'simpleimage' == $id_base ) {
143
+ // Legacy image URL.
144
+ $instance['image'] = empty( $new_instance['image'] ) ? '' : esc_url_raw( $new_instance['image'] );
145
+ if ( empty( $instance['image'] ) ) {
146
+ unset( $instance['image'] );
147
+ }
148
+
149
+ // Legacy alt text.
150
+ $instance['alt'] = empty( $new_instance['alt'] ) ? '' : wp_strip_all_tags( $instance['alt'] );
151
+ if ( empty( $instance['alt'] ) ) {
152
+ unset( $instance['alt'] );
153
+ }
154
+ }
155
+
156
+ return $instance;
157
+ }
158
+ }
includes/class-simple-image-widget-plugin.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Simple Image Widget
4
+ *
5
+ * @package SimpleImageWidget
6
+ * @copyright Copyright (c) 2014, Blazer Six, Inc.
7
+ * @license GPL-2.0+
8
+ * @since 3.0.0
9
+ */
10
+
11
+ /**
12
+ * The main plugin class for loading the widget and attaching hooks.
13
+ *
14
+ * @package SimpleImageWidget
15
+ * @since 3.0.0
16
+ */
17
+ class Simple_Image_Widget_Plugin {
18
+ /**
19
+ * Set up the widget.
20
+ *
21
+ * @since 3.0.0
22
+ */
23
+ public function load() {
24
+ self::load_textdomain();
25
+ add_action( 'widgets_init', array( $this, 'register_widget' ) );
26
+
27
+ $compat = new Simple_Image_Widget_Legacy();
28
+ $compat->load();
29
+
30
+ if ( is_simple_image_widget_legacy() ) {
31
+ return;
32
+ }
33
+
34
+ add_action( 'init', array( $this, 'register_assets' ) );
35
+ add_action( 'sidebar_admin_setup', array( $this, 'enqueue_admin_assets' ) );
36
+ }
37
+
38
+ /**
39
+ * Localize the plugin strings.
40
+ *
41
+ * @since 3.0.0
42
+ */
43
+ public function load_textdomain() {
44
+ load_plugin_textdomain( 'simple-image-widget', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages' );
45
+ }
46
+
47
+ /**
48
+ * Register the image widget.
49
+ *
50
+ * @since 3.0.0
51
+ */
52
+ public function register_widget() {
53
+ register_widget( 'Simple_Image_Widget' );
54
+ }
55
+
56
+ /**
57
+ * Register and localize generic scripts and styles.
58
+ *
59
+ * A preliminary attempt has been made to abstract the
60
+ * 'simple-image-widget-control' script a bit in order to allow it to be
61
+ * re-used anywhere a similiar media selection feature is needed.
62
+ *
63
+ * Custom image size labels need to be added using the
64
+ * 'image_size_names_choose' filter.
65
+ *
66
+ * @since 3.0.0
67
+ */
68
+ public function register_assets() {
69
+ wp_register_style(
70
+ 'simple-image-widget-admin',
71
+ dirname( plugin_dir_url( __FILE__ ) ) . '/assets/styles/simple-image-widget.css'
72
+ );
73
+
74
+ wp_register_script(
75
+ 'simple-image-widget-admin',
76
+ dirname( plugin_dir_url( __FILE__ ) ) . '/assets/scripts/simple-image-widget.js',
77
+ array( 'media-upload', 'media-views' )
78
+ );
79
+
80
+ wp_localize_script(
81
+ 'simple-image-widget-admin',
82
+ 'SimpleImageWidget',
83
+ array(
84
+ 'l10n' => array(
85
+ 'frameTitle' => __( 'Choose an Attachment', 'simple-image-widget' ),
86
+ 'frameUpdateText' => __( 'Update Attachment', 'simple-image-widget' ),
87
+ 'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
88
+ 'imageSizeNames' => self::get_image_size_names(),
89
+ ),
90
+ )
91
+ );
92
+ }
93
+
94
+ /**
95
+ * Enqueue scripts needed for selecting media.
96
+ *
97
+ * @since 3.0.0
98
+ *
99
+ * @param string $hook_suffix Screen id.
100
+ */
101
+ public function enqueue_admin_assets() {
102
+ wp_enqueue_media();
103
+ wp_enqueue_script( 'simple-image-widget-admin' );
104
+ wp_enqueue_style( 'simple-image-widget-admin' );
105
+ }
106
+
107
+ /**
108
+ * Get localized image size names.
109
+ *
110
+ * The 'image_size_names_choose' filter exists in core and should be
111
+ * hooked by plugin authors to provide localized labels for custom image
112
+ * sizes added using add_image_size().
113
+ *
114
+ * @see image_size_input_fields()
115
+ * @see http://core.trac.wordpress.org/ticket/20663
116
+ *
117
+ * @since 3.0.0
118
+ *
119
+ * @return array Array of thumbnail sizes.
120
+ */
121
+ public static function get_image_size_names() {
122
+ return apply_filters(
123
+ 'image_size_names_choose',
124
+ array(
125
+ 'thumbnail' => __( 'Thumbnail', 'simple-image-widget' ),
126
+ 'medium' => __( 'Medium', 'simple-image-widget' ),
127
+ 'large' => __( 'Large', 'simple-image-widget' ),
128
+ 'full' => __( 'Full Size', 'simple-image-widget' ),
129
+ )
130
+ );
131
+ }
132
+ }
includes/class-simple-image-widget-template-loader.php ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Template loader.
4
+ *
5
+ * Based off version 1.1.0 of the Gamajo Template Loader by Gary Jones. Changes
6
+ * allow for overriding class properties during instantiation and adding a
7
+ * load_template() method that accepts arbitrary data to extra into the local
8
+ * template scope.
9
+ *
10
+ * @package SimpleImageWidget
11
+ * @since 4.0.0
12
+ * @author Gary Jones
13
+ * @link http://github.com/GaryJones/Gamajo-Template-Loader
14
+ * @copyright 2013 Gary Jones
15
+ * @license GPL-2.0+
16
+ */
17
+
18
+ /**
19
+ * Template loader.
20
+ *
21
+ * @package SimpleImageWidget
22
+ * @since 4.0.0
23
+ * @author Gary Jones
24
+ */
25
+ class Simple_Image_Widget_Template_Loader {
26
+ /**
27
+ * Prefix for filter names.
28
+ *
29
+ * @since 4.0.0
30
+ *
31
+ * @type string
32
+ */
33
+ protected $filter_prefix = 'simple_image_widget';
34
+
35
+ /**
36
+ * Directory name where custom templates for this plugin should be found in
37
+ * the theme.
38
+ *
39
+ * @since 4.0.0
40
+ *
41
+ * @type string
42
+ */
43
+ protected $theme_template_directory = 'simple-image-widget';
44
+
45
+ /**
46
+ * Reference to the root directory path of this plugin.
47
+ *
48
+ * @since 4.0.0
49
+ *
50
+ * @type string
51
+ */
52
+ protected $plugin_directory = SIW_DIR;
53
+
54
+ /**
55
+ * Directory name where templates are found in this plugin.
56
+ *
57
+ * @since 4.0.0
58
+ *
59
+ * @type string
60
+ */
61
+ protected $plugin_template_directory = 'templates';
62
+
63
+ /**
64
+ * Contructor method to set up the loader.
65
+ *
66
+ * Accepts an array of class properties when instantiated to override the
67
+ * defaults.
68
+ *
69
+ * @since 4.0.0
70
+ *
71
+ * @param array $args List of class properties.
72
+ */
73
+ public function __construct( $args = array() ) {
74
+ $keys = array_keys( get_object_vars( $this ) );
75
+ foreach ( $keys as $key ) {
76
+ if ( isset( $args[ $key ] ) ) {
77
+ $this->$key = $args[ $key ];
78
+ }
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Retrieve a template part.
84
+ *
85
+ * @since 4.0.0
86
+ *
87
+ * @uses Simple_Image_Widget_Template_Loader::get_template_possble_parts()
88
+ * Create file names of templates.
89
+ * @uses Simple_Image_Widget_Template_Loader::locate_template() Retrieve the
90
+ * name of the highest priority template file that exists.
91
+ *
92
+ * @param string $slug
93
+ * @param string $name Optional. Default null.
94
+ * @param bool $load Optional. Default true.
95
+ * @return string
96
+ */
97
+ public function get_template_part( $slug, $name = null, $load = true ) {
98
+ /**
99
+ * Execute code for this part.
100
+ *
101
+ * @since 4.0.0
102
+ *
103
+ * @param string $slug
104
+ * @param string $name
105
+ */
106
+ do_action( 'get_template_part_' . $slug, $slug, $name );
107
+
108
+ // Get files names of templates, for given slug and name.
109
+ $templates = $this->get_template_file_names( $slug, $name );
110
+
111
+ // Return the part that is found.
112
+ return $this->locate_template( $templates, $load, false );
113
+ }
114
+
115
+ /**
116
+ * Given a slug and optional name, create the file names of templates.
117
+ *
118
+ * @since 4.0.0
119
+ *
120
+ * @param string $slug
121
+ * @param string $name
122
+ * @return array
123
+ */
124
+ protected function get_template_file_names( $slug, $name ) {
125
+ $templates = array();
126
+ if ( isset( $name ) ) {
127
+ $templates[] = $slug . '-' . $name . '.php';
128
+ }
129
+ $templates[] = $slug . '.php';
130
+
131
+ /**
132
+ * Allow template choices to be filtered.
133
+ *
134
+ * The resulting array should be in the order of most specific first, to
135
+ * least specific last.
136
+ * e.g. 0 => recipe-instructions.php, 1 => recipe.php
137
+ *
138
+ * @since 4.0.0
139
+ *
140
+ * @param array $templates Names of template files that should be looked for, for given slug and name.
141
+ * @param string $slug Template slug.
142
+ * @param string $name Template name.
143
+ */
144
+ return apply_filters( $this->filter_prefix . '_get_template_part', $templates, $slug, $name );
145
+ }
146
+
147
+ /**
148
+ * Retrieve the name of the highest priority template file that exists.
149
+ *
150
+ * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
151
+ * inherit from a parent theme can just overload one file. If the template is
152
+ * not found in either of those, it looks in the theme-compat folder last.
153
+ *
154
+ * @since 4.0.0
155
+ *
156
+ * @uses Simple_Image_Widget_Template_Loader::get_template_paths() Return a
157
+ * list of paths to check for template locations.
158
+ *
159
+ * @param string|array $template_names Template file(s) to search for, in order.
160
+ * @param bool $load If true the template file will be loaded if it is found.
161
+ * @param bool $require_once Whether to require_once or require. Default true.
162
+ * Has no effect if $load is false.
163
+ * @return string The template filename if one is located.
164
+ */
165
+ public function locate_template( $template_names, $load = false, $require_once = true ) {
166
+ // No file found yet.
167
+ $located = false;
168
+
169
+ // Remove empty entries.
170
+ $template_names = array_filter( (array) $template_names );
171
+ $template_paths = $this->get_template_paths();
172
+
173
+ // Try to find a template file.
174
+ foreach ( $template_names as $template_name ) {
175
+ // Trim off any slashes from the template name.
176
+ $template_name = ltrim( $template_name, '/' );
177
+
178
+ // Try locating this template file by looping through the template paths.
179
+ foreach ( $template_paths as $template_path ) {
180
+ if ( file_exists( $template_path . $template_name ) ) {
181
+ $located = $template_path . $template_name;
182
+ break 2;
183
+ }
184
+ }
185
+ }
186
+
187
+ if ( $load && $located ) {
188
+ load_template( $located, $require_once );
189
+ }
190
+
191
+ return $located;
192
+ }
193
+
194
+ /**
195
+ * Load a template file.
196
+ *
197
+ * @since 4.0.0
198
+ *
199
+ * @param string $template_file Absolute path to a file or list of template parts.
200
+ * @param array $data Optional. List of variables to extract into the template scope.
201
+ */
202
+ public function load_template( $template_file, $data = array() ) {
203
+ global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
204
+
205
+ if ( is_array( $data ) && ! empty( $data ) ) {
206
+ extract( $data, EXTR_SKIP );
207
+ unset( $data );
208
+ }
209
+
210
+ if ( file_exists( $template_file ) ) {
211
+ require( $template_file );
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Return a list of paths to check for template locations.
217
+ *
218
+ * Default is to check in a child theme (if relevant) before a parent theme,
219
+ * so that themes which inherit from a parent theme can just overload one
220
+ * file. If the template is not found in either of those, it looks in the
221
+ * theme-compat folder last.
222
+ *
223
+ * @since 4.0.0
224
+ *
225
+ * @return mixed|void
226
+ */
227
+ protected function get_template_paths() {
228
+ $theme_directory = trailingslashit( $this->theme_template_directory );
229
+
230
+ $file_paths = array(
231
+ 10 => trailingslashit( get_template_directory() ) . $theme_directory,
232
+ 100 => $this->get_templates_dir(),
233
+ );
234
+
235
+ // Only add this conditionally, so non-child themes don't redundantly check active theme twice.
236
+ if ( is_child_theme() ) {
237
+ $file_paths[1] = trailingslashit( get_stylesheet_directory() ) . $theme_directory;
238
+ }
239
+
240
+ /**
241
+ * Allow ordered list of template paths to be amended.
242
+ *
243
+ * @since 4.0.0
244
+ *
245
+ * @param array $var Default is directory in child theme at index 1, parent theme at 10, and plugin at 100.
246
+ */
247
+ $file_paths = apply_filters( $this->filter_prefix . '_template_paths', $file_paths );
248
+
249
+ // sort the file paths based on priority
250
+ ksort( $file_paths, SORT_NUMERIC );
251
+
252
+ return array_map( 'trailingslashit', $file_paths );
253
+ }
254
+
255
+ /**
256
+ * Return the path to the templates directory in this plugin.
257
+ *
258
+ * @since 4.0.0
259
+ *
260
+ * @return string
261
+ */
262
+ protected function get_templates_dir() {
263
+ return trailingslashit( $this->plugin_directory ) . $this->plugin_template_directory;
264
+ }
265
+ }
includes/class-simple-image-widget.php ADDED
@@ -0,0 +1,478 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * The image widget.
4
+ *
5
+ * @package SimpleImageWidget
6
+ * @copyright Copyright (c) 2014, Blazer Six, Inc.
7
+ * @license GPL-2.0+
8
+ * @since 3.0.0
9
+ */
10
+
11
+ /**
12
+ * Image widget class.
13
+ *
14
+ * @package SimpleImageWidget
15
+ * @since 3.0.0
16
+ */
17
+ class Simple_Image_Widget extends WP_Widget {
18
+ /**
19
+ * Setup widget options.
20
+ *
21
+ * Child classes may override the defaults.
22
+ *
23
+ * @since 3.0.0
24
+ * @see WP_Widget::construct()
25
+ *
26
+ * @param string $id_base Optional Base ID for the widget, lower case, if
27
+ * left empty a portion of the widget's class name will be used. Must be unique.
28
+ * @param string $name Name for the widget displayed on the configuration page.
29
+ * @param array $widget_options {
30
+ * Widget options. Passed to wp_register_sidebar_widget(). Optional.
31
+ *
32
+ * @type string $description Widget description. Shown on the configuration page.
33
+ * @type string $classname HTML class.
34
+ * }
35
+ * @param array $control_options {
36
+ * Passed to wp_register_widget_control(). Optional.
37
+ *
38
+ * @type int $width Width of the widget edit form.
39
+ * )
40
+ */
41
+ public function __construct( $id_base = false, $name = false, $widget_options = array(), $control_options = array() ) {
42
+ $id_base = ( $id_base ) ? $id_base : 'simpleimage'; // Legacy ID.
43
+ $name = ( $name ) ? $name : __( 'Image', 'simple-image-widget' );
44
+
45
+ $widget_options = wp_parse_args(
46
+ $widget_options,
47
+ array(
48
+ 'classname' => 'widget_simpleimage', // Legacy class name.
49
+ 'description' => __( 'An image from your Media Library.', 'simple-image-widget' ),
50
+ 'customizer_support' => true,
51
+ )
52
+ );
53
+
54
+ $control_options = wp_parse_args( $control_options, array() );
55
+
56
+ parent::__construct( $id_base, $name, $widget_options, $control_options );
57
+
58
+ // Flush widget group cache when an attachment is saved, deleted, or the theme is switched.
59
+ add_action( 'save_post', array( $this, 'flush_group_cache' ) );
60
+ add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) );
61
+ add_action( 'switch_theme', array( $this, 'flush_group_cache' ) );
62
+ }
63
+
64
+ /**
65
+ * Display the widget.
66
+ *
67
+ * Filters the instance data, fetches the output, displays it, then caches
68
+ * it. Overload or filter the render() method to modify output.
69
+ *
70
+ * @since 3.0.0
71
+ *
72
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
73
+ * @param array $instance The widget instance settings.
74
+ */
75
+ public function widget( $args, $instance ) {
76
+ $cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
77
+
78
+ if ( isset( $cache[ $this->id ] ) ) {
79
+ echo $cache[ $this->id ];
80
+ return;
81
+ }
82
+
83
+ // Copy the original values so they can be used in hooks.
84
+ $instance['text_raw'] = $instance['text'];
85
+ $instance['title_raw'] = $instance['title'];
86
+ $instance['text'] = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance, $this->id_base );
87
+ $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
88
+
89
+ // Start building the output.
90
+ $output = '';
91
+
92
+ // Make sure the image ID is a valid attachment.
93
+ if ( ! empty( $instance['image_id'] ) ) {
94
+ $image = get_post( $instance['image_id'] );
95
+ if ( ! $image || 'attachment' != get_post_type( $image ) ) {
96
+ $output = '<!-- Image Widget Error: Invalid Attachment ID -->';
97
+ }
98
+ }
99
+
100
+ if ( empty( $output ) ) {
101
+ $instance['link_open'] = '';
102
+ $instance['link_close'] = '';
103
+
104
+ if ( ! empty ( $instance['link'] ) ) {
105
+ $target = ( empty( $instance['new_window'] ) ) ? '' : ' target="_blank"';
106
+
107
+ $instance['link_open'] = '<a href="' . esc_url( $instance['link'] ) . '"' . $target . '>';
108
+ $instance['link_close'] = '</a>';
109
+ }
110
+
111
+ $output = $this->render( $args, $instance );
112
+ }
113
+
114
+ echo $output;
115
+
116
+ $cache[ $this->id ] = $output;
117
+ wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
118
+ }
119
+
120
+ /**
121
+ * Generate the widget output.
122
+ *
123
+ * This is typically done in the widget() method, but moving it to a
124
+ * separate method allows for the routine to be easily overloaded by a class
125
+ * extending this one without having to reimplement all the caching and
126
+ * filtering, or resorting to adding a filter, calling the parent method,
127
+ * then removing the filter.
128
+ *
129
+ * @since 3.0.0
130
+ *
131
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
132
+ * @param array $instance The widget instance settings.
133
+ * @return string HTML output.
134
+ */
135
+ public function render( $args, $instance ) {
136
+ $output = $args['before_widget'];
137
+
138
+ /**
139
+ * Widget HTML output.
140
+ *
141
+ * @since 3.0.0
142
+ *
143
+ * @param string $output Widget output.
144
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
145
+ * @param array $instance The widget instance settings.
146
+ * @param string $id_base Widget type id.
147
+ */
148
+ $inside = apply_filters( 'simple_image_widget_output', '', $args, $instance, $this->id_base );
149
+
150
+ if ( $inside ) {
151
+ $output .= $inside;
152
+ } else {
153
+ $data = array();
154
+ $data['args'] = $args;
155
+ $data['after_title'] = $args['after_title'];
156
+ $data['before_title'] = $args['before_title'];
157
+ $data['image_size'] = $image_size = ( ! empty( $instance['image_size'] ) ) ? $instance['image_size'] : apply_filters( 'simple_image_widget_output_default_size', 'medium', $this->id_base );
158
+ $data['title'] = ( empty( $instance['title'] ) ) ? '' : $instance['title'];
159
+ $data = array_merge( $instance, $data );
160
+ $data = apply_filters( 'simple_image_widget_template_data', $data );
161
+
162
+ ob_start();
163
+ $templates = $this->get_template_names( $args, $instance );
164
+
165
+ $template_loader = new Simple_Image_Widget_Template_Loader();
166
+ $template = $template_loader->locate_template( $templates );
167
+ $template_loader->load_template( $template, $data );
168
+ $output .= ob_get_clean();
169
+ }
170
+
171
+ $output .= $args['after_widget'];
172
+
173
+ return $output;
174
+ }
175
+
176
+ /**
177
+ * Display the form to edit widget settings.
178
+ *
179
+ * @since 3.0.0
180
+ *
181
+ * @param array $instance The widget settings.
182
+ */
183
+ public function form( $instance ) {
184
+ $instance = wp_parse_args(
185
+ (array) $instance,
186
+ array(
187
+ 'alt' => '', // Legacy.
188
+ 'image' => '', // Legacy URL field.
189
+ 'image_id' => '',
190
+ 'image_size' => 'full',
191
+ 'link' => '',
192
+ 'link_text' => '',
193
+ 'new_window' => '',
194
+ 'title' => '',
195
+ 'text' => '',
196
+ )
197
+ );
198
+
199
+ $instance['image_id'] = absint( $instance['image_id'] );
200
+ $instance['title'] = wp_strip_all_tags( $instance['title'] );
201
+
202
+ $button_class = array( 'button', 'button-hero', 'simple-image-widget-control-choose' );
203
+ $image_id = $instance['image_id'];
204
+
205
+ /**
206
+ * The list of fields to display.
207
+ *
208
+ * The order of fields can be modified, new fields can be registered, or
209
+ * existing fields can be removed here. Use the widget type id to limit
210
+ * fields to a particular type of widget.
211
+ *
212
+ * @since 3.0.0
213
+ *
214
+ * @param array $fields List of field ids.
215
+ * @param string $id_base Widget type id.
216
+ */
217
+ $fields = (array) apply_filters( 'simple_image_widget_fields', $this->form_fields(), $this->id_base );
218
+ ?>
219
+
220
+ <div class="simple-image-widget-form">
221
+
222
+ <?php
223
+ /**
224
+ * Display additional information or HTML before the widget edit form.
225
+ *
226
+ * @since 3.0.0
227
+ *
228
+ * @param array $instance The widget setttings.
229
+ * @param string $id_base Widget type id.
230
+ */
231
+ do_action( 'simple_image_widget_form_before', $instance, $this->id_base );
232
+ ?>
233
+
234
+ <p>
235
+ <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'simple-image-widget' ); ?></label>
236
+ <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat">
237
+ </p>
238
+
239
+ <?php if ( ! is_simple_image_widget_legacy() ) : ?>
240
+ <p class="simple-image-widget-control<?php echo ( $image_id ) ? ' has-image' : ''; ?>"
241
+ data-title="<?php esc_attr_e( 'Choose an Image', 'simple-image-widget' ); ?>"
242
+ data-update-text="<?php esc_attr_e( 'Update Image', 'simple-image-widget' ); ?>"
243
+ data-target=".image-id">
244
+ <?php
245
+ if ( $image_id ) {
246
+ echo wp_get_attachment_image( $image_id, 'medium', false );
247
+ unset( $button_class[ array_search( 'button-hero', $button_class ) ] );
248
+ }
249
+ ?>
250
+ <input type="hidden" name="<?php echo esc_attr( $this->get_field_name( 'image_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>" value="<?php echo absint( $image_id ); ?>" class="image-id simple-image-widget-control-target">
251
+ <a href="#" class="<?php echo esc_attr( join( ' ', $button_class ) ); ?>"><?php _e( 'Choose an Image', 'simple-image-widget' ); ?></a>
252
+ </p>
253
+ <?php endif; ?>
254
+
255
+ <?php
256
+ if ( ! empty( $fields ) ) {
257
+ foreach ( $fields as $field ) {
258
+ switch ( $field ) {
259
+ case 'image_size' :
260
+ $sizes = $this->get_image_sizes( $image_id );
261
+ ?>
262
+ <p>
263
+ <label for="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
264
+ <select name="<?php echo esc_attr( $this->get_field_name( 'image_size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>" class="widefat image-size"<?php echo ( sizeof( $sizes ) < 2 ) ? ' disabled="disabled"' : ''; ?>>
265
+ <?php
266
+ foreach ( $sizes as $id => $label ) {
267
+ printf(
268
+ '<option value="%s"%s>%s</option>',
269
+ esc_attr( $id ),
270
+ selected( $instance['image_size'], $id, false ),
271
+ esc_html( $label )
272
+ );
273
+ }
274
+ ?>
275
+ </select>
276
+ </p>
277
+ <?php
278
+ break;
279
+
280
+ case 'link' :
281
+ ?>
282
+ <p style="margin-bottom: 0.25em">
283
+ <label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
284
+ <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" value="<?php echo esc_url( $instance['link'] ); ?>" class="widefat">
285
+ </p>
286
+ <p style="padding-left: 2px">
287
+ <label for="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>">
288
+ <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'new_window' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>" <?php checked( $instance['new_window'] ); ?>>
289
+ <?php _e( 'Open in new window?', 'simple-image-widget' ); ?>
290
+ </label>
291
+ </p>
292
+ <?php
293
+ break;
294
+
295
+ case 'link_text' :
296
+ ?>
297
+ <p>
298
+ <label for="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
299
+ <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>" value="<?php echo esc_attr( $instance['link_text'] ); ?>" class="widefat">
300
+ </p>
301
+ <?php
302
+ break;
303
+
304
+ case 'text' :
305
+ ?>
306
+ <p>
307
+ <label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
308
+ <textarea name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" rows="4" class="widefat"><?php echo esc_textarea( $instance['text'] ); ?></textarea>
309
+ </p>
310
+ <?php
311
+ break;
312
+
313
+ default :
314
+ /**
315
+ * Display a custom field.
316
+ *
317
+ * This action will fire for custom fields
318
+ * registered with the 'simple_image_widget_fields'
319
+ * filter.
320
+ *
321
+ * @since 3.0.0
322
+ *
323
+ * @param array $instance The widget setttings.
324
+ * @param string $widget Widget instance.
325
+ */
326
+ do_action( 'simple_image_widget_field-' . sanitize_key( $field ), $instance, $this );
327
+ }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Display additional information or HTML after the widget edit form.
333
+ *
334
+ * @since 3.0.0
335
+ *
336
+ * @param array $instance The widget setttings.
337
+ * @param string $id_base Widget type id.
338
+ */
339
+ do_action( 'simple_image_widget_form_after', $instance, $this->id_base );
340
+ ?>
341
+
342
+ </div><!-- /.simple-image-widget-form -->
343
+ <?php
344
+ }
345
+
346
+ /**
347
+ * The list of extra fields that should be shown in the widget form.
348
+ *
349
+ * Can be easily overloaded by a child class.
350
+ *
351
+ * @since 3.0.0
352
+ *
353
+ * @return string List of field ids.
354
+ */
355
+ public function form_fields() {
356
+ return array( 'image_size', 'link', 'link_text', 'text' );
357
+ }
358
+
359
+ /**
360
+ * Save and sanitize widget settings.
361
+ *
362
+ * @since 3.0.0
363
+ *
364
+ * @param array $new_instance New widget settings.
365
+ * @param array $old_instance Previous widget settings.
366
+ * @return array Sanitized settings.
367
+ */
368
+ public function update( $new_instance, $old_instance ) {
369
+ $instance = wp_parse_args( $new_instance, $old_instance );
370
+
371
+ $instance = apply_filters( 'simple_image_widget_instance', $instance, $new_instance, $old_instance, $this->id_base );
372
+
373
+ $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
374
+ $instance['image_id'] = absint( $new_instance['image_id'] );
375
+ $instance['link'] = esc_url_raw( $new_instance['link'] );
376
+ $instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
377
+ $instance['new_window'] = isset( $new_instance['new_window'] );
378
+ $instance['text'] = wp_kses_data( $new_instance['text'] );
379
+
380
+ $this->flush_widget_cache();
381
+
382
+ return $instance;
383
+ }
384
+
385
+ /**
386
+ * Get the various sizes of an image.
387
+ *
388
+ * @since 3.0.0
389
+ *
390
+ * @param int $image_id Image attachment ID.
391
+ * @return array List of image size keys and their localized labels.
392
+ */
393
+ public function get_image_sizes( $image_id ) {
394
+ $sizes = array( 'full' => __( 'Full Size', 'simple-image-widget' ) );
395
+
396
+ $imagedata = wp_get_attachment_metadata( $image_id );
397
+ if ( isset( $imagedata['sizes'] ) ) {
398
+ $size_names = Simple_Image_Widget_Plugin::get_image_size_names();
399
+
400
+ $sizes['full'] .= ( isset( $imagedata['width'] ) && isset( $imagedata['height'] ) ) ? sprintf( ' (%d&times;%d)', $imagedata['width'], $imagedata['height'] ) : '';
401
+
402
+ foreach ( $imagedata['sizes'] as $_size => $data ) {
403
+ $label = ( isset( $size_names[ $_size ] ) ) ? $size_names[ $_size ] : ucwords( $_size );
404
+ $label .= sprintf( ' (%d&times;%d)', $data['width'], $data['height'] );
405
+
406
+ $sizes[ $_size ] = $label;
407
+ }
408
+ }
409
+
410
+ return $sizes;
411
+ }
412
+
413
+ /**
414
+ * Remove a single image widget from the cache.
415
+ *
416
+ * @since 3.0.0
417
+ */
418
+ public function flush_widget_cache() {
419
+ $cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
420
+
421
+ if ( isset( $cache[ $this->id ] ) ) {
422
+ unset( $cache[ $this->id ] );
423
+ }
424
+
425
+ wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
426
+ }
427
+
428
+ /**
429
+ * Flush the cache for all image widgets.
430
+ *
431
+ * @since 3.0.0
432
+ *
433
+ * @param int $post_id Post ID.
434
+ */
435
+ public function flush_group_cache( $post_id = null ) {
436
+ if ( 'save_post' == current_filter() && 'attachment' != get_post_type( $post_id ) ) {
437
+ return;
438
+ }
439
+
440
+ wp_cache_delete( 'simple_image_widget', 'widget' );
441
+ }
442
+
443
+ /**
444
+ * Retrieve a list of templates to look up.
445
+ *
446
+ * @since 4.0.0
447
+ *
448
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
449
+ * @param array $instance The widget instance settings.
450
+ * @return array List of template names.
451
+ */
452
+ protected function get_template_names( $args, $instance ) {
453
+ $templates = array();
454
+ if ( ! empty( $args['id'] ) ) {
455
+ $templates[] = $args['id'] . '_widget.php';
456
+ }
457
+ $templates[] = 'widget.php';
458
+ /**
459
+ * List of template names to look up to render output.
460
+ *
461
+ * Child widgets should consider adding a new template using the widget type id ($this->id_base).
462
+ *
463
+ * @since 4.0.0
464
+ *
465
+ * @param array $templates List of template names.
466
+ * @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
467
+ * @param array $instance The widget instance settings.
468
+ * @param string $id_base Widget type id.
469
+ */
470
+ return apply_filters(
471
+ 'simple_image_widget_templates',
472
+ $templates,
473
+ $args,
474
+ $instance,
475
+ $this->id_base
476
+ );
477
+ }
478
+ }
js/simple-image-widget.js DELETED
@@ -1,113 +0,0 @@
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 CHANGED
@@ -1,103 +1,101 @@
1
- # Copyright (C) 2013 Simple Image Widget
2
  # This file is distributed under the same license as the Simple Image Widget package.
3
  msgid ""
4
  msgstr ""
5
  "Project-Id-Version: Simple Image Widget 3.0.4\n"
6
- "Report-Msgid-Bugs-To: http://wordpress.org/support/plugin/simple-image-"
7
- "widget\n"
8
- "POT-Creation-Date: 2013-10-26 20:24:57+00:00\n"
9
  "MIME-Version: 1.0\n"
10
- "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
- "PO-Revision-Date: 2013-MO-DA HO:MI+ZONE\n"
13
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
  "Language-Team: LANGUAGE <LL@li.org>\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:180
25
- msgid "Title:"
 
 
26
  msgstr ""
27
 
28
- #: class-simple-image-widget.php:186
29
- msgid "Choose an Image for the Widget"
30
  msgstr ""
31
 
32
- #: class-simple-image-widget.php:187
33
- msgid "Update Image"
34
  msgstr ""
35
 
36
- #: class-simple-image-widget.php:196
37
- msgid "Choose an Image"
38
  msgstr ""
39
 
40
- #: class-simple-image-widget.php:204
41
- msgid "These fields are here to maintain your data from an earlier version."
42
  msgstr ""
43
 
44
- #: class-simple-image-widget.php:207
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:212
51
- msgid "Image URL:"
52
  msgstr ""
53
 
54
- #: class-simple-image-widget.php:216
55
- msgid "Alternate Text:"
56
  msgstr ""
57
 
58
- #: class-simple-image-widget.php:230
59
- msgid "Size:"
60
  msgstr ""
61
 
62
- #: class-simple-image-widget.php:249
63
- msgid "Link:"
64
  msgstr ""
65
 
66
- #: class-simple-image-widget.php:255
67
- msgid "Open in new window?"
68
  msgstr ""
69
 
70
- #: class-simple-image-widget.php:264
71
- msgid "Link Text:"
72
  msgstr ""
73
 
74
- #: class-simple-image-widget.php:273
75
- msgid "Text:"
 
76
  msgstr ""
77
 
78
- #: class-simple-image-widget.php:352 simple-image-widget.php:79
79
- #: simple-image-widget.php:180
80
- msgid "Full Size"
81
  msgstr ""
82
 
83
- #: simple-image-widget.php:77
84
- msgid "Choose an Attachment"
85
  msgstr ""
86
 
87
- #: simple-image-widget.php:78
88
- msgid "Update Attachment"
89
  msgstr ""
90
 
91
- #: simple-image-widget.php:177
92
- msgid "Thumbnail"
93
  msgstr ""
94
 
95
- #: simple-image-widget.php:178
96
- msgid "Medium"
97
  msgstr ""
98
 
99
- #: simple-image-widget.php:179
100
- msgid "Large"
101
  msgstr ""
102
 
103
  #. Plugin Name of the plugin/theme
@@ -118,4 +116,4 @@ msgstr ""
118
 
119
  #. Author URI of the plugin/theme
120
  msgid "http://www.blazersix.com/"
121
- msgstr ""
1
+ # Copyright (C) 2014 Simple Image Widget
2
  # This file is distributed under the same license as the Simple Image Widget package.
3
  msgid ""
4
  msgstr ""
5
  "Project-Id-Version: Simple Image Widget 3.0.4\n"
6
+ "Report-Msgid-Bugs-To: "
7
+ "http://wordpress.org/support/plugin/simple-image-widget\n"
8
+ "POT-Creation-Date: 2014-03-14 05:54:01+00:00\n"
9
  "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=utf-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
+ "PO-Revision-Date: 2014-MO-DA HO:MI+ZONE\n"
13
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
  "Language-Team: LANGUAGE <LL@li.org>\n"
15
 
16
+ #: includes/class-simple-image-widget-legacy.php:107
17
+ msgid "These fields are here to maintain your data from an earlier version."
 
 
 
 
18
  msgstr ""
19
 
20
+ #: includes/class-simple-image-widget-legacy.php:110
21
+ msgid ""
22
+ "Select an image, then clear these values, and they will disappear when you "
23
+ "save the widget."
24
  msgstr ""
25
 
26
+ #: includes/class-simple-image-widget-legacy.php:115
27
+ msgid "Image URL:"
28
  msgstr ""
29
 
30
+ #: includes/class-simple-image-widget-legacy.php:119
31
+ msgid "Alternate Text:"
32
  msgstr ""
33
 
34
+ #: includes/class-simple-image-widget-plugin.php:82
35
+ msgid "Choose an Attachment"
36
  msgstr ""
37
 
38
+ #: includes/class-simple-image-widget-plugin.php:83
39
+ msgid "Update Attachment"
40
  msgstr ""
41
 
42
+ #: includes/class-simple-image-widget-plugin.php:84
43
+ #: includes/class-simple-image-widget-plugin.php:124
44
+ #: includes/class-simple-image-widget.php:383
45
+ msgid "Full Size"
46
  msgstr ""
47
 
48
+ #: includes/class-simple-image-widget-plugin.php:121
49
+ msgid "Thumbnail"
50
  msgstr ""
51
 
52
+ #: includes/class-simple-image-widget-plugin.php:122
53
+ msgid "Medium"
54
  msgstr ""
55
 
56
+ #: includes/class-simple-image-widget-plugin.php:123
57
+ msgid "Large"
58
  msgstr ""
59
 
60
+ #: includes/class-simple-image-widget.php:43
61
+ msgid "Image"
62
  msgstr ""
63
 
64
+ #: includes/class-simple-image-widget.php:47
65
+ msgid "Display an image"
66
  msgstr ""
67
 
68
+ #: includes/class-simple-image-widget.php:225
69
+ msgid "Title:"
70
  msgstr ""
71
 
72
+ #: includes/class-simple-image-widget.php:231
73
+ #: includes/class-simple-image-widget.php:241
74
+ msgid "Choose an Image"
75
  msgstr ""
76
 
77
+ #: includes/class-simple-image-widget.php:232
78
+ msgid "Update Image"
 
79
  msgstr ""
80
 
81
+ #: includes/class-simple-image-widget.php:253
82
+ msgid "Size:"
83
  msgstr ""
84
 
85
+ #: includes/class-simple-image-widget.php:272
86
+ msgid "Link:"
87
  msgstr ""
88
 
89
+ #: includes/class-simple-image-widget.php:278
90
+ msgid "Open in new window?"
91
  msgstr ""
92
 
93
+ #: includes/class-simple-image-widget.php:287
94
+ msgid "Link Text:"
95
  msgstr ""
96
 
97
+ #: includes/class-simple-image-widget.php:296
98
+ msgid "Text:"
99
  msgstr ""
100
 
101
  #. Plugin Name of the plugin/theme
116
 
117
  #. Author URI of the plugin/theme
118
  msgid "http://www.blazersix.com/"
119
+ msgstr ""
readme.txt CHANGED
@@ -2,18 +2,20 @@
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.7
6
  Stable tag: trunk
7
  License: GPL-2.0+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
- A simple image widget that makes it a breeze to add images to your sidebars.
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 hooks to create additional image-based widgets.
15
 
16
- Blazer Six took over development and maintenance of Simple Image Widget with version 3.0, rewriting it from the ground up to take advantage of the media improvements in WordPress 3.5. 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/).
 
 
17
 
18
  = Additional Resources =
19
 
@@ -25,12 +27,42 @@ Blazer Six took over development and maintenance of Simple Image Widget with ver
25
 
26
  == Installation ==
27
 
28
- 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.
29
 
30
  == Frequently Asked Questions ==
31
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  = How do I add alt text to images in the widget? =
33
- When selecting an image in the media manager (not in the widget itself), the right section will be titled "Attachment Details" and contains a field for entering your alt text. After entering your alt text, click the "Update Image" button to use the selected image in your widget. You won't be able to see the alt text in most browsers without viewing the HTML source of the page.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  == Screenshots ==
36
 
@@ -39,6 +71,13 @@ When selecting an image in the media manager (not in the widget itself), the rig
39
 
40
  == Changelog ==
41
 
 
 
 
 
 
 
 
42
  = 3.0.4 =
43
  * Fixed a slash preventing custom translations from loading.
44
  * Dropped the text domain from custom translation filenames.
@@ -58,4 +97,4 @@ When selecting an image in the media manager (not in the widget itself), the rig
58
  * Removed the main plugin file for the previous version.
59
 
60
  = 3.0 =
61
- * Complete rewrite with new media manager support.
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.9
6
  Stable tag: trunk
7
  License: GPL-2.0+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
+ A simple widget that makes it a breeze to add images to your sidebars.
11
 
12
  == Description ==
13
 
14
+ Simple Image Widget is what the name implies -- the easiest way to add images to your sidebars. Display advertisements, calls-to-action, or even build a slider based on image widgets.
15
 
16
+ Despite its simplicity, Simple Image Widget is built with extensibility in mind, making it super easy to spin off new image-based widgets, or customize the widget ouput using the available template hierarchy.
17
+
18
+ Blazer Six took over development and maintenance of Simple Image Widget with version 3.0, rewriting it from the ground up to take advantage of the media improvements in WordPress 3.5. Read about the original thought behind creating the plugin and ways it can be extended in [Building a Better Image Widget with the New WordPress Media Manager](http://www.blazersix.com/blog/wordpress-image-widget/).
19
 
20
  = Additional Resources =
21
 
27
 
28
  == Installation ==
29
 
30
+ Install just like most other plugins. [Check out the codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins) if you have any questions.
31
 
32
  == Frequently Asked Questions ==
33
 
34
+ = Is there a way to filter the widget output? =
35
+
36
+ Absolutely. Changing the output can be done a few different ways, but the most common alternatives involve using the "`simple_image_widget_output`" filter or overriding the template in your theme.
37
+
38
+ To use the template method, copy "`widget.php`" from the "`/templates`" directory in the plugin to a "`/simple-image-widget`" directory in your theme. Then update as you wish. It's also possible to create a custom template specific to each sidebar in your theme using the following default template hierarchy:
39
+
40
+ * `{theme}/simple-image-widget/{sidebar_id}_widget.php`
41
+ * `{theme}/simple-image-widget/widget.php`
42
+ * `{plugin}/templates/widget.php`
43
+
44
+ _Always use a [child theme](https://codex.wordpress.org/Child_Themes) to make changes if you acquired your theme from a third-party and you expect it to be updated. Otherwise, you run the risk of losing your customizations._
45
+
46
  = How do I add alt text to images in the widget? =
47
+
48
+ When selecting an image in the media modal (the popup to select images), the right sidebar will be titled "Attachment Details" and contains a field for entering alt text. After entering your alt text, click the "Update Image" button to use the selected image in your widget. Most browsers don't show the alt text, so you'll need to view the HTML source to make sure it exists.
49
+
50
+ = How do I center the widget? =
51
+
52
+ The widget can be centered using CSS. Custom CSS should be added a child theme or using a plugin like [Simple Custom CSS](https://wordpress.org/plugins/simple-custom-css/) or [Jetpack](https://wordpress.org/plugins/jetpack/). The following snippet will center the contents of the widget:
53
+
54
+ `.widget_simpleimage {
55
+ text-align: center;
56
+ }`
57
+
58
+ = Can I remove the width and height attributes? =
59
+
60
+ The widget uses the core function `wp_get_attachment_image()` to display the image and it would be more trouble than it's worth to remove those attributes. Some basic CSS will typically allow you to make the image responsive if necessary:
61
+
62
+ `.widget_simpleimage img {
63
+ height: auto;
64
+ max-width: 100%;
65
+ }`
66
 
67
  == Screenshots ==
68
 
71
 
72
  == Changelog ==
73
 
74
+ = 4.0.0 =
75
+ * New template system to make it easier to override the output.
76
+ * Restructured to make it more intuitive for developers to extend the widget.
77
+ * Moved legacy support into a separate class that hooks into the widget.
78
+ * Works with the Widget Customizer added in WordPress 3.9.
79
+ * Improved compatibility with plugins like Page Builder by SiteOrigin.
80
+
81
  = 3.0.4 =
82
  * Fixed a slash preventing custom translations from loading.
83
  * Dropped the text domain from custom translation filenames.
97
  * Removed the main plugin file for the previous version.
98
 
99
  = 3.0 =
100
+ * Complete rewrite with new media manager support.
simple-image-widget.php CHANGED
@@ -1,191 +1,75 @@
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.4
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
- * The main plugin class for loading the widget and attaching necessary hooks.
27
  *
28
- * @since 3.0.0
 
29
  */
30
- class Simple_Image_Widget_Loader {
31
- /**
32
- * Setup functionality needed by the widget.
33
- *
34
- * @since 3.0.0
35
- */
36
- public static function load() {
37
- self::load_textdomain();
38
- add_action( 'widgets_init', array( __CLASS__, 'register_widget' ) );
39
-
40
- if ( is_simple_image_widget_legacy() ) {
41
- return;
42
- }
43
-
44
- add_action( 'init', array( __CLASS__, 'init' ) );
45
- add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_scripts' ) );
46
- add_action( 'admin_head-widgets.php', array( __CLASS__, 'admin_head_widgets' ) );
47
- add_action( 'admin_footer-widgets.php', array( __CLASS__, 'admin_footer_widgets' ) );
48
- }
49
-
50
- /**
51
- * Plugin localization support.
52
- *
53
- * @since 3.0.0
54
- */
55
- public static function load_textdomain() {
56
- $locale = apply_filters( 'plugin_locale', get_locale(), 'simple-image-widget' );
57
- load_textdomain( 'simple-image-widget', WP_LANG_DIR . '/simple-image-widget/' . $locale . '.mo' );
58
- load_plugin_textdomain( 'simple-image-widget', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
59
- }
60
-
61
- /**
62
- * Register and localize generic script libraries.
63
- *
64
- * A preliminary attempt has been made to abstract the
65
- * 'simple-image-widget-control' script a bit in order to allow it to be
66
- * re-used anywhere a similiar media selection feature is needed.
67
- *
68
- * Custom image size labels need to be added using the
69
- * 'image_size_names_choose' filter.
70
- *
71
- * @since 3.0.0
72
- */
73
- public static function init() {
74
- wp_register_script( 'simple-image-widget', plugin_dir_url( __FILE__ ) . 'js/simple-image-widget.js', array( 'media-upload', 'media-views' ) );
75
-
76
- wp_localize_script( 'simple-image-widget', 'SimpleImageWidget', array(
77
- 'frameTitle' => __( 'Choose an Attachment', 'simple-image-widget' ),
78
- 'frameUpdateText' => __( 'Update Attachment', 'simple-image-widget' ),
79
- 'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
80
- 'imageSizeNames' => self::get_image_size_names(),
81
- ) );
82
- }
83
 
 
84
  /**
85
- * Register the image widget.
86
  *
87
- * @since 3.0.0
 
88
  */
89
- public static function register_widget() {
90
- register_widget( 'Simple_Image_Widget' );
91
- }
92
-
93
- /**
94
- * Enqueue scripts needed for selecting media.
95
- *
96
- * @since 3.0.0
97
- */
98
- public static function admin_scripts( $hook_suffix ) {
99
- if ( 'widgets.php' == $hook_suffix ) {
100
- wp_enqueue_media();
101
- wp_enqueue_script( 'simple-image-widget' );
102
- }
103
- }
104
-
105
- /**
106
- * Output CSS for styling the image widget in the dashboard.
107
- *
108
- * @since 3.0.0
109
- */
110
- public static function admin_head_widgets() {
111
- ?>
112
- <style type="text/css">
113
- .widget .widget-inside .simple-image-widget-form .simple-image-widget-control { padding: 20px 0; text-align: center; border: 1px dashed #aaa;}
114
- .widget .widget-inside .simple-image-widget-form .simple-image-widget-control.has-image { padding: 10px; text-align: left; border: 1px dashed #aaa;}
115
- .widget .widget-inside .simple-image-widget-form .simple-image-widget-control img { display: block; margin-bottom: 10px; max-width: 100%; height: auto;}
116
-
117
- .simple-image-widget-legacy-fields { margin-bottom: 1em; padding: 10px; background-color: #e0e0e0; border-radius: 3px;}
118
- .simple-image-widget-legacy-fields p:last-child { margin-bottom: 0;}
119
- </style>
120
- <?php
121
- }
122
-
123
- /**
124
- * Output custom handler for when an image is selected in the media manager.
125
- *
126
- * @since 3.0.0
127
- */
128
- public static function admin_footer_widgets() {
129
- ?>
130
- <script type="text/javascript">
131
- jQuery(function($) {
132
- $('#wpbody').on('selectionChange.simpleimagewidget', '.simple-image-widget-control', function( e, selection ) {
133
- var $control = $( e.target ),
134
- $sizeField = $control.closest('.simple-image-widget-form').find('select.image-size'),
135
- model = selection.first(),
136
- sizes = model.get('sizes'),
137
- size, image;
138
-
139
- if ( sizes ) {
140
- // The image size to display in the widget.
141
- size = sizes['post-thumbnail'] || sizes.medium;
142
- }
143
-
144
- if ( $sizeField.length ) {
145
- // Builds the option elements for the size dropdown.
146
- SimpleImageWidget.updateSizeDropdownOptions( $sizeField, sizes );
147
- }
148
-
149
- size = size || model.toJSON();
150
-
151
- image = $( '<img />', { src: size.url, width: size.width } );
152
-
153
- $control.find('img').remove().end()
154
- .prepend( image )
155
- .addClass('has-image')
156
- .find('a.simple-image-widget-control-choose').removeClass('button-hero');
157
- });
158
- });
159
- </script>
160
- <?php
161
- }
162
 
 
 
 
 
 
 
163
  /**
164
- * Get localized image size names.
165
- *
166
- * The 'image_size_names_choose' filter exists in core and should be
167
- * hooked by plugin authors to provide localized labels for custom image
168
- * sizes added using add_image_size().
169
  *
170
- * @see image_size_input_fields()
171
- * @see http://core.trac.wordpress.org/ticket/20663
172
  *
173
- * @since 3.0.0
174
  */
175
- public static function get_image_size_names() {
176
- return apply_filters( 'image_size_names_choose', array(
177
- 'thumbnail' => __( 'Thumbnail', 'simple-image-widget' ),
178
- 'medium' => __( 'Medium', 'simple-image-widget' ),
179
- 'large' => __( 'Large', 'simple-image-widget' ),
180
- 'full' => __( 'Full Size', 'simple-image-widget' ),
181
- ) );
182
- }
183
  }
184
- add_action( 'plugins_loaded', array( 'Simple_Image_Widget_Loader', 'load' ) );
185
 
186
  /**
187
- * Check to see if the current version of WordPress supports the new media manager.
188
  */
189
- function is_simple_image_widget_legacy() {
190
- return version_compare( get_bloginfo( 'version' ), '3.4.2', '<=' );
191
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
  /**
3
+ * Simple Image Widget
4
+ *
5
+ * @package SimpleImageWidget
6
+ * @author Brady Vercher
7
+ * @copyright Copyright (c) 2014, Blazer Six, Inc.
8
+ * @license GPL-2.0+
9
+ *
10
+ * @wordpress-plugin
11
  * Plugin Name: Simple Image Widget
12
  * Plugin URI: https://wordpress.org/extend/plugins/simple-image-widget/
13
  * Description: A simple image widget utilizing the new WordPress media manager.
14
+ * Version: 4.0.0
15
  * Author: Blazer Six
16
  * Author URI: http://www.blazersix.com/
17
  * License: GPL-2.0+
18
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
19
  * Text Domain: simple-image-widget
20
  * Domain Path: /languages
 
 
 
 
 
21
  */
22
 
23
  /**
24
+ * Main plugin instance.
 
 
 
 
 
25
  *
26
+ * @since 4.0.0
27
+ * @type Simple_Image_Widget $simple_image_widget
28
  */
29
+ global $simple_image_widget;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ if ( ! defined( 'SIW_DIR' ) ) {
32
  /**
33
+ * Plugin directory path.
34
  *
35
+ * @since 4.0.0
36
+ * @type string SIW_DIR
37
  */
38
+ define( 'SIW_DIR', plugin_dir_path( __FILE__ ) );
39
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ /**
42
+ * Check if the installed version of WordPress supports the new media manager.
43
+ *
44
+ * @since 3.0.0
45
+ */
46
+ function is_simple_image_widget_legacy() {
47
  /**
48
+ * Whether the installed version of WordPress supports the new media manager.
 
 
 
 
49
  *
50
+ * @since 4.0.0
 
51
  *
52
+ * @param bool $is_legacy
53
  */
54
+ return apply_filters( 'is_simple_image_widget_legacy', version_compare( get_bloginfo( 'version' ), '3.4.2', '<=' ) );
 
 
 
 
 
 
 
55
  }
 
56
 
57
  /**
58
+ * Include functions and libraries.
59
  */
60
+ require_once( SIW_DIR . 'includes/class-simple-image-widget.php' );
61
+ require_once( SIW_DIR . 'includes/class-simple-image-widget-legacy.php' );
62
+ require_once( SIW_DIR . 'includes/class-simple-image-widget-plugin.php' );
63
+ require_once( SIW_DIR . 'includes/class-simple-image-widget-template-loader.php' );
64
+
65
+ /**
66
+ * Deprecated main plugin class.
67
+ *
68
+ * @since 3.0.0
69
+ * @deprecated 4.0.0
70
+ */
71
+ class Simple_Image_Widget_Loader extends Simple_Image_Widget_Plugin {}
72
+
73
+ // Initialize and load the plugin.
74
+ $simple_image_widget = new Simple_Image_Widget_Plugin();
75
+ add_action( 'plugins_loaded', array( $simple_image_widget, 'load' ) );
templates/widget.php ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Default widget template.
4
+ *
5
+ * Copy this template to /simple-image-widget/widget.php in your theme or
6
+ * child theme to make edits.
7
+ *
8
+ * @package SimpleImageWidget
9
+ * @copyright Copyright (c) 2014, Blazer Six, Inc.
10
+ * @license GPL-2.0+
11
+ * @since 4.0.0
12
+ */
13
+ ?>
14
+
15
+ <?php
16
+ if ( ! empty( $title ) ) :
17
+ echo $before_title . $title . $after_title;
18
+ endif;
19
+ ?>
20
+
21
+ <?php if ( ! empty( $image_id ) ) : ?>
22
+ <p class="simple-image">
23
+ <?php
24
+ echo $link_open;
25
+ echo wp_get_attachment_image( $image_id, $image_size );
26
+ echo $link_close;
27
+ ?>
28
+ </p>
29
+ <?php endif; ?>
30
+
31
+ <?php
32
+ if ( ! empty( $text ) ) :
33
+ echo apply_filters( 'the_content', $text );
34
+ endif;
35
+ ?>
36
+
37
+ <?php if ( ! empty( $link_text ) ) : ?>
38
+ <p class="more">
39
+ <?php
40
+ echo $link_open;
41
+ echo $link_text;
42
+ echo $link_close;
43
+ ?>
44
+ </p>
45
+ <?php endif; ?>