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