Simple Image Widget - Version 4.1.0

Version Description

  • Added the ability to hide widget fields.
  • Added a field to insert HTML classes on the text link. Hidden by default.
  • Removed "the_content" filter from widget text to prevent other plugins from appending content.
  • Renamed /scripts to /js and /styles to /css.
  • Improved handling of fields that have been removed in child widgets.
Download this release

Release Info

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

Code changes from version 4.0.2 to 4.1.0

assets/{styles → css}/simple-image-widget.css RENAMED
@@ -1,29 +1,33 @@
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
- }
 
 
 
 
1
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-field.is-hidden {
2
+ display: none;
3
+ }
4
+
5
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control {
6
+ border: 1px dashed #aaa;
7
+ padding: 20px 0;
8
+ text-align: center;
9
+ }
10
+
11
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control.has-image {
12
+ border: 1px dashed #aaa;
13
+ padding: 10px;
14
+ text-align: left;
15
+ }
16
+
17
+ .widget .widget-inside .simple-image-widget-form .simple-image-widget-control img {
18
+ display: block;
19
+ height: auto;
20
+ margin-bottom: 10px;
21
+ max-width: 100%;
22
+ }
23
+
24
+ .simple-image-widget-legacy-fields {
25
+ margin-bottom: 1em;
26
+ padding: 10px;
27
+ background-color: #e0e0e0;
28
+ border-radius: 3px;
29
+ }
30
+
31
+ .simple-image-widget-legacy-fields p:last-child {
32
+ margin-bottom: 0;
33
+ }
assets/{scripts → js}/simple-image-widget.js RENAMED
@@ -1,206 +1,230 @@
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 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ // Wire up the toggle checkboxes in the screen options tab.
207
+ $( '.simple-image-widget-field-toggle' ).on( 'click', function() {
208
+ var $this = $( this ),
209
+ field = $this.val(),
210
+ $hiddenFields = $( '.simple-image-widget-field-toggle:not(:checked)' );
211
+
212
+ $( '.simple-image-widget-field-' + field ).toggleClass( 'is-hidden', ! $this.prop( 'checked' ) );
213
+
214
+ $.ajax({
215
+ url: ajaxurl,
216
+ type: 'POST',
217
+ data: {
218
+ action: 'simple_image_widget_preferences',
219
+ hidden: $hiddenFields.map(function() { return this.value; }).get().join( ',' ),
220
+ nonce: SimpleImageWidget.screenOptionsNonce
221
+ },
222
+ success: function( data ) {
223
+ if ( 'nonce' in data ) {
224
+ SimpleImageWidget.screenOptionsNonce = data.nonce;
225
+ }
226
+ }
227
+ });
228
+ });
229
+ });
230
+ })( this, jQuery, _, wp );
includes/class-simple-image-widget-plugin.php CHANGED
@@ -33,6 +33,8 @@ class Simple_Image_Widget_Plugin {
33
 
34
  add_action( 'init', array( $this, 'register_assets' ) );
35
  add_action( 'sidebar_admin_setup', array( $this, 'enqueue_admin_assets' ) );
 
 
36
  }
37
 
38
  /**
@@ -68,12 +70,12 @@ class Simple_Image_Widget_Plugin {
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
 
@@ -87,10 +89,59 @@ class Simple_Image_Widget_Plugin {
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
  *
@@ -129,4 +180,51 @@ class Simple_Image_Widget_Plugin {
129
  )
130
  );
131
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  }
33
 
34
  add_action( 'init', array( $this, 'register_assets' ) );
35
  add_action( 'sidebar_admin_setup', array( $this, 'enqueue_admin_assets' ) );
36
+ add_filter( 'screen_settings', array( $this, 'widgets_screen_settings' ), 10, 2 );
37
+ add_action( 'wp_ajax_simple_image_widget_preferences', array( $this, 'ajax_save_user_preferences' ) );
38
  }
39
 
40
  /**
70
  public function register_assets() {
71
  wp_register_style(
72
  'simple-image-widget-admin',
73
+ dirname( plugin_dir_url( __FILE__ ) ) . '/assets/css/simple-image-widget.css'
74
  );
75
 
76
  wp_register_script(
77
  'simple-image-widget-admin',
78
+ dirname( plugin_dir_url( __FILE__ ) ) . '/assets/js/simple-image-widget.js',
79
  array( 'media-upload', 'media-views' )
80
  );
81
 
89
  'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
90
  'imageSizeNames' => self::get_image_size_names(),
91
  ),
92
+ 'screenOptionsNonce' => wp_create_nonce( 'save-siw-preferences' ),
93
  )
94
  );
95
  }
96
 
97
+ /**
98
+ * Add checkboxes to the screen options tab on the Widgets screen for
99
+ * togglable fields.
100
+ *
101
+ * @since 4.1.0
102
+ *
103
+ * @param string $settings Screen options output.
104
+ * @param WP_Screen $screen Current screen.
105
+ * @return string
106
+ */
107
+ public function widgets_screen_settings( $settings, $screen ) {
108
+ if ( 'widgets' !== $screen->id ) {
109
+ return $settings;
110
+ }
111
+
112
+ $settings .= sprintf( '<h5>%s</h5>', __( 'Simple Image Widget', 'simple-image-widget' ) );
113
+
114
+ $fields = array(
115
+ 'image_size' => __( 'Image Size', 'simple-image-widget' ),
116
+ 'link' => __( 'Link', 'simple-image-widget' ),
117
+ 'link_classes' => __( 'Link Classes', 'simple-image-widget' ),
118
+ 'link_text' => __( 'Link Text', 'simple-image-widget' ),
119
+ 'new_window' => __( 'New Window', 'simple-image-widget' ),
120
+ 'text' => __( 'Text', 'simple-image-widget' ),
121
+ );
122
+
123
+ /**
124
+ * List of hideable fields.
125
+ *
126
+ * @since 4.1.0
127
+ *
128
+ * @param array $fields List of fields with ids as keys and labels as values.
129
+ */
130
+ $fields = apply_filters( 'simple_image_widget_hideable_fields', $fields );
131
+ $hidden_fields = $this->get_hidden_fields();
132
+
133
+ foreach ( $fields as $id => $label ) {
134
+ $settings .= sprintf(
135
+ '<label><input type="checkbox" value="%1$s"%2$s class="simple-image-widget-field-toggle"> %3$s</label>',
136
+ esc_attr( $id ),
137
+ checked( in_array( $id, $hidden_fields ), false, false ),
138
+ esc_html( $label )
139
+ );
140
+ }
141
+
142
+ return $settings;
143
+ }
144
+
145
  /**
146
  * Enqueue scripts needed for selecting media.
147
  *
180
  )
181
  );
182
  }
183
+
184
+ /**
185
+ * Retrieve a list of hidden fields.
186
+ *
187
+ * @since 4.1.0
188
+ *
189
+ * @return array List of field ids.
190
+ */
191
+ public static function get_hidden_fields() {
192
+ $hidden_fields = get_user_option( 'siw_hidden_fields', get_current_user_id() );
193
+
194
+ // Fields that are hidden by default.
195
+ if ( false === $hidden_fields ) {
196
+ $hidden_fields = array( 'link_classes' );
197
+ }
198
+
199
+ /**
200
+ * List of hidden field ids.
201
+ *
202
+ * @since 4.1.0
203
+ *
204
+ * @param array $hidden_fields List of hidden field ids.
205
+ */
206
+ return (array) apply_filters( 'simple_image_widget_hidden_fields', $hidden_fields );
207
+ }
208
+
209
+ /**
210
+ * AJAX callback to save the user's hidden fields.
211
+ *
212
+ * @since 4.1.0
213
+ */
214
+ public function ajax_save_user_preferences() {
215
+ $nonce_action = 'save-siw-preferences';
216
+ check_ajax_referer( $nonce_action, 'nonce' );
217
+ $data = array( 'nonce' => wp_create_nonce( $nonce_action ) );
218
+
219
+ if ( ! $user = wp_get_current_user() ) {
220
+ wp_send_json_error( $data );
221
+ }
222
+
223
+ $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
224
+ if ( is_array( $hidden ) ) {
225
+ update_user_option( $user->ID, 'siw_hidden_fields', $hidden );
226
+ }
227
+
228
+ wp_send_json_success( $data );
229
+ }
230
  }
includes/class-simple-image-widget.php CHANGED
@@ -79,7 +79,7 @@ class Simple_Image_Widget extends WP_Widget {
79
  }
80
 
81
  // Copy the original values so they can be used in hooks.
82
- $instance['text_raw'] = $instance['text'];
83
  $instance['title_raw'] = $instance['title'];
84
  $instance['text'] = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance, $this->id_base );
85
  $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
@@ -96,14 +96,30 @@ class Simple_Image_Widget extends WP_Widget {
96
  }
97
 
98
  if ( empty( $output ) ) {
99
- $instance['link_open'] = '';
100
- $instance['link_close'] = '';
 
 
101
 
102
  if ( ! empty ( $instance['link'] ) ) {
103
  $target = ( empty( $instance['new_window'] ) ) ? '' : ' target="_blank"';
104
 
105
  $instance['link_open'] = '<a href="' . esc_url( $instance['link'] ) . '"' . $target . '>';
106
  $instance['link_close'] = '</a>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
108
 
109
  $output = $this->render( $args, $instance );
@@ -184,15 +200,16 @@ class Simple_Image_Widget extends WP_Widget {
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
 
@@ -259,7 +276,7 @@ class Simple_Image_Widget extends WP_Widget {
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
@@ -279,11 +296,11 @@ class Simple_Image_Widget extends WP_Widget {
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' ); ?>
@@ -292,9 +309,18 @@ class Simple_Image_Widget extends WP_Widget {
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>
@@ -303,7 +329,7 @@ class Simple_Image_Widget extends WP_Widget {
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>
@@ -353,7 +379,7 @@ class Simple_Image_Widget extends WP_Widget {
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
  /**
@@ -372,10 +398,29 @@ class Simple_Image_Widget extends WP_Widget {
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'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  $this->flush_widget_cache();
381
 
@@ -494,4 +539,23 @@ class Simple_Image_Widget extends WP_Widget {
494
  $this->id_base
495
  );
496
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
  }
79
  }
80
 
81
  // Copy the original values so they can be used in hooks.
82
+ $instance['text_raw'] = empty( $instance['text'] ) ? '' : $instance['text'];
83
  $instance['title_raw'] = $instance['title'];
84
  $instance['text'] = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance, $this->id_base );
85
  $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
96
  }
97
 
98
  if ( empty( $output ) ) {
99
+ $instance['link_open'] = '';
100
+ $instance['link_close'] = '';
101
+ $instance['text_link_open'] = '';
102
+ $instance['text_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
+ // This is to differentiate between the image link and text link.
111
+ $instance['text_link_open'] = $instance['link_open'];
112
+ $instance['text_link_close'] = $instance['link_close'];
113
+
114
+ // The link classes should only be added to the text link.
115
+ if ( ! empty( $instance['link_classes'] ) ) {
116
+ $instance['text_link_open'] = sprintf(
117
+ '<a href="%1$s" class="%3$s"%2$s>',
118
+ esc_url( $instance['link'] ),
119
+ $target,
120
+ esc_attr( $instance['link_classes'] )
121
+ );
122
+ }
123
  }
124
 
125
  $output = $this->render( $args, $instance );
200
  $instance = wp_parse_args(
201
  (array) $instance,
202
  array(
203
+ 'alt' => '', // Legacy.
204
+ 'image' => '', // Legacy URL field.
205
+ 'image_id' => '',
206
+ 'image_size' => 'full',
207
+ 'link' => '',
208
+ 'link_classes' => '',
209
+ 'link_text' => '',
210
+ 'new_window' => '',
211
+ 'title' => '',
212
+ 'text' => '',
213
  )
214
  );
215
 
276
  case 'image_size' :
277
  $sizes = $this->get_image_sizes( $image_id );
278
  ?>
279
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'image_size' ) ); ?>">
280
  <label for="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
281
  <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"' : ''; ?>>
282
  <?php
296
 
297
  case 'link' :
298
  ?>
299
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'link' ) ); ?>">
300
  <label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
301
  <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">
302
  </p>
303
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'new_window' ) ); ?>" style="margin-top: -0.75em; padding-left: 2px">
304
  <label for="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>">
305
  <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'] ); ?>>
306
  <?php _e( 'Open in new window?', 'simple-image-widget' ); ?>
309
  <?php
310
  break;
311
 
312
+ case 'link_classes' :
313
+ ?>
314
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'link_classes' ) ); ?>">
315
+ <label for="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>"><?php _e( 'Link Classes:', 'simple-image-widget' ); ?></label>
316
+ <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_classes' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>" value="<?php echo esc_attr( $instance['link_classes'] ); ?>" class="widefat">
317
+ </p>
318
+ <?php
319
+ break;
320
+
321
  case 'link_text' :
322
  ?>
323
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'link_text' ) ); ?>">
324
  <label for="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
325
  <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">
326
  </p>
329
 
330
  case 'text' :
331
  ?>
332
+ <p class="<?php echo esc_attr( $this->siw_field_class( 'text' ) ); ?>">
333
  <label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
334
  <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>
335
  </p>
379
  * @return string List of field ids.
380
  */
381
  public function form_fields() {
382
+ return array( 'image_size', 'link', 'link_text', 'link_classes', 'text' );
383
  }
384
 
385
  /**
398
 
399
  $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
400
  $instance['image_id'] = absint( $new_instance['image_id'] );
 
 
401
  $instance['new_window'] = isset( $new_instance['new_window'] );
402
+
403
+ // Optional field that can be removed via a filter.
404
+ foreach ( array( 'link', 'link_classes', 'link_text', 'text' ) as $key ) {
405
+ if ( ! isset( $new_instance[ $key ] ) ) {
406
+ continue;
407
+ }
408
+
409
+ switch ( $key ) {
410
+ case 'link' :
411
+ $instance['link'] = esc_url_raw( $new_instance['link'] );
412
+ break;
413
+ case 'link_classes' :
414
+ $instance['link_classes'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $new_instance['link_classes'] ) ) );
415
+ break;
416
+ case 'link_text' :
417
+ $instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
418
+ break;
419
+ case 'text' :
420
+ $instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
421
+ break;
422
+ }
423
+ }
424
 
425
  $this->flush_widget_cache();
426
 
539
  $this->id_base
540
  );
541
  }
542
+
543
+ /**
544
+ * Retrieve HTML classes for a field container.
545
+ *
546
+ * @since 4.1.0
547
+ *
548
+ * @param string $id Field id.
549
+ * @return string
550
+ */
551
+ protected function siw_field_class( $id ) {
552
+ $classes = array( 'simple-image-widget-field', 'simple-image-widget-field-' . sanitize_html_class( $id ) );
553
+
554
+ $hidden_fields = Simple_Image_Widget_Plugin::get_hidden_fields();
555
+ if ( in_array( $id, $hidden_fields ) ) {
556
+ $classes[] = 'is-hidden';
557
+ }
558
+
559
+ return implode( ' ', $classes );
560
+ }
561
  }
languages/simple-image-widget.pot CHANGED
@@ -2,7 +2,7 @@
2
  # This file is distributed under the GPL-2.0+.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: Simple Image Widget 4.0.2\n"
6
  "Report-Msgid-Bugs-To: "
7
  "http://wordpress.org/support/plugin/simple-image-widget\n"
8
  "POT-Creation-Date: 2014-06-13 21:26:00+00:00\n"
@@ -43,29 +43,57 @@ msgstr ""
43
  msgid "Alternate Text:"
44
  msgstr ""
45
 
46
- #: includes/class-simple-image-widget-plugin.php:85
47
  msgid "Choose an Attachment"
48
  msgstr ""
49
 
50
- #: includes/class-simple-image-widget-plugin.php:86
51
  msgid "Update Attachment"
52
  msgstr ""
53
 
54
- #: includes/class-simple-image-widget-plugin.php:87
55
- #: includes/class-simple-image-widget-plugin.php:128
56
- #: includes/class-simple-image-widget.php:412
57
  msgid "Full Size"
58
  msgstr ""
59
 
60
- #: includes/class-simple-image-widget-plugin.php:125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  msgid "Thumbnail"
62
  msgstr ""
63
 
64
- #: includes/class-simple-image-widget-plugin.php:126
65
  msgid "Medium"
66
  msgstr ""
67
 
68
- #: includes/class-simple-image-widget-plugin.php:127
69
  msgid "Large"
70
  msgstr ""
71
 
@@ -77,41 +105,41 @@ msgstr ""
77
  msgid "An image from your Media Library."
78
  msgstr ""
79
 
80
- #: includes/class-simple-image-widget.php:235
81
  msgid "Title:"
82
  msgstr ""
83
 
84
- #: includes/class-simple-image-widget.php:241
85
- #: includes/class-simple-image-widget.php:251
86
  msgid "Choose an Image"
87
  msgstr ""
88
 
89
- #: includes/class-simple-image-widget.php:242
90
  msgid "Update Image"
91
  msgstr ""
92
 
93
- #: includes/class-simple-image-widget.php:263
94
  msgid "Size:"
95
  msgstr ""
96
 
97
- #: includes/class-simple-image-widget.php:283
98
  msgid "Link:"
99
  msgstr ""
100
 
101
- #: includes/class-simple-image-widget.php:289
102
  msgid "Open in new window?"
103
  msgstr ""
104
 
105
- #: includes/class-simple-image-widget.php:298
106
- msgid "Link Text:"
107
  msgstr ""
108
 
109
- #: includes/class-simple-image-widget.php:307
110
- msgid "Text:"
111
  msgstr ""
112
 
113
- #. Plugin Name of the plugin/theme
114
- msgid "Simple Image Widget"
115
  msgstr ""
116
 
117
  #. Plugin URI of the plugin/theme
2
  # This file is distributed under the GPL-2.0+.
3
  msgid ""
4
  msgstr ""
5
+ "Project-Id-Version: Simple Image Widget 4.1.0\n"
6
  "Report-Msgid-Bugs-To: "
7
  "http://wordpress.org/support/plugin/simple-image-widget\n"
8
  "POT-Creation-Date: 2014-06-13 21:26:00+00:00\n"
43
  msgid "Alternate Text:"
44
  msgstr ""
45
 
46
+ #: includes/class-simple-image-widget-plugin.php:87
47
  msgid "Choose an Attachment"
48
  msgstr ""
49
 
50
+ #: includes/class-simple-image-widget-plugin.php:88
51
  msgid "Update Attachment"
52
  msgstr ""
53
 
54
+ #: includes/class-simple-image-widget-plugin.php:89
55
+ #: includes/class-simple-image-widget-plugin.php:179
56
+ #: includes/class-simple-image-widget.php:457
57
  msgid "Full Size"
58
  msgstr ""
59
 
60
+ #. Plugin Name of the plugin/theme
61
+ msgid "Simple Image Widget"
62
+ msgstr ""
63
+
64
+ #: includes/class-simple-image-widget-plugin.php:115
65
+ msgid "Image Size"
66
+ msgstr ""
67
+
68
+ #: includes/class-simple-image-widget-plugin.php:116
69
+ msgid "Link"
70
+ msgstr ""
71
+
72
+ #: includes/class-simple-image-widget-plugin.php:117
73
+ msgid "Link Classes"
74
+ msgstr ""
75
+
76
+ #: includes/class-simple-image-widget-plugin.php:118
77
+ msgid "Link Text"
78
+ msgstr ""
79
+
80
+ #: includes/class-simple-image-widget-plugin.php:119
81
+ msgid "New Window"
82
+ msgstr ""
83
+
84
+ #: includes/class-simple-image-widget-plugin.php:120
85
+ msgid "Text"
86
+ msgstr ""
87
+
88
+ #: includes/class-simple-image-widget-plugin.php:176
89
  msgid "Thumbnail"
90
  msgstr ""
91
 
92
+ #: includes/class-simple-image-widget-plugin.php:177
93
  msgid "Medium"
94
  msgstr ""
95
 
96
+ #: includes/class-simple-image-widget-plugin.php:178
97
  msgid "Large"
98
  msgstr ""
99
 
105
  msgid "An image from your Media Library."
106
  msgstr ""
107
 
108
+ #: includes/class-simple-image-widget.php:252
109
  msgid "Title:"
110
  msgstr ""
111
 
112
+ #: includes/class-simple-image-widget.php:258
113
+ #: includes/class-simple-image-widget.php:268
114
  msgid "Choose an Image"
115
  msgstr ""
116
 
117
+ #: includes/class-simple-image-widget.php:259
118
  msgid "Update Image"
119
  msgstr ""
120
 
121
+ #: includes/class-simple-image-widget.php:280
122
  msgid "Size:"
123
  msgstr ""
124
 
125
+ #: includes/class-simple-image-widget.php:300
126
  msgid "Link:"
127
  msgstr ""
128
 
129
+ #: includes/class-simple-image-widget.php:306
130
  msgid "Open in new window?"
131
  msgstr ""
132
 
133
+ #: includes/class-simple-image-widget.php:315
134
+ msgid "Link Classes:"
135
  msgstr ""
136
 
137
+ #: includes/class-simple-image-widget.php:324
138
+ msgid "Link Text:"
139
  msgstr ""
140
 
141
+ #: includes/class-simple-image-widget.php:333
142
+ msgid "Text:"
143
  msgstr ""
144
 
145
  #. Plugin URI of the plugin/theme
readme.txt CHANGED
@@ -1,8 +1,8 @@
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.9
6
  Stable tag: trunk
7
  License: GPL-2.0+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -75,6 +75,13 @@ The widget uses the core function `wp_get_attachment_image()` to display the ima
75
 
76
  == Changelog ==
77
 
 
 
 
 
 
 
 
78
  = 4.0.2 =
79
  * Fixed the reference to the widget's parent class to prevent an error.
80
 
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.5
5
+ Tested up to: 4.0
6
  Stable tag: trunk
7
  License: GPL-2.0+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
75
 
76
  == Changelog ==
77
 
78
+ = 4.1.0 =
79
+ * Added the ability to hide widget fields.
80
+ * Added a field to insert HTML classes on the text link. Hidden by default.
81
+ * Removed "the_content" filter from widget text to prevent other plugins from appending content.
82
+ * Renamed /scripts to /js and /styles to /css.
83
+ * Improved handling of fields that have been removed in child widgets.
84
+
85
  = 4.0.2 =
86
  * Fixed the reference to the widget's parent class to prevent an error.
87
 
simple-image-widget.php CHANGED
@@ -11,7 +11,7 @@
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.2
15
  * Author: Blazer Six
16
  * Author URI: http://www.blazersix.com/
17
  * License: GPL-2.0+
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.1.0
15
  * Author: Blazer Six
16
  * Author URI: http://www.blazersix.com/
17
  * License: GPL-2.0+
templates/widget.php CHANGED
@@ -30,16 +30,16 @@ 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; ?>
30
 
31
  <?php
32
  if ( ! empty( $text ) ) :
33
+ echo wpautop( $text );
34
  endif;
35
  ?>
36
 
37
  <?php if ( ! empty( $link_text ) ) : ?>
38
  <p class="more">
39
  <?php
40
+ echo $text_link_open;
41
  echo $link_text;
42
+ echo $text_link_close;
43
  ?>
44
  </p>
45
  <?php endif; ?>