PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.0.8
Image Widget v3.0.8
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
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
336 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.0.8
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 /**
25 * SP Image Widget constructor
26 *
27 * @return void
28 * @author Shane & Peter, Inc. (Peter Chester)
29 */
30 function SP_Image_Widget() {
31 $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'sp_image_widget' ) );
32 $control_ops = array( 'id_base' => 'widget_sp_image' );
33 $this->WP_Widget('widget_sp_image', __('Image Widget', 'sp_image_widget'), $widget_ops, $control_ops);
34
35 global $pagenow;
36 if (WP_ADMIN) {
37 if ( 'widgets.php' == $pagenow ) {
38 wp_enqueue_style( 'thickbox' );
39 wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js',array('thickbox'), false, true );
40 add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
41 } elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
42 add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 7 );
43 add_filter( 'gettext', array( $this, 'replace_text_in_thitckbox' ), 1, 3 );
44 add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) );
45 }
46 }
47
48 }
49
50 /**
51 * Retrieve resized image URL
52 *
53 * @param int $id Post ID or Attachment ID
54 * @param int $width desired width of image (optional)
55 * @param int $height desired height of image (optional)
56 * @return string URL
57 * @author Shane & Peter, Inc. (Peter Chester)
58 */
59 function get_image_url( $id, $width=false, $height=false ) {
60
61 /**/
62 // Get attachment and resize but return attachment path (needs to return url)
63 $attachment = wp_get_attachment_metadata( $id );
64 $attachment_url = wp_get_attachment_url( $id );
65 if (isset($attachment_url)) {
66 if ($width && $height) {
67 $uploads = wp_upload_dir();
68 $imgpath = $uploads['basedir'].'/'.$attachment['file'];
69 if ($image = image_resize( $imgpath, $width, $height )) {
70 $image = path_join( dirname($attachment_url), basename($image) );
71 } else {
72 $image = $attachment_url;
73 }
74 } else {
75 $image = $attachment_url;
76 }
77 if (isset($image)) {
78 return $image;
79 }
80 }
81 }
82
83 /**
84 * Test context to see if the uploader is being used for the image widget or for other regular uploads
85 *
86 * @return void
87 * @author Shane & Peter, Inc. (Peter Chester)
88 */
89 public function is_sp_widget_context() {
90 if ( strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false || strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) {
91 return true;
92 } else {
93 return false;
94 }
95 }
96
97 /**
98 * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
99 *
100 * @param string $translated_text text that has already been translated (normally passed straight through)
101 * @param string $source_text text as it is in the code
102 * @param string $domain domain of the text
103 * @return void
104 * @author Shane & Peter, Inc. (Peter Chester)
105 */
106 public function replace_text_in_thitckbox($translated_text, $source_text, $domain) {
107 if ( $this->is_sp_widget_context() ) {
108 if ('Insert into Post' == $source_text) {
109 return __('Insert Into Widget', 'sp_image_widget' );
110 }
111 }
112 return $translated_text;
113 }
114
115 /**
116 * Filter image_end_to_editor results
117 *
118 * @param string $html
119 * @param int $id
120 * @param string $alt
121 * @param string $title
122 * @param string $align
123 * @param string $url
124 * @param array $size
125 * @return string javascript array of attachment url and id or just the url
126 * @author Shane & Peter, Inc. (Peter Chester)
127 */
128 function image_send_to_editor( $html, $id, $alt, $title, $align, $url, $size ) {
129 // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption).
130 // Don't change that; instead, send custom javascript variables back to opener.
131 // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly.
132 if ( $this->is_sp_widget_context() ) {
133 ?>
134 <script type="text/javascript">
135 // send image variables back to opener
136 var win = window.dialogArguments || opener || parent || top;
137 win.IW_html = '<?php echo addslashes($html) ?>';
138 win.IW_img_id = '<?php echo $id ?>';
139 win.IW_alt = '<?php echo addslashes($alt) ?>';
140 win.IW_title = '<?php echo addslashes($title) ?>';
141 win.IW_align = '<?php echo $align ?>';
142 win.IW_url = '<?php echo $url ?>';
143 win.IW_size = '<?php echo $size ?>';
144 //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);
145 </script>
146 <?php
147 }
148 return $html;
149 }
150
151 /**
152 * Remove from url tab until that functionality is added to widgets.
153 *
154 * @param array $tabs
155 * @return void
156 * @author Shane & Peter, Inc. (Peter Chester)
157 */
158 public function media_upload_tabs($tabs) {
159 if ( $this->is_sp_widget_context() ) {
160 unset($tabs['type_url']);
161 return $tabs;
162 }
163 }
164
165
166 /**
167 * Widget frontend output
168 *
169 * @param array $args
170 * @param array $instance
171 * @return void
172 * @author Shane & Peter, Inc. (Peter Chester)
173 */
174 function widget( $args, $instance ) {
175 extract($args);
176 $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
177 echo $before_widget;
178 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
179 if (!empty($instance['image'])) {
180 if ($instance['link']) {
181 echo '<a class="'.$instance['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">';
182 }
183 if ($instance['imageurl']) {
184 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
185 if (!empty($instance['width']) && is_numeric($instance['width'])) {
186 echo "max-width: {$instance['width']}px;";
187 }
188 if (!empty($instance['height']) && is_numeric($instance['height'])) {
189 echo "max-height: {$instance['height']}px;";
190 }
191 echo "\"";
192 if (!empty($instance['align']) && $instance['align'] != 'none') {
193 echo " class=\"align{$instance['align']}\"";
194 }
195 echo " />";
196 }
197
198 if ($instance['link']) { echo '</a>'; }
199 }
200 if (!empty($instance['description'])) {
201 $text = apply_filters( 'widget_text', $instance['description'] );
202 echo '<p class="'.$this->widget_ops['classname'].'-description" >';
203 echo wpautop($text);
204 echo "</p>";
205 }
206 echo $after_widget;
207 }
208
209 /**
210 * Update widget options
211 *
212 * @param object $new_instance Widget Instance
213 * @param object $old_instance Widget Instance
214 * @return object
215 * @author Shane & Peter, Inc. (Peter Chester)
216 */
217 function update( $new_instance, $old_instance ) {
218 $instance = $old_instance;
219 $instance['title'] = strip_tags($new_instance['title']);
220 if ( isset($new_instance['description']) ) {
221 if ( current_user_can('unfiltered_html') ) {
222 $instance['description'] = $new_instance['description'];
223 } else {
224 $instance['description'] = wp_filter_post_kses($new_instance['description']);
225 }
226 }
227 $instance['link'] = $new_instance['link'];
228 $instance['image'] = $new_instance['image'];
229 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
230 $instance['linktarget'] = $new_instance['linktarget'];
231 $instance['width'] = $new_instance['width'];
232 $instance['height'] = $new_instance['height'];
233 $instance['align'] = $new_instance['align'];
234
235 return $instance;
236 }
237
238 /**
239 * Form UI
240 *
241 * @param object $instance Widget Instance
242 * @return void
243 * @author Shane & Peter, Inc. (Peter Chester)
244 */
245 function form( $instance ) {
246
247 $instance = wp_parse_args( (array) $instance, array(
248 'title' => '',
249 'description' => '',
250 'link' => '',
251 'linktarget' => '',
252 'width' => '',
253 'height' => '',
254 'image' => '',
255 'imageurl' => '',
256 'align' => ''
257 ) );
258 ?>
259
260 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sp_image_widget'); ?></label>
261 <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>
262
263 <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', 'sp_image_widget'); ?></label>
264 <?php
265 $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.
266 $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src");
267 $image_title = __(($instance['image'] ? 'Change Image' : 'Add Image'), 'sp_image_widget');
268 ?><br />
269 <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>
270 <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php
271 if ($instance['imageurl']) {
272 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
273 if ($instance['width'] && is_numeric($instance['width'])) {
274 echo "max-width: {$instance['width']}px;";
275 }
276 if ($instance['height'] && is_numeric($instance['height'])) {
277 echo "max-height: {$instance['height']}px;";
278 }
279 echo "\"";
280 if (!empty($instance['align']) && $instance['align'] != 'none') {
281 echo " class=\"align{$instance['align']}\"";
282 }
283 echo " />";
284 }
285 ?></div>
286 <br clear="all" />
287 <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" />
288 </p>
289
290 <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Caption:', 'sp_image_widget'); ?></label>
291 <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>
292
293 <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'sp_image_widget'); ?></label>
294 <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 />
295 <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>">
296 <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', 'sp_image_widget'); ?></option>
297 <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', 'sp_image_widget'); ?></option>
298 </select></p>
299
300 <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'sp_image_widget'); ?></label>
301 <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>
302
303 <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'sp_image_widget'); ?></label>
304 <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>
305
306 <p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Align:', 'sp_image_widget'); ?></label>
307 <select name="<?php echo $this->get_field_name('align'); ?>" id="<?php echo $this->get_field_id('align'); ?>" onchange="changeImgAlign('<?php echo $this->id; ?>')">
308 <option value="none"<?php selected( $instance['align'], 'none' ); ?>><?php _e('none', 'sp_image_widget'); ?></option>
309 <option value="left"<?php selected( $instance['align'], 'left' ); ?>><?php _e('left', 'sp_image_widget'); ?></option>
310 <option value="center"<?php selected( $instance['align'], 'center' ); ?>><?php _e('center', 'sp_image_widget'); ?></option>
311 <option value="right"<?php selected( $instance['align'], 'right' ); ?>><?php _e('right', 'sp_image_widget'); ?></option>
312 </select></p>
313
314 <?php
315 }
316
317 /**
318 * Admin header css
319 *
320 * @return void
321 * @author Shane & Peter, Inc. (Peter Chester)
322 */
323 function admin_head() {
324 ?>
325 <style type="text/css">
326 .aligncenter {
327 display: block;
328 margin-left: auto;
329 margin-right: auto;
330 }
331 </style>
332 <?php
333 }
334 }
335 ?>
336