Wordpress 2.9
[autoinstalls/wordpress.git] / wp-admin / widgets.php
1 <?php
2 /**
3  * Widgets administration panel.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( 'admin.php' );
11
12 /** WordPress Administration Widgets API */
13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
14
15 if ( ! current_user_can('switch_themes') )
16         wp_die( __( 'Cheatin&#8217; uh?' ));
17
18 wp_admin_css( 'widgets' );
19
20 $widgets_access = get_user_setting( 'widgets_access' );
21 if ( isset($_GET['widgets-access']) ) {
22         $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
23         set_user_setting( 'widgets_access', $widgets_access );
24 }
25
26 if ( 'on' == $widgets_access )
27         add_filter( 'admin_body_class', create_function('', '{return " widgets_access ";}') );
28 else
29         wp_enqueue_script('admin-widgets');
30
31 do_action( 'sidebar_admin_setup' );
32
33 $title = __( 'Widgets' );
34 $parent_file = 'themes.php';
35
36 // register the inactive_widgets area as sidebar
37 register_sidebar(array(
38         'name' => __('Inactive Widgets'),
39         'id' => 'wp_inactive_widgets',
40         'description' => '',
41         'before_widget' => '',
42         'after_widget' => '',
43         'before_title' => '',
44         'after_title' => '',
45 ));
46
47 // These are the widgets grouped by sidebar
48 $sidebars_widgets = wp_get_sidebars_widgets();
49 if ( empty( $sidebars_widgets ) )
50         $sidebars_widgets = wp_get_widget_defaults();
51
52 // look for "lost" widgets, this has to run at least on each theme change
53 function retrieve_widgets() {
54         global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
55
56         $_sidebars_widgets = array();
57         $sidebars = array_keys($wp_registered_sidebars);
58
59         unset( $sidebars_widgets['array_version'] );
60
61         $old = array_keys($sidebars_widgets);
62         sort($old);
63         sort($sidebars);
64
65         if ( $old == $sidebars )
66                 return;
67
68         // Move the known-good ones first
69         foreach ( $sidebars as $id ) {
70                 if ( array_key_exists( $id, $sidebars_widgets ) ) {
71                         $_sidebars_widgets[$id] = $sidebars_widgets[$id];
72                         unset($sidebars_widgets[$id], $sidebars[$id]);
73                 }
74         }
75
76         // if new theme has less sidebars than the old theme
77         if ( !empty($sidebars_widgets) ) {
78                 foreach ( $sidebars_widgets as $lost => $val ) {
79                         if ( is_array($val) )
80                                 $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
81                 }
82         }
83
84         // discard invalid, theme-specific widgets from sidebars
85         $shown_widgets = array();
86         foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
87                 if ( !is_array($widgets) )
88                         continue;
89
90                 $_widgets = array();
91                 foreach ( $widgets as $widget ) {
92                         if ( isset($wp_registered_widgets[$widget]) )
93                                 $_widgets[] = $widget;
94                 }
95                 $_sidebars_widgets[$sidebar] = $_widgets;
96                 $shown_widgets = array_merge($shown_widgets, $_widgets);
97         }
98
99         $sidebars_widgets = $_sidebars_widgets;
100         unset($_sidebars_widgets, $_widgets);
101
102         // find hidden/lost multi-widget instances
103         $lost_widgets = array();
104         foreach ( $wp_registered_widgets as $key => $val ) {
105                 if ( in_array($key, $shown_widgets, true) )
106                         continue;
107
108                 $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
109
110                 if ( 2 > (int) $number )
111                         continue;
112
113                 $lost_widgets[] = $key;
114         }
115
116         $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
117         wp_set_sidebars_widgets($sidebars_widgets);
118 }
119 retrieve_widgets();
120
121 if ( count($wp_registered_sidebars) == 1 ) {
122         // If only "wp_inactive_widgets" is defined the theme has no sidebars, die.
123         require_once( 'admin-header.php' );
124 ?>
125
126         <div class="wrap">
127         <?php screen_icon(); ?>
128         <h2><?php echo esc_html( $title ); ?></h2>
129                 <div class="error">
130                         <p><?php _e( 'No Sidebars Defined' ); ?></p>
131                 </div>
132                 <p><?php _e( 'The theme you are currently using isn&#8217;t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="http://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ); ?></p>
133         </div>
134
135 <?php
136         require_once( 'admin-footer.php' );
137         exit;
138 }
139
140 // We're saving a widget without js
141 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
142         $widget_id = $_POST['widget-id'];
143         check_admin_referer("save-delete-widget-$widget_id");
144
145         $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
146         if ( $number ) {
147                 foreach ( $_POST as $key => $val ) {
148                         if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
149                                 $_POST[$key] = array( $number => array_shift($val) );
150                                 break;
151                         }
152                 }
153         }
154
155         $sidebar_id = $_POST['sidebar'];
156         $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
157
158         $id_base = $_POST['id_base'];
159         $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
160
161         // delete
162         if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
163
164                 if ( !in_array($widget_id, $sidebar, true) ) {
165                         wp_redirect('widgets.php?error=0');
166                         exit;
167                 }
168
169                 $sidebar = array_diff( $sidebar, array($widget_id) );
170                 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
171         }
172
173         $_POST['widget-id'] = $sidebar;
174
175         foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
176                 if ( $name != $id_base || !is_callable($control['callback']) )
177                         continue;
178
179                 ob_start();
180                         call_user_func_array( $control['callback'], $control['params'] );
181                 ob_end_clean();
182
183                 break;
184         }
185
186         $sidebars_widgets[$sidebar_id] = $sidebar;
187
188         // remove old position
189         if ( !isset($_POST['delete_widget']) ) {
190                 foreach ( $sidebars_widgets as $key => $sb ) {
191                         if ( is_array($sb) )
192                                 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
193                 }
194                 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
195         }
196
197         wp_set_sidebars_widgets($sidebars_widgets);
198         wp_redirect('widgets.php?message=0');
199         exit;
200 }
201
202 // Output the widget form without js
203 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
204         $widget_id = $_GET['editwidget'];
205
206         if ( isset($_GET['addnew']) ) {
207                 // Default to the first sidebar
208                 $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
209
210                 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
211                         // Copy minimal info from an existing instance of this widget to a new instance
212                         foreach ( $wp_registered_widget_controls as $control ) {
213                                 if ( $_GET['base'] === $control['id_base'] ) {
214                                         $control_callback = $control['callback'];
215                                         $multi_number = (int) $_GET['num'];
216                                         $control['params'][0]['number'] = -1;
217                                         $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
218                                         $wp_registered_widget_controls[$control['id']] = $control;
219                                         break;
220                                 }
221                         }
222                 }
223         }
224
225         if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
226                 $control = $wp_registered_widget_controls[$widget_id];
227                 $control_callback = $control['callback'];
228         } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
229                 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
230         }
231
232         if ( !isset($name) )
233                 $name = esc_html( strip_tags($control['name']) );
234
235         if ( !isset($sidebar) )
236                 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
237
238         if ( !isset($multi_number) )
239                 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
240
241         $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
242
243         // show the widget form
244         $width = ' style="width:' . max($control['width'], 350) . 'px"';
245         $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
246
247         require_once( 'admin-header.php' ); ?>
248         <div class="wrap">
249         <?php screen_icon(); ?>
250         <h2><?php echo esc_html( $title ); ?></h2>
251         <div class="editwidget"<?php echo $width; ?>>
252         <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
253
254         <form action="widgets.php" method="post">
255         <div class="widget-inside">
256 <?php
257         if ( is_callable( $control_callback ) )
258                 call_user_func_array( $control_callback, $control['params'] );
259         else
260                 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
261         </div>
262
263         <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
264         <div class="widget-position">
265         <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
266 <?php
267         foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
268                 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
269                 if ( 'wp_inactive_widgets' == $sbname ) {
270                         echo '&nbsp;';
271                 } else {
272                         if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
273                                 $j = 1;
274                                 $sidebars_widgets[$sbname] = array();
275                         } else {
276                                 $j = count($sidebars_widgets[$sbname]);
277                                 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
278                                         $j++;
279                         }
280                         $selected = '';
281                         echo "\t\t<select name='{$sbname}_position'>\n";
282                         echo "\t\t<option value=''>" . __('-- select --') . "</option>\n";
283                         for ( $i = 1; $i <= $j; $i++ ) {
284                                 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
285                                         $selected = selected( $i, $key + 1, false );
286                                 echo "\t\t<option value='$i'$selected> $i </option>\n";
287                         }
288                         echo "\t\t</select>\n";
289                 }
290                 echo "</td></tr>\n";
291         } ?>
292         </tbody></table>
293         </div>
294
295         <div class="widget-control-actions">
296 <?php   if ( isset($_GET['addnew']) ) { ?>
297         <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
298 <?php   } else { ?>
299         <input type="submit" name="removewidget" class="button alignleft" value="<?php esc_attr_e('Delete'); ?>" />
300 <?php   } ?>
301         <input type="submit" name="savewidget" class="button-primary alignright" value="<?php esc_attr_e('Save Widget'); ?>" />
302         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
303         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
304         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
305 <?php   wp_nonce_field("save-delete-widget-$widget_id"); ?>
306         <br class="clear" />
307         </div>
308         </form>
309         </div>
310         </div>
311 <?php
312         require_once( 'admin-footer.php' );
313         exit;
314 }
315
316 $messages = array(
317         __('Changes saved.')
318 );
319
320 $errors = array(
321         __('Error while saving.'),
322         __('Error in displaying the widget settings form.')
323 );
324
325 require_once( 'admin-header.php' ); ?>
326
327 <div class="wrap">
328 <?php screen_icon(); ?>
329 <h2><?php echo esc_html( $title ); ?></h2>
330
331 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
332 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
333 <?php } ?>
334 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
335 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
336 <?php } ?>
337
338 <div class="widget-liquid-left">
339 <div id="widgets-left">
340         <div id="available-widgets" class="widgets-holder-wrap">
341                 <div class="sidebar-name">
342                 <div class="sidebar-name-arrow"><br /></div>
343                 <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _e('Deactivate'); ?> <span></span></span></h3></div>
344                 <div class="widget-holder">
345                 <p class="description"><?php _e('Drag widgets from here to a sidebar on the right to activate them. Drag widgets back here to deactivate them and delete their settings.'); ?></p>
346                 <div id="widget-list">
347                 <?php wp_list_widgets(); ?>
348                 </div>
349                 <br class='clear' />
350                 </div>
351                 <br class="clear" />
352         </div>
353
354         <div class="widgets-holder-wrap">
355                 <div class="sidebar-name">
356                 <div class="sidebar-name-arrow"><br /></div>
357                 <h3><?php _e('Inactive Widgets'); ?>
358                 <span><img src="images/wpspin_light.gif" class="ajax-feedback" title="" alt="" /></span></h3></div>
359                 <div class="widget-holder inactive">
360                 <p class="description"><?php _e('Drag widgets here to remove them from the sidebar but keep their settings.'); ?></p>
361                 <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
362                 <br class="clear" />
363                 </div>
364         </div>
365 </div>
366 </div>
367
368 <div class="widget-liquid-right">
369 <div id="widgets-right">
370 <?php
371 $i = 0;
372 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
373         if ( 'wp_inactive_widgets' == $sidebar )
374                 continue;
375         $closed = $i ? ' closed' : ''; ?>
376         <div class="widgets-holder-wrap<?php echo $closed; ?>">
377         <div class="sidebar-name">
378         <div class="sidebar-name-arrow"><br /></div>
379         <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
380         <span><img src="images/wpspin_dark.gif" class="ajax-feedback" title="" alt="" /></span></h3></div>
381         <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
382         </div>
383 <?php
384         $i++;
385 } ?>
386 </div>
387 </div>
388 <form action="" method="post">
389 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
390 </form>
391 <br class="clear" />
392 </div>
393
394 <?php
395 do_action( 'sidebar_admin_page' );
396 require_once( 'admin-footer.php' );