Fix saving and deleting of widgets settings for no-js and for some non-standard widgets, run the actions from the widgets screen when saving with ajax, see #9511

git-svn-id: http://svn.automattic.com/wordpress/trunk@11427 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-05-22 12:08:51 +00:00
parent 54998219dc
commit 0632395ad6
5 changed files with 172 additions and 221 deletions

View File

@ -1282,64 +1282,34 @@ case 'save-widget' :
unset( $_POST['savewidgets'], $_POST['action'] ); unset( $_POST['savewidgets'], $_POST['action'] );
do_action('load-widgets.php');
do_action('widgets.php');
do_action('sidebar_admin_setup');
$id_base = $_POST['id_base']; $id_base = $_POST['id_base'];
$number = isset($_POST['widget_number']) ? $_POST['widget_number'] : ''; $widget_id = $_POST['widget-id'];
$sidebar_id = $_POST['sidebar']; $sidebar_id = $_POST['sidebar'];
$sidebars = wp_get_sidebars_widgets(); $sidebars = wp_get_sidebars_widgets();
$sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array(); $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array();
// delete // delete
if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
$del_id = $_POST['widget-id'];
$widget = isset($wp_registered_widgets[$del_id]) ? $wp_registered_widgets[$del_id] : false;
if ( !in_array($del_id, $sidebar, true) ) if ( !isset($wp_registered_widgets[$widget_id]) )
die('-1'); die('-1');
if ( $widget ) { $sidebar = array_diff( $sidebar, array($widget_id) );
$option = str_replace( '-', '_', 'widget_' . $id_base ); $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
$data = get_option($option); }
$_POST['widget-id'] = $sidebar;
if ( isset($widget['params'][0]['number']) ) {
$number = $widget['params'][0]['number'];
if ( is_array($data) && isset($data[$number]) ) {
unset( $data[$number] );
update_option($option, $data);
}
} else {
if ( $data ) {
$data = array();
update_option($option, $data);
}
}
}
$sidebar = array_diff( $sidebar, array($del_id) );
$sidebars[$sidebar_id] = $sidebar;
wp_set_sidebars_widgets($sidebars);
echo "deleted:$del_id";
die();
}
// save
foreach ( (array) $wp_registered_widget_updates as $name => $control ) { foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
if ( $name == $id_base ) { if ( $name == $id_base ) {
if ( !is_callable( $control['callback'] ) ) if ( !is_callable( $control['callback'] ) )
continue; continue;
if ( $number ) {
// don't delete other instances of the same multi-widget
foreach ( $sidebar as $_widget_id ) {
$_widget = $wp_registered_widgets[$_widget_id];
if ( isset($_widget['params']) &&
is_array($_widget['params'][0]) &&
array_key_exists('number', $_widget['params'][0]) )
unset($wp_registered_widgets[$_widget_id]['params'][0]['number']);
}
}
ob_start(); ob_start();
call_user_func_array( $control['callback'], $control['params'] ); call_user_func_array( $control['callback'], $control['params'] );
ob_end_clean(); ob_end_clean();
@ -1347,6 +1317,13 @@ case 'save-widget' :
} }
} }
if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
$sidebars[$sidebar_id] = $sidebar;
wp_set_sidebars_widgets($sidebars);
echo "deleted:$widget_id";
die();
}
die('1'); die('1');
break; break;
default : default :

View File

@ -240,7 +240,7 @@ div#sidebar-info {
display: block; display: block;
font-size: 11px; font-size: 11px;
font-weight: normal; font-weight: normal;
line-height: 24px; line-height: 26px;
padding: 0 8px 0 0; padding: 0 8px 0 0;
} }
@ -256,7 +256,7 @@ a.widget-control-edit {
#available-widgets .widget-control-edit .add, #available-widgets .widget-control-edit .add,
#widgets-right .widget-control-edit .edit, #widgets-right .widget-control-edit .edit,
#wp_inactive_widgets .widget-control-edit .edit { #wp_inactive_widgets .widget-control-edit .edit {
display: block; display: inline;
} }
.editwidget { .editwidget {

View File

@ -47,8 +47,11 @@ function retrieve_widgets() {
unset( $sidebars_widgets['array_version'] ); unset( $sidebars_widgets['array_version'] );
$diff = array_diff( array_keys($sidebars_widgets), $sidebars ); $old = array_keys($sidebars_widgets);
if ( empty($diff) ) sort($old);
sort($sidebars);
if ( $old == $sidebars )
return; return;
// Move the known-good ones first // Move the known-good ones first
@ -68,6 +71,7 @@ function retrieve_widgets() {
} }
// discard invalid, theme-specific widgets from sidebars // discard invalid, theme-specific widgets from sidebars
$shown_widgets = array();
foreach ( $_sidebars_widgets as $sidebar => $widgets ) { foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
if ( !is_array($widgets) ) if ( !is_array($widgets) )
continue; continue;
@ -78,39 +82,24 @@ function retrieve_widgets() {
$_widgets[] = $widget; $_widgets[] = $widget;
} }
$_sidebars_widgets[$sidebar] = $_widgets; $_sidebars_widgets[$sidebar] = $_widgets;
$shown_widgets = array_merge($shown_widgets, $_widgets);
} }
$sidebars_widgets = $_sidebars_widgets; $sidebars_widgets = $_sidebars_widgets;
unset($_sidebars_widgets, $_widgets); unset($_sidebars_widgets, $_widgets);
// find hidden/lost multi-widget instances // find hidden/lost multi-widget instances
$shown_widgets = array();
foreach ( $sidebars_widgets as $sidebar ) {
if ( is_array($sidebar) )
$shown_widgets = array_merge($shown_widgets, $sidebar);
}
$all_widgets = array();
foreach ( $wp_registered_widget_updates as $key => $val ) {
if ( isset($val['id_base']) )
$all_widgets[] = $val['id_base'];
else
$all_widgets[] = $key;
}
$all_widgets = array_unique($all_widgets);
$lost_widgets = array(); $lost_widgets = array();
foreach ( $all_widgets as $name ) { foreach ( $wp_registered_widgets as $key => $val ) {
$data = get_option( str_replace('-', '_', "widget_$name") ); if ( in_array($key, $shown_widgets, true) )
if ( is_array($data) ) {
foreach ( $data as $num => $value ) {
if ( !is_numeric($num) ) // skip single widgets, some don't delete their settings
continue; continue;
if ( is_array($value) && !in_array("$name-$num", $shown_widgets, true) )
$lost_widgets[] = "$name-$num"; $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
}
} if ( 2 > (int) $number )
continue;
$lost_widgets[] = $key;
} }
$sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']); $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
@ -142,74 +131,59 @@ if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
$widget_id = $_POST['widget-id']; $widget_id = $_POST['widget-id'];
check_admin_referer("save-delete-widget-$widget_id"); check_admin_referer("save-delete-widget-$widget_id");
$sidebar_id = $_POST['insidebar']; $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
if ( $number ) {
foreach ( $_POST as $key => $val ) {
if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
$_POST[$key] = array( $number => array_shift($val) );
break;
}
}
}
$sidebar_id = $_POST['sidebar'];
$position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0; $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
$_POST['sidebar'] = $sidebar_id;
$id_base = $_POST['id_base']; $id_base = $_POST['id_base'];
$number = isset($_POST['multi_number']) ? $_POST['multi_number'] : '';
$sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array(); $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
// delete // delete
if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) { if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
$widget = isset($wp_registered_widgets[$widget_id]) ? $wp_registered_widgets[$widget_id] : false;
if ( !in_array($widget_id, $sidebar, true) || !$widget ) { if ( !in_array($widget_id, $sidebar, true) ) {
wp_redirect('widgets.php?error=0'); wp_redirect('widgets.php?error=0');
exit; exit;
} }
$option = str_replace( '-', '_', 'widget_' . $id_base );
$data = get_option($option);
if ( isset($widget['params'][0]['number']) ) {
$number = $widget['params'][0]['number'];
if ( is_array($data) && isset($data[$number]) ) {
unset( $data[$number] );
update_option($option, $data);
}
} else {
if ( $data ) {
$data = array();
update_option($option, $data);
}
}
$sidebar = array_diff( $sidebar, array($widget_id) ); $sidebar = array_diff( $sidebar, array($widget_id) );
} else { $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
// save }
$_POST['widget-id'] = $sidebar;
foreach ( (array) $wp_registered_widget_updates as $name => $control ) { foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
if ( $name != $id_base || !is_callable($control['callback']) ) if ( $name != $id_base || !is_callable($control['callback']) )
continue; continue;
if ( $number ) {
// don't delete other instances of the same multi-widget
foreach ( $sidebar as $_widget_id ) {
if ( isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) )
unset($wp_registered_widgets[$_widget_id]['params'][0]['number']);
}
$widget_id = "$id_base-$number";
}
ob_start(); ob_start();
call_user_func_array( $control['callback'], $control['params'] ); call_user_func_array( $control['callback'], $control['params'] );
ob_end_clean(); ob_end_clean();
// remove old position
$sidebar = array_diff( $sidebar, array($widget_id) );
foreach ( $sidebars_widgets as $key => $sb ) {
if ( is_array($sb) && in_array($widget_id, $sb, true) )
$sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
}
array_splice( $sidebar, $position, 0, $widget_id );
break; break;
} }
}
$sidebars_widgets[$sidebar_id] = $sidebar; $sidebars_widgets[$sidebar_id] = $sidebar;
wp_set_sidebars_widgets($sidebars_widgets);
// remove old position
if ( !isset($_POST['delete_widget']) ) {
foreach ( $sidebars_widgets as $key => $sb ) {
if ( is_array($sb) )
$sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
}
array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
}
wp_set_sidebars_widgets($sidebars_widgets);
wp_redirect('widgets.php?message=0'); wp_redirect('widgets.php?message=0');
exit; exit;
} }
@ -228,8 +202,8 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
if ( $_GET['base'] === $control['id_base'] ) { if ( $_GET['base'] === $control['id_base'] ) {
$control_callback = $control['callback']; $control_callback = $control['callback'];
$multi_number = (int) $_GET['num']; $multi_number = (int) $_GET['num'];
$control['params'][0]['number'] = $multi_number; $control['params'][0]['number'] = -1;
$control['id'] = $control['id_base'] . '-' . $multi_number; $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
$wp_registered_widget_controls[$control['id']] = $control; $wp_registered_widget_controls[$control['id']] = $control;
break; break;
} }
@ -240,8 +214,13 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) { if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
$control = $wp_registered_widget_controls[$widget_id]; $control = $wp_registered_widget_controls[$widget_id];
$control_callback = $control['callback']; $control_callback = $control['callback'];
} elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
$name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
} }
if ( !isset($name) )
$name = esc_html( strip_tags($control['name']) );
if ( !isset($sidebar) ) if ( !isset($sidebar) )
$sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets'; $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
@ -251,7 +230,6 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
$id_base = isset($control['id_base']) ? $control['id_base'] : $control['id']; $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
// show the widget form // show the widget form
if ( is_callable( $control_callback ) ) {
$width = ' style="width:' . max($control['width'], 350) . 'px"'; $width = ' style="width:' . max($control['width'], 350) . 'px"';
$key = isset($_GET['key']) ? (int) $_GET['key'] : 0; $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
@ -260,18 +238,22 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
<?php screen_icon(); ?> <?php screen_icon(); ?>
<h2><?php echo esc_html( $title ); ?></h2> <h2><?php echo esc_html( $title ); ?></h2>
<div class="editwidget"<?php echo $width; ?>> <div class="editwidget"<?php echo $width; ?>>
<h3><?php printf( __( 'Widget %s' ), esc_html( strip_tags($control['name']) ) ); ?></h3> <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
<form action="widgets.php" method="post"> <form action="widgets.php" method="post">
<div class="widget-inside"> <div class="widget-inside">
<?php call_user_func_array( $control_callback, $control['params'] ); ?> <?php
if ( is_callable( $control_callback ) )
call_user_func_array( $control_callback, $control['params'] );
else
echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
</div> </div>
<p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p> <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
<div class="widget-position"> <div class="widget-position">
<table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody> <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
<?php foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) { <?php foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
echo "\t\t<tr><td><label><input type='radio' name='insidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>"; echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
if ( 'wp_inactive_widgets' == $sbname ) { if ( 'wp_inactive_widgets' == $sbname ) {
echo '&nbsp;'; echo '&nbsp;';
} else { } else {
@ -316,9 +298,6 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
<?php <?php
require_once( 'admin-footer.php' ); require_once( 'admin-footer.php' );
exit; exit;
}
wp_redirect('widgets.php?error=1');
exit;
} }
$messages = array( $messages = array(

View File

@ -759,8 +759,7 @@ class WP_Widget_RSS extends WP_Widget {
function form($instance) { function form($instance) {
if ( empty($instance) ) if ( empty($instance) )
$instance = array( 'number' => '__i__', 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 ); $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
else
$instance['number'] = $this->number; $instance['number'] = $this->number;
wp_widget_rss_form( $instance ); wp_widget_rss_form( $instance );

View File

@ -191,37 +191,32 @@ class WP_Widget {
$all_instances = $this->get_settings(); $all_instances = $this->get_settings();
// We need to update the data // We need to update the data
if ( !$this->updated && !empty($_POST['sidebar']) ) { if ( $this->updated )
return;
// Tells us what sidebar to put the data in
$sidebar = (string) $_POST['sidebar'];
$sidebars_widgets = wp_get_sidebars_widgets(); $sidebars_widgets = wp_get_sidebars_widgets();
if ( isset($sidebars_widgets[$sidebar]) )
$this_sidebar =& $sidebars_widgets[$sidebar];
else
$this_sidebar = array();
if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
// Delete the settings for this instance of the widget // Delete the settings for this instance of the widget
if ( isset($_POST['widget-id']) ) if ( isset($_POST['the-widget-id']) )
$del_id = $_POST['widget-id']; $del_id = $_POST['the-widget-id'];
else else
return; return;
if ( $this->_get_display_callback() == $wp_registered_widgets[$del_id]['callback'] && isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) { if ( isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
$number = $wp_registered_widgets[$del_id]['params'][0]['number']; $number = $wp_registered_widgets[$del_id]['params'][0]['number'];
if ( $this->id_base . '-' . $number == $del_id ) { if ( $this->id_base . '-' . $number == $del_id )
unset($all_instances[$number]); unset($all_instances[$number]);
} }
}
} else { } else {
if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) { if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
$settings = $_POST['widget-' . $this->id_base]; $settings = $_POST['widget-' . $this->id_base];
} else { } elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
$num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number']; $num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
$settings = array( $num => array() ); $settings = array( $num => array() );
} else {
return;
} }
foreach ( $settings as $number => $new_instance ) { foreach ( $settings as $number => $new_instance ) {
@ -237,13 +232,14 @@ class WP_Widget {
$instance = apply_filters('widget_update_callback', $instance, $new_instance, $this); $instance = apply_filters('widget_update_callback', $instance, $new_instance, $this);
if ( false !== $instance ) if ( false !== $instance )
$all_instances[$number] = $instance; $all_instances[$number] = $instance;
break; // run only once
} }
} }
$this->save_settings($all_instances); $this->save_settings($all_instances);
$this->updated = true; $this->updated = true;
} }
}
/** Generate the control form. /** Generate the control form.
* Do NOT over-ride this function. */ * Do NOT over-ride this function. */