PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.3
Image Widget v3.3
trunk 1.0 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2 3.2.1 3.2.10 3.2.11 3.2.2 3.2.3 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9
image-widget / image-widget.php
image-widget Last commit date
lang 14 years ago views 15 years ago image-widget-fix-browser-upload.js 14 years ago image-widget.js 14 years ago image-widget.php 14 years ago readme.txt 14 years ago screenshot-1.png 16 years ago screenshot-2.png 16 years ago screenshot-3.png 16 years ago
image-widget.php
312 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
8 Author URI: http://tri.be/
9 */
10
11 // Load the widget on widgets_init
12 function tribe_load_image_widget() {
13 register_widget('Tribe_Image_Widget');
14 }
15 add_action('widgets_init', 'tribe_load_image_widget');
16
17 /**
18 * Tribe_Image_Widget class
19 *
20 * @author Shane & Peter, Inc. (Peter Chester)
21 **/
22 class Tribe_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 Tribe_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 $this->register_scripts_and_styles();
38
39 global $pagenow;
40 if (defined("WP_ADMIN") && WP_ADMIN) {
41 add_action( 'admin_init', array( $this, 'fix_async_upload_image' ) );
42
43 if ( 'widgets.php' == $pagenow ) {
44 wp_enqueue_style( 'thickbox' );
45 wp_enqueue_script( 'tribe-image-widget' );
46 add_action( 'admin_head-widgets.php', array( $this, 'admin_head' ) );
47 }
48 elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
49 wp_enqueue_script( 'fix-browser-upload' );
50 add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 8 );
51 add_filter( 'gettext', array( $this, 'replace_text_in_thickbox' ), 1, 3 );
52 add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) );
53 }
54 }
55
56 }
57
58 function register_scripts_and_styles() {
59 $dir = plugins_url('/', __FILE__);
60 wp_register_script( 'tribe-image-widget', $dir . 'image-widget.js', array('thickbox'), false, true );
61 wp_register_script( 'fix-browser-upload', $dir . 'image-widget-fix-browser-upload.js', array('jquery'), false, true );
62 }
63
64 function fix_async_upload_image() {
65 if(isset($_REQUEST['attachment_id'])) {
66 $id = (int) $_REQUEST['attachment_id'];
67 $GLOBALS['post'] = get_post( $id );
68 }
69 }
70
71 function loadPluginTextDomain() {
72 load_plugin_textdomain( $this->pluginDomain, false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
73 }
74
75 /**
76 * Retrieve resized image URL
77 *
78 * @param int $id Post ID or Attachment ID
79 * @param int $width desired width of image (optional)
80 * @param int $height desired height of image (optional)
81 * @return string URL
82 * @author Shane & Peter, Inc. (Peter Chester)
83 */
84 function get_image_url( $id, $width=false, $height=false ) {
85
86 /**/
87 // Get attachment and resize but return attachment path (needs to return url)
88 $attachment = wp_get_attachment_metadata( $id );
89 $attachment_url = wp_get_attachment_url( $id );
90 if (isset($attachment_url)) {
91 if ($width && $height) {
92 $uploads = wp_upload_dir();
93 $imgpath = $uploads['basedir'].'/'.$attachment['file'];
94 error_log($imgpath);
95 $image = image_resize( $imgpath, $width, $height );
96 if ( $image && !is_wp_error( $image ) ) {
97 error_log( is_wp_error($image) );
98 $image = path_join( dirname($attachment_url), basename($image) );
99 } else {
100 $image = $attachment_url;
101 }
102 } else {
103 $image = $attachment_url;
104 }
105 if (isset($image)) {
106 return $image;
107 }
108 }
109 }
110
111 /**
112 * Test context to see if the uploader is being used for the image widget or for other regular uploads
113 *
114 * @return void
115 * @author Shane & Peter, Inc. (Peter Chester)
116 */
117 function is_sp_widget_context() {
118 if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) {
119 return true;
120 } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) {
121 return true;
122 } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) {
123 return true;
124 }
125 return false;
126 }
127
128 /**
129 * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
130 *
131 * @param string $translated_text text that has already been translated (normally passed straight through)
132 * @param string $source_text text as it is in the code
133 * @param string $domain domain of the text
134 * @return void
135 * @author Shane & Peter, Inc. (Peter Chester)
136 */
137 function replace_text_in_thickbox($translated_text, $source_text, $domain) {
138 if ( $this->is_sp_widget_context() ) {
139 if ('Insert into Post' == $source_text) {
140 return __('Insert Into Widget', $this->pluginDomain );
141 }
142 }
143 return $translated_text;
144 }
145
146 /**
147 * Filter image_end_to_editor results
148 *
149 * @param string $html
150 * @param int $id
151 * @param string $alt
152 * @param string $title
153 * @param string $align
154 * @param string $url
155 * @param array $size
156 * @return string javascript array of attachment url and id or just the url
157 * @author Shane & Peter, Inc. (Peter Chester)
158 */
159 function image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
160 // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption).
161 // Don't change that; instead, send custom javascript variables back to opener.
162 // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly.
163 if ( $this->is_sp_widget_context() ) {
164 if ($alt=='') $alt = $title;
165 ?>
166 <script type="text/javascript">
167 // send image variables back to opener
168 var win = window.dialogArguments || opener || parent || top;
169 win.IW_html = '<?php echo addslashes($html) ?>';
170 win.IW_img_id = '<?php echo $id ?>';
171 win.IW_alt = '<?php echo addslashes($alt) ?>';
172 win.IW_caption = '<?php echo addslashes($caption) ?>';
173 win.IW_title = '<?php echo addslashes($title) ?>';
174 win.IW_align = '<?php echo $align ?>';
175 win.IW_url = '<?php echo $url ?>';
176 win.IW_size = '<?php echo $size ?>';
177 </script>
178 <?php
179 }
180 return $html;
181 }
182
183 /**
184 * Remove from url tab until that functionality is added to widgets.
185 *
186 * @param array $tabs
187 * @return void
188 * @author Shane & Peter, Inc. (Peter Chester)
189 */
190 function media_upload_tabs($tabs) {
191 if ( $this->is_sp_widget_context() ) {
192 unset($tabs['type_url']);
193 }
194 return $tabs;
195 }
196
197
198 /**
199 * Widget frontend output
200 *
201 * @param array $args
202 * @param array $instance
203 * @return void
204 * @author Shane & Peter, Inc. (Peter Chester)
205 */
206 function widget( $args, $instance ) {
207 extract( $args );
208 extract( $instance );
209 $title = apply_filters( 'widget_title', empty( $title ) ? '' : $title );
210
211 include( $this->getTemplateHierarchy( 'widget' ) );
212 }
213
214 /**
215 * Update widget options
216 *
217 * @param object $new_instance Widget Instance
218 * @param object $old_instance Widget Instance
219 * @return object
220 * @author Shane & Peter, Inc. (Peter Chester)
221 */
222 function update( $new_instance, $old_instance ) {
223 $instance = $old_instance;
224 $instance['title'] = strip_tags($new_instance['title']);
225 if ( isset($new_instance['description']) ) {
226 if ( current_user_can('unfiltered_html') ) {
227 $instance['description'] = $new_instance['description'];
228 } else {
229 $instance['description'] = wp_filter_post_kses($new_instance['description']);
230 }
231 }
232 $instance['link'] = $new_instance['link'];
233 $instance['image'] = $new_instance['image'];
234 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
235 if( $_SERVER["HTTPS"] == "on" ) {
236 $instance['imageurl'] = str_replace('http://', 'https://', $instance['imageurl']);
237 }
238 $instance['linktarget'] = $new_instance['linktarget'];
239 $instance['width'] = $new_instance['width'];
240 $instance['height'] = $new_instance['height'];
241 $instance['align'] = $new_instance['align'];
242 $instance['alt'] = $new_instance['alt'];
243
244 return $instance;
245 }
246
247 /**
248 * Form UI
249 *
250 * @param object $instance Widget Instance
251 * @return void
252 * @author Shane & Peter, Inc. (Peter Chester)
253 */
254 function form( $instance ) {
255
256 $instance = wp_parse_args( (array) $instance, array(
257 'title' => '',
258 'description' => '',
259 'link' => '',
260 'linktarget' => '',
261 'width' => '',
262 'height' => '',
263 'image' => '',
264 'imageurl' => '',
265 'align' => '',
266 'alt' => ''
267 ) );
268 include( $this->getTemplateHierarchy( 'widget-admin' ) );
269 }
270
271 /**
272 * Admin header css
273 *
274 * @return void
275 * @author Shane & Peter, Inc. (Peter Chester)
276 */
277 function admin_head() {
278 ?>
279 <style type="text/css">
280 .aligncenter {
281 display: block;
282 margin-left: auto;
283 margin-right: auto;
284 }
285 </style>
286 <?php
287 }
288
289 /**
290 * Loads theme files in appropriate hierarchy: 1) child theme,
291 * 2) parent template, 3) plugin resources. will look in the image-widget/
292 * directory in a theme and the views/ directory in the plugin
293 *
294 * @param string $template template file to search for
295 * @return template path
296 * @author Modern Tribe, Inc. (Matt Wiebe)
297 **/
298
299 function getTemplateHierarchy($template) {
300 // whether or not .php was added
301 $template_slug = rtrim($template, '.php');
302 $template = $template_slug . '.php';
303
304 if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
305 $file = $theme_file;
306 } else {
307 $file = 'views/' . $template;
308 }
309 return apply_filters( 'sp_template_image-widget_'.$template, $file);
310 }
311 }
312