Image Widget - Version 3.3

Version Description

  • Fix to allow the widget to work in the non-async (browser) uploader. Props Bjorn Wijers
Download this release

Release Info

Developer mattwiebe
Plugin Icon 128x128 Image Widget
Version 3.3
Comparing to
See all releases

Code changes from version 3.2.11 to 3.3

image-widget-fix-browser-upload.js ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function() {
2
+
3
+ jQuery('form#image-form').submit(function(){
4
+ var wp_ref = jQuery("input[name='_wp_http_referer']").val();
5
+ // _wp_http_referer only contains the widget_sp_image if the
6
+ // previous action was pressing the add image link in an Image Widget
7
+ // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf
8
+ if( wp_ref.indexOf('widget_sp_image') != -1 ) {
9
+ var parsed_url = parse_url(wp_ref);
10
+ var nw_action_url = jQuery('form#image-form').attr('action');
11
+
12
+ // make sure the widget_sp_image is not part of the form action url
13
+ // so we will add it to fix the context
14
+ if( nw_action_url.indexOf('widget_sp_image') == -1 ) {
15
+ nw_action_url = nw_action_url + '&' + parsed_url.query;
16
+ jQuery('form#image-form').attr('action', nw_action_url);
17
+ }
18
+ }
19
+ return true;
20
+ });
21
+ });
22
+
23
+
24
+ /*
25
+ * Thanks to http://github.com/kvz/phpjs/raw/master/functions/url/parse_url.js
26
+ */
27
+ function parse_url (str, component) {
28
+ // http://kevin.vanzonneveld.net
29
+ // + original by: Steven Levithan (http://blog.stevenlevithan.com)
30
+ // + reimplemented by: Brett Zamir (http://brett-zamir.me)
31
+ // + input by: Lorenzo Pisani
32
+ // + input by: Tony
33
+ // + improved by: Brett Zamir (http://brett-zamir.me)
34
+ // % note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
35
+ // % note: blog post at http://blog.stevenlevithan.com/archives/parseuri
36
+ // % note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
37
+ // % note: Does not replace invalid characters with '_' as in PHP, nor does it return false with
38
+ // % note: a seriously malformed URL.
39
+ // % note: Besides function name, is essentially the same as parseUri as well as our allowing
40
+ // % note: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
41
+ // * example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
42
+ // * returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
43
+ var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port',
44
+ 'relative', 'path', 'directory', 'file', 'query', 'fragment'],
45
+ ini = (this.php_js && this.php_js.ini) || {},
46
+ mode = (ini['phpjs.parse_url.mode'] &&
47
+ ini['phpjs.parse_url.mode'].local_value) || 'php',
48
+ parser = {
49
+ php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
50
+ strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
51
+ loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
52
+ };
53
+
54
+ var m = parser[mode].exec(str),
55
+ uri = {},
56
+ i = 14;
57
+ while (i--) {
58
+ if (m[i]) {
59
+ uri[key[i]] = m[i];
60
+ }
61
+ }
62
+
63
+ if (component) {
64
+ return uri[component.replace('PHP_URL_', '').toLowerCase()];
65
+ }
66
+ if (mode !== 'php') {
67
+ var name = (ini['phpjs.parse_url.queryKey'] &&
68
+ ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
69
+ parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
70
+ uri[name] = {};
71
+ uri[key[12]].replace(parser, function ($0, $1, $2) {
72
+ if ($1) {uri[name][$1] = $2;}
73
+ });
74
+ }
75
+ delete uri.source;
76
+ return uri;
77
+ }
78
+
79
+
80
+
81
+
82
+ /* /wp-admin/media-upload.php?type=image&widget_id=widget_sp_image-11& */
83
+
image-widget.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Image Widget
4
  Plugin URI: http://wordpress.org/extend/plugins/image-widget/
5
  Description: Simple image widget that uses native Wordpress upload thickbox to add image widgets to your site.
6
  Author: Modern Tribe, Inc.
7
- Version: 3.2.11
8
  Author URI: http://tri.be/
9
  */
10
 
@@ -15,12 +15,12 @@ function tribe_load_image_widget() {
15
  add_action('widgets_init', 'tribe_load_image_widget');
16
 
17
  /**
18
- * SP Image Widget class
19
  *
20
  * @author Shane & Peter, Inc. (Peter Chester)
21
  **/
22
  class Tribe_Image_Widget extends WP_Widget {
23
-
24
  var $pluginDomain = 'sp_image_widget';
25
 
26
  /**
@@ -34,33 +34,44 @@ class Tribe_Image_Widget extends WP_Widget {
34
  $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', $this->pluginDomain ) );
35
  $control_ops = array( 'id_base' => 'widget_sp_image' );
36
  $this->WP_Widget('widget_sp_image', __('Image Widget', $this->pluginDomain), $widget_ops, $control_ops);
 
37
 
38
  global $pagenow;
39
  if (defined("WP_ADMIN") && WP_ADMIN) {
40
- add_action( 'admin_init', array( $this, 'fix_async_upload_image' ) );
 
41
  if ( 'widgets.php' == $pagenow ) {
42
  wp_enqueue_style( 'thickbox' );
43
- wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js',array('thickbox'), false, true );
44
  add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
45
- } elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
 
 
46
  add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 8 );
47
  add_filter( 'gettext', array( $this, 'replace_text_in_thickbox' ), 1, 3 );
48
  add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) );
49
  }
50
  }
51
-
52
  }
53
-
 
 
 
 
 
 
54
  function fix_async_upload_image() {
55
  if(isset($_REQUEST['attachment_id'])) {
56
- $GLOBALS['post'] = get_post($_REQUEST['attachment_id']);
 
57
  }
58
  }
59
-
60
  function loadPluginTextDomain() {
61
  load_plugin_textdomain( $this->pluginDomain, false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
62
  }
63
-
64
  /**
65
  * Retrieve resized image URL
66
  *
@@ -71,7 +82,7 @@ class Tribe_Image_Widget extends WP_Widget {
71
  * @author Shane & Peter, Inc. (Peter Chester)
72
  */
73
  function get_image_url( $id, $width=false, $height=false ) {
74
-
75
  /**/
76
  // Get attachment and resize but return attachment path (needs to return url)
77
  $attachment = wp_get_attachment_metadata( $id );
@@ -113,7 +124,7 @@ class Tribe_Image_Widget extends WP_Widget {
113
  }
114
  return false;
115
  }
116
-
117
  /**
118
  * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
119
  *
@@ -131,17 +142,17 @@ class Tribe_Image_Widget extends WP_Widget {
131
  }
132
  return $translated_text;
133
  }
134
-
135
  /**
136
  * Filter image_end_to_editor results
137
  *
138
- * @param string $html
139
- * @param int $id
140
- * @param string $alt
141
- * @param string $title
142
- * @param string $align
143
- * @param string $url
144
- * @param array $size
145
  * @return string javascript array of attachment url and id or just the url
146
  * @author Shane & Peter, Inc. (Peter Chester)
147
  */
@@ -172,7 +183,7 @@ class Tribe_Image_Widget extends WP_Widget {
172
  /**
173
  * Remove from url tab until that functionality is added to widgets.
174
  *
175
- * @param array $tabs
176
  * @return void
177
  * @author Shane & Peter, Inc. (Peter Chester)
178
  */
@@ -183,12 +194,12 @@ class Tribe_Image_Widget extends WP_Widget {
183
  return $tabs;
184
  }
185
 
186
-
187
  /**
188
  * Widget frontend output
189
  *
190
- * @param array $args
191
- * @param array $instance
192
  * @return void
193
  * @author Shane & Peter, Inc. (Peter Chester)
194
  */
@@ -196,7 +207,7 @@ class Tribe_Image_Widget extends WP_Widget {
196
  extract( $args );
197
  extract( $instance );
198
  $title = apply_filters( 'widget_title', empty( $title ) ? '' : $title );
199
-
200
  include( $this->getTemplateHierarchy( 'widget' ) );
201
  }
202
 
@@ -204,7 +215,7 @@ class Tribe_Image_Widget extends WP_Widget {
204
  * Update widget options
205
  *
206
  * @param object $new_instance Widget Instance
207
- * @param object $old_instance Widget Instance
208
  * @return object
209
  * @author Shane & Peter, Inc. (Peter Chester)
210
  */
@@ -242,13 +253,13 @@ class Tribe_Image_Widget extends WP_Widget {
242
  */
243
  function form( $instance ) {
244
 
245
- $instance = wp_parse_args( (array) $instance, array(
246
- 'title' => '',
247
- 'description' => '',
248
- 'link' => '',
249
- 'linktarget' => '',
250
- 'width' => '',
251
- 'height' => '',
252
  'image' => '',
253
  'imageurl' => '',
254
  'align' => '',
@@ -256,7 +267,7 @@ class Tribe_Image_Widget extends WP_Widget {
256
  ) );
257
  include( $this->getTemplateHierarchy( 'widget-admin' ) );
258
  }
259
-
260
  /**
261
  * Admin header css
262
  *
@@ -276,20 +287,20 @@ class Tribe_Image_Widget extends WP_Widget {
276
  }
277
 
278
  /**
279
- * Loads theme files in appropriate hierarchy: 1) child theme,
280
  * 2) parent template, 3) plugin resources. will look in the image-widget/
281
  * directory in a theme and the views/ directory in the plugin
282
  *
283
  * @param string $template template file to search for
284
  * @return template path
285
- * @author Shane & Peter, Inc. (Matt Wiebe)
286
  **/
287
 
288
  function getTemplateHierarchy($template) {
289
  // whether or not .php was added
290
  $template_slug = rtrim($template, '.php');
291
  $template = $template_slug . '.php';
292
-
293
  if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
294
  $file = $theme_file;
295
  } else {
@@ -298,4 +309,3 @@ class Tribe_Image_Widget extends WP_Widget {
298
  return apply_filters( 'sp_template_image-widget_'.$template, $file);
299
  }
300
  }
301
- ?>
4
  Plugin URI: http://wordpress.org/extend/plugins/image-widget/
5
  Description: Simple image widget that uses native Wordpress upload thickbox to add image widgets to your site.
6
  Author: Modern Tribe, Inc.
7
+ Version: 3.3
8
  Author URI: http://tri.be/
9
  */
10
 
15
  add_action('widgets_init', 'tribe_load_image_widget');
16
 
17
  /**
18
+ * Tribe_Image_Widget class
19
  *
20
  * @author Shane & Peter, Inc. (Peter Chester)
21
  **/
22
  class Tribe_Image_Widget extends WP_Widget {
23
+
24
  var $pluginDomain = 'sp_image_widget';
25
 
26
  /**
34
  $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', $this->pluginDomain ) );
35
  $control_ops = array( 'id_base' => 'widget_sp_image' );
36
  $this->WP_Widget('widget_sp_image', __('Image Widget', $this->pluginDomain), $widget_ops, $control_ops);
37
+ $this->register_scripts_and_styles();
38
 
39
  global $pagenow;
40
  if (defined("WP_ADMIN") && WP_ADMIN) {
41
+ add_action( 'admin_init', array( $this, 'fix_async_upload_image' ) );
42
+
43
  if ( 'widgets.php' == $pagenow ) {
44
  wp_enqueue_style( 'thickbox' );
45
+ wp_enqueue_script( 'tribe-image-widget' );
46
  add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
47
+ }
48
+ elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
49
+ wp_enqueue_script( 'fix-browser-upload' );
50
  add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 8 );
51
  add_filter( 'gettext', array( $this, 'replace_text_in_thickbox' ), 1, 3 );
52
  add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) );
53
  }
54
  }
55
+
56
  }
57
+
58
+ function register_scripts_and_styles() {
59
+ $dir = plugins_url('/', __FILE__);
60
+ wp_register_script( 'tribe-image-widget', $dir . 'image-widget.js', array('thickbox'), false, true );
61
+ wp_register_script( 'fix-browser-upload', $dir . 'image-widget-fix-browser-upload.js', array('jquery'), false, true );
62
+ }
63
+
64
  function fix_async_upload_image() {
65
  if(isset($_REQUEST['attachment_id'])) {
66
+ $id = (int) $_REQUEST['attachment_id'];
67
+ $GLOBALS['post'] = get_post( $id );
68
  }
69
  }
70
+
71
  function loadPluginTextDomain() {
72
  load_plugin_textdomain( $this->pluginDomain, false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
73
  }
74
+
75
  /**
76
  * Retrieve resized image URL
77
  *
82
  * @author Shane & Peter, Inc. (Peter Chester)
83
  */
84
  function get_image_url( $id, $width=false, $height=false ) {
85
+
86
  /**/
87
  // Get attachment and resize but return attachment path (needs to return url)
88
  $attachment = wp_get_attachment_metadata( $id );
124
  }
125
  return false;
126
  }
127
+
128
  /**
129
  * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
130
  *
142
  }
143
  return $translated_text;
144
  }
145
+
146
  /**
147
  * Filter image_end_to_editor results
148
  *
149
+ * @param string $html
150
+ * @param int $id
151
+ * @param string $alt
152
+ * @param string $title
153
+ * @param string $align
154
+ * @param string $url
155
+ * @param array $size
156
  * @return string javascript array of attachment url and id or just the url
157
  * @author Shane & Peter, Inc. (Peter Chester)
158
  */
183
  /**
184
  * Remove from url tab until that functionality is added to widgets.
185
  *
186
+ * @param array $tabs
187
  * @return void
188
  * @author Shane & Peter, Inc. (Peter Chester)
189
  */
194
  return $tabs;
195
  }
196
 
197
+
198
  /**
199
  * Widget frontend output
200
  *
201
+ * @param array $args
202
+ * @param array $instance
203
  * @return void
204
  * @author Shane & Peter, Inc. (Peter Chester)
205
  */
207
  extract( $args );
208
  extract( $instance );
209
  $title = apply_filters( 'widget_title', empty( $title ) ? '' : $title );
210
+
211
  include( $this->getTemplateHierarchy( 'widget' ) );
212
  }
213
 
215
  * Update widget options
216
  *
217
  * @param object $new_instance Widget Instance
218
+ * @param object $old_instance Widget Instance
219
  * @return object
220
  * @author Shane & Peter, Inc. (Peter Chester)
221
  */
253
  */
254
  function form( $instance ) {
255
 
256
+ $instance = wp_parse_args( (array) $instance, array(
257
+ 'title' => '',
258
+ 'description' => '',
259
+ 'link' => '',
260
+ 'linktarget' => '',
261
+ 'width' => '',
262
+ 'height' => '',
263
  'image' => '',
264
  'imageurl' => '',
265
  'align' => '',
267
  ) );
268
  include( $this->getTemplateHierarchy( 'widget-admin' ) );
269
  }
270
+
271
  /**
272
  * Admin header css
273
  *
287
  }
288
 
289
  /**
290
+ * Loads theme files in appropriate hierarchy: 1) child theme,
291
  * 2) parent template, 3) plugin resources. will look in the image-widget/
292
  * directory in a theme and the views/ directory in the plugin
293
  *
294
  * @param string $template template file to search for
295
  * @return template path
296
+ * @author Modern Tribe, Inc. (Matt Wiebe)
297
  **/
298
 
299
  function getTemplateHierarchy($template) {
300
  // whether or not .php was added
301
  $template_slug = rtrim($template, '.php');
302
  $template = $template_slug . '.php';
303
+
304
  if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
305
  $file = $theme_file;
306
  } else {
309
  return apply_filters( 'sp_template_image-widget_'.$template, $file);
310
  }
311
  }
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: widget, image, ad, banner, simple, upload, sidebar, admin, thickbox, resize
5
  Requires at least: 3.0
6
  Tested up to: 3.3
7
- Stable tag: 3.2.11
8
 
9
  == Description ==
10
 
@@ -59,15 +59,19 @@ The Image Widget comes with a default template for the widget output. If you wou
59
 
60
  Edit the new file to your hearts content. Please do not edit the one in the plugin folder as that will cause conflicts when you update the plugin to the latest release.
61
 
62
- New in 3.2: You may now also use the "sp_template_image-widget_widget" filter to override the default template behavior for .php template files. Eg: if you wanted widget.php to reside in a folder called my-custom-templates/ and wanted it to be called my-custom-name.php:
63
 
64
- `add_filter('sp_template_image-widget_widget', 'my_template_filter');
65
  function my_template_filter($template) {
66
  return get_template_directory() . '/my-custom-templates/my-custom-name.php';
67
  }`
68
 
69
  == Changelog ==
70
 
 
 
 
 
71
  = 3.2.11 =
72
 
73
  * Yet another minor JS fix to hopefully address issues of lightbox not working
4
  Tags: widget, image, ad, banner, simple, upload, sidebar, admin, thickbox, resize
5
  Requires at least: 3.0
6
  Tested up to: 3.3
7
+ Stable tag: 3.3
8
 
9
  == Description ==
10
 
59
 
60
  Edit the new file to your hearts content. Please do not edit the one in the plugin folder as that will cause conflicts when you update the plugin to the latest release.
61
 
62
+ New in 3.2: You may now also use the "sp_template_image-widget_widget.php" filter to override the default template behavior for .php template files. Eg: if you wanted widget.php to reside in a folder called my-custom-templates/ and wanted it to be called my-custom-name.php:
63
 
64
+ `add_filter('sp_template_image-widget_widget.php', 'my_template_filter');
65
  function my_template_filter($template) {
66
  return get_template_directory() . '/my-custom-templates/my-custom-name.php';
67
  }`
68
 
69
  == Changelog ==
70
 
71
+ = 3.3 =
72
+
73
+ * Fix to allow the widget to work in the non-async (browser) uploader. Props Bjorn Wijers
74
+
75
  = 3.2.11 =
76
 
77
  * Yet another minor JS fix to hopefully address issues of lightbox not working