Wordpress 2.7.1
[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( array( 'wp-lists', '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 // $sidebar = What sidebar are we editing?
27 if ( isset($_GET['sidebar']) && isset($wp_registered_sidebars[$_GET['sidebar']]) ) {
28         $sidebar = attribute_escape( $_GET['sidebar'] );
29 } elseif ( is_array($wp_registered_sidebars) && !empty($wp_registered_sidebars) ) {
30         // By default we look at the first defined sidebar
31         $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
32 } else {
33         // If no sidebars, die.
34         require_once( 'admin-header.php' );
35 ?>
36
37         <div class="wrap">
38         <?php screen_icon(); ?>
39         <h2><?php echo wp_specialchars( $title ); ?></h2>
40                 <div class="error">
41                         <p><?php _e( 'No Sidebars Defined' ); ?></p>
42                 </div>
43                 <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>
44         </div>
45
46 <?php
47         require_once( 'admin-footer.php' );
48         exit;
49 }
50
51 // These are the widgets grouped by sidebar
52 $sidebars_widgets = wp_get_sidebars_widgets();
53 if ( empty( $sidebars_widgets ) )
54         $sidebars_widgets = wp_get_widget_defaults();
55
56 // for the sake of PHP warnings
57 if ( empty( $sidebars_widgets[$sidebar] ) )
58         $sidebars_widgets[$sidebar] = array();
59
60 $http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
61
62 // We're updating a sidebar
63 if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
64         check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
65
66         /* Hack #1
67          * The widget_control is overloaded.  It updates the widget's options AND echoes out the widget's HTML form.
68          * Since we want to update before sending out any headers, we have to catch it with an output buffer,
69          */
70         ob_start();
71                 /* There can be multiple widgets of the same type, but the widget_control for that
72                  * widget type needs only be called once if it's a multi-widget.
73                  */
74                 $already_done = array();
75
76                 foreach ( $wp_registered_widget_controls as $name => $control ) {
77                         if ( in_array( $control['callback'], $already_done ) )
78                                 continue;
79
80                         if ( is_callable( $control['callback'] ) ) {
81                                 call_user_func_array( $control['callback'], $control['params'] );
82                                 $control_output = ob_get_contents();
83                                 if ( false !== strpos( $control_output, '%i%' ) ) // if it's a multi-widget, only call control function once.
84                                         $already_done[] = $control['callback'];
85                         }
86
87                         ob_clean();
88                 }
89         ob_end_clean();
90
91         // Prophylactic.  Take out empty ids.
92         foreach ( (array) $_POST['widget-id'] as $key => $val )
93                 if ( !$val )
94                         unset($_POST['widget-id'][$key]);
95
96         // Reset the key numbering and store
97         $new_sidebar = isset( $_POST['widget-id'] ) && is_array( $_POST['widget-id'] ) ? array_values( $_POST['widget-id'] ) : array();
98         $sidebars_widgets[$_POST['sidebar']] = $new_sidebar;
99         wp_set_sidebars_widgets( $sidebars_widgets );
100
101         wp_redirect( add_query_arg( 'message', 'updated' ) );
102         exit;
103 }
104
105 // What widget (if any) are we editing
106 $edit_widget = -1;
107
108 $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
109
110 if ( isset($_GET['add']) && $_GET['add'] ) {
111         // Add to the end of the sidebar
112         $control_callback;
113         if ( isset($wp_registered_widgets[$_GET['add']]) ) {
114                 check_admin_referer( "add-widget_$_GET[add]" );
115                 $sidebars_widgets[$sidebar][] = $_GET['add'];
116                 wp_set_sidebars_widgets( $sidebars_widgets );
117         } elseif ( isset($_GET['base']) && isset($_GET['key']) ) { // It's a multi-widget
118                 check_admin_referer( "add-widget_$_GET[add]" );
119                 // Copy minimal info from an existing instance of this widget to a new instance
120                 foreach ( $wp_registered_widget_controls as $control ) {
121                         if ( $_GET['base'] === $control['id_base'] ) {
122                                 $control_callback = $control['callback'];
123                                 $num = (int) $_GET['key'];
124                                 $control['params'][0]['number'] = $num;
125                                 $control['id'] = $control['id_base'] . '-' . $num;
126                                 $wp_registered_widget_controls[$control['id']] = $control;
127                                 $sidebars_widgets[$sidebar][] = $control['id'];
128                                 break;
129                         }
130                 }
131         }
132
133         // it's a multi-widget.  The only way to add multi-widgets without JS is to actually submit POST content...
134         // so here we go
135         if ( is_callable( $control_callback ) ) {
136                 require_once( 'admin-header.php' );
137         ?>
138                 <div class="wrap">
139                 <h2><?php _e( 'Add Widget' ); ?></h2>
140                 <br />
141                 <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
142
143                         <ul class="widget-control-list">
144                                 <li class="widget-list-control-item">
145                                         <div class="widget-top">
146                                         <h4 class="widget-title"><?php echo $control['name']; ?></h4>
147                                         </div>
148                                         <div class="widget-control" style="display: block;">
149         <?php
150                                                 call_user_func_array( $control_callback, $control['params'] );
151         ?>
152                                                 <div class="widget-control-actions">
153                                                         <input type="submit" class="button" value="<?php _e( 'Add Widget' ); ?>" />
154                                                         <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
155         <?php   wp_nonce_field ( 'edit-sidebar_' . $sidebar );
156                 foreach ( $sidebars_widgets[$sidebar] as $sidebar_widget_id ) : ?>
157                                                         <input type="hidden" name='widget-id[]' value="<?php echo $sidebar_widget_id; ?>" />
158         <?php   endforeach; ?>
159                                                 </div>
160                                         </div>
161                                 </li>
162                         </ul>
163                 </form>
164                 </div>
165         <?php
166
167                 require_once( 'admin-footer.php' );
168                 exit;
169         }
170         wp_redirect( remove_query_arg( $query_args ) );
171         exit;
172 } elseif ( isset($_GET['remove']) && $_GET['remove'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
173         // Remove from sidebar the widget of type $_GET['remove'] and in position $_GET['key']
174         $key = (int) $_GET['key'];
175         if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['remove']) ) && in_array($key, $keys) ) {
176                 check_admin_referer( "remove-widget_$_GET[remove]" );
177                 unset($sidebars_widgets[$sidebar][$key]);
178                 $sidebars_widgets[$sidebar] = array_values($sidebars_widgets[$sidebar]);
179                 wp_set_sidebars_widgets( $sidebars_widgets );
180         }
181         wp_redirect( remove_query_arg( $query_args ) );
182         exit;
183 } elseif ( isset($_GET['edit']) && $_GET['edit'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
184         // Edit widget of type $_GET['edit'] and position $_GET['key']
185         $key = (int) $_GET['key'];
186         if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['edit']) ) && in_array($key, $keys) )
187                 $edit_widget = $key;
188 }
189
190 // Total number of registered sidebars
191 $sidebar_widget_count = count($sidebars_widgets[$sidebar]);
192
193 // This is sort of lame since "widget" won't be converted to "widgets" in the JS
194 if ( 1 < $sidebars_count = count($wp_registered_sidebars) )
195         $sidebar_info_text = __ngettext( 'You are using %1$s widget in the "%2$s" sidebar.', 'You are using %1$s widgets in the "%2$s" sidebar.', $sidebar_widget_count );
196 else
197         $sidebar_info_text = __ngettext( 'You are using %1$s widget in the sidebar.', 'You are using %1$s widgets in the sidebar.', $sidebar_widget_count );
198
199
200 $sidebar_info_text = sprintf( wp_specialchars( $sidebar_info_text ), "<span id='widget-count'>$sidebar_widget_count</span>", $wp_registered_sidebars[$sidebar]['name'] );
201
202 $page = isset($_GET['apage']) ? abs( (int) $_GET['apage'] ) : 1;
203
204 /* TODO: Paginate widgets list
205 $page_links = paginate_links( array(
206         'base'    => add_query_arg( 'apage', '%#%' ),
207         'format'  => '',
208         'total'   => ceil(($total = 105 )/ 10),
209         'current' => $page
210 ));
211 */
212 $page_links = '&nbsp;';
213
214 // Unsanitized!
215 $widget_search = isset($_GET['s']) ? $_GET['s'] : false;
216
217 // Not entirely sure what all should be here
218 $show_values = array(
219         ''       => $widget_search ? __( 'Show any widgets' ) : __( 'Show all widgets' ),
220         'unused' => __( 'Show unused widgets' ),
221         'used'   => __( 'Show used widgets' )
222 );
223
224 $show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
225
226 $messages = array(
227         'updated' => __('Changes saved.')
228 );
229
230 require_once( 'admin-header.php' ); ?>
231
232 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) : ?>
233 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
234 <?php endif; ?>
235
236 <div class="wrap">
237 <?php screen_icon(); ?>
238 <h2><?php echo wp_specialchars( $title ); ?></h2>
239
240         <form id="widgets-filter" action="" method="get">
241
242         <div class="widget-liquid-left-holder">
243         <div id="available-widgets-filter" class="widget-liquid-left">
244                 <h3><label for="show"><?php _e('Available Widgets'); ?></label></h3>
245                 <div class="nav">
246                         <select name="show" id="show">
247 <?php foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
248                                 <option value='<?php echo $show_value; ?>'<?php selected( $show_value, $show ); ?>><?php echo wp_specialchars( $show_text ); ?></option>
249 <?php endforeach; ?>
250                         </select>
251                         <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
252                         <p class="pagenav">
253                                 <?php echo $page_links; ?>
254                         </p>
255                 </div>
256         </div>
257         </div>
258
259         <div id="available-sidebars" class="widget-liquid-right">
260                 <h3><label for="sidebar-selector"><?php _e('Current Widgets'); ?></label></h3>
261
262                 <div class="nav">
263                         <select id="sidebar-selector" name="sidebar">
264 <?php foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
265                                 <option value='<?php echo $sidebar_id; ?>'<?php selected( $sidebar_id, $sidebar ); ?>><?php echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
266 <?php endforeach; ?>
267                         </select>
268                         <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
269                 </div>
270
271         </div>
272
273         </form>
274
275         <div id="widget-content" class="widget-liquid-left-holder">
276
277                 <div id="available-widgets" class="widget-liquid-left">
278
279                         <?php wp_list_widgets( $show, $widget_search ); // This lists all the widgets for the query ( $show, $search ) ?>
280
281                         <div class="nav">
282                                 <p class="pagenav">
283                                         <?php echo $page_links; ?>
284                                 </p>
285                         </div>
286                 </div>
287         </div>
288
289         <form id="widget-controls" action="" method="post">
290
291         <div id="current-widgets-head" class="widget-liquid-right">
292
293                 <div id="sidebar-info">
294                         <p><?php echo $sidebar_info_text; ?></p>
295                         <p><?php _e( 'Add more from the Available Widgets section.' ); ?></p>
296                 </div>
297
298         </div>
299
300         <div id="current-widgets" class="widget-liquid-right">
301                 <div id="current-sidebar">
302
303                         <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
304
305                 </div>
306
307                 <p class="submit">
308                         <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
309                         <input type="hidden" id="generated-time" name="generated-time" value="<?php echo time() - 1199145600; // Jan 1, 2008 ?>" />
310                         <input type="submit" name="save-widgets" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
311 <?php
312                         wp_nonce_field( 'edit-sidebar_' . $sidebar );
313 ?>
314                 </p>
315         </div>
316
317         </form>
318         <br class="clear" />
319
320 </div>
321
322 <?php do_action( 'sidebar_admin_page' ); ?>
323
324 <?php require_once( 'admin-footer.php' ); ?>
325
326