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
339 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.1 |
| 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 | function is_sp_widget_context() { |
| 90 | if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) { |
| 91 | return true; |
| 92 | } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) { |
| 93 | return true; |
| 94 | } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) { |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget" |
| 102 | * |
| 103 | * @param string $translated_text text that has already been translated (normally passed straight through) |
| 104 | * @param string $source_text text as it is in the code |
| 105 | * @param string $domain domain of the text |
| 106 | * @return void |
| 107 | * @author Shane & Peter, Inc. (Peter Chester) |
| 108 | */ |
| 109 | function replace_text_in_thitckbox($translated_text, $source_text, $domain) { |
| 110 | if ( $this->is_sp_widget_context() ) { |
| 111 | if ('Insert into Post' == $source_text) { |
| 112 | return __('Insert Into Widget', 'sp_image_widget' ); |
| 113 | } |
| 114 | } |
| 115 | return $translated_text; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Filter image_end_to_editor results |
| 120 | * |
| 121 | * @param string $html |
| 122 | * @param int $id |
| 123 | * @param string $alt |
| 124 | * @param string $title |
| 125 | * @param string $align |
| 126 | * @param string $url |
| 127 | * @param array $size |
| 128 | * @return string javascript array of attachment url and id or just the url |
| 129 | * @author Shane & Peter, Inc. (Peter Chester) |
| 130 | */ |
| 131 | function image_send_to_editor( $html, $id, $alt, $title, $align, $url, $size ) { |
| 132 | // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption). |
| 133 | // Don't change that; instead, send custom javascript variables back to opener. |
| 134 | // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly. |
| 135 | if ( $this->is_sp_widget_context() ) { |
| 136 | ?> |
| 137 | <script type="text/javascript"> |
| 138 | // send image variables back to opener |
| 139 | var win = window.dialogArguments || opener || parent || top; |
| 140 | win.IW_html = '<?php echo addslashes($html) ?>'; |
| 141 | win.IW_img_id = '<?php echo $id ?>'; |
| 142 | win.IW_alt = '<?php echo addslashes($alt) ?>'; |
| 143 | win.IW_title = '<?php echo addslashes($title) ?>'; |
| 144 | win.IW_align = '<?php echo $align ?>'; |
| 145 | win.IW_url = '<?php echo $url ?>'; |
| 146 | win.IW_size = '<?php echo $size ?>'; |
| 147 | //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); |
| 148 | </script> |
| 149 | <?php |
| 150 | } |
| 151 | return $html; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Remove from url tab until that functionality is added to widgets. |
| 156 | * |
| 157 | * @param array $tabs |
| 158 | * @return void |
| 159 | * @author Shane & Peter, Inc. (Peter Chester) |
| 160 | */ |
| 161 | function media_upload_tabs($tabs) { |
| 162 | if ( $this->is_sp_widget_context() ) { |
| 163 | unset($tabs['type_url']); |
| 164 | } |
| 165 | return $tabs; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Widget frontend output |
| 171 | * |
| 172 | * @param array $args |
| 173 | * @param array $instance |
| 174 | * @return void |
| 175 | * @author Shane & Peter, Inc. (Peter Chester) |
| 176 | */ |
| 177 | function widget( $args, $instance ) { |
| 178 | extract($args); |
| 179 | $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); |
| 180 | echo $before_widget; |
| 181 | if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } |
| 182 | if (!empty($instance['image'])) { |
| 183 | if ($instance['link']) { |
| 184 | echo '<a class="'.$instance['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">'; |
| 185 | } |
| 186 | if ($instance['imageurl']) { |
| 187 | echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\""; |
| 188 | if (!empty($instance['width']) && is_numeric($instance['width'])) { |
| 189 | echo "max-width: {$instance['width']}px;"; |
| 190 | } |
| 191 | if (!empty($instance['height']) && is_numeric($instance['height'])) { |
| 192 | echo "max-height: {$instance['height']}px;"; |
| 193 | } |
| 194 | echo "\""; |
| 195 | if (!empty($instance['align']) && $instance['align'] != 'none') { |
| 196 | echo " class=\"align{$instance['align']}\""; |
| 197 | } |
| 198 | echo " />"; |
| 199 | } |
| 200 | |
| 201 | if ($instance['link']) { echo '</a>'; } |
| 202 | } |
| 203 | if (!empty($instance['description'])) { |
| 204 | $text = apply_filters( 'widget_text', $instance['description'] ); |
| 205 | echo '<p class="'.$this->widget_ops['classname'].'-description" >'; |
| 206 | echo wpautop($text); |
| 207 | echo "</p>"; |
| 208 | } |
| 209 | echo $after_widget; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Update widget options |
| 214 | * |
| 215 | * @param object $new_instance Widget Instance |
| 216 | * @param object $old_instance Widget Instance |
| 217 | * @return object |
| 218 | * @author Shane & Peter, Inc. (Peter Chester) |
| 219 | */ |
| 220 | function update( $new_instance, $old_instance ) { |
| 221 | $instance = $old_instance; |
| 222 | $instance['title'] = strip_tags($new_instance['title']); |
| 223 | if ( isset($new_instance['description']) ) { |
| 224 | if ( current_user_can('unfiltered_html') ) { |
| 225 | $instance['description'] = $new_instance['description']; |
| 226 | } else { |
| 227 | $instance['description'] = wp_filter_post_kses($new_instance['description']); |
| 228 | } |
| 229 | } |
| 230 | $instance['link'] = $new_instance['link']; |
| 231 | $instance['image'] = $new_instance['image']; |
| 232 | $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now |
| 233 | $instance['linktarget'] = $new_instance['linktarget']; |
| 234 | $instance['width'] = $new_instance['width']; |
| 235 | $instance['height'] = $new_instance['height']; |
| 236 | $instance['align'] = $new_instance['align']; |
| 237 | |
| 238 | return $instance; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Form UI |
| 243 | * |
| 244 | * @param object $instance Widget Instance |
| 245 | * @return void |
| 246 | * @author Shane & Peter, Inc. (Peter Chester) |
| 247 | */ |
| 248 | function form( $instance ) { |
| 249 | |
| 250 | $instance = wp_parse_args( (array) $instance, array( |
| 251 | 'title' => '', |
| 252 | 'description' => '', |
| 253 | 'link' => '', |
| 254 | 'linktarget' => '', |
| 255 | 'width' => '', |
| 256 | 'height' => '', |
| 257 | 'image' => '', |
| 258 | 'imageurl' => '', |
| 259 | 'align' => '' |
| 260 | ) ); |
| 261 | ?> |
| 262 | |
| 263 | <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sp_image_widget'); ?></label> |
| 264 | <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> |
| 265 | |
| 266 | <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', 'sp_image_widget'); ?></label> |
| 267 | <?php |
| 268 | $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. |
| 269 | $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src"); |
| 270 | $image_title = __(($instance['image'] ? 'Change Image' : 'Add Image'), 'sp_image_widget'); |
| 271 | ?><br /> |
| 272 | <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> |
| 273 | <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php |
| 274 | if ($instance['imageurl']) { |
| 275 | echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" style=\""; |
| 276 | if ($instance['width'] && is_numeric($instance['width'])) { |
| 277 | echo "max-width: {$instance['width']}px;"; |
| 278 | } |
| 279 | if ($instance['height'] && is_numeric($instance['height'])) { |
| 280 | echo "max-height: {$instance['height']}px;"; |
| 281 | } |
| 282 | echo "\""; |
| 283 | if (!empty($instance['align']) && $instance['align'] != 'none') { |
| 284 | echo " class=\"align{$instance['align']}\""; |
| 285 | } |
| 286 | echo " />"; |
| 287 | } |
| 288 | ?></div> |
| 289 | <br clear="all" /> |
| 290 | <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" /> |
| 291 | </p> |
| 292 | |
| 293 | <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Caption:', 'sp_image_widget'); ?></label> |
| 294 | <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> |
| 295 | |
| 296 | <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'sp_image_widget'); ?></label> |
| 297 | <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 /> |
| 298 | <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>"> |
| 299 | <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', 'sp_image_widget'); ?></option> |
| 300 | <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', 'sp_image_widget'); ?></option> |
| 301 | </select></p> |
| 302 | |
| 303 | <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'sp_image_widget'); ?></label> |
| 304 | <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> |
| 305 | |
| 306 | <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'sp_image_widget'); ?></label> |
| 307 | <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> |
| 308 | |
| 309 | <p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('Align:', 'sp_image_widget'); ?></label> |
| 310 | <select name="<?php echo $this->get_field_name('align'); ?>" id="<?php echo $this->get_field_id('align'); ?>" onchange="changeImgAlign('<?php echo $this->id; ?>')"> |
| 311 | <option value="none"<?php selected( $instance['align'], 'none' ); ?>><?php _e('none', 'sp_image_widget'); ?></option> |
| 312 | <option value="left"<?php selected( $instance['align'], 'left' ); ?>><?php _e('left', 'sp_image_widget'); ?></option> |
| 313 | <option value="center"<?php selected( $instance['align'], 'center' ); ?>><?php _e('center', 'sp_image_widget'); ?></option> |
| 314 | <option value="right"<?php selected( $instance['align'], 'right' ); ?>><?php _e('right', 'sp_image_widget'); ?></option> |
| 315 | </select></p> |
| 316 | |
| 317 | <?php |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Admin header css |
| 322 | * |
| 323 | * @return void |
| 324 | * @author Shane & Peter, Inc. (Peter Chester) |
| 325 | */ |
| 326 | function admin_head() { |
| 327 | ?> |
| 328 | <style type="text/css"> |
| 329 | .aligncenter { |
| 330 | display: block; |
| 331 | margin-left: auto; |
| 332 | margin-right: auto; |
| 333 | } |
| 334 | </style> |
| 335 | <?php |
| 336 | } |
| 337 | } |
| 338 | ?> |
| 339 |