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