PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.1.3
Image Widget v3.1.3
trunk 1.0 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2 3.2.1 3.2.10 3.2.11 3.2.2 3.2.3 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9
image-widget / image-widget.php
image-widget Last commit date
lang 16 years ago image-widget.js 16 years ago image-widget.php 16 years ago readme.txt 16 years ago screenshot-1.png 16 years ago screenshot-2.png 16 years ago screenshot-3.png 16 years ago
image-widget.php
346 lines
1 <?php
2 /*
3 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: Shane and Peter, Inc.
7 Version: 3.1.3
8 Author URI: http://www.shaneandpeter.com
9 */
10
11 // Load the widget on widgets_init
12 function load_sp_image_widget() {
13 register_widget('SP_Image_Widget');
14 }
15 add_action('widgets_init', 'load_sp_image_widget');
16
17 /**
18 * SP Image Widget class
19 *
20 * @author Shane & Peter, Inc. (Peter Chester)
21 **/
22 class SP_Image_Widget extends WP_Widget {
23
24 public $pluginDomain = 'sp_image_widget';
25
26 /**
27 * SP Image Widget constructor
28 *
29 * @return void
30 * @author Shane & Peter, Inc. (Peter Chester)
31 */
32 function SP_Image_Widget() {
33 $this->loadPluginTextDomain();
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 (WP_ADMIN) {
40 if ( 'widgets.php' == $pagenow ) {
41 wp_enqueue_style( 'thickbox' );
42 wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js',array('thickbox'), false, true );
43 add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
44 } elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
45 add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 7 );
46 add_filter( 'gettext', array( $this, 'replace_text_in_thitckbox' ), 1, 3 );
47 add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) );
48 }
49 }
50
51 }
52
53 function loadPluginTextDomain() {
54 load_plugin_textdomain( $this->pluginDomain, false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
55 }
56
57 /**
58 * Retrieve resized image URL
59 *
60 * @param int $id Post ID or Attachment ID
61 * @param int $width desired width of image (optional)
62 * @param int $height desired height of image (optional)
63 * @return string URL
64 * @author Shane & Peter, Inc. (Peter Chester)
65 */
66 function get_image_url( $id, $width=false, $height=false ) {
67
68 /**/
69 // Get attachment and resize but return attachment path (needs to return url)
70 $attachment = wp_get_attachment_metadata( $id );
71 $attachment_url = wp_get_attachment_url( $id );
72 if (isset($attachment_url)) {
73 if ($width && $height) {
74 $uploads = wp_upload_dir();
75 $imgpath = $uploads['basedir'].'/'.$attachment['file'];
76 if ($image = image_resize( $imgpath, $width, $height )) {
77 $image = path_join( dirname($attachment_url), basename($image) );
78 } else {
79 $image = $attachment_url;
80 }
81 } else {
82 $image = $attachment_url;
83 }
84 if (isset($image)) {
85 return $image;
86 }
87 }
88 }
89
90 /**
91 * Test context to see if the uploader is being used for the image widget or for other regular uploads
92 *
93 * @return void
94 * @author Shane & Peter, Inc. (Peter Chester)
95 */
96 function is_sp_widget_context() {
97 if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) {
98 return true;
99 } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) {
100 return true;
101 } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) {
102 return true;
103 }
104 return false;
105 }
106
107 /**
108 * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
109 *
110 * @param string $translated_text text that has already been translated (normally passed straight through)
111 * @param string $source_text text as it is in the code
112 * @param string $domain domain of the text
113 * @return void
114 * @author Shane & Peter, Inc. (Peter Chester)
115 */
116 function replace_text_in_thitckbox($translated_text, $source_text, $domain) {
117 if ( $this->is_sp_widget_context() ) {
118 if ('Insert into Post' == $source_text) {
119 return __('Insert Into Widget', $this->pluginDomain );
120 }
121 }
122 return $translated_text;
123 }
124
125 /**
126 * Filter image_end_to_editor results
127 *
128 * @param string $html
129 * @param int $id
130 * @param string $alt
131 * @param string $title
132 * @param string $align
133 * @param string $url
134 * @param array $size
135 * @return string javascript array of attachment url and id or just the url
136 * @author Shane & Peter, Inc. (Peter Chester)
137 */
138 function image_send_to_editor( $html, $id, $alt, $title, $align, $url, $size ) {
139 // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption).
140 // Don't change that; instead, send custom javascript variables back to opener.
141 // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly.
142 if ( $this->is_sp_widget_context() ) {
143 ?>
144 <script type="text/javascript">
145 // send image variables back to opener
146 var win = window.dialogArguments || opener || parent || top;
147 win.IW_html = '<?php echo addslashes($html) ?>';
148 win.IW_img_id = '<?php echo $id ?>';
149 win.IW_alt = '<?php echo addslashes($alt) ?>';
150 win.IW_title = '<?php echo addslashes($title) ?>';
151 win.IW_align = '<?php echo $align ?>';
152 win.IW_url = '<?php echo $url ?>';
153 win.IW_size = '<?php echo $size ?>';
154 //alert("sending variables: id: "+win.IW_img_id+"\n"+"alt: "+win.IW_alt+"\n"+"title: "+win.IW_title+"\n"+"align: "+win.IW_align+"\n"+"url: "+win.IW_url+"\n"+"size: "+win.IW_size);
155 </script>
156 <?php
157 }
158 return $html;
159 }
160
161 /**
162 * Remove from url tab until that functionality is added to widgets.
163 *
164 * @param array $tabs
165 * @return void
166 * @author Shane & Peter, Inc. (Peter Chester)
167 */
168 function media_upload_tabs($tabs) {
169 if ( $this->is_sp_widget_context() ) {
170 unset($tabs['type_url']);
171 }
172 return $tabs;
173 }
174
175
176 /**
177 * Widget frontend output
178 *
179 * @param array $args
180 * @param array $instance
181 * @return void
182 * @author Shane & Peter, Inc. (Peter Chester)
183 */
184 function widget( $args, $instance ) {
185 extract($args);
186 $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
187 echo $before_widget;
188 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
189 if (!empty($instance['image'])) {
190 if ($instance['link']) {
191 echo '<a class="'.$this->widget_options['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">';
192 }
193 if ($instance['imageurl']) {
194 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
195 if (!empty($instance['width']) && is_numeric($instance['width'])) {
196 echo "max-width: {$instance['width']}px;";
197 }
198 if (!empty($instance['height']) && is_numeric($instance['height'])) {
199 echo "max-height: {$instance['height']}px;";
200 }
201 echo "\"";
202 if (!empty($instance['align']) && $instance['align'] != 'none') {
203 echo " class=\"align{$instance['align']}\"";
204 }
205 echo " />";
206 }
207
208 if ($instance['link']) { echo '</a>'; }
209 }
210 if (!empty($instance['description'])) {
211 $text = apply_filters( 'widget_text', $instance['description'] );
212 echo '<div class="'.$this->widget_options['classname'].'-description" >';
213 echo wpautop($text);
214 echo "</div>";
215 }
216 echo $after_widget;
217 }
218
219 /**
220 * Update widget options
221 *
222 * @param object $new_instance Widget Instance
223 * @param object $old_instance Widget Instance
224 * @return object
225 * @author Shane & Peter, Inc. (Peter Chester)
226 */
227 function update( $new_instance, $old_instance ) {
228 $instance = $old_instance;
229 $instance['title'] = strip_tags($new_instance['title']);
230 if ( isset($new_instance['description']) ) {
231 if ( current_user_can('unfiltered_html') ) {
232 $instance['description'] = $new_instance['description'];
233 } else {
234 $instance['description'] = wp_filter_post_kses($new_instance['description']);
235 }
236 }
237 $instance['link'] = $new_instance['link'];
238 $instance['image'] = $new_instance['image'];
239 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
240 $instance['linktarget'] = $new_instance['linktarget'];
241 $instance['width'] = $new_instance['width'];
242 $instance['height'] = $new_instance['height'];
243 $instance['align'] = $new_instance['align'];
244
245 return $instance;
246 }
247
248 /**
249 * Form UI
250 *
251 * @param object $instance Widget Instance
252 * @return void
253 * @author Shane & Peter, Inc. (Peter Chester)
254 */
255 function form( $instance ) {
256
257 $instance = wp_parse_args( (array) $instance, array(
258 'title' => '',
259 'description' => '',
260 'link' => '',
261 'linktarget' => '',
262 'width' => '',
263 'height' => '',
264 'image' => '',
265 'imageurl' => '',
266 'align' => ''
267 ) );
268 ?>
269
270 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', $this->pluginDomain); ?></label>
271 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr(strip_tags($instance['title'])); ?>" /></p>
272
273 <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', $this->pluginDomain); ?></label>
274 <?php
275 $media_upload_iframe_src = "media-upload.php?type=image&widget_id=".$this->id; //NOTE #1: the widget id is added here to allow uploader to only return array if this is used with image widget so that all other uploads are not harmed.
276 $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src");
277 $image_title = __(($instance['image'] ? 'Change Image' : 'Add Image'), $this->pluginDomain);
278 ?><br />
279 <a href="<?php echo $image_upload_iframe_src; ?>&TB_iframe=true" id="add_image-<?php echo $this->get_field_id('image'); ?>" class="thickbox-image-widget" title='<?php echo $image_title; ?>' onClick="set_active_widget('<?php echo $this->id; ?>');return false;" style="text-decoration:none"><img src='images/media-button-image.gif' alt='<?php echo $image_title; ?>' align="absmiddle" /> <?php echo $image_title; ?></a>
280 <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php
281 if ($instance['imageurl']) {
282 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
283 if ($instance['width'] && is_numeric($instance['width'])) {
284 echo "max-width: {$instance['width']}px;";
285 }
286 if ($instance['height'] && is_numeric($instance['height'])) {
287 echo "max-height: {$instance['height']}px;";
288 }
289 echo "\"";
290 if (!empty($instance['align']) && $instance['align'] != 'none') {
291 echo " class=\"align{$instance['align']}\"";
292 }
293 echo " />";
294 }
295 ?></div>
296 <br clear="all" />
297 <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" />
298 </p>
299
300 <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Caption:', $this->pluginDomain); ?></label>
301 <textarea rows="8" class="widefat" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>"><?php echo format_to_edit($instance['description']); ?></textarea></p>
302
303 <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', $this->pluginDomain); ?></label>
304 <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo esc_attr(strip_tags($instance['link'])); ?>" /><br />
305 <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>">
306 <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', $this->pluginDomain); ?></option>
307 <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', $this->pluginDomain); ?></option>
308 </select></p>
309
310 <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', $this->pluginDomain); ?></label>
311 <input id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="text" value="<?php echo esc_attr(strip_tags($instance['width'])); ?>" onchange="changeImgWidth('<?php echo $this->id; ?>')" /></p>
312
313 <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', $this->pluginDomain); ?></label>
314 <input id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" type="text" value="<?php echo esc_attr(strip_tags($instance['height'])); ?>" onchange="changeImgHeight('<?php echo $this->id; ?>')" /></p>
315
316 <p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Align:', $this->pluginDomain); ?></label>
317 <select name="<?php echo $this->get_field_name('align'); ?>" id="<?php echo $this->get_field_id('align'); ?>" onchange="changeImgAlign('<?php echo $this->id; ?>')">
318 <option value="none"<?php selected( $instance['align'], 'none' ); ?>><?php _e('none', $this->pluginDomain); ?></option>
319 <option value="left"<?php selected( $instance['align'], 'left' ); ?>><?php _e('left', $this->pluginDomain); ?></option>
320 <option value="center"<?php selected( $instance['align'], 'center' ); ?>><?php _e('center', $this->pluginDomain); ?></option>
321 <option value="right"<?php selected( $instance['align'], 'right' ); ?>><?php _e('right', $this->pluginDomain); ?></option>
322 </select></p>
323
324 <?php
325 }
326
327 /**
328 * Admin header css
329 *
330 * @return void
331 * @author Shane & Peter, Inc. (Peter Chester)
332 */
333 function admin_head() {
334 ?>
335 <style type="text/css">
336 .aligncenter {
337 display: block;
338 margin-left: auto;
339 margin-right: auto;
340 }
341 </style>
342 <?php
343 }
344 }
345 ?>
346