PluginProbe ʕ •ᴥ•ʔ
Image Widget / 3.3.4
Image Widget v3.3.4
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 resources 14 years ago views 14 years ago image-widget.php 14 years ago readme.txt 14 years ago screenshot-1.png 14 years ago screenshot-2.png 14 years ago screenshot-3.png 14 years ago
image-widget.php
298 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.4
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 * SP 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 }
52 $this->fix_async_upload_image();
53 }
54
55 function fix_async_upload_image() {
56 if(isset($_REQUEST['attachment_id'])) {
57 $id = (int) $_REQUEST['attachment_id'];
58 $GLOBALS['post'] = get_post( $id );
59 }
60 }
61
62 function loadPluginTextDomain() {
63 load_plugin_textdomain( 'image_widget', false, trailingslashit(basename(dirname(__FILE__))) . 'lang/');
64 }
65
66 /**
67 * Retrieve resized image URL
68 *
69 * @param int $id Post ID or Attachment ID
70 * @param int $width desired width of image (optional)
71 * @param int $height desired height of image (optional)
72 * @return string URL
73 * @author Modern Tribe, Inc. (Peter Chester)
74 */
75 function get_image_url( $id, $width=false, $height=false ) {
76
77 /**/
78 // Get attachment and resize but return attachment path (needs to return url)
79 $attachment = wp_get_attachment_metadata( $id );
80 $attachment_url = wp_get_attachment_url( $id );
81 if (isset($attachment_url)) {
82 if ($width && $height) {
83 $uploads = wp_upload_dir();
84 $imgpath = $uploads['basedir'].'/'.$attachment['file'];
85 if (WP_DEBUG) {
86 error_log(__CLASS__.'->'.__FUNCTION__.'() $imgpath = '.$imgpath);
87 }
88 $image = image_resize( $imgpath, $width, $height );
89 if ( $image && !is_wp_error( $image ) ) {
90 $image = path_join( dirname($attachment_url), basename($image) );
91 } else {
92 $image = $attachment_url;
93 }
94 } else {
95 $image = $attachment_url;
96 }
97 if (isset($image)) {
98 return $image;
99 }
100 }
101 }
102
103 /**
104 * Test context to see if the uploader is being used for the image widget or for other regular uploads
105 *
106 * @author Modern Tribe, Inc. (Peter Chester)
107 */
108 function is_sp_widget_context() {
109 if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) {
110 return true;
111 } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) {
112 return true;
113 } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) {
114 return true;
115 }
116 return false;
117 }
118
119 /**
120 * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget"
121 *
122 * @param string $translated_text text that has already been translated (normally passed straight through)
123 * @param string $source_text text as it is in the code
124 * @param string $domain domain of the text
125 * @author Modern Tribe, Inc. (Peter Chester)
126 */
127 function replace_text_in_thickbox($translated_text, $source_text, $domain) {
128 if ( $this->is_sp_widget_context() ) {
129 if ('Insert into Post' == $source_text) {
130 return __('Insert Into Widget', 'image_widget' );
131 }
132 }
133 return $translated_text;
134 }
135
136 /**
137 * Filter image_end_to_editor results
138 *
139 * @param string $html
140 * @param int $id
141 * @param string $alt
142 * @param string $title
143 * @param string $align
144 * @param string $url
145 * @param array $size
146 * @return string javascript array of attachment url and id or just the url
147 * @author Modern Tribe, Inc. (Peter Chester)
148 */
149 function image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
150 // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption).
151 // Don't change that; instead, send custom javascript variables back to opener.
152 // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly.
153 if ( $this->is_sp_widget_context() ) {
154 if ($alt=='') $alt = $title;
155 ?>
156 <script type="text/javascript">
157 // send image variables back to opener
158 var win = window.dialogArguments || opener || parent || top;
159 win.IW_html = '<?php echo addslashes($html); ?>';
160 win.IW_img_id = '<?php echo $id; ?>';
161 win.IW_alt = '<?php echo addslashes($alt); ?>';
162 win.IW_caption = '<?php echo addslashes($caption); ?>';
163 win.IW_title = '<?php echo addslashes($title); ?>';
164 win.IW_align = '<?php echo esc_attr($align); ?>';
165 win.IW_url = '<?php echo esc_url($url); ?>';
166 win.IW_size = '<?php echo esc_attr($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 * @author Modern Tribe, Inc. (Peter Chester)
178 */
179 function media_upload_tabs($tabs) {
180 if ( $this->is_sp_widget_context() ) {
181 unset($tabs['type_url']);
182 }
183 return $tabs;
184 }
185
186
187 /**
188 * Widget frontend output
189 *
190 * @param array $args
191 * @param array $instance
192 * @author Modern Tribe, Inc. (Peter Chester)
193 */
194 function widget( $args, $instance ) {
195 extract( $args );
196 extract( $instance );
197 $title = apply_filters( 'widget_title', empty( $title ) ? '' : $title );
198
199 include( $this->getTemplateHierarchy( 'widget' ) );
200 }
201
202 /**
203 * Update widget options
204 *
205 * @param object $new_instance Widget Instance
206 * @param object $old_instance Widget Instance
207 * @return object
208 * @author Modern Tribe, Inc. (Peter Chester)
209 */
210 function update( $new_instance, $old_instance ) {
211 $instance = $old_instance;
212 $instance['title'] = strip_tags($new_instance['title']);
213 if ( isset($new_instance['description']) ) {
214 if ( current_user_can('unfiltered_html') ) {
215 $instance['description'] = $new_instance['description'];
216 } else {
217 $instance['description'] = wp_filter_post_kses($new_instance['description']);
218 }
219 }
220 $instance['link'] = $new_instance['link'];
221 $instance['image'] = $new_instance['image'];
222 $instance['imageurl'] = $this->get_image_url($new_instance['image'],$new_instance['width'],$new_instance['height']); // image resizing not working right now
223 if( isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ) {
224 $instance['imageurl'] = str_replace('http://', 'https://', $instance['imageurl']);
225 }
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 * Loads theme files in appropriate hierarchy: 1) child theme,
277 * 2) parent template, 3) plugin resources. will look in the image-widget/
278 * directory in a theme and the views/ directory in the plugin
279 *
280 * @param string $template template file to search for
281 * @return template path
282 * @author Modern Tribe, Inc. (Matt Wiebe)
283 **/
284
285 function getTemplateHierarchy($template) {
286 // whether or not .php was added
287 $template_slug = rtrim($template, '.php');
288 $template = $template_slug . '.php';
289
290 if ( $theme_file = locate_template(array('image-widget/'.$template)) ) {
291 $file = $theme_file;
292 } else {
293 $file = 'views/' . $template;
294 }
295 return apply_filters( 'sp_template_image-widget_'.$template, $file);
296 }
297 }
298