image-widget
Last commit date
lang
14 years ago
resources
13 years ago
views
14 years ago
image-widget.php
13 years ago
readme.txt
13 years ago
screenshot-1.png
14 years ago
screenshot-2.png
14 years ago
screenshot-3.png
14 years ago
image-widget.php
314 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: Modern Tribe, Inc. |
| 7 | Version: 3.3.5 |
| 8 | Author URI: http://tri.be |
| 9 | */ |
| 10 | |
| 11 | // Block direct requests |
| 12 | if ( !defined('ABSPATH') ) |
| 13 | die('-1'); |
| 14 | |
| 15 | // Load the widget on widgets_init |
| 16 | function tribe_load_image_widget() { |
| 17 | register_widget('Tribe_Image_Widget'); |
| 18 | } |
| 19 | add_action('widgets_init', 'tribe_load_image_widget'); |
| 20 | |
| 21 | /** |
| 22 | * Tribe_Image_Widget class |
| 23 | **/ |
| 24 | class Tribe_Image_Widget extends WP_Widget { |
| 25 | |
| 26 | /** |
| 27 | * Tribe Image Widget constructor |
| 28 | * |
| 29 | * @author Modern Tribe, Inc. (Peter Chester) |
| 30 | */ |
| 31 | function Tribe_Image_Widget() { |
| 32 | $this->loadPluginTextDomain(); |
| 33 | $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image_widget' ) ); |
| 34 | $control_ops = array( 'id_base' => 'widget_sp_image' ); |
| 35 | $this->WP_Widget('widget_sp_image', __('Image Widget', 'image_widget'), $widget_ops, $control_ops); |
| 36 | add_action( 'admin_init', array( $this, 'admin_setup' ) ); |
| 37 | } |
| 38 | |
| 39 | function admin_setup() { |
| 40 | global $pagenow; |
| 41 | if ( 'widgets.php' == $pagenow ) { |
| 42 | wp_enqueue_style( 'thickbox' ); |
| 43 | wp_enqueue_script( 'tribe-image-widget', plugins_url('resources/js/image-widget.js', __FILE__), array('thickbox'), FALSE, TRUE ); |
| 44 | add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) ); |
| 45 | } |
| 46 | elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) { |
| 47 | wp_enqueue_script( 'tribe-image-widget-fix-uploader', plugins_url('resources/js/image-widget-upload-fixer.js', __FILE__), array('jquery'), FALSE, TRUE ); |
| 48 | add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 8 ); |
| 49 | add_filter( 'gettext', array( $this, 'replace_text_in_thickbox' ), 1, 3 ); |
| 50 | add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) ); |
| 51 | add_filter( 'image_widget_image_url', array( $this, 'https_cleanup' ) ); |
| 52 | } |
| 53 | $this->fix_async_upload_image(); |
| 54 | } |
| 55 | |
| 56 | function fix_async_upload_image() { |
| 57 | if(isset($_REQUEST['attachment_id'])) { |
| 58 | $id = (int) $_REQUEST['attachment_id']; |
| 59 | $GLOBALS['post'] = get_post( $id ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function loadPluginTextDomain() { |
| 64 | load_plugin_textdomain( 'image_widget', false, trailingslashit(basename(dirname(__FILE__))) . 'lang/'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieve resized image URL |
| 69 | * |
| 70 | * @param int $id Post ID or Attachment ID |
| 71 | * @param int $width desired width of image (optional) |
| 72 | * @param int $height desired height of image (optional) |
| 73 | * @return string URL |
| 74 | * @author Modern Tribe, Inc. (Peter Chester) |
| 75 | */ |
| 76 | function get_image_url( $id, $width=false, $height=false ) { |
| 77 | |
| 78 | /**/ |
| 79 | // Get attachment and resize but return attachment path (needs to return url) |
| 80 | $attachment = wp_get_attachment_metadata( $id ); |
| 81 | $attachment_url = wp_get_attachment_url( $id ); |
| 82 | if (isset($attachment_url)) { |
| 83 | if ($width && $height) { |
| 84 | $uploads = wp_upload_dir(); |
| 85 | $imgpath = $uploads['basedir'].'/'.$attachment['file']; |
| 86 | if (WP_DEBUG) { |
| 87 | error_log(__CLASS__.'->'.__FUNCTION__.'() $imgpath = '.$imgpath); |
| 88 | } |
| 89 | $image = image_resize( $imgpath, $width, $height ); |
| 90 | if ( $image && !is_wp_error( $image ) ) { |
| 91 | $image = path_join( dirname($attachment_url), basename($image) ); |
| 92 | } else { |
| 93 | $image = $attachment_url; |
| 94 | } |
| 95 | } else { |
| 96 | $image = $attachment_url; |
| 97 | } |
| 98 | if (isset($image)) { |
| 99 | return $image; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Test context to see if the uploader is being used for the image widget or for other regular uploads |
| 106 | * |
| 107 | * @author Modern Tribe, Inc. (Peter Chester) |
| 108 | */ |
| 109 | function is_sp_widget_context() { |
| 110 | if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) { |
| 111 | return true; |
| 112 | } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) { |
| 113 | return true; |
| 114 | } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) { |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget" |
| 122 | * |
| 123 | * @param string $translated_text text that has already been translated (normally passed straight through) |
| 124 | * @param string $source_text text as it is in the code |
| 125 | * @param string $domain domain of the text |
| 126 | * @author Modern Tribe, Inc. (Peter Chester) |
| 127 | */ |
| 128 | function replace_text_in_thickbox($translated_text, $source_text, $domain) { |
| 129 | if ( $this->is_sp_widget_context() ) { |
| 130 | if ('Insert into Post' == $source_text) { |
| 131 | return __('Insert Into Widget', 'image_widget' ); |
| 132 | } |
| 133 | } |
| 134 | return $translated_text; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Filter image_end_to_editor results |
| 139 | * |
| 140 | * @param string $html |
| 141 | * @param int $id |
| 142 | * @param string $alt |
| 143 | * @param string $title |
| 144 | * @param string $align |
| 145 | * @param string $url |
| 146 | * @param array $size |
| 147 | * @return string javascript array of attachment url and id or just the url |
| 148 | * @author Modern Tribe, Inc. (Peter Chester) |
| 149 | */ |
| 150 | function image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { |
| 151 | // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption). |
| 152 | // Don't change that; instead, send custom javascript variables back to opener. |
| 153 | // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly. |
| 154 | if ( $this->is_sp_widget_context() ) { |
| 155 | if ($alt=='') $alt = $title; |
| 156 | ?> |
| 157 | <script type="text/javascript"> |
| 158 | // send image variables back to opener |
| 159 | var win = window.dialogArguments || opener || parent || top; |
| 160 | win.IW_html = '<?php echo addslashes($html); ?>'; |
| 161 | win.IW_img_id = '<?php echo $id; ?>'; |
| 162 | win.IW_alt = '<?php echo addslashes($alt); ?>'; |
| 163 | win.IW_caption = '<?php echo addslashes($caption); ?>'; |
| 164 | win.IW_title = '<?php echo addslashes($title); ?>'; |
| 165 | win.IW_align = '<?php echo esc_attr($align); ?>'; |
| 166 | win.IW_url = '<?php echo esc_url($url); ?>'; |
| 167 | win.IW_size = '<?php echo esc_attr($size); ?>'; |
| 168 | </script> |
| 169 | <?php |
| 170 | } |
| 171 | return $html; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Remove from url tab until that functionality is added to widgets. |
| 176 | * |
| 177 | * @param array $tabs |
| 178 | * @author Modern Tribe, 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 | * @author Modern Tribe, Inc. (Peter Chester) |
| 194 | */ |
| 195 | function widget( $args, $instance ) { |
| 196 | extract( $args ); |
| 197 | extract( $instance ); |
| 198 | if ( !empty( $imageurl ) ) { |
| 199 | $title = apply_filters( 'widget_title', empty( $title ) ? '' : $title ); |
| 200 | $imageurl = apply_filters( 'image_widget_image_url', $imageurl, $args, $instance ); |
| 201 | include( $this->getTemplateHierarchy( 'widget' ) ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Update widget options |
| 207 | * |
| 208 | * @param object $new_instance Widget Instance |
| 209 | * @param object $old_instance Widget Instance |
| 210 | * @return object |
| 211 | * @author Modern Tribe, Inc. (Peter Chester) |
| 212 | */ |
| 213 | function update( $new_instance, $old_instance ) { |
| 214 | $instance = $old_instance; |
| 215 | $instance['title'] = strip_tags($new_instance['title']); |
| 216 | if ( isset($new_instance['description']) ) { |
| 217 | if ( current_user_can('unfiltered_html') ) { |
| 218 | $instance['description'] = $new_instance['description']; |
| 219 | } else { |
| 220 | $instance['description'] = wp_filter_post_kses($new_instance['description']); |
| 221 | } |
| 222 | } |
| 223 | $instance['link'] = $new_instance['link']; |
| 224 | $instance['image'] = $new_instance['image']; |
| 225 | $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now |
| 226 | $instance['linktarget'] = $new_instance['linktarget']; |
| 227 | $instance['width'] = $new_instance['width']; |
| 228 | $instance['height'] = $new_instance['height']; |
| 229 | $instance['align'] = $new_instance['align']; |
| 230 | $instance['alt'] = $new_instance['alt']; |
| 231 | |
| 232 | return $instance; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Form UI |
| 237 | * |
| 238 | * @param object $instance Widget Instance |
| 239 | * @author Modern Tribe, Inc. (Peter Chester) |
| 240 | */ |
| 241 | function form( $instance ) { |
| 242 | |
| 243 | $instance = wp_parse_args( (array) $instance, array( |
| 244 | 'title' => '', |
| 245 | 'description' => '', |
| 246 | 'link' => '', |
| 247 | 'linktarget' => '', |
| 248 | 'width' => '', |
| 249 | 'height' => '', |
| 250 | 'image' => '', |
| 251 | 'imageurl' => '', |
| 252 | 'align' => '', |
| 253 | 'alt' => '' |
| 254 | ) ); |
| 255 | include( $this->getTemplateHierarchy( 'widget-admin' ) ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Admin header css |
| 260 | * |
| 261 | * @author Modern Tribe, Inc. (Peter Chester) |
| 262 | */ |
| 263 | function admin_head() { |
| 264 | ?> |
| 265 | <style type="text/css"> |
| 266 | .aligncenter { |
| 267 | display: block; |
| 268 | margin-left: auto; |
| 269 | margin-right: auto; |
| 270 | } |
| 271 | </style> |
| 272 | <?php |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Adjust the image url on output to account for SSL. |
| 277 | * |
| 278 | * @param string $imageurl |
| 279 | * @return string $imageurl |
| 280 | * @author Modern Tribe, Inc. (Peter Chester) |
| 281 | */ |
| 282 | function https_cleanup( $imageurl = '' ) { |
| 283 | if( isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ) { |
| 284 | $imageurl = str_replace('http://', 'https://', $imageurl); |
| 285 | } else { |
| 286 | $imageurl = str_replace('https://', 'http://', $imageurl); |
| 287 | } |
| 288 | return $imageurl; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Loads theme files in appropriate hierarchy: 1) child theme, |
| 293 | * 2) parent template, 3) plugin resources. will look in the image-widget/ |
| 294 | * directory in a theme and the views/ directory in the plugin |
| 295 | * |
| 296 | * @param string $template template file to search for |
| 297 | * @return template path |
| 298 | * @author Modern Tribe, Inc. (Matt Wiebe) |
| 299 | **/ |
| 300 | |
| 301 | function getTemplateHierarchy($template) { |
| 302 | // whether or not .php was added |
| 303 | $template_slug = rtrim($template, '.php'); |
| 304 | $template = $template_slug . '.php'; |
| 305 | |
| 306 | if ( $theme_file = locate_template(array('image-widget/'.$template)) ) { |
| 307 | $file = $theme_file; |
| 308 | } else { |
| 309 | $file = 'views/' . $template; |
| 310 | } |
| 311 | return apply_filters( 'sp_template_image-widget_'.$template, $file); |
| 312 | } |
| 313 | } |
| 314 |