PluginProbe ʕ •ᴥ•ʔ
Image Widget / 2.2.2
Image Widget v2.2.2
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
image-widget.php 17 years ago readme.txt 17 years ago screenshot-1.png 17 years ago
image-widget.php
563 lines
1 <?php
2
3 /*
4 Plugin Name: Image Widget
5 Plugin URI: http://www.shaneandpeter.com/wordpress
6 Description: This widget accepts a title, a link and an image and displays them. The admin panel is separated from the widget to offer independant control
7 Author: Shane and Peter, Inc. [Contributors: Kevin Miller, Nick Ohrn]
8 Version: 2.2.2
9 Author URI: http://www.shaneandpeter.com
10 */
11
12 /*
13 Feature Ideas
14
15 * Settings in the widget editor that mirror the Editor view
16 * Size setting in the widget editor
17
18 */
19
20 class sp_image_widget {
21
22 var $options = array(
23 'widget_options' => array(
24 'classname' => 'widget_sp_image',
25 'description' => 'Showcase a single image with a Title/URL/Description'
26 ),
27 'control_options' => array(
28 'width' => null,
29 'height' => 200,
30 'id_base' => 'sp_image'
31 ),
32 'default_widget_options' => array(
33 'title' => '',
34 'link' => '',
35 'linktarget' => '',
36 'description' => '',
37 'image' => ''
38 ),
39 'widget_name' => 'Image Widget'
40 );
41
42 var $admin_menu_header = 'Image Widgets';
43
44 var $is_admin_page = false;
45
46 var $is_widget_id = false;
47
48 // Setup Widget
49 function sp_image_widget() {
50
51 $this->is_admin_page = (isset($_GET['page']) && $_GET['page'] == $this->options['control_options']['id_base']) ? true : false;
52 $this->is_widget_id = (isset($_GET['widget_id'])) ? $_GET['widget_id'] : false;
53
54 add_action('admin_head', array(&$this, 'admin_head'));
55 add_action('admin_menu', array(&$this, 'admin_menu'));
56 }
57
58 // Admin Header
59 function admin_head() {
60
61 // TODO: Submit this as a patch to Wordpress
62 //
63 // FIX: Fixes the control div from hiccupping when it opens.
64 ?>
65
66 <style type="text/css">
67
68 .widget-control {
69 padding: 0px !important;
70 }
71
72 .widget-control div.widget-control-actions {
73 padding: 15px 15px 15px 15px !important;
74 }
75
76 .widget-control p {
77 padding: 15px 15px 0 15px !important;
78 }
79
80 </style>
81
82 <?php
83 }
84
85 // Admin Menu
86 function admin_menu() {
87 add_management_page($this->options['widget_name'], $this->options['widget_name'], 5, $this->options['control_options']['id_base'], array(&$this, 'control'));
88 }
89
90 // Get Widget Options
91 function get_options() {
92 return get_option('widget_' . $this->options['control_options']['id_base']);
93 }
94
95 // Set Widget Options
96 function update_options($options) {
97 return update_option('widget_' . $this->options['control_options']['id_base'], $options);
98 }
99
100
101 // Display Widget Output
102 function widget($arguments, $widget_arguments = 1) {
103
104 extract($arguments, EXTR_SKIP);
105
106 if (is_numeric($widget_arguments)) {
107 $widget_arguments = array( 'number' => $widget_arguments );
108 }
109 $widget_arguments = wp_parse_args($widget_arguments, array('number' => -1));
110 extract($widget_arguments, EXTR_SKIP);
111
112 $options = $this->get_options();
113 if (!isset($options[$number])) {
114 return;
115 }
116
117 $widget_options = $options[$number];
118
119
120 $link = !empty($widget_options['link']);
121 $linktarget = !empty($widget_options['linktarget']);
122
123 echo $before_widget;
124
125
126 if (!empty($widget_options['title'])) {
127 echo $before_title;
128 echo $widget_options['title'];
129 echo $after_title;
130 }
131
132 if (!empty($widget_options['image'])) {
133
134 if ($link) {
135 echo '<a class="' . $this->options['widget_options']['classname'] . '-image-link" href="' . $widget_options['link'] . '" target="' . $widget_options['linktarget'] . '">';
136 }
137
138 echo '<img class="' . $this->options['widget_options']['classname'] . '-image" src="' . $widget_options['image'] . '" alt="image widget" />';
139
140 if ($link) {
141 echo '</a>';
142 }
143
144 }
145
146 if (!empty($widget_options['description'])) {
147
148 echo '<p class="' . $this->options['widget_options']['classname'] . '-description" >';
149 if ($link) {
150 echo '<a class="' . $this->options['widget_options']['classname'] . '-image-link-p" href="' . $widget_options['link'] . '" target="' . $widget_options['linktarget'] . '">';
151 }
152
153 echo html_entity_decode($widget_options['description']);
154
155 if ($link) { echo '</a>'; }
156
157 echo "</p>";
158
159 }
160
161 echo $after_widget;
162
163 }
164
165
166 // Widget Registration
167 function register() {
168
169 if (!$options = $this->get_options()) {
170 $options = array();
171 }
172
173 $id = false;
174
175 foreach (array_keys($options) as $option) {
176 $widget_options = array_merge(array(), $this->options);
177
178 $id = $this->options['control_options']['id_base'] . '-' . $option;
179
180 wp_register_sidebar_widget($id, $this->options['widget_name'], array(&$this, 'widget'), $this->options['widget_options'], array('number' => $option));
181 wp_register_widget_control($id, $this->options['widget_name'], array(&$this, 'control'), $this->options['control_options'], array('number' => $option));
182 }
183
184 if (!$id) {
185 wp_register_sidebar_widget($this->options['control_options']['id_base'] . '-1', $this->options['widget_name'], array(&$this, 'widget'), $this->options['widget_options'], array('number' => -1));
186 wp_register_widget_control($this->options['control_options']['id_base'] . '-1', $this->options['widget_name'], array(&$this, 'control'), $this->options['control_options'], array('number' => -1));
187 }
188 }
189
190
191 // Widget Controller
192 function control($widget_arguments = 1) {
193
194 global $wp_registered_sidebars, $wp_registered_widgets;
195 static $updated = false;
196
197 if (is_numeric($widget_arguments)) {
198 $widget_arguments = array('number' => $widget_arguments);
199 }
200 $widget_arguments = wp_parse_args( $widget_arguments, array('number' => -1));
201 extract($widget_arguments, EXTR_SKIP);
202
203 $options = $this->get_options();
204 if (!is_array($options)) {
205 $options = array();
206 }
207
208 if ((!$updated && !empty($_POST['sidebar'])) || ($this->is_admin_page)) {
209
210 $sidebar = (string) $_POST['sidebar'];
211
212 $sidebars_widgets = wp_get_sidebars_widgets();
213 if (isset($sidebars_widgets[$sidebar])) {
214 $this_sidebar =& $sidebars_widgets[$sidebar];
215 } else {
216 $this_sidebar = array();
217 }
218 }
219
220
221 // Widget Control
222 if (!$updated && !empty($_POST['sidebar'])) {
223
224 foreach ($this_sidebar as $_widget_id) {
225
226 if ('widget_' . $this->options['control_options']['id_base'] == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number'])) {
227
228 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
229
230 if (!in_array($this->options['control_options']['id_base'] . '-$widget_number', $_POST['widget-id'])) {
231 unset($options[$widget_number]);
232 }
233 }
234 }
235
236 //
237 // UNCOMMENT TO USE THE WIDGET CONTROL FOR UPDATING THE INFORMATION INSTEAD, WILL NEED THE FORM PROCESSING CODE
238 //
239 foreach ((array) $_POST['widget-' . $this->options['control_options']['id_base']] as $widget_number => $widget_image_instance) {
240
241 $new_options = array_merge($this->options['default_widget_options'], array());
242
243 // ---------- Update Options: CUSTOM ---------- //
244
245 // WE DON'T USE THIS AS WE DON'T HAVE
246 // FORM IN THE WIDGET CONTROL!!!
247
248 // ---------- Update Options: CUSTOM ---------- //
249
250 if (!isset($options[$widget_number])) {
251 $options[$widget_number] = $new_options;
252 }
253 }
254
255 $this->update_options($options);
256
257 $updated = true;
258
259 }
260
261 $number = ($number == -1 ? '%i%' : $number);
262
263 // ---------- CUSTOMIZATIONS FOR ADMIN MENU AS WELL AS WIDGET CONTROL ---------- //
264 if ($this->is_admin_page):
265
266 // Process Form Submission
267 if ($_POST[$this->options['control_options']['id_base'] . '-submit']) {
268
269 $new_options = array_merge($this->options['default_widget_options'], array());
270
271 // ---------- Update Options: CUSTOM ---------- //
272
273 // strip off the sidebar ID that we appended in the dropdown form for navigation
274 $split_it = explode('&',$_POST['sp_image_admin_dropdown']);
275
276 $_POST['sp_image_admin_dropdown'] = $split_it[0];
277
278 // sanitize the title by removing all non ASCII characters - this include funky quotes, etc. from Word documents
279 $new_options['title'] = $_POST[$this->options['control_options']['id_base'] . '-title'];
280 $new_options['title'] = ereg_replace("[^A-Za-z0-9 _!-@#$%^&*()_+={}\":<>?/.,;'|\\~`]", "", $new_options['title']);
281 $new_options['title'] = htmlentities(stripslashes($new_options['title']));
282
283 $new_options['link'] = htmlentities(stripslashes($_POST[$this->options['control_options']['id_base'] . '-link']));
284
285 $new_options['linktarget'] = htmlentities(stripslashes($_POST[$this->options['control_options']['id_base'] . '-linktarget']));
286
287 $new_options['description'] = $_POST[$this->options['control_options']['id_base'] . '-description'];
288 $new_options['description'] = ereg_replace("[^A-Za-z0-9 _!-@#$%^&*()_+={}\":<>?/.,;'|\\~`]", "", $new_options['description']);
289 $new_options['description'] = htmlentities(stripslashes($new_options['description']));
290
291 if ($_FILES[$this->options['control_options']['id_base'] . '-image']['size'] > 0) {
292
293 $file = wp_handle_upload($_FILES[$this->options['control_options']['id_base'] . '-image'], array('test_form' => false, 'unique_filename_callback' => array($this,'sp_unique_filename') ));
294
295 // Required Debug
296 if (isset($file['error'])) {
297 die($file['error']);
298 }
299
300 $_url = str_replace(basename($file['file']), '', $file['url']);
301 $_path = str_replace(basename($file['file']), '', $file['file']);
302 $_extension = explode('/', $_FILES[$this->options['control_options']['id_base'] . '-image']['type']);
303 $_target = $this->options['control_options']['id_base'] . '-' . $_POST['sp_image_admin_dropdown'] . '-' . time() . '.' . $_extension[1];
304
305 rename($file['file'], $_path . $_target);
306
307 $url = $_url . $_target;
308 $type = $file['type'];
309 $file = $_path . $_target;
310 $file_name = basename($file);
311
312 // Construct the object array
313 $_post_object = array(
314 'post_title' => $file_name,
315 'post_content' => $url,
316 'post_mime_type' => $type,
317 'guid' => $url
318 );
319
320 $_post_id = wp_insert_attachment($_post_object, $file);
321 list($width, $height, $type, $attributes) = getimagesize($file);
322
323 wp_update_attachment_metadata($id, wp_generate_attachment_metadata($_post_id, $file));
324
325 do_action('wp_create_file_in_uploads', $file, $_post_id);
326
327 $new_options['image'] = htmlentities(stripslashes($url));
328
329 } else {
330 $new_options['image'] = $options[$_POST['sp_image_admin_dropdown']]['image'];
331 }
332
333 $options[$_POST['sp_image_admin_dropdown']] = $new_options;
334
335 // ---------- Update Options: CUSTOM ---------- //
336
337 $this->update_options($options);
338 }
339
340 $dropdown = array();
341 $first_widget_id = false;
342 $first_sidebar = false;
343
344 foreach ($sidebars_widgets as $_sidebar => $_widgets) {
345
346 if (!isset($dropdown[$_sidebar])) {
347 $dropdown[$_sidebar] = array();
348 }
349
350 foreach ($sidebars_widgets[$_sidebar] as $_widget) {
351
352 $_t = explode('-', $_widget);
353 $_widget_class = $_t[0];
354 $_widget_id = $_t[1];
355
356 if (isset($options[$_widget_id])) {
357
358 $first_widget_id = $first_widget_id ? $first_widget_id : $_widget_id;
359 $first_sidebar = $first_sidebar ? $first_sidebar : $_sidebar;
360
361 array_push($dropdown[$_sidebar],
362 array(
363 'id' => $_widget_id,
364 'classname' => $_widget_class,
365 'options' => $options[$_widget_id],
366 'selected' => (($this->is_widget_id == $_widget_id) ? true : false)
367 )
368 );
369
370 }
371
372 }
373
374 }
375
376 if ($this->is_widget_id && isset($options[$this->is_widget_id])) {
377 $form_options = $options[$this->is_widget_id];
378 } else {
379 $form_options = $options[$first_widget_id];
380 $dropdown[$first_sidebar][0]['selected'] = true;
381 }
382
383 ?>
384
385 <div class="wrap">
386
387 <h2><?php echo $this->admin_menu_header; ?></h2>
388
389 <?php if (!$first_widget_id): ?>
390
391 <p>
392 You must add a widget to a sidebar (Design &raquo; Widgets) before you can edit one.
393 </p>
394
395 <?php else: ?>
396
397 <form name="form_<?php echo $this->options['control_options']['id_base']; ?>" method="post" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
398
399 <p>
400 Select which Image Widget you would like to edit.
401 </p>
402
403 <p>
404
405 <select id="sp_image_admin_dropdown" name="sp_image_admin_dropdown" style="width: 400px;" >
406
407 <?php foreach ($dropdown as $_sidebar => $_info):
408
409 $_widget_count = 1;
410
411 foreach ($dropdown[$_sidebar] as $_widget):
412
413
414 echo '<option value="' . $_widget['id'] . '&sidebar=' . $_sidebar . '"';
415 if ($_widget['selected']) { echo ' SELECTED '; }
416 echo '>' . $wp_registered_sidebars[$_sidebar]['name'] . '&nbsp;&raquo;&nbsp;' . $this->ordinalize($_widget_count) . ' widget</option>';
417
418 $_widget_count++;
419
420 endforeach;
421
422 endforeach; ?>
423
424 </select>
425
426 <script type='text/javascript'>
427 /* <![CDATA[ */
428 var sp_image_admin_dropdown = document.getElementById('sp_image_admin_dropdown');
429 sp_image_admin_dropdown.onchange = function() {
430 widget_num = sp_image_admin_dropdown.options[sp_image_admin_dropdown.selectedIndex].value.split('&');
431 if (widget_num[0] > 0) {
432 location.href = '<?php echo get_option('home'); ?>/wp-admin/tools.php?page=<?php echo $this->options['control_options']['id_base']; ?>&widget_id=' + sp_image_admin_dropdown.options[sp_image_admin_dropdown.selectedIndex].value;
433 }
434 }
435 /* ]]> */
436 </script>
437
438 </p>
439
440 <table class="form-table">
441 <tbody>
442
443 <tr>
444 <th>
445 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][title]"><?php echo _e('Title:') ?></label>
446 </th>
447 <td>
448 <input type="text" id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][title]" name="<?php echo $this->options['control_options']['id_base']; ?>-title" value="<?php echo $form_options['title']; ?>" />
449 </td>
450 </tr>
451
452 <tr>
453 <th>
454 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][link]"><?php echo _e('Link:'); ?></label>
455 </th>
456 <td>
457 <input type="text" id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][link]" name="<?php echo $this->options['control_options']['id_base']; ?>-link" value="<?php echo $form_options['link']; ?>" >
458 <select id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][linktarget]" name="<?php echo $this->options['control_options']['id_base']; ?>-linktarget">
459 <option value="_self"<?php if ($form_options['linktarget']=="_self") { echo " selected"; } ?>>Same Window</option>
460 <option value="_blank"<?php if ($form_options['linktarget']=="_blank") { echo " selected"; } ?>>New Window</option>
461 </select>
462 </td>
463 </tr>
464
465 <tr>
466 <th>
467 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][description]"><?php echo _e('Description:'); ?></label>
468 </th>
469 <td>
470 <textarea type="text" id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][description]" name="<?php echo $this->options['control_options']['id_base']; ?>-description"><?php echo $form_options['description']; ?></textarea>
471 </td>
472 </tr>
473 <tr>
474 <th>
475 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][image]"><?php echo _e('Image:'); ?></label>
476 </th>
477 <td>
478 <input type="file" id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][image]" name="<?php echo $this->options['control_options']['id_base']; ?>-image" />
479 </td>
480 </tr>
481
482 <tr>
483 <th>
484 <label><?php echo _e('Preview Image:'); ?></label>
485 </th>
486 <td>
487 <?php if ($form_options['image']): ?>
488 <img src="<?php echo $form_options['image']; ?>" border="0" />
489 <?php endif; ?>
490 </td>
491 </tr>
492
493 </tbody>
494 </table>
495
496 <p class="submit">
497 <input type="submit" value="Save" id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][submit]" name="<?php echo $this->options['control_options']['id_base']; ?>-submit" value="1" />
498 </p>
499
500 <?php echo wp_nonce_field($this->options['control_options']['id_base']); ?>
501
502 </form>
503
504 <?php endif; ?>
505
506 </div>
507
508 <?php else: ?>
509
510 <p>
511 <small>To edit the properties of this widget visit:
512 <br />
513 <?php if ($_GET['sidebar']) $_sidebar = $_GET['sidebar']; else $_sidebar = 'sidebar-1'; ?>
514 Manage &raquo; <a href="../wp-admin/tools.php?page=<?php echo $this->options['control_options']['id_base']; ?>&widget_id=<?php echo $number; ?>&sidebar=<?php echo $_sidebar; ?>"><?php echo $this->options['widget_name']; ?></a></small>
515 <input type="hidden" id="widget-sp_image-submit-<?php echo $number; ?>" name="widget-sp_image[<?php echo $number; ?>][submit]" value="1" />
516 </p>
517
518 <?php
519 endif;
520
521 }
522
523
524 // Widget Positioning Suffixes
525 function ordinalize($number) {
526
527 if (in_array(($number % 100), range(11, 13))) {
528 return $number . 'th';
529 } else {
530
531 switch (($number % 10)) {
532 case 1:
533
534 return $number . 'st';
535 break;
536
537 case 2:
538
539 return $number . 'nd';
540 break;
541
542 case 3:
543
544 return $number . 'rd';
545
546 default:
547
548 return $number . 'th';
549 break;
550 }
551 }
552 }
553
554 }
555
556 // Instantiate class
557 $sp_image = new sp_image_widget();
558
559 // Load actions
560 add_action('widgets_init', array($sp_image, 'register'));
561
562 ?>
563