~ubuntu-branches/ubuntu/lucid/wordpress/lucid-security

« back to all changes in this revision

Viewing changes to wp-admin/includes/widgets.php

  • Committer: Bazaar Package Importer
  • Author(s): Michael Bienia
  • Date: 2009-08-14 13:32:33 UTC
  • mfrom: (11.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090814133233-n1qy24xncscu6yvj
Tags: 2.8.3-2ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/apache.conf:
    + Changed to use /var/www instead of /srv/www for virtual webroot.
  - debian/setup-mysql:
    + Changed to use /var/www instead of /srv/www.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 */
8
8
 
9
9
/**
10
 
 * Display list of widgets, either all or matching search.
 
10
 * Display list of the available widgets, either all or matching search.
11
11
 *
12
12
 * The search parameter are search terms separated by spaces.
13
13
 *
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.
18
18
 */
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;
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();
 
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 );
28
51
        }
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
52
}
167
53
 
168
54
/**
174
60
 */
175
61
function wp_list_widget_controls( $sidebar ) {
176
62
        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
 
63
 
 
64
        echo "\t<div id='$sidebar' class='widgets-sortables'>\n";
 
65
        dynamic_sidebar( $sidebar );
 
66
        echo "\t</div>\n";
186
67
}
187
68
 
188
69
/**
199
80
        $i++;
200
81
 
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;"' : '';
202
85
 
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';
210
93
        }
 
94
 
211
95
        return $params;
212
96
}
213
97
 
 
98
function next_widget_id_number($id_base) {
 
99
        global $wp_registered_widgets;
 
100
        $number = 1;
 
101
 
 
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]);
 
105
        }
 
106
        $number++;
 
107
 
 
108
        return $number;
 
109
}
 
110
 
214
111
/**
215
112
 * Meta widget used to display the control form for a widget.
216
113
 *
222
119
 * @return array
223
120
 */
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;
 
123
 
226
124
        $widget_id = $sidebar_args['widget_id'];
227
125
        $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
 
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];
235
129
 
236
130
        $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'] = '';
 
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'] : '';
 
135
 
 
136
        $query_arg = array( 'editwidget' => $widget['id'] );
 
137
        if ( $add_new ) {
 
138
                $query_arg['addnew'] = 1;
 
139
                if ( $multi_number ) {
 
140
                        $query_arg['num'] = $multi_number;
 
141
                        $query_arg['base'] = $id_base;
 
142
                }
 
143
        } else {
 
144
                $query_arg['sidebar'] = $sidebar_id;
 
145
                $query_arg['key'] = $key;
 
146
        }
243
147
 
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__';
251
155
        }
252
156
 
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
157
        $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
263
158
        unset($wp_registered_widgets[$widget_id]['_callback']);
264
159
 
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';
 
162
 
 
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>
 
168
        </div>
 
169
        <div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
 
170
        </div>
 
171
 
 
172
        <div class="widget-inside">
 
173
        <form action="" method="post">
 
174
        <div class="widget-content">
 
175
<?php
 
176
        if ( isset($control['callback']) )
 
177
                $has_form = call_user_func_array( $control['callback'], $control['params'] );
267
178
        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>
 
179
                echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
 
180
        </div>
 
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); ?>" />
 
188
 
 
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>
 
193
                </div>
 
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'); ?>" />
 
197
                </div>
 
198
                <br class="clear" />
 
199
        </div>
 
200
        </form>
 
201
        </div>
 
202
 
 
203
        <div class="widget-description">
 
204
<?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
 
205
        </div>
316
206
<?php
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;
320
209
}
321
210
 
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
 
?>
 
 
b'\\ No newline at end of file'