PluginProbe ʕ •ᴥ•ʔ
Image Widget / 2.2
Image Widget v2.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
565 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
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 '<div id="'.$this->options['control_options']['id_base'].'-'.$number.'" class="widget '.$this->options['widget_options']['classname'].'">';
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 echo "</div>\n";
164
165 }
166
167
168 // Widget Registration
169 function register() {
170
171 if (!$options = $this->get_options()) {
172 $options = array();
173 }
174
175 $id = false;
176
177 foreach (array_keys($options) as $option) {
178 $widget_options = array_merge(array(), $this->options);
179
180 $id = $this->options['control_options']['id_base'] . '-' . $option;
181
182 wp_register_sidebar_widget($id, $this->options['widget_name'], array(&$this, 'widget'), $this->options['widget_options'], array('number' => $option));
183 wp_register_widget_control($id, $this->options['widget_name'], array(&$this, 'control'), $this->options['control_options'], array('number' => $option));
184 }
185
186 if (!$id) {
187 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));
188 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));
189 }
190 }
191
192
193 // Widget Controller
194 function control($widget_arguments = 1) {
195
196 global $wp_registered_sidebars, $wp_registered_widgets;
197 static $updated = false;
198
199 if (is_numeric($widget_arguments)) {
200 $widget_arguments = array('number' => $widget_arguments);
201 }
202 $widget_arguments = wp_parse_args( $widget_arguments, array('number' => -1));
203 extract($widget_arguments, EXTR_SKIP);
204
205 $options = $this->get_options();
206 if (!is_array($options)) {
207 $options = array();
208 }
209
210 if ((!$updated && !empty($_POST['sidebar'])) || ($this->is_admin_page)) {
211
212 $sidebar = (string) $_POST['sidebar'];
213
214 $sidebars_widgets = wp_get_sidebars_widgets();
215 if (isset($sidebars_widgets[$sidebar])) {
216 $this_sidebar =& $sidebars_widgets[$sidebar];
217 } else {
218 $this_sidebar = array();
219 }
220 }
221
222
223 // Widget Control
224 if (!$updated && !empty($_POST['sidebar'])) {
225
226 foreach ($this_sidebar as $_widget_id) {
227
228 if ('widget_' . $this->options['control_options']['id_base'] == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number'])) {
229
230 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
231
232 if (!in_array($this->options['control_options']['id_base'] . '-$widget_number', $_POST['widget-id'])) {
233 unset($options[$widget_number]);
234 }
235 }
236 }
237
238 //
239 // UNCOMMENT TO USE THE WIDGET CONTROL FOR UPDATING THE INFORMATION INSTEAD, WILL NEED THE FORM PROCESSING CODE
240 //
241 foreach ((array) $_POST['widget-' . $this->options['control_options']['id_base']] as $widget_number => $widget_image_instance) {
242
243 $new_options = array_merge($this->options['default_widget_options'], array());
244
245 // ---------- Update Options: CUSTOM ---------- //
246
247 // WE DON'T USE THIS AS WE DON'T HAVE
248 // FORM IN THE WIDGET CONTROL!!!
249
250 // ---------- Update Options: CUSTOM ---------- //
251
252 if (!isset($options[$widget_number])) {
253 $options[$widget_number] = $new_options;
254 }
255 }
256
257 $this->update_options($options);
258
259 $updated = true;
260
261 }
262
263 $number = ($number == -1 ? '%i%' : $number);
264
265 // ---------- CUSTOMIZATIONS FOR ADMIN MENU AS WELL AS WIDGET CONTROL ---------- //
266 if ($this->is_admin_page):
267
268 // Process Form Submission
269 if ($_POST[$this->options['control_options']['id_base'] . '-submit']) {
270
271 $new_options = array_merge($this->options['default_widget_options'], array());
272
273 // ---------- Update Options: CUSTOM ---------- //
274
275 // strip off the sidebar ID that we appended in the dropdown form for navigation
276 $split_it = explode('&',$_POST['sp_image_admin_dropdown']);
277
278 $_POST['sp_image_admin_dropdown'] = $split_it[0];
279
280 // sanitize the title by removing all non ASCII characters - this include funky quotes, etc. from Word documents
281 $new_options['title'] = $_POST[$this->options['control_options']['id_base'] . '-title'];
282 $new_options['title'] = ereg_replace("[^A-Za-z0-9 _!-@#$%^&*()_+={}\":<>?/.,;'|\\~`]", "", $new_options['title']);
283 $new_options['title'] = htmlentities(stripslashes($new_options['title']));
284
285 $new_options['link'] = htmlentities(stripslashes($_POST[$this->options['control_options']['id_base'] . '-link']));
286
287 $new_options['linktarget'] = htmlentities(stripslashes($_POST[$this->options['control_options']['id_base'] . '-linktarget']));
288
289 $new_options['description'] = $_POST[$this->options['control_options']['id_base'] . '-description'];
290 $new_options['description'] = ereg_replace("[^A-Za-z0-9 _!-@#$%^&*()_+={}\":<>?/.,;'|\\~`]", "", $new_options['description']);
291 $new_options['description'] = htmlentities(stripslashes($new_options['description']));
292
293 if ($_FILES[$this->options['control_options']['id_base'] . '-image']['size'] > 0) {
294
295 $file = wp_handle_upload($_FILES[$this->options['control_options']['id_base'] . '-image'], array('test_form' => false, 'unique_filename_callback' => array($this,'sp_unique_filename') ));
296
297 // Required Debug
298 if (isset($file['error'])) {
299 die($file['error']);
300 }
301
302 $_url = str_replace(basename($file['file']), '', $file['url']);
303 $_path = str_replace(basename($file['file']), '', $file['file']);
304 $_extension = explode('/', $_FILES[$this->options['control_options']['id_base'] . '-image']['type']);
305 $_target = $this->options['control_options']['id_base'] . '-' . $_POST['sp_image_admin_dropdown'] . '-' . time() . '.' . $_extension[1];
306
307 rename($file['file'], $_path . $_target);
308
309 $url = $_url . $_target;
310 $type = $file['type'];
311 $file = $_path . $_target;
312 $file_name = basename($file);
313
314 // Construct the object array
315 $_post_object = array(
316 'post_title' => $file_name,
317 'post_content' => $url,
318 'post_mime_type' => $type,
319 'guid' => $url
320 );
321
322 $_post_id = wp_insert_attachment($_post_object, $file);
323 list($width, $height, $type, $attributes) = getimagesize($file);
324
325 wp_update_attachment_metadata($id, wp_generate_attachment_metadata($_post_id, $file));
326
327 do_action('wp_create_file_in_uploads', $file, $_post_id);
328
329 $new_options['image'] = htmlentities(stripslashes($url));
330
331 } else {
332 $new_options['image'] = $options[$_POST['sp_image_admin_dropdown']]['image'];
333 }
334
335 $options[$_POST['sp_image_admin_dropdown']] = $new_options;
336
337 // ---------- Update Options: CUSTOM ---------- //
338
339 $this->update_options($options);
340 }
341
342 $dropdown = array();
343 $first_widget_id = false;
344 $first_sidebar = false;
345
346 foreach ($sidebars_widgets as $_sidebar => $_widgets) {
347
348 if (!isset($dropdown[$_sidebar])) {
349 $dropdown[$_sidebar] = array();
350 }
351
352 foreach ($sidebars_widgets[$_sidebar] as $_widget) {
353
354 $_t = explode('-', $_widget);
355 $_widget_class = $_t[0];
356 $_widget_id = $_t[1];
357
358 if (isset($options[$_widget_id])) {
359
360 $first_widget_id = $first_widget_id ? $first_widget_id : $_widget_id;
361 $first_sidebar = $first_sidebar ? $first_sidebar : $_sidebar;
362
363 array_push($dropdown[$_sidebar],
364 array(
365 'id' => $_widget_id,
366 'classname' => $_widget_class,
367 'options' => $options[$_widget_id],
368 'selected' => (($this->is_widget_id == $_widget_id) ? true : false)
369 )
370 );
371
372 }
373
374 }
375
376 }
377
378 if ($this->is_widget_id && isset($options[$this->is_widget_id])) {
379 $form_options = $options[$this->is_widget_id];
380 } else {
381 $form_options = $options[$first_widget_id];
382 $dropdown[$first_sidebar][0]['selected'] = true;
383 }
384
385 ?>
386
387 <div class="wrap">
388
389 <h2><?php echo $this->admin_menu_header; ?></h2>
390
391 <?php if (!$first_widget_id): ?>
392
393 <p>
394 You must add a widget to a sidebar (Design &raquo; Widgets) before you can edit one.
395 </p>
396
397 <?php else: ?>
398
399 <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">
400
401 <p>
402 Select which Image Widget you would like to edit.
403 </p>
404
405 <p>
406
407 <select id="sp_image_admin_dropdown" name="sp_image_admin_dropdown" style="width: 400px;" >
408
409 <?php foreach ($dropdown as $_sidebar => $_info):
410
411 $_widget_count = 1;
412
413 foreach ($dropdown[$_sidebar] as $_widget):
414
415
416 echo '<option value="' . $_widget['id'] . '&sidebar=' . $_sidebar . '"';
417 if ($_widget['selected']) { echo ' SELECTED '; }
418 echo '>' . $wp_registered_sidebars[$_sidebar]['name'] . '&nbsp;&raquo;&nbsp;' . $this->ordinalize($_widget_count) . ' widget</option>';
419
420 $_widget_count++;
421
422 endforeach;
423
424 endforeach; ?>
425
426 </select>
427
428 <script type='text/javascript'>
429 /* <![CDATA[ */
430 var sp_image_admin_dropdown = document.getElementById('sp_image_admin_dropdown');
431 sp_image_admin_dropdown.onchange = function() {
432 widget_num = sp_image_admin_dropdown.options[sp_image_admin_dropdown.selectedIndex].value.split('&');
433 if (widget_num[0] > 0) {
434 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;
435 }
436 }
437 /* ]]> */
438 </script>
439
440 </p>
441
442 <table class="form-table">
443 <tbody>
444
445 <tr>
446 <th>
447 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][title]"><?php echo _e('Title:') ?></label>
448 </th>
449 <td>
450 <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']; ?>" />
451 </td>
452 </tr>
453
454 <tr>
455 <th>
456 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][link]"><?php echo _e('Link:'); ?></label>
457 </th>
458 <td>
459 <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']; ?>" >
460 <select id="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][linktarget]" name="<?php echo $this->options['control_options']['id_base']; ?>-linktarget">
461 <option value="_self"<?php if ($form_options['linktarget']=="_self") { echo " selected"; } ?>>Same Window</option>
462 <option value="_blank"<?php if ($form_options['linktarget']=="_blank") { echo " selected"; } ?>>New Window</option>
463 </select>
464 </td>
465 </tr>
466
467 <tr>
468 <th>
469 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][description]"><?php echo _e('Description:'); ?></label>
470 </th>
471 <td>
472 <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>
473 </td>
474 </tr>
475 <tr>
476 <th>
477 <label for="<?php echo $this->options['control_options']['id_base']; ?>[<?php echo $number; ?>][image]"><?php echo _e('Image:'); ?></label>
478 </th>
479 <td>
480 <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" />
481 </td>
482 </tr>
483
484 <tr>
485 <th>
486 <label><?php echo _e('Preview Image:'); ?></label>
487 </th>
488 <td>
489 <?php if ($form_options['image']): ?>
490 <img src="<?php echo $form_options['image']; ?>" border="0" />
491 <?php endif; ?>
492 </td>
493 </tr>
494
495 </tbody>
496 </table>
497
498 <p class="submit">
499 <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" />
500 </p>
501
502 <?php echo wp_nonce_field($this->options['control_options']['id_base']); ?>
503
504 </form>
505
506 <?php endif; ?>
507
508 </div>
509
510 <?php else: ?>
511
512 <p>
513 <small>To edit the properties of this widget visit:
514 <br />
515 <?php if ($_GET['sidebar']) $_sidebar = $_GET['sidebar']; else $_sidebar = 'sidebar-1'; ?>
516 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>
517 <input type="hidden" id="widget-sp_image-submit-<?php echo $number; ?>" name="widget-sp_image[<?php echo $number; ?>][submit]" value="1" />
518 </p>
519
520 <?php
521 endif;
522
523 }
524
525
526 // Widget Positioning Suffixes
527 function ordinalize($number) {
528
529 if (in_array(($number % 100), range(11, 13))) {
530 return $number . 'th';
531 } else {
532
533 switch (($number % 10)) {
534 case 1:
535
536 return $number . 'st';
537 break;
538
539 case 2:
540
541 return $number . 'nd';
542 break;
543
544 case 3:
545
546 return $number . 'rd';
547
548 default:
549
550 return $number . 'th';
551 break;
552 }
553 }
554 }
555
556 }
557
558 // Instantiate class
559 $sp_image = new sp_image_widget();
560
561 // Load actions
562 add_action('widgets_init', array($sp_image, 'register'));
563
564 ?>
565