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