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