16
16
* @param string $show Optional, default is all. What to display, can be 'all', 'unused', or 'used'.
17
17
* @param string $_search Optional. Search for widgets. Should be unsanitized.
19
function wp_list_widgets( $show = 'all', $_search = false ) {
19
function wp_list_widgets() {
20
20
global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
23
$search = preg_replace( '/[^\w\s]/', '', $_search );
25
$search_terms = preg_split( '/[\s]/', $search, -1, PREG_SPLIT_NO_EMPTY );
27
$search_terms = array();
22
$sort = $wp_registered_widgets;
23
usort( $sort, create_function( '$a, $b', 'return strnatcasecmp( $a["name"], $b["name"] );' ) );
26
foreach ( $sort as $widget ) {
27
if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget
30
$sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false );
31
$done[] = $widget['callback'];
33
if ( ! isset( $widget['params'][0] ) )
34
$widget['params'][0] = array();
36
$args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' );
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';
44
$args['_add'] = 'single';
49
$args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
50
call_user_func_array( 'wp_widget_control', $args );
30
if ( !in_array( $show, array( 'all', 'unused', 'used' ) ) )
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
42
if ( $search_terms ) {
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'] );
49
foreach ( $search_terms as $search_term ) {
50
if ( stristr( $search_text, $search_term ) ) {
59
$sidebar = is_active_widget( $widget['callback'], $widget['id'] );
61
if ( ( 'unused' == $show && $sidebar ) || ( 'used' == $show && !$sidebar ) )
64
if ( ! isset( $widget['params'][0] ) )
65
$widget['params'][0] = array();
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();
72
$widget_id = $widget['id']; // save this for later in case we mess with $widget['id']
74
$is_multi = false !== strpos( $widget_control_template, '%i%' );
75
if ( !$sidebar || $is_multi ) {
77
'sidebar' => $sidebar,
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"]) )
89
$widget['id'] = "$id_base-$num";
90
$add_query['base'] = $id_base;
91
$add_query['key'] = $num;
92
$add_query['sidebar'] = $GLOBALS['sidebar'];
94
$add_query['add'] = $widget['id'];
96
$add_url = clean_url( wp_nonce_url( add_query_arg( $add_query ), "add-widget_$widget[id]" ) );
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] ),
105
$widget_control_template = '<textarea rows="1" cols="1">' . htmlspecialchars( $widget_control_template ) . '</textarea>';
108
$widget_control_template = $sidebar_args['before_widget'] . $widget_control_template . $sidebar_args['after_widget'];
110
$no_widgets_shown = false;
113
if ( 'all' != $show && $sidebar_args['_widget_title'] )
114
$widget_title = $sidebar_args['_widget_title'];
116
$widget_title = $widget['name'];
119
<li id="widget-list-item-<?php echo attribute_escape( $widget['id'] ); ?>" class="widget-list-item">
120
<h4 class="widget-title widget-draggable">
122
<span><?php echo $widget_title; ?></span>
124
<?php if ( 'add' == $action ) : ?>
126
<a class="widget-action widget-control-add" href="<?php echo $add_url; ?>"><?php _e( 'Add' ); ?></a>
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.
132
<a class="widget-action widget-control-edit" href="<?php echo $edit_url; ?>" style="display: none;"><?php _e( 'Edit' ); ?></a>
141
<ul id="widget-control-info-<?php echo $widget['id']; ?>" class="widget-control-info">
143
<?php echo $widget_control_template; ?>
147
<?php if ( 'add' == $action ) : ?>
150
<div class="widget-description">
151
<?php echo ( $widget_description = wp_widget_description( $widget_id ) ) ? $widget_description : ' '; ?>
158
<?php endforeach; if ( $no_widgets_shown ) : ?>
160
<li><?php _e( 'No matching widgets' ); ?></li>
201
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;"' : '';
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%";
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
207
90
if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
208
91
$wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
209
92
$wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
98
function next_widget_id_number($id_base) {
99
global $wp_registered_widgets;
102
foreach ( $wp_registered_widgets as $widget_id => $widget ) {
103
if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
104
$number = max($number, $matches[1]);
215
112
* Meta widget used to display the control form for a widget.
224
121
function wp_widget_control( $sidebar_args ) {
225
global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets, $edit_widget;
122
global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
226
124
$widget_id = $sidebar_args['widget_id'];
227
125
$sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
229
$control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : 0;
230
$widget = $wp_registered_widgets[$widget_id];
232
$key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : 'no-key'; // position of widget in sidebar
234
$edit = -1 < $edit_widget && is_numeric($key) && $edit_widget === $key; // (bool) are we currently editing this widget
126
$key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
127
$control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
128
$widget = $wp_registered_widgets[$widget_id];
236
130
$id_format = $widget['id'];
238
if ( ! isset( $sidebar_args['_show'] ) )
239
$sidebar_args['_show'] = '';
241
if ( ! isset( $sidebar_args['_display'] ) )
242
$sidebar_args['_display'] = '';
131
$widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
132
$id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
133
$multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
134
$add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
136
$query_arg = array( 'editwidget' => $widget['id'] );
138
$query_arg['addnew'] = 1;
139
if ( $multi_number ) {
140
$query_arg['num'] = $multi_number;
141
$query_arg['base'] = $id_base;
144
$query_arg['sidebar'] = $sidebar_id;
145
$query_arg['key'] = $key;
244
148
// 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%'
149
if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
150
// number == -1 implies a template where id numbers are replaced by a generic '__i__'
247
151
$control['params'][0]['number'] = -1;
248
// if given, id_base means widget id's should be constructed like {$id_base}-{$id_number}
152
// with id_base widget id's are constructed like {$id_base}-{$id_number}
249
153
if ( isset($control['id_base']) )
250
$id_format = $control['id_base'] . '-%i%';
154
$id_format = $control['id_base'] . '-__i__';
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'] ) ) {
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 );
262
157
$wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
263
158
unset($wp_registered_widgets[$widget_id]['_callback']);
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 );
160
$widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
161
$has_form = 'noform';
163
echo $sidebar_args['before_widget']; ?>
164
<div class="widget-top">
165
<div class="widget-title-action">
166
<a class="widget-action hide-if-no-js" href="#available-widgets"></a>
167
<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>
169
<div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
172
<div class="widget-inside">
173
<form action="" method="post">
174
<div class="widget-content">
176
if ( isset($control['callback']) )
177
$has_form = call_user_func_array( $control['callback'], $control['params'] );
268
$widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
270
$sidebar_args['_widget_title'] = $widget_title;
272
if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
273
echo $sidebar_args['before_widget'];
275
<div class="widget-top">
276
<h4 class="widget-title"><span><?php echo $widget_title ?></span>
278
<?php if ( $edit ) : ?>
280
<a class="widget-action widget-control-edit" href="<?php echo clean_url( remove_query_arg( array( 'edit', 'key' ) ) ); ?>"><?php _e('Cancel'); ?></a>
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>
292
<div class="widget-control"<?php if ( $edit ) echo ' style="display: block;"'; ?>>
296
call_user_func_array( $control['callback'], $control['params'] );
298
echo '<p>' . __('There are no options for this widget.') . '</p>';
301
<input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
302
<input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
304
<div class="widget-control-actions">
306
<?php if ( $control ) : ?>
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>
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>
179
echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
181
<input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
182
<input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
183
<input type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr($control['width']); ?>" />
184
<input type="hidden" name="widget-height" class="widget-height" value="<?php echo esc_attr($control['height']); ?>" />
185
<input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
186
<input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
187
<input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
189
<div class="widget-control-actions">
190
<div class="alignleft">
191
<a class="widget-control-remove" href="#remove"><?php _e('Remove'); ?></a> |
192
<a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
194
<div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
195
<img src="images/wpspin_light.gif" class="ajax-feedback " title="" alt="" />
196
<input type="submit" name="savewidget" class="button-primary widget-control-save" value="<?php esc_attr_e('Save'); ?>" />
203
<div class="widget-description">
204
<?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
317
if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
318
echo $sidebar_args['after_widget'];
207
echo $sidebar_args['after_widget'];
319
208
return $sidebar_args;
323
* {@internal Missing Short Description}}
327
* @param string $string
330
function wp_widget_control_ob_filter( $string ) {
331
if ( false === $beg = strpos( $string, '%BEG_OF_TITLE%' ) )
333
if ( false === $end = strpos( $string, '%END_OF_TITLE%' ) )
335
$string = substr( $string, $beg + 14 , $end - $beg - 14);
336
$string = str_replace( ' ', ' ', $string );
337
return trim( wp_specialchars( strip_tags( $string ) ) );
b'\\ No newline at end of file'