]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/widgets.php
Wordpress 2.7.1-scripts
[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 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( $show = 'all', $_search = false ) {
20         global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
21         if ( $_search ) {
22                 // sanitize
23                 $search = preg_replace( '/[^\w\s]/', '', $_search );
24                 // array of terms
25                 $search_terms = preg_split( '/[\s]/', $search, -1, PREG_SPLIT_NO_EMPTY );
26         } else {
27                 $search_terms = array();
28         }
29
30         if ( !in_array( $show, array( 'all', 'unused', 'used' ) ) )
31                 $show = 'all';
32 ?>
33
34         <ul id='widget-list'>
35                 <?php
36                 $no_widgets_shown = true;
37                 $already_shown = array();
38                 foreach ( $wp_registered_widgets as $name => $widget ) :
39                         if ( 'all' == $show && in_array( $widget['callback'], $already_shown ) ) // We already showed this multi-widget
40                                 continue;
41
42                         if ( $search_terms ) {
43                                 $hit = false;
44                                 // Simple case-insensitive search.  Boolean OR.
45                                 $search_text = preg_replace( '/[^\w]/', '', $widget['name'] );
46                                 if ( isset($widget['description']) )
47                                         $search_text .= preg_replace( '/[^\w]/', '', $widget['description'] );
48
49                                 foreach ( $search_terms as $search_term ) {
50                                         if ( stristr( $search_text, $search_term ) ) {
51                                                 $hit = true;
52                                                 break;
53                                         }
54                                 }
55                                 if ( !$hit )
56                                         continue;
57                         }
58
59                         $sidebar = is_active_widget( $widget['callback'], $widget['id'] );
60
61                         if ( ( 'unused' == $show && $sidebar ) || ( 'used' == $show && !$sidebar ) )
62                                 continue;
63
64                         if ( ! isset( $widget['params'][0] ) )
65                                 $widget['params'][0] = array();
66                         ob_start();
67                         $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template', '_show' => $show ), 1 => $widget['params'][0] ) );
68                         $sidebar_args = call_user_func_array( 'wp_widget_control', $args );
69                         $widget_control_template = ob_get_contents();
70                         ob_end_clean();
71
72                         $widget_id = $widget['id']; // save this for later in case we mess with $widget['id']
73
74                         $is_multi = false !== strpos( $widget_control_template, '%i%' );
75                         if ( !$sidebar || $is_multi ) {
76                                 $add_query = array(
77                                         'sidebar' => $sidebar,
78                                         'key' => false,
79                                         'edit' => false
80                                 );
81                                 if ( 'all' == $show && $is_multi ) {
82                                         // it's a multi-widget.  We only need to show it in the list once.
83                                         $already_shown[] = $widget['callback'];
84                                         $num = (int) array_pop( $ids = explode( '-', $widget['id'] ) );
85                                         $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
86                                         // so that we always add a new one when clicking "add"
87                                         while ( isset($wp_registered_widgets["$id_base-$num"]) )
88                                                 $num++;
89                                         $widget['id'] = "$id_base-$num";
90                                         $add_query['base'] = $id_base;
91                                         $add_query['key'] = $num;
92                                         $add_query['sidebar'] = $GLOBALS['sidebar'];
93                                 }
94                                 $add_query['add'] = $widget['id'];
95                                 $action = 'add';
96                                 $add_url = clean_url( wp_nonce_url( add_query_arg( $add_query ), "add-widget_$widget[id]" ) );
97                         } else {
98                                 $action = 'edit';
99                                 $edit_url = clean_url( add_query_arg( array(
100                                         'sidebar' => $sidebar,
101                                         'edit' => $widget['id'],
102                                         'key' => array_search( $widget['id'], $sidebars_widgets[$sidebar] ),
103                                 ) ) );
104
105                                 $widget_control_template = '<textarea rows="1" cols="1">' . htmlspecialchars( $widget_control_template ) . '</textarea>';
106                         }
107
108                         $widget_control_template = $sidebar_args['before_widget'] . $widget_control_template . $sidebar_args['after_widget'];
109
110                         $no_widgets_shown = false;
111
112
113                         if ( 'all' != $show && $sidebar_args['_widget_title'] )
114                                 $widget_title = $sidebar_args['_widget_title'];
115                         else
116                                 $widget_title = $widget['name'];
117                 ?>
118
119                 <li id="widget-list-item-<?php echo attribute_escape( $widget['id'] ); ?>" class="widget-list-item">
120                         <h4 class="widget-title widget-draggable">
121
122                                 <span><?php echo $widget_title; ?></span>
123
124                                 <?php if ( 'add' == $action ) : ?>
125
126                                 <a class="widget-action widget-control-add" href="<?php echo $add_url; ?>"><?php _e( 'Add' ); ?></a>
127
128                                 <?php elseif ( 'edit' == $action ) :
129                                         // We echo a hidden edit link for the sake of the JS.  Edit links are shown (needlessly?) after a widget is added.
130                                 ?>
131
132                                 <a class="widget-action widget-control-edit" href="<?php echo $edit_url; ?>" style="display: none;"><?php _e( 'Edit' ); ?></a>
133
134                                 <?php endif; ?>
135
136                                 <br class="clear" />
137
138                         </h4>
139
140
141                         <ul id="widget-control-info-<?php echo $widget['id']; ?>" class="widget-control-info">
142
143                                 <?php echo $widget_control_template; ?>
144
145                         </ul>
146
147                         <?php if ( 'add' == $action ) : ?>
148                         <?php endif; ?>
149
150                         <div class="widget-description">
151                                 <?php echo ( $widget_description = wp_widget_description( $widget_id ) ) ? $widget_description : '&nbsp;'; ?>
152                         </div>
153
154                         <br class="clear" />
155
156                 </li>
157
158                 <?php endforeach; if ( $no_widgets_shown ) : ?>
159
160                 <li><?php _e( 'No matching widgets' ); ?></li>
161
162                 <?php endif; ?>
163
164         </ul>
165 <?php
166 }
167
168 /**
169  * {@internal Missing Short Description}}
170  *
171  * @since unknown
172  *
173  * @param string $sidebar
174  */
175 function wp_list_widget_controls( $sidebar ) {
176         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
177 ?>
178
179         <ul class="widget-control-list">
180
181                 <?php if ( !dynamic_sidebar( $sidebar ) ) echo "<li />"; ?>
182
183         </ul>
184
185 <?php
186 }
187
188 /**
189  * {@internal Missing Short Description}}
190  *
191  * @since unknown
192  *
193  * @param array $params
194  * @return array
195  */
196 function wp_list_widget_controls_dynamic_sidebar( $params ) {
197         global $wp_registered_widgets;
198         static $i = 0;
199         $i++;
200
201         $widget_id = $params[0]['widget_id'];
202
203         $params[0]['before_widget'] = "<li id='widget-list-control-item-$i-$widget_id' class='widget-list-control-item widget-sortable'>\n";
204         $params[0]['after_widget'] = "</li>";
205         $params[0]['before_title'] = "%BEG_OF_TITLE%";
206         $params[0]['after_title'] = "%END_OF_TITLE%";
207         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
208                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
209                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
210         }
211         return $params;
212 }
213
214 /**
215  * Meta widget used to display the control form for a widget.
216  *
217  * Called from dynamic_sidebar().
218  *
219  * @since unknown
220  *
221  * @param array $sidebar_args
222  * @return array
223  */
224 function wp_widget_control( $sidebar_args ) {
225         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets, $edit_widget;
226         $widget_id = $sidebar_args['widget_id'];
227         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
228
229         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : 0;
230         $widget  = $wp_registered_widgets[$widget_id];
231
232         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : 'no-key'; // position of widget in sidebar
233
234         $edit = -1 <  $edit_widget && is_numeric($key) && $edit_widget === $key; // (bool) are we currently editing this widget
235
236         $id_format = $widget['id'];
237
238         if ( ! isset( $sidebar_args['_show'] ) )
239                 $sidebar_args['_show'] = '';
240
241         if ( ! isset( $sidebar_args['_display'] ) )
242                 $sidebar_args['_display'] = '';
243
244         // We aren't showing a widget control, we're outputing a template for a mult-widget control
245         if ( 'all' == $sidebar_args['_show'] && 'template' == $sidebar_args['_display'] && isset($control['params'][0]['number']) ) {
246                 // number == -1 implies a template where id numbers are replaced by a generic '%i%'
247                 $control['params'][0]['number'] = -1;
248                 // if given, id_base means widget id's should be constructed like {$id_base}-{$id_number}
249                 if ( isset($control['id_base']) )
250                         $id_format = $control['id_base'] . '-%i%';
251         }
252
253         $widget_title = '';
254         // We grab the normal widget output to find the widget's title
255         if ( ( 'all' != $sidebar_args['_show'] || 'template' != $sidebar_args['_display'] ) && is_callable( $widget['_callback'] ) ) {
256                 ob_start();
257                 $args = func_get_args();
258                 call_user_func_array( $widget['_callback'], $args );
259                 $widget_title = ob_get_clean();
260                 $widget_title = wp_widget_control_ob_filter( $widget_title );
261         }
262         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
263         unset($wp_registered_widgets[$widget_id]['_callback']);
264
265         if ( $widget_title && $widget_title != $sidebar_args['widget_name'] )
266                 $widget_title = sprintf( _c('%1$s: %2$s|1: widget name, 2: widget title' ), $sidebar_args['widget_name'], $widget_title );
267         else
268                 $widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
269
270         $sidebar_args['_widget_title'] = $widget_title;
271
272         if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
273                 echo $sidebar_args['before_widget'];
274 ?>
275                 <div class="widget-top">
276                 <h4 class="widget-title"><span><?php echo $widget_title ?></span>
277
278                         <?php if ( $edit ) : ?>
279
280                         <a class="widget-action widget-control-edit" href="<?php echo clean_url( remove_query_arg( array( 'edit', 'key' ) ) ); ?>"><?php _e('Cancel'); ?></a>
281
282                         <?php else : ?>
283
284                         <a class="widget-action widget-control-edit" href="<?php echo clean_url( add_query_arg( array( 'edit' => $id_format, 'key' => $key ) ) ); ?>"><?php _e('Edit'); ?></a>
285
286                         <?php endif; ?>
287
288                         <br class="clear" />
289
290                 </h4></div>
291
292                 <div class="widget-control"<?php if ( $edit ) echo ' style="display: block;"'; ?>>
293
294                         <?php
295                         if ( $control )
296                                 call_user_func_array( $control['callback'], $control['params'] );
297                         else
298                                 echo '<p>' . __('There are no options for this widget.') . '</p>';
299                         ?>
300
301                         <input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
302                         <input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
303
304                         <div class="widget-control-actions">
305
306                                 <?php if ( $control ) : ?>
307
308                                 <a class="button widget-action widget-control-save hide-if-no-js edit alignleft" href="#save:<?php echo $id_format; ?>"><?php _e('Done'); ?></a>
309
310                                 <?php endif; ?>
311
312                                 <a class="button widget-action widget-control-remove alignright" href="<?php echo clean_url( wp_nonce_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key ) ), "remove-widget_$widget[id]" ) ); ?>"><?php _e('Remove'); ?></a>
313                                 <br class="clear" />
314                         </div>
315                 </div>
316 <?php
317         if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
318                 echo $sidebar_args['after_widget'];
319         return $sidebar_args;
320 }
321
322 /**
323  * {@internal Missing Short Description}}
324  *
325  * @since unknown
326  *
327  * @param string $string
328  * @return string
329  */
330 function wp_widget_control_ob_filter( $string ) {
331         if ( false === $beg = strpos( $string, '%BEG_OF_TITLE%' ) )
332                 return '';
333         if ( false === $end = strpos( $string, '%END_OF_TITLE%' ) )
334                 return '';
335         $string = substr( $string, $beg + 14 , $end - $beg - 14);
336         $string = str_replace( '&nbsp;', ' ', $string );
337         return trim( wp_specialchars( strip_tags( $string ) ) );
338 }
339
340 ?>