PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.0.6
Image Widget v3.0.6
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
284 lines
1 <?php
2 /*
3 Plugin Name: Image Widget
4 Plugin URI: http://wordpress.org/extend/plugins/image-widget/
5 Description: This widget accepts a title, an image, a link and a description and displays them.
6 Author: Shane and Peter, Inc.
7 Version: 3.0.6
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 if (WP_ADMIN) {
36 wp_enqueue_script( 'thickbox' );
37 wp_enqueue_style( 'thickbox' );
38 wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js' );
39 // add our filter to send modified output back to image widget
40 add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 7 );
41 add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
42 }
43 }
44
45 /**
46 * Retrieve resized image URL
47 *
48 * @param int $id Post ID or Attachment ID
49 * @param int $width desired width of image (optional)
50 * @param int $height desired height of image (optional)
51 * @return string URL
52 * @author Shane & Peter, Inc. (Peter Chester)
53 */
54 function get_image_url( $id, $width=false, $height=false ) {
55
56 /**/
57 // Get attachment and resize but return attachment path (needs to return url)
58 $attachment = wp_get_attachment_metadata( $id );
59 $attachment_url = wp_get_attachment_url( $id );
60 if (isset($attachment_url)) {
61 if ($width && $height) {
62 $uploads = wp_upload_dir();
63 $imgpath = $uploads['basedir'].'/'.$attachment['file'];
64 if ($image = image_resize( $imgpath, $width, $height )) {
65 $image = path_join( dirname($attachment_url), basename($image) );
66 } else {
67 $image = $attachment_url;
68 }
69 } else {
70 $image = $attachment_url;
71 }
72 if (isset($image)) {
73 return $image;
74 }
75 }
76 }
77
78 /**
79 * Filter image_end_to_editor results
80 *
81 * @param string $html
82 * @param int $id
83 * @param string $alt
84 * @param string $title
85 * @param string $align
86 * @param string $url
87 * @param array $size
88 * @return string javascript array of attachment url and id or just the url
89 * @author Shane & Peter, Inc. (Peter Chester)
90 */
91 function image_send_to_editor( $html, $id, $alt, $title, $align, $url, $size ) {
92 // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption).
93 // Don't change that; instead, send custom javascript variables back to opener.
94 // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly.
95 if (strpos($_REQUEST['_wp_http_referer'],$this->id_base)) {
96 ?>
97 <script type="text/javascript">
98 // send image variables back to opener
99 var win = window.dialogArguments || opener || parent || top;
100 win.IW_html = '<?php echo addslashes($html) ?>';
101 win.IW_img_id = '<?php echo $id ?>';
102 win.IW_alt = '<?php echo addslashes($alt) ?>';
103 win.IW_title = '<?php echo addslashes($title) ?>';
104 win.IW_align = '<?php echo $align ?>';
105 win.IW_url = '<?php echo $url ?>';
106 win.IW_size = '<?php echo $size ?>';
107 //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);
108 </script>
109 <?php
110 }
111 return $html;
112 }
113
114 /**
115 * Widget frontend output
116 *
117 * @param array $args
118 * @param array $instance
119 * @return void
120 * @author Shane & Peter, Inc. (Peter Chester)
121 */
122 function widget( $args, $instance ) {
123 extract($args);
124 $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
125 echo $before_widget;
126 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
127 if (!empty($instance['image'])) {
128 if ($instance['link']) {
129 echo '<a class="'.$instance['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">';
130 }
131 if ($instance['imageurl']) {
132 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
133 if (!empty($instance['width']) && is_numeric($instance['width'])) {
134 echo "max-width: {$instance['width']}px;";
135 }
136 if (!empty($instance['height']) && is_numeric($instance['height'])) {
137 echo "max-height: {$instance['height']}px;";
138 }
139 echo "\"";
140 if (!empty($instance['align']) && $instance['align'] != 'none') {
141 echo " class=\"align{$instance['align']}\"";
142 }
143 echo " />";
144 }
145
146 if ($instance['link']) { echo '</a>'; }
147 }
148 if (!empty($instance['description'])) {
149 $text = apply_filters( 'widget_text', $instance['description'] );
150 echo '<p class="'.$this->widget_ops['classname'].'-description" >';
151 echo wpautop($text);
152 echo "</p>";
153 }
154 echo $after_widget;
155 }
156
157 /**
158 * Update widget options
159 *
160 * @param object $new_instance Widget Instance
161 * @param object $old_instance Widget Instance
162 * @return object
163 * @author Shane & Peter, Inc. (Peter Chester)
164 */
165 function update( $new_instance, $old_instance ) {
166 $instance = $old_instance;
167 $instance['title'] = strip_tags($new_instance['title']);
168 if ( isset($new_instance['description']) ) {
169 if ( current_user_can('unfiltered_html') ) {
170 $instance['description'] = $new_instance['description'];
171 } else {
172 $instance['description'] = wp_filter_post_kses($new_instance['description']);
173 }
174 }
175 $instance['link'] = $new_instance['link'];
176 $instance['image'] = $new_instance['image'];
177 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
178 $instance['linktarget'] = $new_instance['linktarget'];
179 $instance['width'] = $new_instance['width'];
180 $instance['height'] = $new_instance['height'];
181 $instance['align'] = $new_instance['align'];
182
183 return $instance;
184 }
185
186 /**
187 * Form UI
188 *
189 * @param object $instance Widget Instance
190 * @return void
191 * @author Shane & Peter, Inc. (Peter Chester)
192 */
193 function form( $instance ) {
194
195 $instance = wp_parse_args( (array) $instance, array(
196 'title' => '',
197 'description' => '',
198 'link' => '',
199 'linktarget' => '',
200 'width' => '',
201 'height' => '',
202 'image' => '',
203 'imageurl' => '',
204 'align' => ''
205 ) );
206 ?>
207
208 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sp_image_widget'); ?></label>
209 <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>
210
211 <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', 'sp_image_widget'); ?></label>
212 <?php
213 $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.
214 $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src");
215 $image_title = __(($instance['image'] ? 'Change Image' : 'Add Image'), 'sp_image_widget');
216 ?><br />
217 <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>
218 <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php
219 if ($instance['imageurl']) {
220 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
221 if ($instance['width'] && is_numeric($instance['width'])) {
222 echo "max-width: {$instance['width']}px;";
223 }
224 if ($instance['height'] && is_numeric($instance['height'])) {
225 echo "max-height: {$instance['height']}px;";
226 }
227 echo "\"";
228 if (!empty($instance['align']) && $instance['align'] != 'none') {
229 echo " class=\"align{$instance['align']}\"";
230 }
231 echo " />";
232 }
233 ?></div>
234 <br clear="all" />
235 <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" />
236 </p>
237
238 <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Caption:', 'sp_image_widget'); ?></label>
239 <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>
240
241 <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'sp_image_widget'); ?></label>
242 <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 />
243 <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>">
244 <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', 'sp_image_widget'); ?></option>
245 <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', 'sp_image_widget'); ?></option>
246 </select></p>
247
248 <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'sp_image_widget'); ?></label>
249 <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>
250
251 <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'sp_image_widget'); ?></label>
252 <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>
253
254 <p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Align:', 'sp_image_widget'); ?></label>
255 <select name="<?php echo $this->get_field_name('align'); ?>" id="<?php echo $this->get_field_id('align'); ?>" onchange="changeImgAlign('<?php echo $this->id; ?>')">
256 <option value="none"<?php selected( $instance['align'], 'none' ); ?>><?php _e('none', 'sp_image_widget'); ?></option>
257 <option value="left"<?php selected( $instance['align'], 'left' ); ?>><?php _e('left', 'sp_image_widget'); ?></option>
258 <option value="center"<?php selected( $instance['align'], 'center' ); ?>><?php _e('center', 'sp_image_widget'); ?></option>
259 <option value="right"<?php selected( $instance['align'], 'right' ); ?>><?php _e('right', 'sp_image_widget'); ?></option>
260 </select></p>
261
262 <?php
263 }
264
265 /**
266 * Admin header css
267 *
268 * @return void
269 * @author Shane & Peter, Inc. (Peter Chester)
270 */
271 function admin_head() {
272 ?>
273 <style type="text/css">
274 .aligncenter {
275 display: block;
276 margin-left: auto;
277 margin-right: auto;
278 }
279 </style>
280 <?php
281 }
282 }
283 ?>
284