PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.0.5
Image Widget v3.0.5
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
image-widget.php
288 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.5
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'), 10, 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 if ($instance['link']) {
152 echo '<a class="'.$this->widget_ops['classname'].'-image-link-p" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">';
153 }
154 echo wpautop($text);
155 if ($instance['link']) { echo '</a>'; }
156 echo "</p>";
157 }
158 echo $after_widget;
159 }
160
161 /**
162 * Update widget options
163 *
164 * @param object $new_instance Widget Instance
165 * @param object $old_instance Widget Instance
166 * @return object
167 * @author Shane & Peter, Inc. (Peter Chester)
168 */
169 function update( $new_instance, $old_instance ) {
170 $instance = $old_instance;
171 $instance['title'] = strip_tags($new_instance['title']);
172 if ( isset($new_instance['description']) ) {
173 if ( current_user_can('unfiltered_html') ) {
174 $instance['description'] = $new_instance['description'];
175 } else {
176 $instance['description'] = wp_filter_post_kses($new_instance['description']);
177 }
178 }
179 $instance['link'] = $new_instance['link'];
180 $instance['image'] = $new_instance['image'];
181 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
182 $instance['linktarget'] = $new_instance['linktarget'];
183 $instance['width'] = $new_instance['width'];
184 $instance['height'] = $new_instance['height'];
185 $instance['align'] = $new_instance['align'];
186
187 return $instance;
188 }
189
190 /**
191 * Form UI
192 *
193 * @param object $instance Widget Instance
194 * @return void
195 * @author Shane & Peter, Inc. (Peter Chester)
196 */
197 function form( $instance ) {
198
199 $instance = wp_parse_args( (array) $instance, array(
200 'title' => '',
201 'description' => '',
202 'link' => '',
203 'linktarget' => '',
204 'width' => '',
205 'height' => '',
206 'image' => '',
207 'imageurl' => '',
208 'align' => ''
209 ) );
210 ?>
211
212 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sp_image_widget'); ?></label>
213 <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>
214
215 <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', 'sp_image_widget'); ?></label>
216 <?php
217 $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.
218 $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src");
219 $image_title = __(($instance['image'] ? 'Change Image' : 'Add Image'), 'sp_image_widget');
220 ?><br />
221 <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>
222 <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php
223 if ($instance['imageurl']) {
224 echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\"";
225 if ($instance['width'] && is_numeric($instance['width'])) {
226 echo "max-width: {$instance['width']}px;";
227 }
228 if ($instance['height'] && is_numeric($instance['height'])) {
229 echo "max-height: {$instance['height']}px;";
230 }
231 echo "\"";
232 if (!empty($instance['align']) && $instance['align'] != 'none') {
233 echo " class=\"align{$instance['align']}\"";
234 }
235 echo " />";
236 }
237 ?></div>
238 <br clear="all" />
239 <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" />
240 </p>
241
242 <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Caption:', 'sp_image_widget'); ?></label>
243 <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>
244
245 <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'sp_image_widget'); ?></label>
246 <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 />
247 <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>">
248 <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', 'sp_image_widget'); ?></option>
249 <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', 'sp_image_widget'); ?></option>
250 </select></p>
251
252 <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'sp_image_widget'); ?></label>
253 <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>
254
255 <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'sp_image_widget'); ?></label>
256 <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>
257
258 <p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Align:', 'sp_image_widget'); ?></label>
259 <select name="<?php echo $this->get_field_name('align'); ?>" id="<?php echo $this->get_field_id('align'); ?>" onchange="changeImgAlign('<?php echo $this->id; ?>')">
260 <option value="none"<?php selected( $instance['align'], 'none' ); ?>><?php _e('none', 'sp_image_widget'); ?></option>
261 <option value="left"<?php selected( $instance['align'], 'left' ); ?>><?php _e('left', 'sp_image_widget'); ?></option>
262 <option value="center"<?php selected( $instance['align'], 'center' ); ?>><?php _e('center', 'sp_image_widget'); ?></option>
263 <option value="right"<?php selected( $instance['align'], 'right' ); ?>><?php _e('right', 'sp_image_widget'); ?></option>
264 </select></p>
265
266 <?php
267 }
268
269 /**
270 * Admin header css
271 *
272 * @return void
273 * @author Shane & Peter, Inc. (Peter Chester)
274 */
275 function admin_head() {
276 ?>
277 <style type="text/css">
278 .aligncenter {
279 display: block;
280 margin-left: auto;
281 margin-right: auto;
282 }
283 </style>
284 <?php
285 }
286 }
287 ?>
288