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