Wordpress 2.8
[autoinstalls/wordpress.git] / wp-admin / includes / widgets.php
1 <?php
2 /**
3  * WordPress Widgets Administration API
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Display list of the available widgets, either all or matching search.
11  *
12  * The search parameter are search terms separated by spaces.
13  *
14  * @since unknown
15  *
16  * @param string $show Optional, default is all. What to display, can be 'all', 'unused', or 'used'.
17  * @param string $_search Optional. Search for widgets. Should be unsanitized.
18  */
19 function wp_list_widgets() {
20         global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
21
22         $sort = $wp_registered_widgets;
23         usort( $sort, create_function( '$a, $b', 'return strnatcasecmp( $a["name"], $b["name"] );' ) );
24         $done = array();
25
26         foreach ( $sort as $widget ) {
27                 if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget
28                         continue;
29
30                 $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false );
31                 $done[] = $widget['callback'];
32
33                 if ( ! isset( $widget['params'][0] ) )
34                         $widget['params'][0] = array();
35
36                 $args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' );
37
38                 if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) {
39                         $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
40                         $args['_temp_id'] = "$id_base-__i__";
41                         $args['_multi_num'] = next_widget_id_number($id_base);
42                         $args['_add'] = 'multi';
43                 } else {
44                         $args['_add'] = 'single';
45                         if ( $sidebar )
46                                 $args['_hide'] = '1';
47                 }
48
49                 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
50                 call_user_func_array( 'wp_widget_control', $args );
51         }
52 }
53
54 /**
55  * {@internal Missing Short Description}}
56  *
57  * @since unknown
58  *
59  * @param string $sidebar
60  */
61 function wp_list_widget_controls( $sidebar ) {
62         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
63
64         echo "\t<div id='$sidebar' class='widgets-sortables'>\n";
65         dynamic_sidebar( $sidebar );
66         echo "\t</div>\n";
67 }
68
69 /**
70  * {@internal Missing Short Description}}
71  *
72  * @since unknown
73  *
74  * @param array $params
75  * @return array
76  */
77 function wp_list_widget_controls_dynamic_sidebar( $params ) {
78         global $wp_registered_widgets;
79         static $i = 0;
80         $i++;
81
82         $widget_id = $params[0]['widget_id'];
83         $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
84         $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
85
86         $params[0]['before_widget'] = "<div id='widget-${i}_$id' class='widget'$hidden>";
87         $params[0]['after_widget'] = "</div>";
88         $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
89         $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
90         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
91                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
92                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
93         }
94
95         return $params;
96 }
97
98 function next_widget_id_number($id_base) {
99         global $wp_registered_widgets;
100         $number = 2;
101
102         while ( isset($wp_registered_widgets["$id_base-$number"]) )
103                 $number++;
104
105         return $number;
106 }
107
108 /**
109  * Meta widget used to display the control form for a widget.
110  *
111  * Called from dynamic_sidebar().
112  *
113  * @since unknown
114  *
115  * @param array $sidebar_args
116  * @return array
117  */
118 function wp_widget_control( $sidebar_args ) {
119         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
120
121         $widget_id = $sidebar_args['widget_id'];
122         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
123         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
124         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
125         $widget = $wp_registered_widgets[$widget_id];
126
127         $id_format = $widget['id'];
128         $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
129         $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
130         $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
131         $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
132
133         $query_arg = array( 'editwidget' => $widget['id'] );
134         if ( $add_new ) {
135                 $query_arg['addnew'] = 1;
136                 if ( $multi_number ) {
137                         $query_arg['num'] = $multi_number;
138                         $query_arg['base'] = $id_base;
139                 }
140         } else {
141                 $query_arg['sidebar'] = $sidebar_id;
142                 $query_arg['key'] = $key;
143         }
144
145         // We aren't showing a widget control, we're outputing a template for a mult-widget control
146         if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
147                 // number == -1 implies a template where id numbers are replaced by a generic '__i__'
148                 $control['params'][0]['number'] = -1;
149                 // with id_base widget id's are constructed like {$id_base}-{$id_number}
150                 if ( isset($control['id_base']) )
151                         $id_format = $control['id_base'] . '-__i__';
152         }
153
154         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
155         unset($wp_registered_widgets[$widget_id]['_callback']);
156
157         $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
158         $has_form = 'noform';
159
160         echo $sidebar_args['before_widget']; ?>
161         <div class="widget-top">
162         <div class="widget-title-action">
163                 <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
164                 <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>"><span class="edit"><?php _e('Edit'); ?></span><span class="add"><?php _e('Add'); ?></span></a>
165         </div>
166         <div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
167         </div>
168
169         <div class="widget-inside">
170         <form action="" method="post">
171         <div class="widget-content">
172 <?php
173         if ( isset($control['callback']) )
174                 $has_form = call_user_func_array( $control['callback'], $control['params'] );
175         else
176                 echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
177         </div>
178         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
179         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
180         <input type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr($control['width']); ?>" />
181         <input type="hidden" name="widget-height" class="widget-height" value="<?php echo esc_attr($control['height']); ?>" />
182         <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
183         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
184         <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
185
186         <div class="widget-control-actions">
187                 <div class="alignleft">
188                 <a class="widget-control-remove" href="#remove"><?php _e('Remove'); ?></a> |
189                 <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
190                 </div>
191                 <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
192                 <img src="images/wpspin_light.gif" class="ajax-feedback " title="" alt="" />
193                 <input type="submit" name="savewidget" class="button-primary widget-control-save" value="<?php esc_attr_e('Save'); ?>" />
194                 </div>
195                 <br class="clear" />
196         </div>
197         </form>
198         </div>
199
200         <div class="widget-description">
201 <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
202         </div>
203 <?php
204         echo $sidebar_args['after_widget'];
205         return $sidebar_args;
206 }
207