3 require_once( 'admin.php' );
4 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
6 if ( ! current_user_can('switch_themes') )
7 wp_die( __( 'Cheatin’ uh?' ));
9 wp_enqueue_script( array( 'wp-lists', 'admin-widgets' ) );
11 do_action( 'sidebar_admin_setup' );
13 $title = __( 'Widgets' );
14 $parent_file = 'themes.php';
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) );
23 // If no sidebars, die.
24 require_once( 'admin-header.php' );
28 <p><?php _e( 'No Sidebars Defined' ); ?></p>
32 <p><?php _e( 'You are seeing this message because the theme you are currently using isn’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>
36 require_once( 'admin-footer.php' );
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();
45 // for the sake of PHP warnings
46 if ( empty( $sidebars_widgets[$sidebar] ) )
47 $sidebars_widgets[$sidebar] = array();
49 $http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
51 // We're updating a sidebar
52 if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
53 check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
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,
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.
63 $already_done = array();
65 foreach ( $wp_registered_widget_controls as $name => $control ) {
66 if ( in_array( $control['callback'], $already_done ) )
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'];
80 // Prophylactic. Take out empty ids.
81 foreach ( (array) $_POST['widget-id'] as $key => $val )
83 unset($_POST['widget-id'][$key]);
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 );
90 wp_redirect( add_query_arg( 'message', 'updated' ) );
97 // What widget (if any) are we editing
100 $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
102 if ( isset($_GET['add']) && $_GET['add'] ) {
103 // Add to the end of the sidebar
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'];
125 // it's a multi-widget. The only way to add multi-widgets without JS is to actually submit POST content...
127 if ( is_callable( $control_callback ) ) {
128 require_once( 'admin-header.php' );
131 <h2><?php _e( 'Add Widget' ); ?></h2>
133 <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
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;">
140 call_user_func_array( $control_callback, $control['params'] );
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; ?>" />
157 require_once( 'admin-footer.php' );
160 wp_redirect( remove_query_arg( $query_args ) );
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 );
171 wp_redirect( remove_query_arg( $query_args ) );
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) )
180 // Total number of registered sidebars
181 $sidebar_widget_count = count($sidebars_widgets[$sidebar]);
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 );
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 );
190 $sidebar_info_text = sprintf( wp_specialchars( $sidebar_info_text ), "<span id='widget-count'>$sidebar_widget_count</span>", $wp_registered_sidebars[$sidebar]['name'] );
192 $page = isset($_GET['apage']) ? abs( (int) $_GET['apage'] ) : 1;
194 /* TODO: Paginate widgets list
195 $page_links = paginate_links( array(
196 'base' => add_query_arg( 'apage', '%#%' ),
198 'total' => ceil(($total = 105 )/ 10),
202 $page_links = ' ';
205 $widget_search = isset($_GET['s']) ? $_GET['s'] : false;
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' )
214 $show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
218 'updated' => __('Changes saved.')
221 require_once( 'admin-header.php' );
223 if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) : ?>
225 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
231 <form id="widgets-filter" action="" method="get">
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' ); ?>" />
239 <div class="widget-liquid-left-holder">
240 <div id="available-widgets-filter" class="widget-liquid-left">
241 <h3><?php _e('Available Widgets'); ?></h3>
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>
248 <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
250 <?php echo $page_links; ?>
256 <div id="available-sidebars" class="widget-liquid-right">
257 <h3><?php _e('Current Widgets'); ?></h3>
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>
265 <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
272 <div id="widget-content" class="widget-liquid-left-holder">
274 <div id="available-widgets" class="widget-liquid-left">
276 <?php wp_list_widgets( $show, $widget_search ); // This lists all the widgets for the query ( $show, $search ) ?>
280 <?php echo $page_links; ?>
286 <form id="widget-controls" action="" method="post">
288 <div id="current-widgets-head" class="widget-liquid-right">
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>
297 <div id="current-widgets" class="widget-liquid-right">
298 <div id="current-sidebar">
300 <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
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' ); ?>" />
309 wp_nonce_field( 'edit-sidebar_' . $sidebar );
318 <?php do_action( 'sidebar_admin_page' ); ?>
322 <?php require_once( 'admin-footer.php' ); ?>