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