]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/default-widgets.php
Wordpress 3.3.1
[autoinstalls/wordpress.git] / wp-includes / default-widgets.php
1 <?php
2 /**
3  * Default Widgets
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  */
8
9 /**
10  * Pages widget class
11  *
12  * @since 2.8.0
13  */
14 class WP_Widget_Pages extends WP_Widget {
15
16         function __construct() {
17                 $widget_ops = array('classname' => 'widget_pages', 'description' => __( 'Your site&#8217;s WordPress Pages') );
18                 parent::__construct('pages', __('Pages'), $widget_ops);
19         }
20
21         function widget( $args, $instance ) {
22                 extract( $args );
23
24                 $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base);
25                 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
26                 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
27
28                 if ( $sortby == 'menu_order' )
29                         $sortby = 'menu_order, post_title';
30
31                 $out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) );
32
33                 if ( !empty( $out ) ) {
34                         echo $before_widget;
35                         if ( $title)
36                                 echo $before_title . $title . $after_title;
37                 ?>
38                 <ul>
39                         <?php echo $out; ?>
40                 </ul>
41                 <?php
42                         echo $after_widget;
43                 }
44         }
45
46         function update( $new_instance, $old_instance ) {
47                 $instance = $old_instance;
48                 $instance['title'] = strip_tags($new_instance['title']);
49                 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
50                         $instance['sortby'] = $new_instance['sortby'];
51                 } else {
52                         $instance['sortby'] = 'menu_order';
53                 }
54
55                 $instance['exclude'] = strip_tags( $new_instance['exclude'] );
56
57                 return $instance;
58         }
59
60         function form( $instance ) {
61                 //Defaults
62                 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
63                 $title = esc_attr( $instance['title'] );
64                 $exclude = esc_attr( $instance['exclude'] );
65         ?>
66                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
67                 <p>
68                         <label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e( 'Sort by:' ); ?></label>
69                         <select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
70                                 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
71                                 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
72                                 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
73                         </select>
74                 </p>
75                 <p>
76                         <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
77                         <br />
78                         <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
79                 </p>
80 <?php
81         }
82
83 }
84
85 /**
86  * Links widget class
87  *
88  * @since 2.8.0
89  */
90 class WP_Widget_Links extends WP_Widget {
91
92         function __construct() {
93                 $widget_ops = array('description' => __( "Your blogroll" ) );
94                 parent::__construct('links', __('Links'), $widget_ops);
95         }
96
97         function widget( $args, $instance ) {
98                 extract($args, EXTR_SKIP);
99
100                 $show_description = isset($instance['description']) ? $instance['description'] : false;
101                 $show_name = isset($instance['name']) ? $instance['name'] : false;
102                 $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
103                 $show_images = isset($instance['images']) ? $instance['images'] : true;
104                 $category = isset($instance['category']) ? $instance['category'] : false;
105
106                 if ( is_admin() && !$category ) {
107                         // Display All Links widget as such in the widgets screen
108                         echo $before_widget . $before_title . _x('All Links', 'links widget') . $after_title . $after_widget;
109                         return;
110                 }
111
112                 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
113                 wp_list_bookmarks(apply_filters('widget_links_args', array(
114                         'title_before' => $before_title, 'title_after' => $after_title,
115                         'category_before' => $before_widget, 'category_after' => $after_widget,
116                         'show_images' => $show_images, 'show_description' => $show_description,
117                         'show_name' => $show_name, 'show_rating' => $show_rating,
118                         'category' => $category, 'class' => 'linkcat widget'
119                 )));
120         }
121
122         function update( $new_instance, $old_instance ) {
123                 $new_instance = (array) $new_instance;
124                 $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0);
125                 foreach ( $instance as $field => $val ) {
126                         if ( isset($new_instance[$field]) )
127                                 $instance[$field] = 1;
128                 }
129                 $instance['category'] = intval($new_instance['category']);
130
131                 return $instance;
132         }
133
134         function form( $instance ) {
135
136                 //Defaults
137                 $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false ) );
138                 $link_cats = get_terms( 'link_category');
139 ?>
140                 <p>
141                 <label for="<?php echo $this->get_field_id('category'); ?>" class="screen-reader-text"><?php _e('Select Link Category'); ?></label>
142                 <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
143                 <option value=""><?php _ex('All Links', 'links widget'); ?></option>
144                 <?php
145                 foreach ( $link_cats as $link_cat ) {
146                         echo '<option value="' . intval($link_cat->term_id) . '"'
147                                 . ( $link_cat->term_id == $instance['category'] ? ' selected="selected"' : '' )
148                                 . '>' . $link_cat->name . "</option>\n";
149                 }
150                 ?>
151                 </select></p>
152                 <p>
153                 <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
154                 <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
155                 <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
156                 <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
157                 <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
158                 <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
159                 <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
160                 <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
161                 </p>
162 <?php
163         }
164 }
165
166 /**
167  * Search widget class
168  *
169  * @since 2.8.0
170  */
171 class WP_Widget_Search extends WP_Widget {
172
173         function __construct() {
174                 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site") );
175                 parent::__construct('search', __('Search'), $widget_ops);
176         }
177
178         function widget( $args, $instance ) {
179                 extract($args);
180                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
181
182                 echo $before_widget;
183                 if ( $title )
184                         echo $before_title . $title . $after_title;
185
186                 // Use current theme search form if it exists
187                 get_search_form();
188
189                 echo $after_widget;
190         }
191
192         function form( $instance ) {
193                 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
194                 $title = $instance['title'];
195 ?>
196                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
197 <?php
198         }
199
200         function update( $new_instance, $old_instance ) {
201                 $instance = $old_instance;
202                 $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
203                 $instance['title'] = strip_tags($new_instance['title']);
204                 return $instance;
205         }
206
207 }
208
209 /**
210  * Archives widget class
211  *
212  * @since 2.8.0
213  */
214 class WP_Widget_Archives extends WP_Widget {
215
216         function __construct() {
217                 $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site&#8217;s posts') );
218                 parent::__construct('archives', __('Archives'), $widget_ops);
219         }
220
221         function widget( $args, $instance ) {
222                 extract($args);
223                 $c = ! empty( $instance['count'] ) ? '1' : '0';
224                 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
225                 $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base);
226
227                 echo $before_widget;
228                 if ( $title )
229                         echo $before_title . $title . $after_title;
230
231                 if ( $d ) {
232 ?>
233                 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select>
234 <?php
235                 } else {
236 ?>
237                 <ul>
238                 <?php wp_get_archives(apply_filters('widget_archives_args', array('type' => 'monthly', 'show_post_count' => $c))); ?>
239                 </ul>
240 <?php
241                 }
242
243                 echo $after_widget;
244         }
245
246         function update( $new_instance, $old_instance ) {
247                 $instance = $old_instance;
248                 $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
249                 $instance['title'] = strip_tags($new_instance['title']);
250                 $instance['count'] = $new_instance['count'] ? 1 : 0;
251                 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
252
253                 return $instance;
254         }
255
256         function form( $instance ) {
257                 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
258                 $title = strip_tags($instance['title']);
259                 $count = $instance['count'] ? 'checked="checked"' : '';
260                 $dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
261 ?>
262                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
263                 <p>
264                         <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
265                         <br/>
266                         <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
267                 </p>
268 <?php
269         }
270 }
271
272 /**
273  * Meta widget class
274  *
275  * Displays log in/out, RSS feed links, etc.
276  *
277  * @since 2.8.0
278  */
279 class WP_Widget_Meta extends WP_Widget {
280
281         function __construct() {
282                 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
283                 parent::__construct('meta', __('Meta'), $widget_ops);
284         }
285
286         function widget( $args, $instance ) {
287                 extract($args);
288                 $title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);
289
290                 echo $before_widget;
291                 if ( $title )
292                         echo $before_title . $title . $after_title;
293 ?>
294                         <ul>
295                         <?php wp_register(); ?>
296                         <li><?php wp_loginout(); ?></li>
297                         <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
298                         <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
299                         <li><a href="http://wordpress.org/" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
300                         <?php wp_meta(); ?>
301                         </ul>
302 <?php
303                 echo $after_widget;
304         }
305
306         function update( $new_instance, $old_instance ) {
307                 $instance = $old_instance;
308                 $instance['title'] = strip_tags($new_instance['title']);
309
310                 return $instance;
311         }
312
313         function form( $instance ) {
314                 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
315                 $title = strip_tags($instance['title']);
316 ?>
317                         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
318 <?php
319         }
320 }
321
322 /**
323  * Calendar widget class
324  *
325  * @since 2.8.0
326  */
327 class WP_Widget_Calendar extends WP_Widget {
328
329         function __construct() {
330                 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( 'A calendar of your site&#8217;s posts') );
331                 parent::__construct('calendar', __('Calendar'), $widget_ops);
332         }
333
334         function widget( $args, $instance ) {
335                 extract($args);
336                 $title = apply_filters('widget_title', empty($instance['title']) ? '&nbsp;' : $instance['title'], $instance, $this->id_base);
337                 echo $before_widget;
338                 if ( $title )
339                         echo $before_title . $title . $after_title;
340                 echo '<div id="calendar_wrap">';
341                 get_calendar();
342                 echo '</div>';
343                 echo $after_widget;
344         }
345
346         function update( $new_instance, $old_instance ) {
347                 $instance = $old_instance;
348                 $instance['title'] = strip_tags($new_instance['title']);
349
350                 return $instance;
351         }
352
353         function form( $instance ) {
354                 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
355                 $title = strip_tags($instance['title']);
356 ?>
357                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
358                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
359 <?php
360         }
361 }
362
363 /**
364  * Text widget class
365  *
366  * @since 2.8.0
367  */
368 class WP_Widget_Text extends WP_Widget {
369
370         function __construct() {
371                 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
372                 $control_ops = array('width' => 400, 'height' => 350);
373                 parent::__construct('text', __('Text'), $widget_ops, $control_ops);
374         }
375
376         function widget( $args, $instance ) {
377                 extract($args);
378                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
379                 $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
380                 echo $before_widget;
381                 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
382                         <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
383                 <?php
384                 echo $after_widget;
385         }
386
387         function update( $new_instance, $old_instance ) {
388                 $instance = $old_instance;
389                 $instance['title'] = strip_tags($new_instance['title']);
390                 if ( current_user_can('unfiltered_html') )
391                         $instance['text'] =  $new_instance['text'];
392                 else
393                         $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
394                 $instance['filter'] = isset($new_instance['filter']);
395                 return $instance;
396         }
397
398         function form( $instance ) {
399                 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
400                 $title = strip_tags($instance['title']);
401                 $text = esc_textarea($instance['text']);
402 ?>
403                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
404                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
405
406                 <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
407
408                 <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
409 <?php
410         }
411 }
412
413 /**
414  * Categories widget class
415  *
416  * @since 2.8.0
417  */
418 class WP_Widget_Categories extends WP_Widget {
419
420         function __construct() {
421                 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
422                 parent::__construct('categories', __('Categories'), $widget_ops);
423         }
424
425         function widget( $args, $instance ) {
426                 extract( $args );
427
428                 $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
429                 $c = ! empty( $instance['count'] ) ? '1' : '0';
430                 $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
431                 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
432
433                 echo $before_widget;
434                 if ( $title )
435                         echo $before_title . $title . $after_title;
436
437                 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
438
439                 if ( $d ) {
440                         $cat_args['show_option_none'] = __('Select Category');
441                         wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
442 ?>
443
444 <script type='text/javascript'>
445 /* <![CDATA[ */
446         var dropdown = document.getElementById("cat");
447         function onCatChange() {
448                 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
449                         location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
450                 }
451         }
452         dropdown.onchange = onCatChange;
453 /* ]]> */
454 </script>
455
456 <?php
457                 } else {
458 ?>
459                 <ul>
460 <?php
461                 $cat_args['title_li'] = '';
462                 wp_list_categories(apply_filters('widget_categories_args', $cat_args));
463 ?>
464                 </ul>
465 <?php
466                 }
467
468                 echo $after_widget;
469         }
470
471         function update( $new_instance, $old_instance ) {
472                 $instance = $old_instance;
473                 $instance['title'] = strip_tags($new_instance['title']);
474                 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
475                 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
476                 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
477
478                 return $instance;
479         }
480
481         function form( $instance ) {
482                 //Defaults
483                 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
484                 $title = esc_attr( $instance['title'] );
485                 $count = isset($instance['count']) ? (bool) $instance['count'] :false;
486                 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
487                 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
488 ?>
489                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
490                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
491
492                 <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
493                 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
494
495                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
496                 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
497
498                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
499                 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
500 <?php
501         }
502
503 }
504
505 /**
506  * Recent_Posts widget class
507  *
508  * @since 2.8.0
509  */
510 class WP_Widget_Recent_Posts extends WP_Widget {
511
512         function __construct() {
513                 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
514                 parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
515                 $this->alt_option_name = 'widget_recent_entries';
516
517                 add_action( 'save_post', array(&$this, 'flush_widget_cache') );
518                 add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
519                 add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
520         }
521
522         function widget($args, $instance) {
523                 $cache = wp_cache_get('widget_recent_posts', 'widget');
524
525                 if ( !is_array($cache) )
526                         $cache = array();
527
528                 if ( ! isset( $args['widget_id'] ) )
529                         $args['widget_id'] = $this->id;
530
531                 if ( isset( $cache[ $args['widget_id'] ] ) ) {
532                         echo $cache[ $args['widget_id'] ];
533                         return;
534                 }
535
536                 ob_start();
537                 extract($args);
538
539                 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
540                 if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
541                         $number = 10;
542
543                 $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true));
544                 if ($r->have_posts()) :
545 ?>
546                 <?php echo $before_widget; ?>
547                 <?php if ( $title ) echo $before_title . $title . $after_title; ?>
548                 <ul>
549                 <?php  while ($r->have_posts()) : $r->the_post(); ?>
550                 <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
551                 <?php endwhile; ?>
552                 </ul>
553                 <?php echo $after_widget; ?>
554 <?php
555                 // Reset the global $the_post as this query will have stomped on it
556                 wp_reset_postdata();
557
558                 endif;
559
560                 $cache[$args['widget_id']] = ob_get_flush();
561                 wp_cache_set('widget_recent_posts', $cache, 'widget');
562         }
563
564         function update( $new_instance, $old_instance ) {
565                 $instance = $old_instance;
566                 $instance['title'] = strip_tags($new_instance['title']);
567                 $instance['number'] = (int) $new_instance['number'];
568                 $this->flush_widget_cache();
569
570                 $alloptions = wp_cache_get( 'alloptions', 'options' );
571                 if ( isset($alloptions['widget_recent_entries']) )
572                         delete_option('widget_recent_entries');
573
574                 return $instance;
575         }
576
577         function flush_widget_cache() {
578                 wp_cache_delete('widget_recent_posts', 'widget');
579         }
580
581         function form( $instance ) {
582                 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
583                 $number = isset($instance['number']) ? absint($instance['number']) : 5;
584 ?>
585                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
586                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
587
588                 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
589                 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
590 <?php
591         }
592 }
593
594 /**
595  * Recent_Comments widget class
596  *
597  * @since 2.8.0
598  */
599 class WP_Widget_Recent_Comments extends WP_Widget {
600
601         function __construct() {
602                 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
603                 parent::__construct('recent-comments', __('Recent Comments'), $widget_ops);
604                 $this->alt_option_name = 'widget_recent_comments';
605
606                 if ( is_active_widget(false, false, $this->id_base) )
607                         add_action( 'wp_head', array(&$this, 'recent_comments_style') );
608
609                 add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
610                 add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
611         }
612
613         function recent_comments_style() {
614                 if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
615                         || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
616                         return;
617                 ?>
618         <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
619 <?php
620         }
621
622         function flush_widget_cache() {
623                 wp_cache_delete('widget_recent_comments', 'widget');
624         }
625
626         function widget( $args, $instance ) {
627                 global $comments, $comment;
628
629                 $cache = wp_cache_get('widget_recent_comments', 'widget');
630
631                 if ( ! is_array( $cache ) )
632                         $cache = array();
633
634                 if ( ! isset( $args['widget_id'] ) )
635                         $args['widget_id'] = $this->id;
636
637                 if ( isset( $cache[ $args['widget_id'] ] ) ) {
638                         echo $cache[ $args['widget_id'] ];
639                         return;
640                 }
641
642                 extract($args, EXTR_SKIP);
643                 $output = '';
644                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );
645
646                 if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
647                         $number = 5;
648
649                 $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) );
650                 $output .= $before_widget;
651                 if ( $title )
652                         $output .= $before_title . $title . $after_title;
653
654                 $output .= '<ul id="recentcomments">';
655                 if ( $comments ) {
656                         foreach ( (array) $comments as $comment) {
657                                 $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
658                         }
659                 }
660                 $output .= '</ul>';
661                 $output .= $after_widget;
662
663                 echo $output;
664                 $cache[$args['widget_id']] = $output;
665                 wp_cache_set('widget_recent_comments', $cache, 'widget');
666         }
667
668         function update( $new_instance, $old_instance ) {
669                 $instance = $old_instance;
670                 $instance['title'] = strip_tags($new_instance['title']);
671                 $instance['number'] = absint( $new_instance['number'] );
672                 $this->flush_widget_cache();
673
674                 $alloptions = wp_cache_get( 'alloptions', 'options' );
675                 if ( isset($alloptions['widget_recent_comments']) )
676                         delete_option('widget_recent_comments');
677
678                 return $instance;
679         }
680
681         function form( $instance ) {
682                 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
683                 $number = isset($instance['number']) ? absint($instance['number']) : 5;
684 ?>
685                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
686                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
687
688                 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
689                 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
690 <?php
691         }
692 }
693
694 /**
695  * RSS widget class
696  *
697  * @since 2.8.0
698  */
699 class WP_Widget_RSS extends WP_Widget {
700
701         function __construct() {
702                 $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed') );
703                 $control_ops = array( 'width' => 400, 'height' => 200 );
704                 parent::__construct( 'rss', __('RSS'), $widget_ops, $control_ops );
705         }
706
707         function widget($args, $instance) {
708
709                 if ( isset($instance['error']) && $instance['error'] )
710                         return;
711
712                 extract($args, EXTR_SKIP);
713
714                 $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
715                 while ( stristr($url, 'http') != $url )
716                         $url = substr($url, 1);
717
718                 if ( empty($url) )
719                         return;
720
721                 // self-url destruction sequence
722                 if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) )
723                         return;
724
725                 $rss = fetch_feed($url);
726                 $title = $instance['title'];
727                 $desc = '';
728                 $link = '';
729
730                 if ( ! is_wp_error($rss) ) {
731                         $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
732                         if ( empty($title) )
733                                 $title = esc_html(strip_tags($rss->get_title()));
734                         $link = esc_url(strip_tags($rss->get_permalink()));
735                         while ( stristr($link, 'http') != $link )
736                                 $link = substr($link, 1);
737                 }
738
739                 if ( empty($title) )
740                         $title = empty($desc) ? __('Unknown Feed') : $desc;
741
742                 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
743                 $url = esc_url(strip_tags($url));
744                 $icon = includes_url('images/rss.png');
745                 if ( $title )
746                         $title = "<a class='rsswidget' href='$url' title='" . esc_attr__( 'Syndicate this content' ) ."'><img style='border:0' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
747
748                 echo $before_widget;
749                 if ( $title )
750                         echo $before_title . $title . $after_title;
751                 wp_widget_rss_output( $rss, $instance );
752                 echo $after_widget;
753
754                 if ( ! is_wp_error($rss) )
755                         $rss->__destruct();
756                 unset($rss);
757         }
758
759         function update($new_instance, $old_instance) {
760                 $testurl = ( isset( $new_instance['url'] ) && ( !isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) );
761                 return wp_widget_rss_process( $new_instance, $testurl );
762         }
763
764         function form($instance) {
765
766                 if ( empty($instance) )
767                         $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
768                 $instance['number'] = $this->number;
769
770                 wp_widget_rss_form( $instance );
771         }
772 }
773
774 /**
775  * Display the RSS entries in a list.
776  *
777  * @since 2.5.0
778  *
779  * @param string|array|object $rss RSS url.
780  * @param array $args Widget arguments.
781  */
782 function wp_widget_rss_output( $rss, $args = array() ) {
783         if ( is_string( $rss ) ) {
784                 $rss = fetch_feed($rss);
785         } elseif ( is_array($rss) && isset($rss['url']) ) {
786                 $args = $rss;
787                 $rss = fetch_feed($rss['url']);
788         } elseif ( !is_object($rss) ) {
789                 return;
790         }
791
792         if ( is_wp_error($rss) ) {
793                 if ( is_admin() || current_user_can('manage_options') )
794                         echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
795                 return;
796         }
797
798         $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
799         $args = wp_parse_args( $args, $default_args );
800         extract( $args, EXTR_SKIP );
801
802         $items = (int) $items;
803         if ( $items < 1 || 20 < $items )
804                 $items = 10;
805         $show_summary  = (int) $show_summary;
806         $show_author   = (int) $show_author;
807         $show_date     = (int) $show_date;
808
809         if ( !$rss->get_item_quantity() ) {
810                 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
811                 $rss->__destruct();
812                 unset($rss);
813                 return;
814         }
815
816         echo '<ul>';
817         foreach ( $rss->get_items(0, $items) as $item ) {
818                 $link = $item->get_link();
819                 while ( stristr($link, 'http') != $link )
820                         $link = substr($link, 1);
821                 $link = esc_url(strip_tags($link));
822                 $title = esc_attr(strip_tags($item->get_title()));
823                 if ( empty($title) )
824                         $title = __('Untitled');
825
826                 $desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
827                 $desc = wp_html_excerpt( $desc, 360 );
828
829                 // Append ellipsis. Change existing [...] to [&hellip;].
830                 if ( '[...]' == substr( $desc, -5 ) )
831                         $desc = substr( $desc, 0, -5 ) . '[&hellip;]';
832                 elseif ( '[&hellip;]' != substr( $desc, -10 ) )
833                         $desc .= ' [&hellip;]';
834
835                 $desc = esc_html( $desc );
836
837                 if ( $show_summary ) {
838                         $summary = "<div class='rssSummary'>$desc</div>";
839                 } else {
840                         $summary = '';
841                 }
842
843                 $date = '';
844                 if ( $show_date ) {
845                         $date = $item->get_date( 'U' );
846
847                         if ( $date ) {
848                                 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
849                         }
850                 }
851
852                 $author = '';
853                 if ( $show_author ) {
854                         $author = $item->get_author();
855                         if ( is_object($author) ) {
856                                 $author = $author->get_name();
857                                 $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
858                         }
859                 }
860
861                 if ( $link == '' ) {
862                         echo "<li>$title{$date}{$summary}{$author}</li>";
863                 } else {
864                         echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
865                 }
866         }
867         echo '</ul>';
868         $rss->__destruct();
869         unset($rss);
870 }
871
872
873
874 /**
875  * Display RSS widget options form.
876  *
877  * The options for what fields are displayed for the RSS form are all booleans
878  * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
879  * 'show_date'.
880  *
881  * @since 2.5.0
882  *
883  * @param array|string $args Values for input fields.
884  * @param array $inputs Override default display options.
885  */
886 function wp_widget_rss_form( $args, $inputs = null ) {
887
888         $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
889         $inputs = wp_parse_args( $inputs, $default_inputs );
890         extract( $args );
891         extract( $inputs, EXTR_SKIP);
892
893         $number = esc_attr( $number );
894         $title  = esc_attr( $title );
895         $url    = esc_url( $url );
896         $items  = (int) $items;
897         if ( $items < 1 || 20 < $items )
898                 $items  = 10;
899         $show_summary   = (int) $show_summary;
900         $show_author    = (int) $show_author;
901         $show_date      = (int) $show_date;
902
903         if ( !empty($error) )
904                 echo '<p class="widget-error"><strong>' . sprintf( __('RSS Error: %s'), $error) . '</strong></p>';
905
906         if ( $inputs['url'] ) :
907 ?>
908         <p><label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?></label>
909         <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" /></p>
910 <?php endif; if ( $inputs['title'] ) : ?>
911         <p><label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?></label>
912         <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" /></p>
913 <?php endif; if ( $inputs['items'] ) : ?>
914         <p><label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?></label>
915         <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
916 <?php
917                 for ( $i = 1; $i <= 20; ++$i )
918                         echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
919 ?>
920         </select></p>
921 <?php endif; if ( $inputs['show_summary'] ) : ?>
922         <p><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"'; ?>/>
923         <label for="rss-show-summary-<?php echo $number; ?>"><?php _e('Display item content?'); ?></label></p>
924 <?php endif; if ( $inputs['show_author'] ) : ?>
925         <p><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"'; ?>/>
926         <label for="rss-show-author-<?php echo $number; ?>"><?php _e('Display item author if available?'); ?></label></p>
927 <?php endif; if ( $inputs['show_date'] ) : ?>
928         <p><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"'; ?>/>
929         <label for="rss-show-date-<?php echo $number; ?>"><?php _e('Display item date?'); ?></label></p>
930 <?php
931         endif;
932         foreach ( array_keys($default_inputs) as $input ) :
933                 if ( 'hidden' === $inputs[$input] ) :
934                         $id = str_replace( '_', '-', $input );
935 ?>
936         <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
937 <?php
938                 endif;
939         endforeach;
940 }
941
942 /**
943  * Process RSS feed widget data and optionally retrieve feed items.
944  *
945  * The feed widget can not have more than 20 items or it will reset back to the
946  * default, which is 10.
947  *
948  * The resulting array has the feed title, feed url, feed link (from channel),
949  * feed items, error (if any), and whether to show summary, author, and date.
950  * All respectively in the order of the array elements.
951  *
952  * @since 2.5.0
953  *
954  * @param array $widget_rss RSS widget feed data. Expects unescaped data.
955  * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
956  * @return array
957  */
958 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
959         $items = (int) $widget_rss['items'];
960         if ( $items < 1 || 20 < $items )
961                 $items = 10;
962         $url           = esc_url_raw(strip_tags( $widget_rss['url'] ));
963         $title         = trim(strip_tags( $widget_rss['title'] ));
964         $show_summary  = isset($widget_rss['show_summary']) ? (int) $widget_rss['show_summary'] : 0;
965         $show_author   = isset($widget_rss['show_author']) ? (int) $widget_rss['show_author'] :0;
966         $show_date     = isset($widget_rss['show_date']) ? (int) $widget_rss['show_date'] : 0;
967
968         if ( $check_feed ) {
969                 $rss = fetch_feed($url);
970                 $error = false;
971                 $link = '';
972                 if ( is_wp_error($rss) ) {
973                         $error = $rss->get_error_message();
974                 } else {
975                         $link = esc_url(strip_tags($rss->get_permalink()));
976                         while ( stristr($link, 'http') != $link )
977                                 $link = substr($link, 1);
978
979                         $rss->__destruct();
980                         unset($rss);
981                 }
982         }
983
984         return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
985 }
986
987 /**
988  * Tag cloud widget class
989  *
990  * @since 2.8.0
991  */
992 class WP_Widget_Tag_Cloud extends WP_Widget {
993
994         function __construct() {
995                 $widget_ops = array( 'description' => __( "Your most used tags in cloud format") );
996                 parent::__construct('tag_cloud', __('Tag Cloud'), $widget_ops);
997         }
998
999         function widget( $args, $instance ) {
1000                 extract($args);
1001                 $current_taxonomy = $this->_get_current_taxonomy($instance);
1002                 if ( !empty($instance['title']) ) {
1003                         $title = $instance['title'];
1004                 } else {
1005                         if ( 'post_tag' == $current_taxonomy ) {
1006                                 $title = __('Tags');
1007                         } else {
1008                                 $tax = get_taxonomy($current_taxonomy);
1009                                 $title = $tax->labels->name;
1010                         }
1011                 }
1012                 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
1013
1014                 echo $before_widget;
1015                 if ( $title )
1016                         echo $before_title . $title . $after_title;
1017                 echo '<div class="tagcloud">';
1018                 wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
1019                 echo "</div>\n";
1020                 echo $after_widget;
1021         }
1022
1023         function update( $new_instance, $old_instance ) {
1024                 $instance['title'] = strip_tags(stripslashes($new_instance['title']));
1025                 $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
1026                 return $instance;
1027         }
1028
1029         function form( $instance ) {
1030                 $current_taxonomy = $this->_get_current_taxonomy($instance);
1031 ?>
1032         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
1033         <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
1034         <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
1035         <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
1036         <?php foreach ( get_object_taxonomies('post') as $taxonomy ) :
1037                                 $tax = get_taxonomy($taxonomy);
1038                                 if ( !$tax->show_tagcloud || empty($tax->labels->name) )
1039                                         continue;
1040         ?>
1041                 <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
1042         <?php endforeach; ?>
1043         </select></p><?php
1044         }
1045
1046         function _get_current_taxonomy($instance) {
1047                 if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
1048                         return $instance['taxonomy'];
1049
1050                 return 'post_tag';
1051         }
1052 }
1053
1054 /**
1055  * Navigation Menu widget class
1056  *
1057  * @since 3.0.0
1058  */
1059  class WP_Nav_Menu_Widget extends WP_Widget {
1060
1061         function __construct() {
1062                 $widget_ops = array( 'description' => __('Use this widget to add one of your custom menus as a widget.') );
1063                 parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
1064         }
1065
1066         function widget($args, $instance) {
1067                 // Get menu
1068                 $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
1069
1070                 if ( !$nav_menu )
1071                         return;
1072
1073                 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
1074
1075                 echo $args['before_widget'];
1076
1077                 if ( !empty($instance['title']) )
1078                         echo $args['before_title'] . $instance['title'] . $args['after_title'];
1079
1080                 wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
1081
1082                 echo $args['after_widget'];
1083         }
1084
1085         function update( $new_instance, $old_instance ) {
1086                 $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
1087                 $instance['nav_menu'] = (int) $new_instance['nav_menu'];
1088                 return $instance;
1089         }
1090
1091         function form( $instance ) {
1092                 $title = isset( $instance['title'] ) ? $instance['title'] : '';
1093                 $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
1094
1095                 // Get menus
1096                 $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
1097
1098                 // If no menus exists, direct the user to go and create some.
1099                 if ( !$menus ) {
1100                         echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
1101                         return;
1102                 }
1103                 ?>
1104                 <p>
1105                         <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
1106                         <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
1107                 </p>
1108                 <p>
1109                         <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
1110                         <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
1111                 <?php
1112                         foreach ( $menus as $menu ) {
1113                                 $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
1114                                 echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
1115                         }
1116                 ?>
1117                         </select>
1118                 </p>
1119                 <?php
1120         }
1121 }
1122
1123 /**
1124  * Register all of the default WordPress widgets on startup.
1125  *
1126  * Calls 'widgets_init' action after all of the WordPress widgets have been
1127  * registered.
1128  *
1129  * @since 2.2.0
1130  */
1131 function wp_widgets_init() {
1132         if ( !is_blog_installed() )
1133                 return;
1134
1135         register_widget('WP_Widget_Pages');
1136
1137         register_widget('WP_Widget_Calendar');
1138
1139         register_widget('WP_Widget_Archives');
1140
1141         register_widget('WP_Widget_Links');
1142
1143         register_widget('WP_Widget_Meta');
1144
1145         register_widget('WP_Widget_Search');
1146
1147         register_widget('WP_Widget_Text');
1148
1149         register_widget('WP_Widget_Categories');
1150
1151         register_widget('WP_Widget_Recent_Posts');
1152
1153         register_widget('WP_Widget_Recent_Comments');
1154
1155         register_widget('WP_Widget_RSS');
1156
1157         register_widget('WP_Widget_Tag_Cloud');
1158
1159         register_widget('WP_Nav_Menu_Widget');
1160
1161         do_action('widgets_init');
1162 }
1163
1164 add_action('init', 'wp_widgets_init', 1);