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
228 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.3 |
| 8 | Author URI: http://www.shaneandpeter.com |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | Bugs |
| 13 | |
| 14 | * reclicking Add image doesn't work |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | // Load the widget on widgets_init |
| 20 | function load_sp_image_widget() { |
| 21 | register_widget('SP_Image_Widget'); |
| 22 | } |
| 23 | add_action('widgets_init', 'load_sp_image_widget'); |
| 24 | |
| 25 | /** |
| 26 | * SP Image Widget class |
| 27 | * |
| 28 | * @author Shane & Peter, Inc. (Peter Chester) |
| 29 | **/ |
| 30 | class SP_Image_Widget extends WP_Widget { |
| 31 | |
| 32 | /** |
| 33 | * SP Image Widget constructor |
| 34 | * |
| 35 | * @return void |
| 36 | * @author Shane & Peter, Inc. (Peter Chester) |
| 37 | */ |
| 38 | function SP_Image_Widget() { |
| 39 | $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'sp_image_widget' ) ); |
| 40 | $control_ops = array( 'id_base' => 'widget_sp_image' ); |
| 41 | $this->WP_Widget('widget_sp_image', __('Image Widget', 'sp_image_widget'), $widget_ops, $control_ops); |
| 42 | |
| 43 | if (WP_ADMIN) { |
| 44 | wp_enqueue_script( 'thickbox' ); |
| 45 | wp_enqueue_style( 'thickbox' ); |
| 46 | wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js' ); |
| 47 | add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 10, 7 ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Retrieve resized image URL |
| 53 | * |
| 54 | * @param int $id Post ID or Attachment ID |
| 55 | * @param int $width desired width of image (optional) |
| 56 | * @param int $height desired height of image (optional) |
| 57 | * @return string URL |
| 58 | * @author Shane & Peter, Inc. (Peter Chester) |
| 59 | */ |
| 60 | function get_image_url( $id, $width=false, $height=false ) { |
| 61 | |
| 62 | /**/ |
| 63 | // Get attachment and resize but return attachment path (needs to return url) |
| 64 | $attachment = wp_get_attachment_metadata( $id ); |
| 65 | $attachment_url = wp_get_attachment_url( $id ); |
| 66 | if (isset($attachment_url)) { |
| 67 | if ($width && $height) { |
| 68 | $uploads = wp_upload_dir(); |
| 69 | $imgpath = $uploads['basedir'].'/'.$attachment['file']; |
| 70 | if ($image = image_resize( $imgpath, $width, $height )) { |
| 71 | $image = path_join( dirname($attachment_url), basename($image) ); |
| 72 | } else { |
| 73 | $image = $attachment_url; |
| 74 | } |
| 75 | } else { |
| 76 | $image = $attachment_url; |
| 77 | } |
| 78 | if (isset($image)) { |
| 79 | return $image; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Filter image_end_to_editor results |
| 86 | * |
| 87 | * @param string $html |
| 88 | * @param int $id |
| 89 | * @param string $alt |
| 90 | * @param string $title |
| 91 | * @param string $align |
| 92 | * @param string $url |
| 93 | * @param array $size |
| 94 | * @return string javascript array of attachment url and id or just the url |
| 95 | * @author Shane & Peter, Inc. (Peter Chester) |
| 96 | */ |
| 97 | function image_send_to_editor( $html, $id, $alt, $title, $align, $url, $size ) { |
| 98 | if (strpos($_REQUEST['_wp_http_referer'],$this->id)) { // check that this is for the widget. SEE NOTE #1 |
| 99 | $img = addslashes('<img src="' . wp_get_attachment_url( $id ) . '" />'); |
| 100 | return "new Array ( '$id', '$img' )"; |
| 101 | } else { |
| 102 | return $html; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Widget frontend output |
| 108 | * |
| 109 | * @param array $args |
| 110 | * @param array $instance |
| 111 | * @return void |
| 112 | * @author Shane & Peter, Inc. (Peter Chester) |
| 113 | */ |
| 114 | function widget( $args, $instance ) { |
| 115 | extract($args); |
| 116 | $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); |
| 117 | echo $before_widget; |
| 118 | if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } |
| 119 | if (!empty($instance['image'])) { |
| 120 | if ($instance['link']) { |
| 121 | echo '<a class="'.$instance['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">'; |
| 122 | } |
| 123 | |
| 124 | if ($instance['imageurl']) { |
| 125 | echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" />"; |
| 126 | } |
| 127 | |
| 128 | if ($instance['link']) { echo '</a>'; } |
| 129 | } |
| 130 | if (!empty($instance['description'])) { |
| 131 | $text = apply_filters( 'widget_text', $instance['description'] ); |
| 132 | echo '<p class="'.$this->widget_ops['classname'].'-description" >'; |
| 133 | if ($instance['link']) { |
| 134 | echo '<a class="'.$this->widget_ops['classname'].'-image-link-p" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">'; |
| 135 | } |
| 136 | echo wpautop($text); |
| 137 | if ($instance['link']) { echo '</a>'; } |
| 138 | echo "</p>"; |
| 139 | } |
| 140 | echo $after_widget; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Update widget options |
| 145 | * |
| 146 | * @param object $new_instance Widget Instance |
| 147 | * @param object $old_instance Widget Instance |
| 148 | * @return object |
| 149 | * @author Shane & Peter, Inc. (Peter Chester) |
| 150 | */ |
| 151 | function update( $new_instance, $old_instance ) { |
| 152 | $instance = $old_instance; |
| 153 | $instance['title'] = strip_tags($new_instance['title']); |
| 154 | if ( isset($new_instance['description']) ) { |
| 155 | if ( current_user_can('unfiltered_html') ) { |
| 156 | $instance['description'] = $new_instance['description']; |
| 157 | } else { |
| 158 | $instance['description'] = wp_filter_post_kses($new_instance['description']); |
| 159 | } |
| 160 | } |
| 161 | $instance['link'] = $new_instance['link']; |
| 162 | $instance['image'] = $new_instance['image']; |
| 163 | $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); |
| 164 | $instance['linktarget'] = $new_instance['linktarget']; |
| 165 | $instance['width'] = $new_instance['width']; |
| 166 | $instance['height'] = $new_instance['height']; |
| 167 | |
| 168 | return $instance; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Form UI |
| 173 | * |
| 174 | * @param object $instance Widget Instance |
| 175 | * @return void |
| 176 | * @author Shane & Peter, Inc. (Peter Chester) |
| 177 | */ |
| 178 | function form( $instance ) { |
| 179 | |
| 180 | $instance = wp_parse_args( (array) $instance, array( |
| 181 | 'title' => '', |
| 182 | 'description' => '', |
| 183 | 'link' => '', |
| 184 | 'linktarget' => '', |
| 185 | 'width' => '', |
| 186 | 'height' => '', |
| 187 | 'image' => '', |
| 188 | 'imageurl' => '' |
| 189 | ) ); |
| 190 | ?> |
| 191 | |
| 192 | <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sp_image_widget'); ?></label> |
| 193 | <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> |
| 194 | |
| 195 | <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:', 'sp_image_widget'); ?></label> |
| 196 | <?php |
| 197 | $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. |
| 198 | $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src"); |
| 199 | $image_title = __('Add an Image', 'sp_image_widget'); |
| 200 | ?><br /> |
| 201 | <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->get_field_id('image'); ?>','<?php echo $this->get_field_id('width'); ?>','<?php echo $this->get_field_id('height'); ?>');return false;"><img src='images/media-button-image.gif' alt='<?php echo $image_title; ?>' /> <?php echo $image_title; ?></a> |
| 202 | <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php |
| 203 | if ($instance['imageurl']) { echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" />"; } |
| 204 | ?></div> |
| 205 | <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" /> |
| 206 | </p> |
| 207 | |
| 208 | <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Description:', 'sp_image_widget'); ?></label> |
| 209 | <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> |
| 210 | |
| 211 | <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'sp_image_widget'); ?></label> |
| 212 | <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 /> |
| 213 | <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>"> |
| 214 | <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window', 'sp_image_widget'); ?></option> |
| 215 | <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window', 'sp_image_widget'); ?></option> |
| 216 | </select></p> |
| 217 | |
| 218 | <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'sp_image_widget'); ?></label> |
| 219 | <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'])); ?>" /></p> |
| 220 | |
| 221 | <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'sp_image_widget'); ?></label> |
| 222 | <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'])); ?>" /></p> |
| 223 | |
| 224 | <?php |
| 225 | } |
| 226 | } |
| 227 | ?> |
| 228 |