3 * Widgets administration panel.
6 * @subpackage Administration
9 /** WordPress Administration Bootstrap */
10 require_once( 'admin.php' );
12 /** WordPress Administration Widgets API */
13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
15 if ( ! current_user_can('switch_themes') )
16 wp_die( __( 'Cheatin’ uh?' ));
18 wp_admin_css( 'widgets' );
20 $widgets_access = get_user_setting( 'widgets_access' );
21 if ( isset($_GET['widgets-access']) ) {
22 $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
23 set_user_setting( 'widgets_access', $widgets_access );
26 if ( 'on' == $widgets_access )
27 add_filter( 'admin_body_class', create_function('', '{return " widgets_access ";}') );
29 wp_enqueue_script('admin-widgets');
31 do_action( 'sidebar_admin_setup' );
33 $title = __( 'Widgets' );
34 $parent_file = 'themes.php';
36 // register the inactive_widgets area as sidebar
37 register_sidebar(array(
38 'name' => __('Inactive Widgets'),
39 'id' => 'wp_inactive_widgets',
41 'before_widget' => '',
47 // These are the widgets grouped by sidebar
48 $sidebars_widgets = wp_get_sidebars_widgets();
49 if ( empty( $sidebars_widgets ) )
50 $sidebars_widgets = wp_get_widget_defaults();
52 // look for "lost" widgets, this has to run at least on each theme change
53 function retrieve_widgets() {
54 global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
56 $_sidebars_widgets = array();
57 $sidebars = array_keys($wp_registered_sidebars);
59 unset( $sidebars_widgets['array_version'] );
61 $old = array_keys($sidebars_widgets);
65 if ( $old == $sidebars )
68 // Move the known-good ones first
69 foreach ( $sidebars as $id ) {
70 if ( array_key_exists( $id, $sidebars_widgets ) ) {
71 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
72 unset($sidebars_widgets[$id], $sidebars[$id]);
76 // if new theme has less sidebars than the old theme
77 if ( !empty($sidebars_widgets) ) {
78 foreach ( $sidebars_widgets as $lost => $val ) {
80 $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
84 // discard invalid, theme-specific widgets from sidebars
85 $shown_widgets = array();
86 foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
87 if ( !is_array($widgets) )
91 foreach ( $widgets as $widget ) {
92 if ( isset($wp_registered_widgets[$widget]) )
93 $_widgets[] = $widget;
95 $_sidebars_widgets[$sidebar] = $_widgets;
96 $shown_widgets = array_merge($shown_widgets, $_widgets);
99 $sidebars_widgets = $_sidebars_widgets;
100 unset($_sidebars_widgets, $_widgets);
102 // find hidden/lost multi-widget instances
103 $lost_widgets = array();
104 foreach ( $wp_registered_widgets as $key => $val ) {
105 if ( in_array($key, $shown_widgets, true) )
108 $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
110 if ( 2 > (int) $number )
113 $lost_widgets[] = $key;
116 $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
117 wp_set_sidebars_widgets($sidebars_widgets);
121 if ( count($wp_registered_sidebars) == 1 ) {
122 // If only "wp_inactive_widgets" is defined the theme has no sidebars, die.
123 require_once( 'admin-header.php' );
127 <?php screen_icon(); ?>
128 <h2><?php echo esc_html( $title ); ?></h2>
130 <p><?php _e( 'No Sidebars Defined' ); ?></p>
132 <p><?php _e( '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://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ); ?></p>
136 require_once( 'admin-footer.php' );
140 // We're saving a widget without js
141 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
142 $widget_id = $_POST['widget-id'];
143 check_admin_referer("save-delete-widget-$widget_id");
145 $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
147 foreach ( $_POST as $key => $val ) {
148 if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
149 $_POST[$key] = array( $number => array_shift($val) );
155 $sidebar_id = $_POST['sidebar'];
156 $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
158 $id_base = $_POST['id_base'];
159 $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
162 if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
164 if ( !in_array($widget_id, $sidebar, true) ) {
165 wp_redirect('widgets.php?error=0');
169 $sidebar = array_diff( $sidebar, array($widget_id) );
170 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
173 $_POST['widget-id'] = $sidebar;
175 foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
176 if ( $name != $id_base || !is_callable($control['callback']) )
180 call_user_func_array( $control['callback'], $control['params'] );
186 $sidebars_widgets[$sidebar_id] = $sidebar;
188 // remove old position
189 if ( !isset($_POST['delete_widget']) ) {
190 foreach ( $sidebars_widgets as $key => $sb ) {
192 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
194 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
197 wp_set_sidebars_widgets($sidebars_widgets);
198 wp_redirect('widgets.php?message=0');
202 // Output the widget form without js
203 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
204 $widget_id = $_GET['editwidget'];
206 if ( isset($_GET['addnew']) ) {
207 // Default to the first sidebar
208 $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
210 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
211 // Copy minimal info from an existing instance of this widget to a new instance
212 foreach ( $wp_registered_widget_controls as $control ) {
213 if ( $_GET['base'] === $control['id_base'] ) {
214 $control_callback = $control['callback'];
215 $multi_number = (int) $_GET['num'];
216 $control['params'][0]['number'] = -1;
217 $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
218 $wp_registered_widget_controls[$control['id']] = $control;
225 if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
226 $control = $wp_registered_widget_controls[$widget_id];
227 $control_callback = $control['callback'];
228 } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
229 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
233 $name = esc_html( strip_tags($control['name']) );
235 if ( !isset($sidebar) )
236 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
238 if ( !isset($multi_number) )
239 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
241 $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
243 // show the widget form
244 $width = ' style="width:' . max($control['width'], 350) . 'px"';
245 $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
247 require_once( 'admin-header.php' ); ?>
249 <?php screen_icon(); ?>
250 <h2><?php echo esc_html( $title ); ?></h2>
251 <div class="editwidget"<?php echo $width; ?>>
252 <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
254 <form action="widgets.php" method="post">
255 <div class="widget-inside">
257 if ( is_callable( $control_callback ) )
258 call_user_func_array( $control_callback, $control['params'] );
260 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
263 <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
264 <div class="widget-position">
265 <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
267 foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
268 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
269 if ( 'wp_inactive_widgets' == $sbname ) {
272 if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
274 $sidebars_widgets[$sbname] = array();
276 $j = count($sidebars_widgets[$sbname]);
277 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
281 echo "\t\t<select name='{$sbname}_position'>\n";
282 echo "\t\t<option value=''>" . __('-- select --') . "</option>\n";
283 for ( $i = 1; $i <= $j; $i++ ) {
284 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
285 $selected = selected( $i, $key + 1, false );
286 echo "\t\t<option value='$i'$selected> $i </option>\n";
288 echo "\t\t</select>\n";
295 <div class="widget-control-actions">
296 <?php if ( isset($_GET['addnew']) ) { ?>
297 <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
299 <input type="submit" name="removewidget" class="button alignleft" value="<?php esc_attr_e('Delete'); ?>" />
301 <input type="submit" name="savewidget" class="button-primary alignright" value="<?php esc_attr_e('Save Widget'); ?>" />
302 <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
303 <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
304 <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
305 <?php wp_nonce_field("save-delete-widget-$widget_id"); ?>
312 require_once( 'admin-footer.php' );
321 __('Error while saving.'),
322 __('Error in displaying the widget settings form.')
325 require_once( 'admin-header.php' ); ?>
328 <?php screen_icon(); ?>
329 <h2><?php echo esc_html( $title ); ?></h2>
331 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
332 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
334 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
335 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
338 <div class="widget-liquid-left">
339 <div id="widgets-left">
340 <div id="available-widgets" class="widgets-holder-wrap">
341 <div class="sidebar-name">
342 <div class="sidebar-name-arrow"><br /></div>
343 <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _e('Deactivate'); ?> <span></span></span></h3></div>
344 <div class="widget-holder">
345 <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>
346 <div id="widget-list">
347 <?php wp_list_widgets(); ?>
354 <div class="widgets-holder-wrap">
355 <div class="sidebar-name">
356 <div class="sidebar-name-arrow"><br /></div>
357 <h3><?php _e('Inactive Widgets'); ?>
358 <span><img src="images/wpspin_light.gif" class="ajax-feedback" title="" alt="" /></span></h3></div>
359 <div class="widget-holder inactive">
360 <p class="description"><?php _e('Drag widgets here to remove them from the sidebar but keep their settings.'); ?></p>
361 <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
368 <div class="widget-liquid-right">
369 <div id="widgets-right">
372 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
373 if ( 'wp_inactive_widgets' == $sidebar )
375 $closed = $i ? ' closed' : ''; ?>
376 <div class="widgets-holder-wrap<?php echo $closed; ?>">
377 <div class="sidebar-name">
378 <div class="sidebar-name-arrow"><br /></div>
379 <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
380 <span><img src="images/wpspin_dark.gif" class="ajax-feedback" title="" alt="" /></span></h3></div>
381 <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
388 <form action="" method="post">
389 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
395 do_action( 'sidebar_admin_page' );
396 require_once( 'admin-footer.php' );