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