]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / widgets.php
1 <?php
2
3 /* Global Variables */
4
5 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
6
7 $wp_registered_sidebars = array();
8 $wp_registered_widgets = array();
9 $wp_registered_widget_controls = array();
10
11 /* Template tags & API functions */
12
13 function register_sidebars($number = 1, $args = array()) {
14         global $wp_registered_sidebars;
15         $number = (int) $number;
16
17         if ( is_string($args) )
18                 parse_str($args, $args);
19
20         for ( $i=1; $i <= $number; $i++ ) {
21                 $_args = $args;
22
23                 if ( $number > 1 ) {
24                         $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
25                 } else {
26                         $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
27                 }
28
29                 if (isset($args['id'])) {
30                         $_args['id'] = $args['id'];
31                 } else {
32                         $n = count($wp_registered_sidebars);
33                         do {
34                                 $n++;
35                                 $_args['id'] = "sidebar-$n";
36                         } while (isset($wp_registered_sidebars[$_args['id']]));
37                 }
38
39                 register_sidebar($_args);
40         }
41 }
42
43 function register_sidebar($args = array()) {
44         global $wp_registered_sidebars;
45
46         if ( is_string($args) )
47                 parse_str($args, $args);
48
49         $i = count($wp_registered_sidebars) + 1;
50
51         $defaults = array(
52                 'name' => sprintf(__('Sidebar %d'), $i ),
53                 'id' => "sidebar-$i",
54                 'before_widget' => '<li id="%1$s" class="widget %2$s">',
55                 'after_widget' => "</li>\n",
56                 'before_title' => '<h2 class="widgettitle">',
57                 'after_title' => "</h2>\n",
58         );
59
60         $sidebar = array_merge($defaults, $args);
61
62         $wp_registered_sidebars[$sidebar['id']] = $sidebar;
63
64         return $sidebar['id'];
65 }
66
67 function unregister_sidebar( $name ) {
68         global $wp_registered_sidebars;
69
70         if ( isset( $wp_registered_sidebars[$name] ) )
71                 unset( $wp_registered_sidebars[$name] );
72 }
73
74 function register_sidebar_widget($name, $output_callback, $classname = '') {
75         // Compat
76         if ( is_array($name) ) {
77                 if ( count($name) == 3 )
78                         $name = sprintf($name[0], $name[2]);
79                 else
80                         $name = $name[0];
81         }
82
83         $id = sanitize_title($name);
84         $options = array();
85         if ( !empty($classname) && is_string($classname) )
86                 $options['classname'] = $classname;
87         $params = array_slice(func_get_args(), 2);
88         $args = array($id, $name, $output_callback, $options);
89         if ( !empty($params) )
90                 $args = array_merge($args, $params);
91
92         call_user_func_array('wp_register_sidebar_widget', $args);
93 }
94
95 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
96         global $wp_registered_widgets;
97
98         if ( empty($output_callback) ) {
99                 unset($wp_registered_widgets[$id]);
100                 return;
101         }
102
103         $defaults = array('classname' => $output_callback);
104         $options = wp_parse_args($options, $defaults);
105         $widget = array(
106                 'name' => $name,
107                 'id' => $id,
108                 'callback' => $output_callback,
109                 'params' => array_slice(func_get_args(), 4)
110         );
111         $widget = array_merge($widget, $options);
112
113         if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
114                 $wp_registered_widgets[$id] = $widget;
115 }
116
117 function wp_widget_description( $id ) {
118         if ( !is_scalar($id) )
119                 return;
120
121         global $wp_registered_widgets;
122
123         if ( isset($wp_registered_widgets[$id]['description']) )
124                 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
125 }
126
127 function unregister_sidebar_widget($id) {
128         return wp_unregister_sidebar_widget($id);
129 }
130
131 function wp_unregister_sidebar_widget($id) {
132         wp_register_sidebar_widget($id, '', '');
133         wp_unregister_widget_control($id);
134 }
135
136 function register_widget_control($name, $control_callback, $width = '', $height = '') {
137         // Compat
138         if ( is_array($name) ) {
139                 if ( count($name) == 3 )
140                         $name = sprintf($name[0], $name[2]);
141                 else
142                         $name = $name[0];
143         }
144
145         $id = sanitize_title($name);
146         $options = array();
147         if ( !empty($width) )
148                 $options['width'] = $width;
149         if ( !empty($height) )
150                 $options['height'] = $height;
151         $params = array_slice(func_get_args(), 4);
152         $args = array($id, $name, $control_callback, $options);
153         if ( !empty($params) )
154                 $args = array_merge($args, $params);
155
156         call_user_func_array('wp_register_widget_control', $args);
157 }
158
159 /* $options: height, width, id_base
160  *   height: never used
161  *   width:  width of fully expanded control form.  Try hard to use the default width.
162  *   id_base: for multi-widgets (widgets which allow multiple instances such as the text widget), an id_base must be provided.
163  *            the widget id will ennd up looking like {$id_base}-{$unique_number}
164  */
165 function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
166         global $wp_registered_widget_controls;
167
168         if ( empty($control_callback) ) {
169                 unset($wp_registered_widget_controls[$id]);
170                 return;
171         }
172
173         if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
174                 return;
175
176         $defaults = array('width' => 250, 'height' => 200 ); // height is never used
177         $options = wp_parse_args($options, $defaults);
178         $options['width'] = (int) $options['width'];
179         $options['height'] = (int) $options['height'];
180
181         $widget = array(
182                 'name' => $name,
183                 'id' => $id,
184                 'callback' => $control_callback,
185                 'params' => array_slice(func_get_args(), 4)
186         );
187         $widget = array_merge($widget, $options);
188
189         $wp_registered_widget_controls[$id] = $widget;
190 }
191
192 function unregister_widget_control($id) {
193         return wp_unregister_widget_control($id);
194 }
195
196 function wp_unregister_widget_control($id) {
197         return wp_register_widget_control($id, '', '');
198 }
199
200 function dynamic_sidebar($index = 1) {
201         global $wp_registered_sidebars, $wp_registered_widgets;
202
203         if ( is_int($index) ) {
204                 $index = "sidebar-$index";
205         } else {
206                 $index = sanitize_title($index);
207                 foreach ( $wp_registered_sidebars as $key => $value ) {
208                         if ( sanitize_title($value['name']) == $index ) {
209                                 $index = $key;
210                                 break;
211                         }
212                 }
213         }
214
215         $sidebars_widgets = wp_get_sidebars_widgets();
216
217         if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
218                 return false;
219
220         $sidebar = $wp_registered_sidebars[$index];
221
222         $did_one = false;
223         foreach ( $sidebars_widgets[$index] as $id ) {
224                 $params = array_merge(
225                         array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
226                         (array) $wp_registered_widgets[$id]['params']
227                 );
228
229                 // Substitute HTML id and class attributes into before_widget
230                 $classname_ = '';
231                 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
232                         if ( is_string($cn) )
233                                 $classname_ .= '_' . $cn;
234                         elseif ( is_object($cn) )
235                                 $classname_ .= '_' . get_class($cn);
236                 }
237                 $classname_ = ltrim($classname_, '_');
238                 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
239
240                 $params = apply_filters( 'dynamic_sidebar_params', $params );
241
242                 $callback = $wp_registered_widgets[$id]['callback'];
243
244                 if ( is_callable($callback) ) {
245                         call_user_func_array($callback, $params);
246                         $did_one = true;
247                 }
248         }
249
250         return $did_one;
251 }
252
253 /* @return mixed false if widget is not active or id of sidebar in which the widget is active
254  */
255 function is_active_widget($callback, $widget_id = false) {
256         global $wp_registered_widgets;
257
258         $sidebars_widgets = wp_get_sidebars_widgets(false);
259
260         if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
261                 if ( is_array($widgets) ) foreach ( $widgets as $widget )
262                         if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
263                                 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
264                                         return $sidebar;
265
266
267         return false;
268 }
269
270 function is_dynamic_sidebar() {
271         global $wp_registered_widgets, $wp_registered_sidebars;
272         $sidebars_widgets = get_option('sidebars_widgets');
273         foreach ( $wp_registered_sidebars as $index => $sidebar ) {
274                 if ( count($sidebars_widgets[$index]) ) {
275                         foreach ( $sidebars_widgets[$index] as $widget )
276                                 if ( array_key_exists($widget, $wp_registered_widgets) )
277                                         return true;
278                 }
279         }
280         return false;
281 }
282
283 /* Internal Functions */
284
285 function wp_get_sidebars_widgets($update = true) {
286         global $wp_registered_widgets, $wp_registered_sidebars;
287
288         $sidebars_widgets = get_option('sidebars_widgets');
289         $_sidebars_widgets = array();
290
291         if ( !isset($sidebars_widgets['array_version']) )
292                 $sidebars_widgets['array_version'] = 1;
293
294         switch ( $sidebars_widgets['array_version'] ) {
295                 case 1 :
296                         foreach ( $sidebars_widgets as $index => $sidebar )
297                         if ( is_array($sidebar) )
298                         foreach ( $sidebar as $i => $name ) {
299                                 $id = strtolower($name);
300                                 if ( isset($wp_registered_widgets[$id]) ) {
301                                         $_sidebars_widgets[$index][$i] = $id;
302                                         continue;
303                                 }
304                                 $id = sanitize_title($name);
305                                 if ( isset($wp_registered_widgets[$id]) ) {
306                                         $_sidebars_widgets[$index][$i] = $id;
307                                         continue;
308                                 }
309                                 
310                                 $found = false;
311                                 
312                                 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
313                                         if ( strtolower($widget['name']) == strtolower($name) ) {
314                                                 $_sidebars_widgets[$index][$i] = $widget['id'];
315                                                 $found = true;
316                                                 break;
317                                         } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
318                                                 $_sidebars_widgets[$index][$i] = $widget['id'];
319                                                 $found = true;
320                                                 break;
321                                         }
322                                 }
323                                 
324                                 if ( $found )
325                                         continue;
326                                 
327                                 unset($_sidebars_widgets[$index][$i]);
328                         }
329                         $_sidebars_widgets['array_version'] = 2;
330                         $sidebars_widgets = $_sidebars_widgets;
331                         unset($_sidebars_widgets);
332
333                 case 2 :
334                         $sidebars = array_keys( $wp_registered_sidebars );
335                         if ( !empty( $sidebars ) ) {
336                                 // Move the known-good ones first
337                                 foreach ( $sidebars as $id ) {
338                                         if ( array_key_exists( $id, $sidebars_widgets ) ) {
339                                                 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
340                                                 unset($sidebars_widgets[$id], $sidebars[$id]);
341                                         }
342                                 }
343
344                                 // Assign to each unmatched registered sidebar the first available orphan
345                                 unset( $sidebars_widgets[ 'array_version' ] );
346                                 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
347                                         $_sidebars_widgets[ $sidebar ] = $widgets;
348
349                                 $_sidebars_widgets['array_version'] = 3;
350                                 $sidebars_widgets = $_sidebars_widgets;
351                                 unset($_sidebars_widgets);
352                         }
353
354                         if ( $update )
355                                 update_option('sidebars_widgets', $sidebars_widgets);
356         }
357
358         unset($sidebars_widgets['array_version']);
359
360         return $sidebars_widgets;
361 }
362
363 function wp_set_sidebars_widgets( $sidebars_widgets ) {
364         update_option( 'sidebars_widgets', $sidebars_widgets );
365 }
366
367 function wp_get_widget_defaults() {
368         global $wp_registered_sidebars;
369
370         $defaults = array();
371
372         foreach ( $wp_registered_sidebars as $index => $sidebar )
373                 $defaults[$index] = array();
374
375         return $defaults;
376 }
377
378 /* Default Widgets */
379
380 function wp_widget_pages( $args ) {
381         extract( $args );
382         $options = get_option( 'widget_pages' );
383
384         $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title'];
385         $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
386         $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
387
388         if ( $sortby == 'menu_order' ) {
389                 $sortby = 'menu_order, post_title';
390         }
391
392         $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
393
394         if ( !empty( $out ) ) {
395 ?>
396         <?php echo $before_widget; ?>
397                 <?php echo $before_title . $title . $after_title; ?>
398                 <ul>
399                         <?php echo $out; ?>
400                 </ul>
401         <?php echo $after_widget; ?>
402 <?php
403         }
404 }
405
406 function wp_widget_pages_control() {
407         $options = $newoptions = get_option('widget_pages');
408         if ( $_POST['pages-submit'] ) {
409                 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
410
411                 $sortby = stripslashes( $_POST['pages-sortby'] );
412
413                 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
414                         $newoptions['sortby'] = $sortby;
415                 } else {
416                         $newoptions['sortby'] = 'menu_order';
417                 }
418
419                 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
420         }
421         if ( $options != $newoptions ) {
422                 $options = $newoptions;
423                 update_option('widget_pages', $options);
424         }
425         $title = attribute_escape($options['title']);
426         $exclude = attribute_escape( $options['exclude'] );
427 ?>
428                 <p><label for="pages-title"><?php _e('Title:'); ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
429                 <p>
430                         <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
431                                 <select name="pages-sortby" id="pages-sortby" class="widefat">
432                                         <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
433                                         <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
434                                         <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
435                                 </select>
436                         </label>
437                 </p>
438                 <p>
439                         <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
440                         <br />
441                         <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
442                 </p>
443                 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
444 <?php
445 }
446
447 function wp_widget_links($args) {
448         extract($args, EXTR_SKIP);
449
450         $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
451         wp_list_bookmarks(array(
452                 'title_before' => $before_title, 'title_after' => $after_title,
453                 'category_before' => $before_widget, 'category_after' => $after_widget,
454                 'show_images' => true, 'class' => 'linkcat widget'
455         ));
456 }
457
458 function wp_widget_search($args) {
459         extract($args);
460 ?>
461                 <?php echo $before_widget; ?>
462                         <form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
463                         <div>
464                         <input type="text" name="s" id="s" size="15" /><br />
465                         <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
466                         </div>
467                         </form>
468                 <?php echo $after_widget; ?>
469 <?php
470 }
471
472 function wp_widget_archives($args) {
473         extract($args);
474         $options = get_option('widget_archives');
475         $c = $options['count'] ? '1' : '0';
476         $d = $options['dropdown'] ? '1' : '0';
477         $title = empty($options['title']) ? __('Archives') : $options['title'];
478
479         echo $before_widget;
480         echo $before_title . $title . $after_title;
481
482         if($d) {
483 ?>
484                 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
485 <?php
486         } else {
487 ?>
488                 <ul>
489                 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
490                 </ul>
491 <?php
492         }
493
494         echo $after_widget;
495 }
496
497 function wp_widget_archives_control() {
498         $options = $newoptions = get_option('widget_archives');
499         if ( $_POST["archives-submit"] ) {
500                 $newoptions['count'] = isset($_POST['archives-count']);
501                 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
502                 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
503         }
504         if ( $options != $newoptions ) {
505                 $options = $newoptions;
506                 update_option('widget_archives', $options);
507         }
508         $count = $options['count'] ? 'checked="checked"' : '';
509         $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
510         $title = attribute_escape($options['title']);
511 ?>
512                         <p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
513                         <p>
514                                 <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
515                                 <br />
516                                 <label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
517                         </p>
518                         <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
519 <?php
520 }
521
522 function wp_widget_meta($args) {
523         extract($args);
524         $options = get_option('widget_meta');
525         $title = empty($options['title']) ? __('Meta') : $options['title'];
526 ?>
527                 <?php echo $before_widget; ?>
528                         <?php echo $before_title . $title . $after_title; ?>
529                         <ul>
530                         <?php wp_register(); ?>
531                         <li><?php wp_loginout(); ?></li>
532                         <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo attribute_escape(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
533                         <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo attribute_escape(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
534                         <li><a href="http://wordpress.org/" title="<?php echo attribute_escape(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
535                         <?php wp_meta(); ?>
536                         </ul>
537                 <?php echo $after_widget; ?>
538 <?php
539 }
540 function wp_widget_meta_control() {
541         $options = $newoptions = get_option('widget_meta');
542         if ( $_POST["meta-submit"] ) {
543                 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
544         }
545         if ( $options != $newoptions ) {
546                 $options = $newoptions;
547                 update_option('widget_meta', $options);
548         }
549         $title = attribute_escape($options['title']);
550 ?>
551                         <p><label for="meta-title"><?php _e('Title:'); ?> <input class="widefat" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p>
552                         <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
553 <?php
554 }
555
556 function wp_widget_calendar($args) {
557         extract($args);
558         $options = get_option('widget_calendar');
559         $title = $options['title'];
560         if ( empty($title) )
561                 $title = '&nbsp;';
562         echo $before_widget . $before_title . $title . $after_title;
563         echo '<div id="calendar_wrap">';
564         get_calendar();
565         echo '</div>';
566         echo $after_widget;
567 }
568 function wp_widget_calendar_control() {
569         $options = $newoptions = get_option('widget_calendar');
570         if ( $_POST["calendar-submit"] ) {
571                 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
572         }
573         if ( $options != $newoptions ) {
574                 $options = $newoptions;
575                 update_option('widget_calendar', $options);
576         }
577         $title = attribute_escape($options['title']);
578 ?>
579                         <p><label for="calendar-title"><?php _e('Title:'); ?> <input class="widefat" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p>
580                         <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
581 <?php
582 }
583
584 // See large comment section at end of this file
585 function wp_widget_text($args, $widget_args = 1) {
586         extract( $args, EXTR_SKIP );
587         if ( is_numeric($widget_args) )
588                 $widget_args = array( 'number' => $widget_args );
589         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
590         extract( $widget_args, EXTR_SKIP );
591
592         $options = get_option('widget_text');
593         if ( !isset($options[$number]) )
594                 return;
595
596         $title = $options[$number]['title'];
597         $text = apply_filters( 'widget_text', $options[$number]['text'] );
598 ?>
599                 <?php echo $before_widget; ?>
600                         <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
601                         <div class="textwidget"><?php echo $text; ?></div>
602                 <?php echo $after_widget; ?>
603 <?php
604 }
605
606 function wp_widget_text_control($widget_args) {
607         global $wp_registered_widgets;
608         static $updated = false;
609
610         if ( is_numeric($widget_args) )
611                 $widget_args = array( 'number' => $widget_args );
612         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
613         extract( $widget_args, EXTR_SKIP );
614
615         $options = get_option('widget_text');
616         if ( !is_array($options) )
617                 $options = array();
618
619         if ( !$updated && !empty($_POST['sidebar']) ) {
620                 $sidebar = (string) $_POST['sidebar'];
621
622                 $sidebars_widgets = wp_get_sidebars_widgets();
623                 if ( isset($sidebars_widgets[$sidebar]) )
624                         $this_sidebar =& $sidebars_widgets[$sidebar];
625                 else
626                         $this_sidebar = array();
627
628                 foreach ( $this_sidebar as $_widget_id ) {
629                         if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
630                                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
631                                 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
632                                         unset($options[$widget_number]);
633                         }
634                 }
635
636                 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
637                         if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
638                                 continue;
639                         $title = strip_tags(stripslashes($widget_text['title']));
640                         if ( current_user_can('unfiltered_html') )
641                                 $text = stripslashes( $widget_text['text'] );
642                         else
643                                 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
644                         $options[$widget_number] = compact( 'title', 'text' );
645                 }
646
647                 update_option('widget_text', $options);
648                 $updated = true;
649         }
650
651         if ( -1 == $number ) {
652                 $title = '';
653                 $text = '';
654                 $number = '%i%';
655         } else {
656                 $title = attribute_escape($options[$number]['title']);
657                 $text = format_to_edit($options[$number]['text']);
658         }
659 ?>
660                 <p>
661                         <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
662                         <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
663                         <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
664                 </p>
665 <?php
666 }
667
668 function wp_widget_text_register() {
669         if ( !$options = get_option('widget_text') )
670                 $options = array();
671         $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
672         $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
673         $name = __('Text');
674
675         $id = false;
676         foreach ( array_keys($options) as $o ) {
677                 // Old widgets can have null values for some reason
678                 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
679                         continue;
680                 $id = "text-$o"; // Never never never translate an id
681                 wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
682                 wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
683         }
684
685         // If there are none, we register the widget's existance with a generic template
686         if ( !$id ) {
687                 wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
688                 wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
689         }
690 }
691
692 // See large comment section at end of this file
693 function wp_widget_categories($args, $widget_args = 1) {
694         extract($args, EXTR_SKIP);
695         if ( is_numeric($widget_args) )
696                 $widget_args = array( 'number' => $widget_args );
697         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
698         extract($widget_args, EXTR_SKIP);
699
700         $options = get_option('widget_categories');
701         if ( !isset($options[$number]) )
702                 return;
703
704         $c = $options[$number]['count'] ? '1' : '0';
705         $h = $options[$number]['hierarchical'] ? '1' : '0';
706         $d = $options[$number]['dropdown'] ? '1' : '0';
707
708         $title = empty($options[$number]['title']) ? __('Categories') : $options[$number]['title'];
709
710         echo $before_widget;
711         echo $before_title . $title . $after_title;
712
713         $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";
714
715         if ( $d ) {
716                 wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category'));
717 ?>
718
719 <script type='text/javascript'>
720 /* <![CDATA[ */
721     var dropdown = document.getElementById("cat");
722     function onCatChange() {
723                 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
724                         location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
725                 }
726     }
727     dropdown.onchange = onCatChange;
728 /* ]]> */
729 </script>
730
731 <?php
732         } else {
733 ?>
734                 <ul>
735                 <?php wp_list_categories($cat_args . '&title_li='); ?>
736                 </ul>
737 <?php
738         }
739
740         echo $after_widget;
741 }
742
743 function wp_widget_categories_control( $widget_args ) {
744         global $wp_registered_widgets;
745         static $updated = false;
746
747         if ( is_numeric($widget_args) )
748                 $widget_args = array( 'number' => $widget_args );
749         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
750         extract($widget_args, EXTR_SKIP);
751
752         $options = get_option('widget_categories');
753
754         if ( !is_array( $options ) )
755                 $options = array();
756
757         if ( !$updated && !empty($_POST['sidebar']) ) {
758                 $sidebar = (string) $_POST['sidebar'];
759
760                 $sidebars_widgets = wp_get_sidebars_widgets();
761                 if ( isset($sidebars_widgets[$sidebar]) )
762                         $this_sidebar =& $sidebars_widgets[$sidebar];
763                 else
764                         $this_sidebar = array();
765
766                 foreach ( $this_sidebar as $_widget_id ) {
767                         if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
768                                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
769                                 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
770                                         unset($options[$widget_number]);
771                         }
772                 }
773
774                 foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
775                         if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
776                                 continue;
777                         $title = trim(strip_tags(stripslashes($widget_cat['title'])));
778                         $count = isset($widget_cat['count']);
779                         $hierarchical = isset($widget_cat['hierarchical']);
780                         $dropdown = isset($widget_cat['dropdown']);
781                         $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
782                 }
783
784                 update_option('widget_categories', $options);
785                 $updated = true;
786         }
787
788         if ( -1 == $number ) {
789                 $title = '';
790                 $count = false;
791                 $hierarchical = false;
792                 $dropdown = false;
793                 $number = '%i%';
794         } else {
795                 $title = attribute_escape( $options[$number]['title'] );
796                 $count = (bool) $options[$number]['count'];
797                 $hierarchical = (bool) $options[$number]['hierarchical'];
798                 $dropdown = (bool) $options[$number]['dropdown'];
799         }
800 ?>
801                         <p>
802                                 <label for="categories-title-<?php echo $number; ?>">
803                                         <?php _e( 'Title:' ); ?>
804                                         <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
805                                 </label>
806                         </p>
807
808                         <p>
809                                 <label for="categories-dropdown-<?php echo $number; ?>">
810                                         <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown, true ); ?> />
811                                         <?php _e( 'Show as dropdown' ); ?>
812                                 </label>
813                                 <br />
814                                 <label for="categories-count-<?php echo $number; ?>">
815                                         <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count, true ); ?> />
816                                         <?php _e( 'Show post counts' ); ?>
817                                 </label>
818                                 <br />
819                                 <label for="categories-hierarchical-<?php echo $number; ?>">
820                                         <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical, true ); ?> />
821                                         <?php _e( 'Show hierarchy' ); ?>
822                                 </label>
823                         </p>
824
825                         <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
826 <?php
827 }
828
829 function wp_widget_categories_register() {
830         if ( !$options = get_option( 'widget_categories' ) )
831                 $options = array();
832
833         if ( isset($options['title']) )
834                 $options = wp_widget_categories_upgrade();
835
836         $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
837
838         $name = __( 'Categories' );
839
840         $id = false;
841         foreach ( array_keys($options) as $o ) {
842                 // Old widgets can have null values for some reason
843                 if ( !isset($options[$o]['title']) )
844                         continue;
845                 $id = "categories-$o";
846                 wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
847                 wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
848         }
849
850         // If there are none, we register the widget's existance with a generic template
851         if ( !$id ) {
852                 wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
853                 wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
854         }
855 }
856
857 function wp_widget_categories_upgrade() {
858         $options = get_option( 'widget_categories' );
859
860         if ( !isset( $options['title'] ) )
861                 return $options;
862
863         $newoptions = array( 1 => $options );
864
865         update_option( 'widget_categories', $newoptions );
866
867         $sidebars_widgets = get_option( 'sidebars_widgets' );
868         if ( is_array( $sidebars_widgets ) ) {
869                 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
870                         if ( is_array( $widgets ) ) {
871                                 foreach ( $widgets as $widget )
872                                         $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
873                         } else {
874                                 $new_widgets[$sidebar] = $widgets;
875                         }
876                 }
877                 if ( $new_widgets != $sidebars_widgets )
878                         update_option( 'sidebars_widgets', $new_widgets );
879         }
880
881         return $newoptions;
882 }
883
884 function wp_widget_recent_entries($args) {
885         if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
886                 if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
887                         return print($output);
888                 ob_start();
889         }
890
891         extract($args);
892         $options = get_option('widget_recent_entries');
893         $title = empty($options['title']) ? __('Recent Posts') : $options['title'];
894         if ( !$number = (int) $options['number'] )
895                 $number = 10;
896         else if ( $number < 1 )
897                 $number = 1;
898         else if ( $number > 15 )
899                 $number = 15;
900
901         $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish");
902         if ($r->have_posts()) :
903 ?>
904                 <?php echo $before_widget; ?>
905                         <?php echo $before_title . $title . $after_title; ?>
906                         <ul>
907                         <?php  while ($r->have_posts()) : $r->the_post(); ?>
908                         <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
909                         <?php endwhile; ?>
910                         </ul>
911                 <?php echo $after_widget; ?>
912 <?php
913                 wp_reset_query();  // Restore global post data stomped by the_post().
914         endif;
915
916         if ( '%BEG_OF_TITLE%' != $args['before_title'] )
917                 wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
918 }
919
920 function wp_flush_widget_recent_entries() {
921         wp_cache_delete('widget_recent_entries', 'widget');
922 }
923
924 add_action('save_post', 'wp_flush_widget_recent_entries');
925 add_action('deleted_post', 'wp_flush_widget_recent_entries');
926 add_action('switch_theme', 'wp_flush_widget_recent_entries');
927
928 function wp_widget_recent_entries_control() {
929         $options = $newoptions = get_option('widget_recent_entries');
930         if ( $_POST["recent-entries-submit"] ) {
931                 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
932                 $newoptions['number'] = (int) $_POST["recent-entries-number"];
933         }
934         if ( $options != $newoptions ) {
935                 $options = $newoptions;
936                 update_option('widget_recent_entries', $options);
937                 wp_flush_widget_recent_entries();
938         }
939         $title = attribute_escape($options['title']);
940         if ( !$number = (int) $options['number'] )
941                 $number = 5;
942 ?>
943
944                         <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
945                         <p>
946                                 <label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label>
947                                 <br />
948                                 <small><?php _e('(at most 15)'); ?></small>
949                         </p>
950                         <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
951 <?php
952 }
953
954 function wp_widget_recent_comments($args) {
955         global $wpdb, $comments, $comment;
956         extract($args, EXTR_SKIP);
957         $options = get_option('widget_recent_comments');
958         $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
959         if ( !$number = (int) $options['number'] )
960                 $number = 5;
961         else if ( $number < 1 )
962                 $number = 1;
963         else if ( $number > 15 )
964                 $number = 15;
965
966         if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
967                 $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");
968                 wp_cache_add( 'recent_comments', $comments, 'widget' );
969         }
970 ?>
971
972                 <?php echo $before_widget; ?>
973                         <?php echo $before_title . $title . $after_title; ?>
974                         <ul id="recentcomments"><?php
975                         if ( $comments ) : foreach ($comments as $comment) :
976                         echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), 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>';
977                         endforeach; endif;?></ul>
978                 <?php echo $after_widget; ?>
979 <?php
980 }
981
982 function wp_delete_recent_comments_cache() {
983         wp_cache_delete( 'recent_comments', 'widget' );
984 }
985 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
986 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
987
988 function wp_widget_recent_comments_control() {
989         $options = $newoptions = get_option('widget_recent_comments');
990         if ( $_POST["recent-comments-submit"] ) {
991                 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
992                 $newoptions['number'] = (int) $_POST["recent-comments-number"];
993         }
994         if ( $options != $newoptions ) {
995                 $options = $newoptions;
996                 update_option('widget_recent_comments', $options);
997                 wp_delete_recent_comments_cache();
998         }
999         $title = attribute_escape($options['title']);
1000         if ( !$number = (int) $options['number'] )
1001                 $number = 5;
1002 ?>
1003                         <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
1004                         <p>
1005                                 <label for="recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
1006                                 <br />
1007                                 <small><?php _e('(at most 15)'); ?></small>
1008                         </p>
1009                         <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
1010 <?php
1011 }
1012
1013 function wp_widget_recent_comments_style() {
1014 ?>
1015 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
1016 <?php
1017 }
1018
1019 function wp_widget_recent_comments_register() {
1020         $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
1021         wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
1022         wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
1023
1024         if ( is_active_widget('wp_widget_recent_comments') )
1025                 add_action('wp_head', 'wp_widget_recent_comments_style');
1026 }
1027
1028 // See large comment section at end of this file
1029 function wp_widget_rss($args, $widget_args = 1) {
1030         extract($args, EXTR_SKIP);
1031         if ( is_numeric($widget_args) )
1032                 $widget_args = array( 'number' => $widegt_args );
1033         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1034         extract($widget_args, EXTR_SKIP);
1035
1036         $options = get_option('widget_rss');
1037
1038         if ( !isset($options[$number]) )
1039                 return;
1040
1041         if ( isset($options[$number]['error']) && $options[$number]['error'] )
1042                 return;
1043
1044         $url = $options[$number]['url'];
1045         while ( strstr($url, 'http') != $url )
1046                 $url = substr($url, 1);
1047         if ( empty($url) )
1048                 return;
1049
1050         require_once(ABSPATH . WPINC . '/rss.php');
1051
1052         $rss = fetch_rss($url);
1053         $link = clean_url(strip_tags($rss->channel['link']));
1054         while ( strstr($link, 'http') != $link )
1055                 $link = substr($link, 1);
1056         $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
1057         $title = $options[$number]['title'];
1058         if ( empty($title) )
1059                 $title = htmlentities(strip_tags($rss->channel['title']));
1060         if ( empty($title) )
1061                 $title = $desc;
1062         if ( empty($title) )
1063                 $title = __('Unknown Feed');
1064         $url = clean_url(strip_tags($url));
1065         if ( file_exists(dirname(__FILE__) . '/rss.png') )
1066                 $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png';
1067         else
1068                 $icon = get_option('siteurl').'/wp-includes/images/rss.png';
1069         $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('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>";
1070
1071         echo $before_widget;
1072         echo $before_title . $title . $after_title;
1073
1074         wp_widget_rss_output( $rss, $options[$number] );
1075
1076         echo $after_widget;
1077 }
1078
1079 function wp_widget_rss_output( $rss, $args = array() ) {
1080         if ( is_string( $rss ) ) {
1081                 require_once(ABSPATH . WPINC . '/rss.php');
1082                 if ( !$rss = fetch_rss($rss) )
1083                         return;
1084         } elseif ( is_array($rss) && isset($rss['url']) ) {
1085                 require_once(ABSPATH . WPINC . '/rss.php');
1086                 $args = $rss;
1087                 if ( !$rss = fetch_rss($rss['url']) )
1088                         return;
1089         } elseif ( !is_object($rss) ) {
1090                 return;
1091         }
1092
1093         extract( $args, EXTR_SKIP );
1094
1095         $items = (int) $items;
1096         if ( $items < 1 || 20 < $items )
1097                 $items = 10;
1098         $show_summary  = (int) $show_summary;
1099         $show_author   = (int) $show_author;
1100         $show_date     = (int) $show_date;
1101
1102         if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
1103                 $rss->items = array_slice($rss->items, 0, $items);
1104                 echo '<ul>';
1105                 foreach ($rss->items as $item ) {
1106                         while ( strstr($item['link'], 'http') != $item['link'] )
1107                                 $item['link'] = substr($item['link'], 1);
1108                         $link = clean_url(strip_tags($item['link']));
1109                         $title = attribute_escape(strip_tags($item['title']));
1110                         if ( empty($title) )
1111                                 $title = __('Untitled');
1112                         $desc = '';
1113                                 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1114                                         $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
1115                                 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1116                                         $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
1117
1118                         $summary = '';
1119                         if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1120                                 $summary = $item['description'];
1121                         elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1122                                 $summary = $item['summary'];
1123
1124                         $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($summary, ENT_QUOTES))));
1125
1126                         if ( $show_summary ) {
1127                                 $desc = '';
1128                                 $summary = wp_specialchars( $summary );
1129                                 $summary = "<div class='rssSummary'>$summary</div>";
1130                         } else {
1131                                 $summary = '';
1132                         }
1133
1134                         $date = '';
1135                         if ( $show_date ) {
1136                                 if ( isset($item['pubdate']) )
1137                                         $date = $item['pubdate'];
1138                                 elseif ( isset($item['published']) )
1139                                         $date = $item['published'];
1140
1141                                 if ( $date ) {
1142                                         if ( $date_stamp = strtotime( $date ) )
1143                                                 $date = '<span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
1144                                         else
1145                                                 $date = '';
1146                                 }
1147                         }
1148
1149                         $author = '';
1150                         if ( $show_author ) {
1151                                 if ( isset($item['dc']['creator']) )
1152                                         $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
1153                                 elseif ( isset($item['author_name']) )
1154                                         $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
1155                         }
1156
1157                         echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
1158                 }
1159                 echo '</ul>';
1160         } else {
1161                 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
1162         }
1163 }
1164
1165 function wp_widget_rss_control($widget_args) {
1166         global $wp_registered_widgets;
1167         static $updated = false;
1168
1169         if ( is_numeric($widget_args) )
1170                 $widget_args = array( 'number' => $widget_args );
1171         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1172         extract($widget_args, EXTR_SKIP);
1173
1174         $options = get_option('widget_rss');
1175         if ( !is_array($options) )
1176                 $options = array();
1177
1178         $urls = array();
1179         foreach ( $options as $option )
1180                 if ( isset($option['url']) )
1181                         $urls[$option['url']] = true;
1182
1183         if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
1184                 $sidebar = (string) $_POST['sidebar'];
1185
1186                 $sidebars_widgets = wp_get_sidebars_widgets();
1187                 if ( isset($sidebars_widgets[$sidebar]) )
1188                         $this_sidebar =& $sidebars_widgets[$sidebar];
1189                 else
1190                         $this_sidebar = array();
1191
1192                 foreach ( $this_sidebar as $_widget_id ) {
1193                         if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1194                                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1195                                 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
1196                                         unset($options[$widget_number]);
1197                         }
1198                 }
1199
1200                 foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
1201                         if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
1202                                 continue;
1203                         $widget_rss = stripslashes_deep( $widget_rss );
1204                         $url = sanitize_url(strip_tags($widget_rss['url']));
1205                         $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
1206                 }
1207
1208                 update_option('widget_rss', $options);
1209                 $updated = true;
1210         }
1211
1212         if ( -1 == $number ) {
1213                 $title = '';
1214                 $url = '';
1215                 $items = 10;
1216                 $error = false;
1217                 $number = '%i%';
1218                 $show_summary = 0;
1219                 $show_author = 0;
1220                 $show_date = 0;
1221         } else {
1222                 extract( (array) $options[$number] );
1223         }
1224
1225         wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
1226 }
1227
1228 function wp_widget_rss_form( $args, $inputs = null ) {
1229         $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
1230         $inputs = wp_parse_args( $inputs, $default_inputs );
1231         extract( $args );
1232         $number = attribute_escape( $number );
1233         $title  = attribute_escape( $title );
1234         $url    = attribute_escape( $url );
1235         $items  = (int) $items;
1236         if ( $items < 1 || 20 < $items )
1237                 $items  = 10;
1238         $show_summary   = (int) $show_summary;
1239         $show_author    = (int) $show_author;
1240         $show_date      = (int) $show_date;
1241
1242         if ( $inputs['url'] ) :
1243 ?>
1244         <p>
1245                 <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
1246                         <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
1247                 </label>
1248         </p>
1249 <?php endif; if ( $inputs['title'] ) : ?>
1250         <p>
1251                 <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
1252                         <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1253                 </label>
1254         </p>
1255 <?php endif; if ( $inputs['items'] ) : ?>
1256         <p>
1257                 <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
1258                         <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
1259                                 <?php
1260                                         for ( $i = 1; $i <= 20; ++$i )
1261                                                 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
1262                                 ?>
1263                         </select>
1264                 </label>
1265         </p>
1266 <?php endif; if ( $inputs['show_summary'] ) : ?>
1267         <p>
1268                 <label for="rss-show-summary-<?php echo $number; ?>">
1269                         <input id="rss-show-summary-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_summary]" type="checkbox" value="1" <?php if ( $show_summary ) echo 'checked="checked"'; ?>/>
1270                         <?php _e('Display item content?'); ?>
1271                 </label>
1272         </p>
1273 <?php endif; if ( $inputs['show_author'] ) : ?>
1274         <p>
1275                 <label for="rss-show-author-<?php echo $number; ?>">
1276                         <input id="rss-show-author-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_author]" type="checkbox" value="1" <?php if ( $show_author ) echo 'checked="checked"'; ?>/>
1277                         <?php _e('Display item author if available?'); ?>
1278                 </label>
1279         </p>
1280 <?php endif; if ( $inputs['show_date'] ) : ?>
1281         <p>
1282                 <label for="rss-show-date-<?php echo $number; ?>">
1283                         <input id="rss-show-date-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_date]" type="checkbox" value="1" <?php if ( $show_date ) echo 'checked="checked"'; ?>/>
1284                         <?php _e('Display item date?'); ?>
1285                 </label>
1286         </p>
1287         <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
1288 <?php
1289         endif;
1290         foreach ( array_keys($default_inputs) as $input ) :
1291                 if ( 'hidden' === $inputs[$input] ) :
1292                         $id = str_replace( '_', '-', $input );
1293 ?>
1294         <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
1295 <?php
1296                 endif;
1297         endforeach;
1298 }
1299
1300 // Expects unescaped data
1301 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1302         $items = (int) $widget_rss['items'];
1303         if ( $items < 1 || 20 < $items )
1304                 $items = 10;
1305         $url           = sanitize_url(strip_tags( $widget_rss['url'] ));
1306         $title         = trim(strip_tags( $widget_rss['title'] ));
1307         $show_summary  = (int) $widget_rss['show_summary'];
1308         $show_author   = (int) $widget_rss['show_author'];
1309         $show_date     = (int) $widget_rss['show_date'];
1310
1311         if ( $check_feed ) {
1312                 require_once(ABSPATH . WPINC . '/rss.php');
1313                 $rss = fetch_rss($url);
1314                 $error = false;
1315                 $link = '';
1316                 if ( !is_object($rss) ) {
1317                         $url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
1318                         $error = sprintf(__('Error in RSS %1$d'), $widget_number );
1319                 } else {
1320                         $link = clean_url(strip_tags($rss->channel['link']));
1321                         while ( strstr($link, 'http') != $link )
1322                                 $link = substr($link, 1);
1323                 }
1324         }
1325
1326         return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1327 }
1328
1329 function wp_widget_rss_register() {
1330         if ( !$options = get_option('widget_rss') )
1331                 $options = array();
1332         $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
1333         $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
1334         $name = __('RSS');
1335
1336         $id = false;
1337         foreach ( array_keys($options) as $o ) {
1338                 // Old widgets can have null values for some reason
1339                 if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
1340                         continue;
1341                 $id = "rss-$o"; // Never never never translate an id
1342                 wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
1343                 wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
1344         }
1345
1346         // If there are none, we register the widget's existance with a generic template
1347         if ( !$id ) {
1348                 wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
1349                 wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
1350         }
1351 }
1352
1353 function wp_widget_tag_cloud($args) {
1354         extract($args);
1355         $options = get_option('widget_tag_cloud');
1356         $title = empty($options['title']) ? __('Tags') : $options['title'];
1357
1358         echo $before_widget;
1359         echo $before_title . $title . $after_title;
1360         wp_tag_cloud();
1361         echo $after_widget;
1362 }
1363
1364 function wp_widget_tag_cloud_control() {
1365         $options = $newoptions = get_option('widget_tag_cloud');
1366
1367         if ( $_POST['tag-cloud-submit'] ) {
1368                 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
1369         }
1370
1371         if ( $options != $newoptions ) {
1372                 $options = $newoptions;
1373                 update_option('widget_tag_cloud', $options);
1374         }
1375
1376         $title = attribute_escape( $options['title'] );
1377 ?>
1378         <p><label for="tag-cloud-title">
1379         <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
1380         </p>
1381         <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
1382 <?php
1383 }
1384
1385 function wp_widgets_init() {
1386         if ( !is_blog_installed() )
1387                 return;
1388
1389         $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
1390         wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
1391         wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
1392
1393         $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
1394         wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
1395         wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
1396
1397         $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
1398         wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
1399         wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
1400
1401         $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
1402         wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
1403
1404         $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
1405         wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
1406         wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
1407
1408         $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
1409         wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
1410
1411         $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
1412         wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
1413         wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
1414
1415         $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
1416         wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
1417         wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
1418
1419         wp_widget_categories_register();
1420         wp_widget_text_register();
1421         wp_widget_rss_register();
1422         wp_widget_recent_comments_register();
1423
1424         do_action('widgets_init');
1425 }
1426
1427 add_action('init', 'wp_widgets_init', 1);
1428
1429 /* Pattern for multi-widget (allows multiple instances such as the text widget).
1430
1431 // Displays widget on blag
1432 // $widget_args: number
1433 //    number: which of the several widgets of this type do we mean
1434 function widget_many( $args, $widget_args = 1 ) {
1435         extract( $args, EXTR_SKIP );
1436         if ( is_numeric($widget_args) )
1437                 $widget_args = array( 'number' => $widget_args );
1438         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1439         extract( $widget_args, EXTR_SKIP );
1440
1441         // Data should be stored as array:  array( number => data for that instance of the widget, ... )
1442         $options = get_option('widget_many');
1443         if ( !isset($options[$number]) )
1444                 return;
1445
1446         echo $before_widget;
1447
1448         // Do stuff for this widget, drawing data from $options[$number]
1449
1450         echo $after_widget;
1451 }
1452
1453 // Displays form for a particular instance of the widget.  Also updates the data after a POST submit
1454 // $widget_args: number
1455 //    number: which of the several widgets of this type do we mean
1456 function widget_many_control( $widget_args = 1 ) {
1457         global $wp_registered_widgets;
1458         static $updated = false; // Whether or not we have already updated the data after a POST submit
1459
1460         if ( is_numeric($widget_args) )
1461                 $widget_args = array( 'number' => $widget_args );
1462         $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1463         extract( $widget_args, EXTR_SKIP );
1464
1465         // Data should be stored as array:  array( number => data for that instance of the widget, ... )
1466         $options = get_option('widget_many');
1467         if ( !is_array($options) )
1468                 $options = array();
1469
1470         // We need to update the data
1471         if ( !$updated && !empty($_POST['sidebar']) ) {
1472                 // Tells us what sidebar to put the data in
1473                 $sidebar = (string) $_POST['sidebar'];
1474
1475                 $sidebars_widgets = wp_get_sidebars_widgets();
1476                 if ( isset($sidebars_widgets[$sidebar]) )
1477                         $this_sidebar =& $sidebars_widgets[$sidebar];
1478                 else
1479                         $this_sidebar = array();
1480
1481                 foreach ( $this_sidebar as $_widget_id ) {
1482                         // Remove all widgets of this type from the sidebar.  We'll add the new data in a second.  This makes sure we don't get any duplicate data
1483                         // since widget ids aren't necessarily persistent across multiple updates
1484                         if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1485                                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1486                                 if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
1487                                         unset($options[$widget_number]);
1488                         }
1489                 }
1490
1491                 foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
1492                         // compile data from $widget_many_instance
1493                         if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
1494                                 continue;
1495                         $something = wp_specialchars( $widget_many_instance['something'] );
1496                         $options[$widget_number] = array( 'something' => $something );  // Even simple widgets should store stuff in array, rather than in scalar
1497                 }
1498
1499                 update_option('widget_text', $options);
1500
1501                 $updated = true; // So that we don't go through this more than once
1502         }
1503
1504
1505         // Here we echo out the form
1506         if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
1507                 $something = '';
1508                 $number = '%i%';
1509         } else {
1510                 $something = attribute_escape($options[$number]['something']);
1511         }
1512
1513         // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
1514         // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
1515 ?>
1516                 <p>
1517                         <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
1518                         <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
1519                 </p>
1520 <?php
1521 }
1522
1523 // Registers each instance of our widget on startup
1524 function widget_many_register() {
1525         if ( !$options = get_option('widget_many') )
1526                 $options = array();
1527
1528         $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
1529         $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
1530         $name = __('Many');
1531
1532         $registered = false;
1533         foreach ( array_keys($options) as $o ) {
1534                 // Old widgets can have null values for some reason
1535                 if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple.  Replace with with whatever your real data are.
1536                         continue;
1537
1538                 // $id should look like {$id_base}-{$o}
1539                 $id = "many-$o"; // Never never never translate an id
1540                 $registered = true;
1541                 wp_register_sidebar_widget( $id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ) );
1542                 wp_register_widget_control( $id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ) );
1543         }
1544
1545         // If there are none, we register the widget's existance with a generic template
1546         if ( !$registered ) {
1547                 wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
1548                 wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
1549         }
1550 }
1551
1552 // This is important
1553 add_action( 'widgets_init', 'widget_many_register' )
1554
1555 */
1556
1557 ?>