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