1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Sidebar Widgets |
---|
4 | Plugin URI: http://svn.wp-plugins.org/widgets/trunk |
---|
5 | Description: Adds "Sidebar Widgets" panel under Presentation menu |
---|
6 | Author: Automattic, Inc. |
---|
7 | Author URI: http://automattic.com/ |
---|
8 | Version: 1.0.20060724 |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | //////////////////////////////////////////////////////////// Global Variables |
---|
13 | |
---|
14 | $registered_sidebars = array(); |
---|
15 | $registered_widgets = array(); |
---|
16 | $registered_widget_controls = array(); |
---|
17 | $registered_widget_styles = array(); |
---|
18 | $register_widget_defaults = false; // When true, registration is non-destructive. |
---|
19 | |
---|
20 | //////////////////////////////////////////////////////////// Public Functions |
---|
21 | |
---|
22 | function register_sidebars($number = 1, $args = array()) { |
---|
23 | global $registered_sidebars; |
---|
24 | |
---|
25 | $number = (int) $number; |
---|
26 | |
---|
27 | if ( is_string($args) ) |
---|
28 | parse_str($args, $args); |
---|
29 | |
---|
30 | $i = 1; |
---|
31 | |
---|
32 | while ( $i <= $number ) { |
---|
33 | $_args = $args; |
---|
34 | if ( $number > 1 ) { |
---|
35 | $_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d', 'widgets'), $i); |
---|
36 | } else { |
---|
37 | $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar', 'widgets'); |
---|
38 | } |
---|
39 | $_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i"; |
---|
40 | register_sidebar($_args); |
---|
41 | ++$i; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | function register_sidebar($args = array()) { |
---|
46 | global $registered_sidebars; |
---|
47 | |
---|
48 | if ( is_string($args) ) |
---|
49 | parse_str($args, $args); |
---|
50 | |
---|
51 | $i = count($registered_sidebars) + 1; |
---|
52 | |
---|
53 | $defaults = array( |
---|
54 | 'name' => sprintf(__('Sidebar %d', 'widgets'), count($registered_sidebars) + 1 ), |
---|
55 | 'id' => "sidebar-$i", |
---|
56 | 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
---|
57 | 'after_widget' => "</li>\n", |
---|
58 | 'before_title' => '<h2 class="widgettitle">', |
---|
59 | 'after_title' => "</h2>\n", |
---|
60 | ); |
---|
61 | |
---|
62 | $sidebar = array_merge($defaults, $args); |
---|
63 | |
---|
64 | $registered_sidebars[$sidebar['id']] = $sidebar; |
---|
65 | |
---|
66 | return $sidebar['id']; |
---|
67 | } |
---|
68 | |
---|
69 | function unregister_sidebar($index) { |
---|
70 | global $registered_sidebars; |
---|
71 | |
---|
72 | unset( $registered_sidebars[$index] ); |
---|
73 | } |
---|
74 | |
---|
75 | function register_sidebar_widget($name, $output_callback, $classname = '', $id = '') { |
---|
76 | global $registered_widgets, $register_widget_defaults; |
---|
77 | |
---|
78 | // Compat |
---|
79 | if ( is_array($name) ) { |
---|
80 | if ( count($name) == 3 ) |
---|
81 | $name = sprintf($name[0], $name[2]); |
---|
82 | else |
---|
83 | $name = $name[0]; |
---|
84 | } |
---|
85 | |
---|
86 | // Last resort -- this can be broken when names get translated so please provide a unique id. |
---|
87 | if ( !isset($id) ) |
---|
88 | $id = sanitize_title($name); |
---|
89 | |
---|
90 | if ( (!isset($classname) || empty($classname) || !is_string($classname)) && is_string($output_callback) ) |
---|
91 | $classname = $output_callback; |
---|
92 | |
---|
93 | $widget = array( |
---|
94 | 'name' => $name, |
---|
95 | 'id' => $id, |
---|
96 | 'callback' => $output_callback, |
---|
97 | 'classname' => $classname, |
---|
98 | 'params' => array_slice(func_get_args(), 4) |
---|
99 | ); |
---|
100 | |
---|
101 | if ( empty($output_callback) ) |
---|
102 | unset($registered_widgets[$id]); |
---|
103 | elseif ( is_callable($output_callback) && ( !isset($registered_widgets[$id]) || !$register_widget_defaults) ) |
---|
104 | $registered_widgets[$id] = $widget; |
---|
105 | } |
---|
106 | |
---|
107 | function unregister_sidebar_widget($id) { |
---|
108 | $id = sanitize_title($id); |
---|
109 | register_sidebar_widget('', '', '', $id); |
---|
110 | unregister_widget_control($id); |
---|
111 | } |
---|
112 | |
---|
113 | function register_widget_control($name, $control_callback, $width = 300, $height = 200, $id = '') { |
---|
114 | global $registered_widget_controls, $register_widget_defaults; |
---|
115 | |
---|
116 | // Compat |
---|
117 | if ( is_array($name) ) { |
---|
118 | if ( count($name) == 3 ) |
---|
119 | $name = sprintf($name[0], $name[2]); |
---|
120 | else |
---|
121 | $name = $name[0]; |
---|
122 | } |
---|
123 | |
---|
124 | if ( !isset($id) || empty($id) ) |
---|
125 | $id = $name; |
---|
126 | |
---|
127 | $id = sanitize_title($id); |
---|
128 | |
---|
129 | $width = (int) $width > 90 ? (int) $width + 60 : 360; |
---|
130 | $height = (int) $height > 60 ? (int) $height + 40 : 240; |
---|
131 | |
---|
132 | if ( empty($control_callback) ) |
---|
133 | unset($registered_widget_controls[$name]); |
---|
134 | elseif ( !isset($registered_widget_controls[$name]) || !$register_widget_defaults ) |
---|
135 | $registered_widget_controls[$id] = array( |
---|
136 | 'name' => $name, |
---|
137 | 'id' => $id, |
---|
138 | 'callback' => $control_callback, |
---|
139 | 'width' => $width, |
---|
140 | 'height' => $height, |
---|
141 | 'params' => array_slice(func_get_args(), 5) |
---|
142 | ); |
---|
143 | } |
---|
144 | |
---|
145 | function unregister_widget_control($id) { |
---|
146 | $id = sanitize_title($id); |
---|
147 | return register_widget_control($id, ''); |
---|
148 | } |
---|
149 | |
---|
150 | function dynamic_sidebar($index = 1) { |
---|
151 | global $registered_sidebars, $registered_widgets; |
---|
152 | |
---|
153 | if ( is_int($index) ) { |
---|
154 | $index = "sidebar-$index"; |
---|
155 | } else { |
---|
156 | $index = sanitize_title($index); |
---|
157 | } |
---|
158 | |
---|
159 | $sidebars_widgets = get_sidebars_widgets(); |
---|
160 | |
---|
161 | if ( empty($registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) ) |
---|
162 | return false; |
---|
163 | |
---|
164 | $sidebar = $registered_sidebars[$index]; |
---|
165 | |
---|
166 | $did_one = false; |
---|
167 | foreach ( $sidebars_widgets[$index] as $id ) { |
---|
168 | $callback = $registered_widgets[$id]['callback']; |
---|
169 | |
---|
170 | $params = array_merge(array($sidebar), (array) $registered_widgets[$id]['params']); |
---|
171 | |
---|
172 | // Substitute HTML id and class attributes into before_widget |
---|
173 | $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $registered_widgets[$id]['classname']); |
---|
174 | |
---|
175 | if ( is_callable($callback) ) { |
---|
176 | call_user_func_array($callback, $params); |
---|
177 | $did_one = true; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | return $did_one; |
---|
182 | } |
---|
183 | |
---|
184 | function is_active_widget($callback) { |
---|
185 | global $registered_widgets; |
---|
186 | |
---|
187 | $sidebars_widgets = get_sidebars_widgets(false); |
---|
188 | |
---|
189 | if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets ) |
---|
190 | if ( is_array($widgets) ) foreach ( $widgets as $widget ) |
---|
191 | if ( $registered_widgets[$widget]['callback'] == $callback ) |
---|
192 | return true; |
---|
193 | |
---|
194 | return false; |
---|
195 | } |
---|
196 | |
---|
197 | function is_dynamic_sidebar() { |
---|
198 | global $registered_widgets, $registered_sidebars; |
---|
199 | $sidebars_widgets = get_option('sidebars_widgets'); |
---|
200 | foreach ( $registered_sidebars as $index => $sidebar ) { |
---|
201 | if ( count($sidebars_widgets[$index]) ) { |
---|
202 | foreach ( $sidebars_widgets[$index] as $widget ) |
---|
203 | if ( array_key_exists($widget, $registered_widgets) ) |
---|
204 | return true; |
---|
205 | } |
---|
206 | } |
---|
207 | return false; |
---|
208 | } |
---|
209 | |
---|
210 | //////////////////////////////////////////////////////////// Private Functions |
---|
211 | |
---|
212 | function get_sidebars_widgets($update = true) { |
---|
213 | global $registered_widgets; |
---|
214 | |
---|
215 | $sidebars_widgets = get_option('sidebars_widgets'); |
---|
216 | $_sidebars_widgets = array(); |
---|
217 | |
---|
218 | if ( !isset($sidebars_widgets['array_version']) ) |
---|
219 | $sidebars_widgets['array_version'] = 1; |
---|
220 | |
---|
221 | switch ( $sidebars_widgets['array_version'] ) { |
---|
222 | case 1 : |
---|
223 | foreach ( $sidebars_widgets as $index => $sidebar ) |
---|
224 | if ( is_array($sidebar) ) |
---|
225 | foreach ( $sidebar as $i => $name ) { |
---|
226 | $id = strtolower($name); |
---|
227 | if ( isset($registered_widgets[$id]) ) { |
---|
228 | $_sidebars_widgets[$index][$i] = $id; |
---|
229 | continue; |
---|
230 | } |
---|
231 | $id = sanitize_title($name); |
---|
232 | if ( isset($registered_widgets[$id]) ) { |
---|
233 | $_sidebars_widgets[$index][$i] = $id; |
---|
234 | continue; |
---|
235 | } |
---|
236 | unset($_sidebars_widgets[$index][$i]); |
---|
237 | } |
---|
238 | $_sidebars_widgets['array_version'] = 2; |
---|
239 | if ( $update ) |
---|
240 | update_option('sidebars_widgets', $_sidebars_widgets); |
---|
241 | break; |
---|
242 | case 2 : |
---|
243 | $_sidebars_widgets = $sidebars_widgets; |
---|
244 | break; |
---|
245 | } |
---|
246 | |
---|
247 | unset($_sidebars_widgets['array_version']); |
---|
248 | |
---|
249 | return $_sidebars_widgets; |
---|
250 | } |
---|
251 | |
---|
252 | function sidebar_admin_setup() { |
---|
253 | global $registered_sidebars, $registered_widgets; |
---|
254 | if ( count($registered_sidebars) < 1 ) |
---|
255 | return; |
---|
256 | $page = preg_replace('!^.*[\\\\/]wp-content[\\\\/][^\\\\/]*plugins[\\\\/]!', '', __FILE__); |
---|
257 | $page = str_replace('\\', '/', $page); |
---|
258 | add_submenu_page('themes.php', __('Sidebar Widgets', 'widgets'), __('Sidebar Widgets', 'widgets'), 5, $page, 'sidebar_admin_page'); |
---|
259 | if ( $_GET['page'] == $page ) { |
---|
260 | if ( isset($_POST['action']) && $_POST['action'] == 'save_widget_order' ) { |
---|
261 | check_admin_referer('widgets-save-widget-order'); |
---|
262 | $sidebars_widgets = array(); |
---|
263 | foreach ( $registered_sidebars as $index => $sidebar ) { |
---|
264 | $postindex = $index . 'order'; |
---|
265 | parse_str($_POST[$postindex], $order); |
---|
266 | $new_order = $order[$index]; |
---|
267 | if ( is_array($new_order) ) |
---|
268 | foreach ( $new_order as $_id ) |
---|
269 | foreach ( $registered_widgets as $id => $widget ) |
---|
270 | if ( $_id == $id ) |
---|
271 | $sidebars_widgets[$index][] = $id; |
---|
272 | } |
---|
273 | $sidebars_widgets['array_version'] = 2; |
---|
274 | $saved = update_option('sidebars_widgets', $sidebars_widgets) ? 'yes' : 'no'; |
---|
275 | wp_redirect(get_option('siteurl')."/wp-admin/themes.php?page=$page&saved=$saved"); |
---|
276 | } |
---|
277 | wp_enqueue_script( 'scriptaculous-dragdrop' ); |
---|
278 | add_action('admin_head', 'sidebar_admin_head'); |
---|
279 | do_action('sidebar_admin_setup'); |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | function sidebar_admin_head() { |
---|
284 | global $registered_widgets, $registered_sidebars, $registered_widget_controls; |
---|
285 | |
---|
286 | $width = 1 + 262 * ( count($registered_sidebars)); |
---|
287 | $height = 35 * count($registered_widgets); |
---|
288 | ?> |
---|
289 | <style type="text/css"> |
---|
290 | body { |
---|
291 | height: 100%; |
---|
292 | } |
---|
293 | #sbadmin #zones { |
---|
294 | width: <?php echo $width; ?>px; |
---|
295 | -moz-user-select: none; |
---|
296 | -khtml-user-select: none; |
---|
297 | user-select: none; |
---|
298 | } |
---|
299 | #sbreset { |
---|
300 | float: left; |
---|
301 | margin: 1px 0; |
---|
302 | } |
---|
303 | .dropzone { |
---|
304 | float: left; |
---|
305 | margin-right: 10px; |
---|
306 | padding: 5px; |
---|
307 | border: 1px solid #bbb; |
---|
308 | background-color: #f0f8ff; |
---|
309 | } |
---|
310 | .dropzone h3 { |
---|
311 | text-align: center; |
---|
312 | color: #333; |
---|
313 | } |
---|
314 | .dropzone ul { |
---|
315 | list-style-type: none; |
---|
316 | width: 240px; |
---|
317 | height: <?php echo $height; ?>px; |
---|
318 | float: left; |
---|
319 | margin: 0; |
---|
320 | padding: 0; |
---|
321 | } |
---|
322 | * .module, #lastmodule { |
---|
323 | width: 238px; |
---|
324 | padding: 0; |
---|
325 | margin: 5px 0; |
---|
326 | cursor: move; |
---|
327 | display: block; |
---|
328 | border: 1px solid #ccc; |
---|
329 | background-color: #fbfbfb; |
---|
330 | text-align: left; |
---|
331 | line-height: 25px; |
---|
332 | } |
---|
333 | * .handle, #lastmodule span { |
---|
334 | display: block; |
---|
335 | width: 216px; |
---|
336 | padding: 0 10px; |
---|
337 | border-top: 1px solid #f2f2f2; |
---|
338 | border-right: 1px solid #e8e8e8; |
---|
339 | border-bottom: 1px solid #e8e8e8; |
---|
340 | border-left: 1px solid #f2f2f2; |
---|
341 | } |
---|
342 | * .popper { |
---|
343 | margin: 0; |
---|
344 | display: inline; |
---|
345 | position: absolute; |
---|
346 | top: 3px; |
---|
347 | right: 3px; |
---|
348 | overflow: hidden; |
---|
349 | text-align: center; |
---|
350 | height: 16px; |
---|
351 | font-size: 18px; |
---|
352 | line-height: 14px; |
---|
353 | cursor: pointer; |
---|
354 | padding: 0 3px 1px; |
---|
355 | border-top: 4px solid #6da6d1; |
---|
356 | background: url( images/fade-butt.png ) -5px 0px; |
---|
357 | } |
---|
358 | * html .popper { |
---|
359 | padding: 1px 6px 0; |
---|
360 | font-size: 16px; |
---|
361 | } |
---|
362 | #sbadmin p.submit { |
---|
363 | padding-right: 10px; |
---|
364 | clear: left; |
---|
365 | } |
---|
366 | .placematt { |
---|
367 | position: absolute; |
---|
368 | cursor: default; |
---|
369 | margin: 10px 0 0; |
---|
370 | padding: 0; |
---|
371 | width: 238px; |
---|
372 | background-color: #ffe; |
---|
373 | } |
---|
374 | * html .placematt { |
---|
375 | margin-top: 5px; |
---|
376 | } |
---|
377 | .placematt h4 { |
---|
378 | text-align: center; |
---|
379 | margin-bottom: 5px; |
---|
380 | } |
---|
381 | .placematt span { |
---|
382 | padding: 0 10px 10px; |
---|
383 | text-align: justify; |
---|
384 | } |
---|
385 | #palettediv { |
---|
386 | border: 1px solid #bbb; |
---|
387 | background-color: #f0f8ff; |
---|
388 | height: 180px; |
---|
389 | margin-top: 10px; |
---|
390 | } |
---|
391 | #palettediv h3 { |
---|
392 | text-align: center; |
---|
393 | color: #333; |
---|
394 | } |
---|
395 | #palettediv ul { |
---|
396 | padding: 0 0 0 10px; |
---|
397 | } |
---|
398 | #palettediv .module, #lastmodule { |
---|
399 | margin-right: 10px; |
---|
400 | float: left; |
---|
401 | width: 120px; |
---|
402 | } |
---|
403 | #palettediv .handle, #lastmodule span { |
---|
404 | height: 40px; |
---|
405 | font-size: 0.9em; |
---|
406 | line-height: 1.6em; |
---|
407 | width: 110px; |
---|
408 | padding: 0 5px; |
---|
409 | } |
---|
410 | #palettediv .popper { |
---|
411 | visibility: hidden; |
---|
412 | } |
---|
413 | #lastmodule { |
---|
414 | visibility: hidden; |
---|
415 | } |
---|
416 | * html #palettediv ul { |
---|
417 | margin: 0; |
---|
418 | padding: 0 0 0 10px; |
---|
419 | } |
---|
420 | * html #palettediv .module { |
---|
421 | float: none; |
---|
422 | display: inline; |
---|
423 | } |
---|
424 | #controls { |
---|
425 | height: 0px; |
---|
426 | } |
---|
427 | .control { |
---|
428 | position: absolute; |
---|
429 | display: block; |
---|
430 | background: #f9fcfe; |
---|
431 | padding: 0; |
---|
432 | } |
---|
433 | .controlhandle { |
---|
434 | cursor: move; |
---|
435 | background-color: #6da6d1; |
---|
436 | border-bottom: 2px solid #448abd; |
---|
437 | color: #333; |
---|
438 | display: block; |
---|
439 | margin: 0 0 5px; |
---|
440 | padding: 4px; |
---|
441 | font-size: 120%; |
---|
442 | } |
---|
443 | .controlcloser { |
---|
444 | cursor: pointer; |
---|
445 | font-size: 120%; |
---|
446 | display: block; |
---|
447 | position: absolute; |
---|
448 | top: 2px; |
---|
449 | right: 8px; |
---|
450 | padding: 0 3px; |
---|
451 | font-weight: bold; |
---|
452 | } |
---|
453 | .controlform { |
---|
454 | margin: 20px 30px; |
---|
455 | } |
---|
456 | .controlform p { |
---|
457 | text-align: center; |
---|
458 | } |
---|
459 | .control .checkbox { |
---|
460 | border: none; |
---|
461 | background: transparent; |
---|
462 | } |
---|
463 | .hidden { |
---|
464 | display: none; |
---|
465 | } |
---|
466 | #shadow { |
---|
467 | background: black; |
---|
468 | display: none; |
---|
469 | position: absolute; |
---|
470 | top: 0px; |
---|
471 | left: 0px; |
---|
472 | width: 100%; |
---|
473 | } |
---|
474 | </style> |
---|
475 | |
---|
476 | <script type="text/javascript"> |
---|
477 | // <![CDATA[ |
---|
478 | var cols = [<?php $a = array(); foreach ( $registered_sidebars as $index => $sidebar ) $a[] = "'$index'"; echo implode(', ', $a); ?>]; |
---|
479 | var widgets = [<?php $a = array(); foreach ( $registered_widgets as $name => $widget ) $a[] = "'{$widget['id']}'"; echo implode(', ', $a); ?>]; |
---|
480 | var controldims = new Array; |
---|
481 | <?php foreach ( $registered_widget_controls as $id => $widget ) : ?> |
---|
482 | controldims['<?php echo $id; ?>control'] = new Array; |
---|
483 | controldims['<?php echo $id; ?>control']['width'] = <?php echo (int) $widget['width']; ?>; |
---|
484 | controldims['<?php echo $id; ?>control']['height'] = <?php echo (int) $widget['height']; ?>; |
---|
485 | <?php endforeach; ?> |
---|
486 | function initWidgets() { |
---|
487 | <?php foreach ( $registered_widget_controls as $name => $widget ) : ?> |
---|
488 | $('<?php echo $widget['id']; ?>popper').onclick = function() {popControl('<?php echo $widget['id']; ?>control');}; |
---|
489 | $('<?php echo $widget['id']; ?>closer').onclick = function() {unpopControl('<?php echo $widget['id']; ?>control');}; |
---|
490 | new Draggable('<?php echo $widget['id']; ?>control', {revert:false,handle:'controlhandle',starteffect:function(){},endeffect:function(){},change:function(o){dragChange(o);}}); |
---|
491 | if ( true && window.opera ) |
---|
492 | $('<?php echo $widget['id']; ?>control').style.border = '1px solid #bbb'; |
---|
493 | <?php endforeach; ?> |
---|
494 | if ( true && window.opera ) |
---|
495 | $('shadow').style.background = 'transparent'; |
---|
496 | new Effect.Opacity('shadow', {to:0.0}); |
---|
497 | widgets.map(function(o) {o='widgetprefix-'+o; Position.absolutize(o); Position.relativize(o);} ); |
---|
498 | $A(Draggables.drags).map(function(o) {o.startDrag(null); o.finishDrag(null);}); |
---|
499 | for ( var n in Draggables.drags ) { |
---|
500 | if ( Draggables.drags[n].element.id == 'lastmodule' ) { |
---|
501 | Draggables.drags[n].destroy(); |
---|
502 | break; |
---|
503 | } |
---|
504 | } |
---|
505 | resetPaletteHeight(); |
---|
506 | } |
---|
507 | function resetDroppableHeights() { |
---|
508 | var max = 6; |
---|
509 | cols.map(function(o) {var c = $(o).childNodes.length; if ( c > max ) max = c;} ); |
---|
510 | var height = 35 * ( max + 1); |
---|
511 | cols.map(function(o) {h = (($(o).childNodes.length + 1) * 35); $(o).style.height = (h > 280 ? h : 280) + 'px';} ); |
---|
512 | } |
---|
513 | function resetPaletteHeight() { |
---|
514 | var p = $('palette'), pd = $('palettediv'), last = $('lastmodule'); |
---|
515 | p.appendChild(last); |
---|
516 | if ( Draggables.activeDraggable && last.id == Draggables.activeDraggable.element.id ) |
---|
517 | last = last.previousSibling; |
---|
518 | var y1 = Position.cumulativeOffset(last)[1] + last.offsetHeight; |
---|
519 | var y2 = Position.cumulativeOffset(pd)[1] + pd.offsetHeight; |
---|
520 | var dy = y1 - y2; |
---|
521 | pd.style.height = (pd.offsetHeight + dy + 9) + "px"; |
---|
522 | } |
---|
523 | function maxHeight(elm) { |
---|
524 | htmlheight = document.body.parentNode.clientHeight; |
---|
525 | bodyheight = document.body.clientHeight; |
---|
526 | var height = htmlheight > bodyheight ? htmlheight : bodyheight; |
---|
527 | $(elm).style.height = height + 'px'; |
---|
528 | } |
---|
529 | function dragChange(o) { |
---|
530 | el = o.element ? o.element : $(o); |
---|
531 | var p = Position.page(el); |
---|
532 | var right = p[0]; |
---|
533 | var top = p[1]; |
---|
534 | var left = $('shadow').offsetWidth - (el.offsetWidth + left); |
---|
535 | var bottom = $('shadow').offsetHeight - (el.offsetHeight + top); |
---|
536 | if ( right < 1 ) el.style.left = 0; |
---|
537 | if ( top < 1 ) el.style.top = 0; |
---|
538 | if ( left < 1 ) el.style.left = (left + right) + 'px'; |
---|
539 | if ( bottom < 1 ) el.style.top = (top + bottom) + 'px'; |
---|
540 | } |
---|
541 | function popControl(elm) { |
---|
542 | el = $(elm); |
---|
543 | el.style.width = controldims[elm]['width'] + 'px'; |
---|
544 | el.style.height = controldims[elm]['height'] + 'px'; |
---|
545 | var x = ( document.body.clientWidth - controldims[elm]['width'] ) / 2; |
---|
546 | var y = ( document.body.parentNode.clientHeight - controldims[elm]['height'] ) / 2; |
---|
547 | el.style.position = 'absolute'; |
---|
548 | el.style.right = '' + x + 'px'; |
---|
549 | el.style.top = '' + y + 'px'; |
---|
550 | el.style.zIndex = 1000; |
---|
551 | el.className='control'; |
---|
552 | $('shadow').onclick = function() {unpopControl(elm);}; |
---|
553 | window.onresize = function(){maxHeight('shadow');dragChange(elm);}; |
---|
554 | popShadow(); |
---|
555 | } |
---|
556 | function popShadow() { |
---|
557 | maxHeight('shadow'); |
---|
558 | var shadow = $('shadow'); |
---|
559 | shadow.style.zIndex = 999; |
---|
560 | shadow.style.display = 'block'; |
---|
561 | new Effect.Opacity('shadow', {duration:0.5, from:0.0, to:0.2}); |
---|
562 | } |
---|
563 | function unpopShadow() { |
---|
564 | new Effect.Opacity('shadow', {to:0.0}); |
---|
565 | $('shadow').style.display = 'none'; |
---|
566 | } |
---|
567 | function unpopControl(el) { |
---|
568 | $(el).className='hidden'; |
---|
569 | unpopShadow(); |
---|
570 | } |
---|
571 | function serializeAll() { |
---|
572 | <?php foreach ( $registered_sidebars as $index => $sidebar ) : ?> |
---|
573 | $('<?php echo $index; ?>order').value = Sortable.serialize('<?php echo $index; ?>'); |
---|
574 | <?php endforeach; ?> |
---|
575 | } |
---|
576 | function updateAll() { |
---|
577 | resetDroppableHeights(); |
---|
578 | resetPaletteHeight(); |
---|
579 | cols.map(function(o){ |
---|
580 | var pm = $(o+'placematt'); |
---|
581 | if ( $(o).childNodes.length == 0 ) { |
---|
582 | pm.style.display = 'block'; |
---|
583 | Position.absolutize(o+'placematt'); |
---|
584 | } else { |
---|
585 | pm.style.display = 'none'; |
---|
586 | } |
---|
587 | }); |
---|
588 | } |
---|
589 | function noSelection(event) { |
---|
590 | if ( document.selection ) { |
---|
591 | var range = document.selection.createRange(); |
---|
592 | range.collapse(false); |
---|
593 | range.select(); |
---|
594 | return false; |
---|
595 | } |
---|
596 | } |
---|
597 | addLoadEvent(updateAll); |
---|
598 | addLoadEvent(initWidgets); |
---|
599 | Event.observe(window, 'resize', resetPaletteHeight); |
---|
600 | // ]]> |
---|
601 | </script> |
---|
602 | <?php |
---|
603 | do_action('sidebar_admin_head'); |
---|
604 | } |
---|
605 | |
---|
606 | function sidebar_admin_page() { |
---|
607 | global $registered_widgets, $registered_sidebars, $registered_widget_controls; |
---|
608 | |
---|
609 | if ( count($registered_sidebars) < 1 ) { |
---|
610 | ?> |
---|
611 | <div class="wrap"> |
---|
612 | <h2><?php _e('About Dynamic Sidebars'); ?></h2> |
---|
613 | <p><?php _e("You can modify your theme's sidebar, rearranging and configuring widgets right in this screen! Well, you could if you had a compatible theme. You're seeing this message because your theme isn't ready for widgets. <a href='http://automattic.com/code/widgets/'>Get it ready!</a>"); ?></p> |
---|
614 | |
---|
615 | </div> |
---|
616 | <?php |
---|
617 | return; |
---|
618 | } |
---|
619 | $sidebars_widgets = get_sidebars_widgets(); |
---|
620 | if ( empty($sidebars_widgets) ) { |
---|
621 | $sidebars_widgets = get_widget_defaults(); |
---|
622 | } |
---|
623 | |
---|
624 | ksort($registered_widgets); |
---|
625 | |
---|
626 | $inactive_widgets = array(); |
---|
627 | foreach ( $registered_widgets as $id => $widget ) { |
---|
628 | $is_active = false; |
---|
629 | foreach ( $registered_sidebars as $index => $sidebar ) { |
---|
630 | if ( is_array($sidebars_widgets[$index]) && in_array($id, $sidebars_widgets[$index]) ) { |
---|
631 | $is_active = true; |
---|
632 | break; |
---|
633 | } |
---|
634 | } |
---|
635 | if ( ! $is_active ) |
---|
636 | $inactive_widgets[] = $id; |
---|
637 | } |
---|
638 | |
---|
639 | $containers = array('palette'); |
---|
640 | foreach ( $registered_sidebars as $index => $sidebar ) |
---|
641 | $containers[] = $index; |
---|
642 | $c_string = ''; |
---|
643 | foreach ( $containers as $container ) |
---|
644 | $c_string .= "\"$container\","; |
---|
645 | $c_string = substr($c_string, 0, -1); |
---|
646 | ?> |
---|
647 | <?php if ( $_GET['saved'] ) { ?> |
---|
648 | <div class="fade updated" id="message"> |
---|
649 | <p><?php printf(__('Sidebar Updated. <a href="%s">View site »</a>', 'widgets'), get_option('home') . '/'); ?></p> |
---|
650 | </div> |
---|
651 | <?php } ?> |
---|
652 | <div class="wrap"> |
---|
653 | <h2><?php _e('Sidebar Arrangement'); ?></h2> |
---|
654 | <p><?php _e("You can drag and drop widgets into your sidebar below."); ?></p> |
---|
655 | <form id="sbadmin" method="POST" onsubmit="serializeAll()"> |
---|
656 | <p class="submit"> |
---|
657 | <?php if ( function_exists('wp_nonce_field') ) wp_nonce_field('widgets-save-widget-order'); ?> |
---|
658 | <input type="hidden" name="action" id="action" value="save_widget_order" /> |
---|
659 | <input type="submit" value="<?php _e('Save Changes »', 'widgets'); ?>" /> |
---|
660 | </p> |
---|
661 | <div id="zones"> |
---|
662 | <?php $i = 1; foreach ( $registered_sidebars as $index => $sidebar ) : ?> |
---|
663 | <input type="hidden" id="<?php echo $index; ?>order" name="<?php echo $index; ?>order" value="" /> |
---|
664 | <div class="dropzone"> |
---|
665 | <h3><?php echo $sidebar['name']; ?></h3> |
---|
666 | <div id="<?php echo $index; ?>placematt" class="module placematt"><span class="handle"><h4><?php _e('Default Sidebar'); ?></h4><?php _e('Your theme will display its usual sidebar when this box is empty. Dragging widgets into this box will replace the usual sidebar with your customized sidebar.'); ?></span></div> |
---|
667 | <ul id="<?php echo $index; ?>"><?php if ( is_array($sidebars_widgets[$index]) ) foreach ( $sidebars_widgets[$index] as $id ) widget_draggable($id); ?></ul> |
---|
668 | </div> |
---|
669 | <?php endforeach; ?> |
---|
670 | <br class="clear" /> |
---|
671 | </div> |
---|
672 | <div id="palettediv"> |
---|
673 | <h3><?php _e('Available Widgets'); ?></h3> |
---|
674 | <ul id="palette"><?php foreach ( $inactive_widgets as $id ) widget_draggable($id); ?><li id="lastmodule"><span></span></li></ul> |
---|
675 | </div> |
---|
676 | |
---|
677 | <script type="text/javascript"> |
---|
678 | // <![CDATA[ |
---|
679 | <?php foreach ( $containers as $container ) : ?> |
---|
680 | Sortable.create("<?php echo $container; ?>", |
---|
681 | {dropOnEmpty:true,containment:[<?php echo $c_string; ?>],handle:'handle',constraint:false,onUpdate:updateAll,format:/^widgetprefix-(.*)$/}); |
---|
682 | <?php endforeach; ?> |
---|
683 | // ]]> |
---|
684 | </script> |
---|
685 | <p class="submit"> |
---|
686 | <?php if ( function_exists('wp_nonce_field') ) wp_nonce_field('widgets-save-widget-order'); ?> |
---|
687 | <input type="hidden" name="action" id="action" value="save_widget_order" /> |
---|
688 | <input type="submit" value="<?php _e('Save Changes »', 'widgets'); ?>" /> |
---|
689 | </p> |
---|
690 | <div id="controls"> |
---|
691 | <?php foreach ( $registered_widget_controls as $id => $widget ) : ?> |
---|
692 | <div class="hidden" id="<?php echo $id; ?>control"> |
---|
693 | <span class="controlhandle"><?php echo $widget['name']; ?></span> |
---|
694 | <span id="<?php echo $id; ?>closer" class="controlcloser">×</span> |
---|
695 | <div class="controlform"> |
---|
696 | <?php call_user_func_array($widget['callback'], $widget['params']); ?> |
---|
697 | </div> |
---|
698 | </div> |
---|
699 | <?php endforeach; ?> |
---|
700 | </div> |
---|
701 | </form> |
---|
702 | <br class="clear" /> |
---|
703 | </div> |
---|
704 | <div id="shadow"> </div> |
---|
705 | <?php |
---|
706 | do_action('sidebar_admin_page'); |
---|
707 | } |
---|
708 | |
---|
709 | function widget_draggable($id) { |
---|
710 | global $registered_widgets, $registered_widget_controls; |
---|
711 | if ( !isset($registered_widgets[$id]) ) |
---|
712 | return; |
---|
713 | $poptitle = __('Configure', 'widgets'); |
---|
714 | $name = $registered_widgets[$id]['name']; |
---|
715 | if ( isset($registered_widget_controls[$id]) ) |
---|
716 | $popper = " <div class='popper' id='{$id}popper' title='$poptitle'>≡</div>"; |
---|
717 | else |
---|
718 | $poppper = ''; |
---|
719 | echo "<li class='module' id='widgetprefix-$id'><span class='handle'>$name$popper</span></li>"; |
---|
720 | } |
---|
721 | |
---|
722 | function get_widget_defaults() { |
---|
723 | global $registered_sidebars; |
---|
724 | foreach ( $registered_sidebars as $index => $sidebar ) |
---|
725 | $defaults[$index] = array(); |
---|
726 | return $defaults; |
---|
727 | } |
---|
728 | |
---|
729 | |
---|
730 | //////////////////////////////////////////////////////////// Standard Widgets |
---|
731 | |
---|
732 | function widget_pages($args) { |
---|
733 | extract($args); |
---|
734 | $options = get_option('widget_pages'); |
---|
735 | $title = empty($options['title']) ? __('Pages', 'widgets') : $options['title']; |
---|
736 | echo $before_widget . $before_title . $title . $after_title . "<ul>\n"; |
---|
737 | wp_list_pages("title_li="); |
---|
738 | echo "</ul>\n" . $after_widget; |
---|
739 | } |
---|
740 | |
---|
741 | function widget_pages_control() { |
---|
742 | $options = $newoptions = get_option('widget_pages'); |
---|
743 | if ( $_POST["pages-submit"] ) { |
---|
744 | $newoptions['title'] = strip_tags(stripslashes($_POST["pages-title"])); |
---|
745 | } |
---|
746 | if ( $options != $newoptions ) { |
---|
747 | $options = $newoptions; |
---|
748 | update_option('widget_pages', $options); |
---|
749 | } |
---|
750 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
751 | ?> |
---|
752 | <p><label for="pages-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
753 | <input type="hidden" id="pages-submit" name="pages-submit" value="1" /> |
---|
754 | <?php |
---|
755 | } |
---|
756 | |
---|
757 | function widget_links($args) { |
---|
758 | global $wp_db_version; |
---|
759 | extract($args); |
---|
760 | if ( $wp_db_version < 3582 ) { |
---|
761 | // This ONLY works with li/h2 sidebars. |
---|
762 | get_links_list(); |
---|
763 | } else { |
---|
764 | wp_list_bookmarks(array('title_before'=>$before_title, 'title_after'=>$after_title, 'show_images'=>true, 'class'=>'linkcat widget')); |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | function widget_search($args) { |
---|
769 | extract($args); |
---|
770 | ?> |
---|
771 | <?php echo $before_widget; ?> |
---|
772 | <form id="searchform" method="get" action="<?php bloginfo('home'); ?>"> |
---|
773 | <div> |
---|
774 | <input type="text" name="s" id="s" size="15" /><br /> |
---|
775 | <input type="submit" value="<?php _e('Search'); ?>" /> |
---|
776 | </div> |
---|
777 | </form> |
---|
778 | <?php echo $after_widget; ?> |
---|
779 | <?php |
---|
780 | } |
---|
781 | |
---|
782 | function widget_archives($args) { |
---|
783 | extract($args); |
---|
784 | $options = get_option('widget_archives'); |
---|
785 | $c = $options['count'] ? '1' : '0'; |
---|
786 | $title = empty($options['title']) ? __('Archives', 'widgets') : $options['title']; |
---|
787 | ?> |
---|
788 | <?php echo $before_widget; ?> |
---|
789 | <?php echo $before_title . $title . $after_title; ?> |
---|
790 | <ul> |
---|
791 | <?php wp_get_archives("type=monthly&show_post_count=$c"); ?> |
---|
792 | </ul> |
---|
793 | <?php echo $after_widget; ?> |
---|
794 | <?php |
---|
795 | } |
---|
796 | |
---|
797 | function widget_archives_control() { |
---|
798 | $options = $newoptions = get_option('widget_archives'); |
---|
799 | if ( $_POST["archives-submit"] ) { |
---|
800 | $newoptions['count'] = isset($_POST['archives-count']); |
---|
801 | $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"])); |
---|
802 | } |
---|
803 | if ( $options != $newoptions ) { |
---|
804 | $options = $newoptions; |
---|
805 | update_option('widget_archives', $options); |
---|
806 | } |
---|
807 | $count = $options['count'] ? 'checked="checked"' : ''; |
---|
808 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
809 | ?> |
---|
810 | <p><label for="archives-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
811 | <p style="text-align:right;margin-right:40px;"><label for="archives-count">Show post counts <input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /></label></p> |
---|
812 | <input type="hidden" id="archives-submit" name="archives-submit" value="1" /> |
---|
813 | <?php |
---|
814 | } |
---|
815 | |
---|
816 | function widget_meta($args) { |
---|
817 | extract($args); |
---|
818 | $options = get_option('widget_meta'); |
---|
819 | $title = empty($options['title']) ? __('Meta', 'widgets') : $options['title']; |
---|
820 | ?> |
---|
821 | <?php echo $before_widget; ?> |
---|
822 | <?php echo $before_title . $title . $after_title; ?> |
---|
823 | <ul> |
---|
824 | <?php wp_register(); ?> |
---|
825 | <li><?php wp_loginout(); ?></li> |
---|
826 | <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php _e('Syndicate this site using RSS 2.0'); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li> |
---|
827 | <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php _e('The latest comments to all posts in RSS'); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li> |
---|
828 | <li><a href="http://wordpress.com/" title="<?php _e('Powered by WordPress, state-of-the-art semantic personal publishing platform.'); ?>">WordPress.com</a></li> |
---|
829 | <?php wp_meta(); ?> |
---|
830 | </ul> |
---|
831 | <?php echo $after_widget; ?> |
---|
832 | <?php |
---|
833 | } |
---|
834 | function widget_meta_control() { |
---|
835 | $options = $newoptions = get_option('widget_meta'); |
---|
836 | if ( $_POST["meta-submit"] ) { |
---|
837 | $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"])); |
---|
838 | } |
---|
839 | if ( $options != $newoptions ) { |
---|
840 | $options = $newoptions; |
---|
841 | update_option('widget_meta', $options); |
---|
842 | } |
---|
843 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
844 | ?> |
---|
845 | <p><label for="meta-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
846 | <input type="hidden" id="meta-submit" name="meta-submit" value="1" /> |
---|
847 | <?php |
---|
848 | } |
---|
849 | |
---|
850 | function widget_calendar($args) { |
---|
851 | extract($args); |
---|
852 | $options = get_option('widget_calendar'); |
---|
853 | $title = $options['title']; |
---|
854 | if ( empty($title) ) |
---|
855 | $title = ' '; |
---|
856 | echo $before_widget . $before_title . $title . $after_title; |
---|
857 | echo '<div id="calendar_wrap">'; |
---|
858 | get_calendar(); |
---|
859 | echo '</div>'; |
---|
860 | echo $after_widget; |
---|
861 | } |
---|
862 | function widget_calendar_control() { |
---|
863 | $options = $newoptions = get_option('widget_calendar'); |
---|
864 | if ( $_POST["calendar-submit"] ) { |
---|
865 | $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"])); |
---|
866 | } |
---|
867 | if ( $options != $newoptions ) { |
---|
868 | $options = $newoptions; |
---|
869 | update_option('widget_calendar', $options); |
---|
870 | } |
---|
871 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
872 | ?> |
---|
873 | <p><label for="calendar-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
874 | <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" /> |
---|
875 | <?php |
---|
876 | } |
---|
877 | |
---|
878 | function widget_text($args, $number = 1) { |
---|
879 | extract($args); |
---|
880 | $options = get_option('widget_text'); |
---|
881 | $title = $options[$number]['title']; |
---|
882 | if ( empty($title) ) |
---|
883 | $title = ' '; |
---|
884 | $text = $options[$number]['text']; |
---|
885 | ?> |
---|
886 | <?php echo $before_widget; ?> |
---|
887 | <?php $title ? print($before_title . $title . $after_title) : null; ?> |
---|
888 | <div class="textwidget"><?php echo $text; ?></div> |
---|
889 | <?php echo $after_widget; ?> |
---|
890 | <?php |
---|
891 | } |
---|
892 | |
---|
893 | function widget_text_control($number) { |
---|
894 | $options = $newoptions = get_option('widget_text'); |
---|
895 | if ( !is_array($options) ) |
---|
896 | $options = $newoptions = array(); |
---|
897 | if ( $_POST["text-submit-$number"] ) { |
---|
898 | $newoptions[$number]['title'] = strip_tags(stripslashes($_POST["text-title-$number"])); |
---|
899 | $newoptions[$number]['text'] = stripslashes($_POST["text-text-$number"]); |
---|
900 | if ( !current_user_can('unfiltered_html') ) |
---|
901 | $newoptions[$number]['text'] = stripslashes(wp_filter_post_kses($newoptions[$number]['text'])); |
---|
902 | } |
---|
903 | if ( $options != $newoptions ) { |
---|
904 | $options = $newoptions; |
---|
905 | update_option('widget_text', $options); |
---|
906 | } |
---|
907 | $title = htmlspecialchars($options[$number]['title'], ENT_QUOTES); |
---|
908 | $text = htmlspecialchars($options[$number]['text'], ENT_QUOTES); |
---|
909 | ?> |
---|
910 | <input style="width: 450px;" id="text-title-<?php echo "$number"; ?>" name="text-title-<?php echo "$number"; ?>" type="text" value="<?php echo $title; ?>" /> |
---|
911 | <textarea style="width: 450px; height: 280px;" id="text-text-<?php echo "$number"; ?>" name="text-text-<?php echo "$number"; ?>"><?php echo $text; ?></textarea> |
---|
912 | <input type="hidden" id="text-submit-<?php echo "$number"; ?>" name="text-submit-<?php echo "$number"; ?>" value="1" /> |
---|
913 | <?php |
---|
914 | } |
---|
915 | |
---|
916 | function widget_text_setup() { |
---|
917 | $options = $newoptions = get_option('widget_text'); |
---|
918 | if ( isset($_POST['text-number-submit']) ) { |
---|
919 | $number = (int) $_POST['text-number']; |
---|
920 | if ( $number > 9 ) $number = 9; |
---|
921 | if ( $number < 1 ) $number = 1; |
---|
922 | $newoptions['number'] = $number; |
---|
923 | } |
---|
924 | if ( $options != $newoptions ) { |
---|
925 | $options = $newoptions; |
---|
926 | update_option('widget_text', $options); |
---|
927 | widget_text_register($options['number']); |
---|
928 | } |
---|
929 | } |
---|
930 | |
---|
931 | function widget_text_page() { |
---|
932 | $options = $newoptions = get_option('widget_text'); |
---|
933 | ?> |
---|
934 | <div class="wrap"> |
---|
935 | <form method="POST"> |
---|
936 | <h2><?php _e('Text Widgets', 'widgets'); ?></h2> |
---|
937 | <p style="line-height: 30px;"><?php _e('How many text widgets would you like?', 'widgets'); ?> |
---|
938 | <select id="text-number" name="text-number" value="<?php echo $options['number']; ?>"> |
---|
939 | <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?> |
---|
940 | </select> |
---|
941 | <span class="submit"><input type="submit" name="text-number-submit" id="text-number-submit" value="<?php _e('Save'); ?>" /></span></p> |
---|
942 | </form> |
---|
943 | </div> |
---|
944 | <?php |
---|
945 | } |
---|
946 | |
---|
947 | function widget_text_register() { |
---|
948 | $options = get_option('widget_text'); |
---|
949 | $number = $options['number']; |
---|
950 | if ( $number < 1 ) $number = 1; |
---|
951 | if ( $number > 9 ) $number = 9; |
---|
952 | for ($i = 1; $i <= 9; $i++) { |
---|
953 | $name = sprintf(__('Text %d', 'widgets'), $i); |
---|
954 | $id = "text-$i"; // Never never never translate an id |
---|
955 | register_sidebar_widget($name, $i <= $number ? 'widget_text' : /* unregister */ '', null, $id, $i); |
---|
956 | register_widget_control($name, $i <= $number ? 'widget_text_control' : /* unregister */ '', 460, 350, $id, $i); |
---|
957 | } |
---|
958 | add_action('sidebar_admin_setup', 'widget_text_setup'); |
---|
959 | add_action('sidebar_admin_page', 'widget_text_page'); |
---|
960 | } |
---|
961 | |
---|
962 | function widget_categories($args) { |
---|
963 | extract($args); |
---|
964 | $options = get_option('widget_categories'); |
---|
965 | $c = $options['count'] ? '1' : '0'; |
---|
966 | $h = $options['hierarchical'] ? '1' : '0'; |
---|
967 | $title = empty($options['title']) ? __('Categories', 'widgets') : $options['title']; |
---|
968 | ?> |
---|
969 | <?php echo $before_widget; ?> |
---|
970 | <?php echo $before_title . $title . $after_title; ?> |
---|
971 | <ul> |
---|
972 | <?php wp_list_cats("sort_column=name&optioncount=$c&hierarchical=$h"); ?> |
---|
973 | </ul> |
---|
974 | <?php echo $after_widget; ?> |
---|
975 | <?php |
---|
976 | } |
---|
977 | |
---|
978 | function widget_categories_control() { |
---|
979 | $options = $newoptions = get_option('widget_categories'); |
---|
980 | if ( $_POST['categories-submit'] ) { |
---|
981 | $newoptions['count'] = isset($_POST['categories-count']); |
---|
982 | $newoptions['hierarchical'] = isset($_POST['categories-hierarchical']); |
---|
983 | $newoptions['title'] = strip_tags(stripslashes($_POST['categories-title'])); |
---|
984 | } |
---|
985 | if ( $options != $newoptions ) { |
---|
986 | $options = $newoptions; |
---|
987 | update_option('widget_categories', $options); |
---|
988 | } |
---|
989 | $count = $options['count'] ? 'checked="checked"' : ''; |
---|
990 | $hierarchical = $options['hierarchical'] ? 'checked="checked"' : ''; |
---|
991 | $title = wp_specialchars($options['title']); |
---|
992 | ?> |
---|
993 | <p><label for="categories-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="categories-title" name="categories-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
994 | <p style="text-align:right;margin-right:40px;"><label for="categories-count"><?php _e('Show post counts', 'widgets'); ?> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="categories-count" name="categories-count" /></label></p> |
---|
995 | <p style="text-align:right;margin-right:40px;"><label for="categories-hierarchical" style="text-align:right;"><?php _e('Show hierarchy', 'widgets'); ?> <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="categories-hierarchical" name="categories-hierarchical" /></label></p> |
---|
996 | <input type="hidden" id="categories-submit" name="categories-submit" value="1" /> |
---|
997 | <?php |
---|
998 | } |
---|
999 | |
---|
1000 | function widget_recent_entries($args) { |
---|
1001 | if ( $output = wp_cache_get('widget_recent_entries') ) |
---|
1002 | return print($output); |
---|
1003 | |
---|
1004 | ob_start(); |
---|
1005 | extract($args); |
---|
1006 | $options = get_option('widget_recent_entries'); |
---|
1007 | $title = empty($options['title']) ? __('Recent Posts', 'widgets') : $options['title']; |
---|
1008 | if ( !$number = (int) $options['number'] ) |
---|
1009 | $number = 10; |
---|
1010 | else if ( $number < 1 ) |
---|
1011 | $number = 1; |
---|
1012 | else if ( $number > 15 ) |
---|
1013 | $number = 15; |
---|
1014 | |
---|
1015 | $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0"); |
---|
1016 | if ($r->have_posts()) : |
---|
1017 | ?> |
---|
1018 | <?php echo $before_widget; ?> |
---|
1019 | <?php echo $before_title . $title . $after_title; ?> |
---|
1020 | <ul> |
---|
1021 | <?php while ($r->have_posts()) : $r->the_post(); ?> |
---|
1022 | <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> |
---|
1023 | <?php endwhile; ?> |
---|
1024 | </ul> |
---|
1025 | <?php echo $after_widget; ?> |
---|
1026 | <?php |
---|
1027 | endif; |
---|
1028 | wp_cache_add('widget_recent_entries', ob_get_flush()); |
---|
1029 | } |
---|
1030 | |
---|
1031 | function flush_widget_recent_entries() { |
---|
1032 | wp_cache_delete('widget_recent_entries'); |
---|
1033 | } |
---|
1034 | add_action('save_post', 'flush_widget_recent_entries'); |
---|
1035 | add_action('post_deleted', 'flush_widget_recent_entries'); |
---|
1036 | |
---|
1037 | function widget_recent_entries_control() { |
---|
1038 | $options = $newoptions = get_option('widget_recent_entries'); |
---|
1039 | if ( $_POST["recent-entries-submit"] ) { |
---|
1040 | $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"])); |
---|
1041 | $newoptions['number'] = (int) $_POST["recent-entries-number"]; |
---|
1042 | } |
---|
1043 | if ( $options != $newoptions ) { |
---|
1044 | $options = $newoptions; |
---|
1045 | update_option('widget_recent_entries', $options); |
---|
1046 | flush_widget_recent_entries(); |
---|
1047 | } |
---|
1048 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
1049 | if ( !$number = (int) $options['number'] ) |
---|
1050 | $number = 5; |
---|
1051 | ?> |
---|
1052 | <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
1053 | <p><label for="recent-entries-number"><?php _e('Number of posts to show:', 'widgets'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label> <?php _e('(at most 15)', 'widgets'); ?></p> |
---|
1054 | <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" /> |
---|
1055 | <?php |
---|
1056 | } |
---|
1057 | |
---|
1058 | function widget_recent_comments($args) { |
---|
1059 | global $wpdb, $comments, $comment; |
---|
1060 | extract($args, EXTR_SKIP); |
---|
1061 | $options = get_option('widget_recent_comments'); |
---|
1062 | $title = empty($options['title']) ? __('Recent Comments', 'widgets') : $options['title']; |
---|
1063 | if ( !$number = (int) $options['number'] ) |
---|
1064 | $number = 5; |
---|
1065 | else if ( $number < 1 ) |
---|
1066 | $number = 1; |
---|
1067 | else if ( $number > 15 ) |
---|
1068 | $number = 15; |
---|
1069 | |
---|
1070 | if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { |
---|
1071 | $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); |
---|
1072 | wp_cache_add( 'recent_comments', $comments, 'widget' ); |
---|
1073 | } |
---|
1074 | ?> |
---|
1075 | |
---|
1076 | <?php echo $before_widget; ?> |
---|
1077 | <?php echo $before_title . $title . $after_title; ?> |
---|
1078 | <ul id="recentcomments"><?php |
---|
1079 | if ( $comments ) : foreach ($comments as $comment) : |
---|
1080 | echo '<li class="recentcomments">' . sprintf(__('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>'; |
---|
1081 | endforeach; endif;?></ul> |
---|
1082 | <?php echo $after_widget; ?> |
---|
1083 | <?php |
---|
1084 | } |
---|
1085 | |
---|
1086 | function delete_recent_comments_cache() { |
---|
1087 | wp_cache_delete( 'recent_comments', 'widget' ); |
---|
1088 | } |
---|
1089 | add_action( 'comment_post', 'delete_recent_comments_cache' ); |
---|
1090 | add_action( 'wp_set_comment_status', 'delete_recent_comments_cache' ); |
---|
1091 | |
---|
1092 | function widget_recent_comments_control() { |
---|
1093 | $options = $newoptions = get_option('widget_recent_comments'); |
---|
1094 | if ( $_POST["recent-comments-submit"] ) { |
---|
1095 | $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"])); |
---|
1096 | $newoptions['number'] = (int) $_POST["recent-comments-number"]; |
---|
1097 | } |
---|
1098 | if ( $options != $newoptions ) { |
---|
1099 | $options = $newoptions; |
---|
1100 | update_option('widget_recent_comments', $options); |
---|
1101 | delete_recent_comments_cache(); |
---|
1102 | } |
---|
1103 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
---|
1104 | if ( !$number = (int) $options['number'] ) |
---|
1105 | $number = 5; |
---|
1106 | ?> |
---|
1107 | <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p> |
---|
1108 | <p><label for="recent-comments-number"><?php _e('Number of comments to show:', 'widgets'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label> <?php _e('(at most 15)', 'widgets'); ?></p> |
---|
1109 | <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" /> |
---|
1110 | <?php |
---|
1111 | } |
---|
1112 | |
---|
1113 | function widget_recent_comments_style() { |
---|
1114 | ?> |
---|
1115 | <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style> |
---|
1116 | <?php |
---|
1117 | } |
---|
1118 | |
---|
1119 | function widget_recent_comments_register() { |
---|
1120 | register_sidebar_widget(__('Recent Comments', 'widgets'), 'widget_recent_comments', null, 'recent-comments'); |
---|
1121 | register_widget_control(__('Recent Comments', 'widgets'), 'widget_recent_comments_control', 320, 90, 'recent-comments'); |
---|
1122 | |
---|
1123 | if ( is_active_widget('widget_recent_comments') ) |
---|
1124 | add_action('wp_head', 'widget_recent_comments_style'); |
---|
1125 | } |
---|
1126 | |
---|
1127 | function widget_rss($args, $number = 1) { |
---|
1128 | if ( file_exists(ABSPATH . WPINC . '/rss.php') ) |
---|
1129 | require_once(ABSPATH . WPINC . '/rss.php'); |
---|
1130 | else |
---|
1131 | require_once(ABSPATH . WPINC . '/rss-functions.php'); |
---|
1132 | extract($args); |
---|
1133 | $options = get_option('widget_rss'); |
---|
1134 | if ( isset($options['error']) && $options['error'] ) |
---|
1135 | return; |
---|
1136 | $num_items = (int) $options[$number]['items']; |
---|
1137 | $show_summary = $options[$number]['show_summary']; |
---|
1138 | if ( empty($num_items) || $num_items < 1 || $num_items > 10 ) $num_items = 10; |
---|
1139 | $url = $options[$number]['url']; |
---|
1140 | while ( strstr($url, 'http') != $url ) |
---|
1141 | $url = substr($url, 1); |
---|
1142 | if ( empty($url) ) |
---|
1143 | return; |
---|
1144 | $rss = fetch_rss_summary($url, array( 'link', 'title', 'description' ) ); |
---|
1145 | $link = wp_specialchars(strip_tags($rss->channel['link']), 1); |
---|
1146 | while ( strstr($link, 'http') != $link ) |
---|
1147 | $link = substr($link, 1); |
---|
1148 | $desc = wp_specialchars(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)), 1); |
---|
1149 | $title = $options[$number]['title']; |
---|
1150 | if ( empty($title) ) |
---|
1151 | $title = htmlentities(strip_tags($rss->channel['title'])); |
---|
1152 | if ( empty($title) ) |
---|
1153 | $title = $desc; |
---|
1154 | if ( empty($title) ) |
---|
1155 | $title = __('Unknown Feed', 'widgets'); |
---|
1156 | $url = wp_specialchars(strip_tags($url), 1); |
---|
1157 | if ( file_exists(dirname(__FILE__) . '/rss.png') ) |
---|
1158 | $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png'; |
---|
1159 | else |
---|
1160 | $icon = get_option('siteurl').'/wp-includes/images/rss.png'; |
---|
1161 | $title = "<a class='rsswidget' href='$url' title='Syndicate this content'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>"; |
---|
1162 | ?> |
---|
1163 | <?php echo $before_widget; ?> |
---|
1164 | <?php $title ? print($before_title . $title . $after_title) : null; ?> |
---|
1165 | <ul> |
---|
1166 | <?php |
---|
1167 | if ( is_array( $rss->items ) ) { |
---|
1168 | $rss->items = array_slice($rss->items, 0, $num_items); |
---|
1169 | foreach ($rss->items as $item ) { |
---|
1170 | while ( strstr($item['link'], 'http') != $item['link'] ) |
---|
1171 | $item['link'] = substr($item['link'], 1); |
---|
1172 | $link = wp_specialchars(strip_tags($item['link']), 1); |
---|
1173 | $title = wp_specialchars(strip_tags($item['title']), 1); |
---|
1174 | if ( empty($title) ) |
---|
1175 | $title = __('Untitled', 'widgets'); |
---|
1176 | $desc = ''; |
---|
1177 | if ( $show_summary ) { |
---|
1178 | $summary = '<div class="rssSummary">' . $item['description'] . '</div>'; |
---|
1179 | } else { |
---|
1180 | if ( isset( $item['description'] ) && is_string( $item['description'] ) ) |
---|
1181 | $desc = str_replace(array("\n", "\r"), ' ', wp_specialchars(strip_tags(html_entity_decode($item['description'], ENT_QUOTES)), 1)); |
---|
1182 | $summary = ''; |
---|
1183 | } |
---|
1184 | echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>$summary</li>"; |
---|
1185 | } |
---|
1186 | } else { |
---|
1187 | echo __('<li>An error has occured; the feed is probably down. Try again later.</li>', 'widgets'); |
---|
1188 | } |
---|
1189 | ?> |
---|
1190 | </ul> |
---|
1191 | <?php echo $after_widget; ?> |
---|
1192 | <?php |
---|
1193 | } |
---|
1194 | |
---|
1195 | function widget_rss_control($number) { |
---|
1196 | $options = $newoptions = get_option('widget_rss'); |
---|
1197 | if ( $_POST["rss-submit-$number"] ) { |
---|
1198 | $newoptions[$number]['items'] = (int) $_POST["rss-items-$number"]; |
---|
1199 | $url = strip_tags(stripslashes($_POST["rss-url-$number"])); |
---|
1200 | $newoptions[$number]['title'] = trim(strip_tags(stripslashes($_POST["rss-title-$number"])));print_r($_POST); |
---|
1201 | if ( $url !== $options[$number]['url'] ) { |
---|
1202 | if ( file_exists(ABSPATH . WPINC . '/rss.php') ) |
---|
1203 | require_once(ABSPATH . WPINC . '/rss.php'); |
---|
1204 | else |
---|
1205 | require_once(ABSPATH . WPINC . '/rss-functions.php'); |
---|
1206 | $rss = fetch_rss_summary($url); |
---|
1207 | if ( is_object($rss) && $rss->status == 200 ) { |
---|
1208 | $newoptions[$number]['url'] = $url; |
---|
1209 | $newoptions[$number]['error'] = false; |
---|
1210 | } else { |
---|
1211 | $newoptions[$number]['error'] = true; |
---|
1212 | $newoptions[$number]['url'] = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.', 'widgets'), 1); |
---|
1213 | $error = sprintf(__('Error in RSS %1$d: %2$s', 'sandbox'), $number, $newoptions[$number]['error']); |
---|
1214 | } |
---|
1215 | } |
---|
1216 | } |
---|
1217 | if ( $options != $newoptions ) { |
---|
1218 | $options = $newoptions; |
---|
1219 | update_option('widget_rss', $options); |
---|
1220 | } |
---|
1221 | $url = htmlspecialchars($options[$number]['url'], ENT_QUOTES); |
---|
1222 | $items = (int) $options[$number]['items']; |
---|
1223 | $title = htmlspecialchars($options[$number]['title'], ENT_QUOTES); |
---|
1224 | if ( empty($items) || $items < 1 ) $items = 10; |
---|
1225 | ?> |
---|
1226 | <p style="text-align:center;"><?php _e('Enter the RSS feed URL here:', 'widgets'); ?></p> |
---|
1227 | <input style="width: 400px;" id="rss-url-<?php echo "$number"; ?>" name="rss-url-<?php echo "$number"; ?>" type="text" value="<?php echo $url; ?>" /> |
---|
1228 | <p style="text-align:center;"><?php _e('Give the feed a title (optional):', 'widgets'); ?></p> |
---|
1229 | <input style="width: 400px;" id="rss-title-<?php echo "$number"; ?>" name="rss-title-<?php echo "$number"; ?>" type="text" value="<?php echo $title; ?>" /> |
---|
1230 | <p style="text-align:center; line-height: 30px;"><?php _e('How many items would you like to display?', 'widgets'); ?> <select id="rss-items-<?php echo $number; ?>" name="rss-items-<?php echo $number; ?>"><?php for ( $i = 1; $i <= 10; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?></select></p> |
---|
1231 | <input type="hidden" id="rss-submit-<?php echo "$number"; ?>" name="rss-submit-<?php echo "$number"; ?>" value="1" /> |
---|
1232 | <?php |
---|
1233 | } |
---|
1234 | |
---|
1235 | function widget_rss_setup() { |
---|
1236 | $options = $newoptions = get_option('widget_rss'); |
---|
1237 | if ( isset($_POST['rss-number-submit']) ) { |
---|
1238 | $number = (int) $_POST['rss-number']; |
---|
1239 | if ( $number > 9 ) $number = 9; |
---|
1240 | if ( $number < 1 ) $number = 1; |
---|
1241 | $newoptions['number'] = $number; |
---|
1242 | } |
---|
1243 | if ( $options != $newoptions ) { |
---|
1244 | $options = $newoptions; |
---|
1245 | update_option('widget_rss', $options); |
---|
1246 | widget_rss_register($options['number']); |
---|
1247 | } |
---|
1248 | } |
---|
1249 | |
---|
1250 | function widget_rss_page() { |
---|
1251 | $options = $newoptions = get_option('widget_rss'); |
---|
1252 | ?> |
---|
1253 | <div class="wrap"> |
---|
1254 | <form method="POST"> |
---|
1255 | <h2><?php _e('RSS Feed Widgets', 'widgets'); ?></h2> |
---|
1256 | <p style="line-height: 30px;"><?php _e('How many RSS widgets would you like?', 'widgets'); ?> |
---|
1257 | <select id="rss-number" name="rss-number" value="<?php echo $options['number']; ?>"> |
---|
1258 | <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?> |
---|
1259 | </select> |
---|
1260 | <span class="submit"><input type="submit" name="rss-number-submit" id="rss-number-submit" value="<?php _e('Save'); ?>" /></span></p> |
---|
1261 | </form> |
---|
1262 | </div> |
---|
1263 | <?php |
---|
1264 | } |
---|
1265 | |
---|
1266 | function widget_rss_register() { |
---|
1267 | $options = get_option('widget_rss'); |
---|
1268 | $number = $options['number']; |
---|
1269 | if ( $number < 1 ) $number = 1; |
---|
1270 | if ( $number > 9 ) $number = 9; |
---|
1271 | for ($i = 1; $i <= 9; $i++) { |
---|
1272 | $name = sprintf(__('RSS %d', 'widgets'), $i); |
---|
1273 | $id = "rss-$i"; // Never never never translate an id |
---|
1274 | register_sidebar_widget($name, $i <= $number ? 'widget_rss' : /* unregister */ '', null, $id, $i); |
---|
1275 | register_widget_control($name, $i <= $number ? 'widget_rss_control' : /* unregister */ '', 410, 200, $id, $i); |
---|
1276 | } |
---|
1277 | add_action('sidebar_admin_setup', 'widget_rss_setup'); |
---|
1278 | add_action('sidebar_admin_page', 'widget_rss_page'); |
---|
1279 | } |
---|
1280 | |
---|
1281 | function widgets_init() { |
---|
1282 | global $register_widget_defaults; |
---|
1283 | load_plugin_textdomain('widgets', 'wp-content/plugins/widgets'); |
---|
1284 | add_action('admin_menu', 'sidebar_admin_setup'); |
---|
1285 | |
---|
1286 | $register_widget_defaults = true; |
---|
1287 | register_sidebar_widget(__('Pages', 'widgets'), 'widget_pages', null, 'pages'); |
---|
1288 | register_widget_control(__('Pages', 'widgets'), 'widget_pages_control', 300, 90, 'pages'); |
---|
1289 | register_sidebar_widget(__('Calendar', 'widgets'), 'widget_calendar', null, 'calendar'); |
---|
1290 | register_widget_control(__('Calendar', 'widgets'), 'widget_calendar_control', 300, 90, 'calendar'); |
---|
1291 | register_sidebar_widget(__('Archives', 'widgets'), 'widget_archives', null, 'archives'); |
---|
1292 | register_widget_control(__('Archives', 'widgets'), 'widget_archives_control', 300, 90, 'archives'); |
---|
1293 | register_sidebar_widget(__('Links', 'widgets'), 'widget_links', null, 'links'); |
---|
1294 | register_sidebar_widget(__('Meta', 'widgets'), 'widget_meta', null, 'meta'); |
---|
1295 | register_widget_control(__('Meta', 'widgets'), 'widget_meta_control', 300, 90, 'meta'); |
---|
1296 | register_sidebar_widget(__('Search', 'widgets'), 'widget_search', null, 'search'); |
---|
1297 | register_sidebar_widget(__('Categories', 'widgets'), 'widget_categories', null, 'categories'); |
---|
1298 | register_widget_control(__('Categories', 'widgets'), 'widget_categories_control', 300, 150, 'categories'); |
---|
1299 | register_sidebar_widget(__('Recent Posts', 'widgets'), 'widget_recent_entries', null, 'recent-posts'); |
---|
1300 | register_widget_control(__('Recent Posts', 'widgets'), 'widget_recent_entries_control', 300, 90, 'recent-posts'); |
---|
1301 | widget_text_register(); |
---|
1302 | widget_rss_register(); |
---|
1303 | widget_recent_comments_register(); |
---|
1304 | $register_widget_defaults = false; |
---|
1305 | |
---|
1306 | do_action('widgets_init'); |
---|
1307 | } |
---|
1308 | |
---|
1309 | add_action('init', 'widgets_init', 5); |
---|
1310 | |
---|
1311 | ?> |
---|