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
167 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Image Widget |
| 4 | Plugin URI: http://www.shaneandpeter.com/wordpress |
| 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 |
| 8 | Author URI: http://www.shaneandpeter.com |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | Bugs |
| 13 | |
| 14 | * reclicking Add image doesn't work |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | function load_sp_image_widget() { |
| 19 | register_widget('SP_Image_Widget'); |
| 20 | } |
| 21 | add_action('widgets_init', 'load_sp_image_widget'); |
| 22 | |
| 23 | class SP_Image_Widget extends WP_Widget { |
| 24 | |
| 25 | function SP_Image_Widget() { |
| 26 | $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description' ) ); |
| 27 | $control_ops = array( 'id_base' => 'widget_sp_image' ); |
| 28 | $this->WP_Widget('widget_sp_image', __('Image Widget'), $widget_ops, $control_ops); |
| 29 | |
| 30 | if (WP_ADMIN) { |
| 31 | wp_enqueue_script( 'thickbox' ); |
| 32 | wp_enqueue_style( 'thickbox' ); |
| 33 | wp_enqueue_script( $control_ops['id_base'], WP_PLUGIN_URL.'/image-widget/image-widget.js' ); |
| 34 | add_filter( 'image_send_to_editor', array( $this,'imageurl'), 10, 7 ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function get_image_url( $id, $width=false, $height=false ) { |
| 39 | |
| 40 | /**/ |
| 41 | // Get attachment and resize but return attachment path (needs to return url) |
| 42 | $attachment = wp_get_attachment_metadata( $id ); |
| 43 | $attachment_url = wp_get_attachment_url( $id ); |
| 44 | if (isset($attachment_url)) { |
| 45 | if ($width && $height) { |
| 46 | $uploads = wp_upload_dir(); |
| 47 | $imgpath = $uploads['basedir'].'/'.$attachment['file']; |
| 48 | $image = image_resize( $imgpath, $width, $height ); |
| 49 | $image = path_join( dirname($attachment_url), basename($image) ); |
| 50 | } else { |
| 51 | $image = $attachment_url; |
| 52 | } |
| 53 | if (isset($image)) { |
| 54 | return $image; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function imageurl( $html, $id, $alt, $title, $align, $url, $size ) { |
| 60 | if (strpos($_REQUEST['_wp_http_referer'],$this->id)) { // check that this is for the widget. SEE NOTE #1 |
| 61 | $img = addslashes('<img src="' . wp_get_attachment_url( $id ) . '" />'); |
| 62 | return "new Array ( '$id', '$img' )"; |
| 63 | } else { |
| 64 | return $html; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | function widget( $args, $instance ) { |
| 69 | extract($args); |
| 70 | $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); |
| 71 | echo $before_widget; |
| 72 | if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } |
| 73 | if (!empty($instance['image'])) { |
| 74 | if ($instance['link']) { |
| 75 | echo '<a class="'.$instance['classname'].'-image-link" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">'; |
| 76 | } |
| 77 | |
| 78 | if ($instance['imageurl']) { |
| 79 | echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" />"; |
| 80 | } |
| 81 | |
| 82 | if ($instance['link']) { echo '</a>'; } |
| 83 | } |
| 84 | if (!empty($instance['description'])) { |
| 85 | $text = apply_filters( 'widget_text', $instance['description'] ); |
| 86 | echo '<p class="'.$this->widget_ops['classname'].'-description" >'; |
| 87 | if ($instance['link']) { |
| 88 | echo '<a class="'.$this->widget_ops['classname'].'-image-link-p" href="'.$instance['link'].'" target="'.$instance['linktarget'].'">'; |
| 89 | } |
| 90 | echo wpautop($text); |
| 91 | if ($instance['link']) { echo '</a>'; } |
| 92 | echo "</p>"; |
| 93 | } |
| 94 | echo $after_widget; |
| 95 | } |
| 96 | |
| 97 | function update( $new_instance, $old_instance ) { |
| 98 | $instance = $old_instance; |
| 99 | $instance['title'] = strip_tags($new_instance['title']); |
| 100 | if ( isset($new_instance['description']) ) { |
| 101 | if ( current_user_can('unfiltered_html') ) { |
| 102 | $instance['description'] = $new_instance['description']; |
| 103 | } else { |
| 104 | $instance['description'] = wp_filter_post_kses($new_instance['description']); |
| 105 | } |
| 106 | } |
| 107 | $instance['link'] = $new_instance['link']; |
| 108 | $instance['image'] = $new_instance['image']; |
| 109 | $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); |
| 110 | $instance['linktarget'] = $new_instance['linktarget']; |
| 111 | $instance['width'] = $new_instance['width']; |
| 112 | $instance['height'] = $new_instance['height']; |
| 113 | |
| 114 | return $instance; |
| 115 | } |
| 116 | |
| 117 | function form( $instance ) { |
| 118 | |
| 119 | $instance = wp_parse_args( (array) $instance, array( |
| 120 | 'title' => '', |
| 121 | 'description' => '', |
| 122 | 'link' => '', |
| 123 | 'linktarget' => '', |
| 124 | 'width' => '', |
| 125 | 'height' => '', |
| 126 | 'image' => '', |
| 127 | 'imageurl' => '' |
| 128 | ) ); |
| 129 | ?> |
| 130 | |
| 131 | <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 132 | <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> |
| 133 | |
| 134 | <p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image:'); ?></label> |
| 135 | <?php |
| 136 | $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. |
| 137 | $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src"); |
| 138 | $image_title = __('Add an Image'); |
| 139 | ?><br /> |
| 140 | <a href="<?php echo $image_upload_iframe_src; ?>&TB_iframe=true" id="add_image-<?php echo $this->get_field_id('image'); ?>" class="thickbox" 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> |
| 141 | <div id="display-<?php echo $this->get_field_id('image'); ?>"><?php |
| 142 | if ($instance['imageurl']) { echo "<img src=\"{$instance['imageurl']}\" alt=\"{$instance['title']}\" />"; } |
| 143 | ?></div> |
| 144 | <input id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="hidden" value="<?php echo $instance['image']; ?>" /> |
| 145 | </p> |
| 146 | |
| 147 | <p><label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Description:'); ?></label> |
| 148 | <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> |
| 149 | |
| 150 | <p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:'); ?></label> |
| 151 | <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 /> |
| 152 | <select name="<?php echo $this->get_field_name('linktarget'); ?>" id="<?php echo $this->get_field_id('linktarget'); ?>"> |
| 153 | <option value="_self"<?php selected( $instance['linktarget'], '_self' ); ?>><?php _e('Stay in Window'); ?></option> |
| 154 | <option value="_blank"<?php selected( $instance['linktarget'], '_blank' ); ?>><?php _e('Open New Window'); ?></option> |
| 155 | </select></p> |
| 156 | |
| 157 | <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:'); ?></label> |
| 158 | <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> |
| 159 | |
| 160 | <p><label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:'); ?></label> |
| 161 | <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> |
| 162 | |
| 163 | <?php |
| 164 | } |
| 165 | } |
| 166 | ?> |
| 167 |