Widget Customizer: Improve error handling. First pass.

* Replace Widget_Customizer_Exception with WP_Error
* Call Previewer.cheatin() to show the cheating message if a user can't change widgets
* Call Previewer.login() to show the login form if a user is logged out
* Show a generic error message on failures

see #27419.
Built from https://develop.svn.wordpress.org/trunk@27652


git-svn-id: http://core.svn.wordpress.org/trunk@27495 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2014-03-22 20:55:18 +00:00
parent 04cb00c9e1
commit 9b0ec406d1
3 changed files with 152 additions and 149 deletions

View File

@ -16,7 +16,8 @@ var WidgetCustomizer = ( function ($) {
save_btn_label: '', save_btn_label: '',
save_btn_tooltip: '', save_btn_tooltip: '',
remove_btn_label: '', remove_btn_label: '',
remove_btn_tooltip: '' remove_btn_tooltip: '',
error: '',
}, },
available_widgets: [], // available widgets for instantiating available_widgets: [], // available widgets for instantiating
registered_widgets: [], // all widgets registered registered_widgets: [], // all widgets registered
@ -1172,6 +1173,9 @@ var WidgetCustomizer = ( function ($) {
widget_content = control.container.find( '.widget-content' ); widget_content = control.container.find( '.widget-content' );
// Remove a previous error message
widget_content.find( '.widget-error' ).remove();
// @todo Support more selectors than IDs? // @todo Support more selectors than IDs?
if ( $.contains( control.container[0], document.activeElement ) && $( document.activeElement ).is( '[id]' ) ) { if ( $.contains( control.container[0], document.activeElement ) && $( document.activeElement ).is( '[id]' ) ) {
element_id_to_refocus = $( document.activeElement ).prop( 'id' ); element_id_to_refocus = $( document.activeElement ).prop( 'id' );
@ -1219,6 +1223,22 @@ var WidgetCustomizer = ( function ($) {
has_same_inputs_in_response, has_same_inputs_in_response,
is_instance_identical; is_instance_identical;
// Check if the user is logged out.
if ( '0' === r ) {
self.previewer.preview.iframe.hide();
self.previewer.login().done( function() {
control.updateWidget( args );
self.previewer.preview.iframe.show();
} );
return;
}
// Check for cheaters.
if ( '-1' === r ) {
self.previewer.cheatin();
return;
}
if ( r.success ) { if ( r.success ) {
sanitized_form = $( '<div>' + r.data.form + '</div>' ); sanitized_form = $( '<div>' + r.data.form + '</div>' );
@ -1274,9 +1294,7 @@ var WidgetCustomizer = ( function ($) {
* preview finishing loading. * preview finishing loading.
*/ */
is_instance_identical = _( control.setting() ).isEqual( r.data.instance ); is_instance_identical = _( control.setting() ).isEqual( r.data.instance );
if ( is_instance_identical ) { if ( ! is_instance_identical ) {
control.container.removeClass( 'previewer-loading' );
} else {
control.is_widget_updating = true; // suppress triggering another updateWidget control.is_widget_updating = true; // suppress triggering another updateWidget
control.setting( r.data.instance ); control.setting( r.data.instance );
control.is_widget_updating = false; control.is_widget_updating = false;
@ -1286,26 +1304,24 @@ var WidgetCustomizer = ( function ($) {
complete_callback.call( control, null, { no_change: is_instance_identical, ajax_finished: true } ); complete_callback.call( control, null, { no_change: is_instance_identical, ajax_finished: true } );
} }
} else { } else {
window.console && window.console.log( r ); message = self.i18n.error;
message = 'FAIL';
if ( r.data && r.data.message ) { if ( r.data && r.data.message ) {
message = r.data.message; message = r.data.message;
} }
if ( complete_callback ) { if ( complete_callback ) {
complete_callback.call( control, message ); complete_callback.call( control, message );
} else { } else {
throw new Error( message ); widget_content.prepend( '<p class="widget-error"><strong>' + message + '</strong></p>' );
} }
} }
} ); } );
jqxhr.fail( function ( jqXHR, textStatus ) { jqxhr.fail( function ( jqXHR, textStatus ) {
if ( complete_callback ) { if ( complete_callback ) {
complete_callback.call( control, textStatus ); complete_callback.call( control, textStatus );
} else {
throw new Error( textStatus );
} }
} ); } );
jqxhr.always( function () { jqxhr.always( function () {
control.container.removeClass( 'previewer-loading' );
control.container.removeClass( 'widget-form-loading' ); control.container.removeClass( 'widget-form-loading' );
inputs.each( function () { inputs.each( function () {
$( this ).removeData( 'state' + update_number ); $( this ).removeData( 'state' + update_number );

File diff suppressed because one or more lines are too long

View File

@ -424,17 +424,14 @@ class WP_Customize_Widgets {
/** /**
* Convert a widget setting ID (option path) to its id_base and number components * Convert a widget setting ID (option path) to its id_base and number components
* *
* @throws Widget_Customizer_Exception
* @throws Exception
*
* @param string $setting_id * @param string $setting_id
* @param array * @return WP_Error|array
* @return array
*/ */
static function parse_widget_setting_id( $setting_id ) { static function parse_widget_setting_id( $setting_id ) {
if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) {
throw new Widget_Customizer_Exception( sprintf( 'Invalid widget setting ID: %s', $setting_id ) ); return new WP_Error( 'invalid_setting_id', 'Invalid widget setting ID' );
} }
$id_base = $matches[2]; $id_base = $matches[2];
$number = isset( $matches[3] ) ? intval( $matches[3] ) : null; $number = isset( $matches[3] ) ? intval( $matches[3] ) : null;
return compact( 'id_base', 'number' ); return compact( 'id_base', 'number' );
@ -500,6 +497,7 @@ class WP_Customize_Widgets {
'save_btn_tooltip' => ( 'Save and preview changes before publishing them.' ), 'save_btn_tooltip' => ( 'Save and preview changes before publishing them.' ),
'remove_btn_label' => __( 'Remove' ), 'remove_btn_label' => __( 'Remove' ),
'remove_btn_tooltip' => ( 'Trash widget by moving it to the inactive widgets sidebar.' ), 'remove_btn_tooltip' => ( 'Trash widget by moving it to the inactive widgets sidebar.' ),
'error' => __('An error has occurred. Please reload the page and try again.'),
), ),
'tpl' => array( 'tpl' => array(
'widget_reorder_nav' => $widget_reorder_nav_tpl, 'widget_reorder_nav' => $widget_reorder_nav_tpl,
@ -912,108 +910,106 @@ class WP_Customize_Widgets {
* Find and invoke the widget update and control callbacks. Requires that * Find and invoke the widget update and control callbacks. Requires that
* $_POST be populated with the instance data. * $_POST be populated with the instance data.
* *
* @throws Widget_Customizer_Exception * @param string $widget_id
* @throws Exception * @return WP_Error|array
*
* @param string $widget_id
* @return array
*/ */
static function call_widget_update( $widget_id ) { static function call_widget_update( $widget_id ) {
global $wp_registered_widget_updates, $wp_registered_widget_controls; global $wp_registered_widget_updates, $wp_registered_widget_controls;
$options_transaction = new Options_Transaction(); $options_transaction = new Options_Transaction();
try { $options_transaction->start();
$options_transaction->start(); $parsed_id = self::parse_widget_id( $widget_id );
$parsed_id = self::parse_widget_id( $widget_id ); $option_name = 'widget_' . $parsed_id['id_base'];
$option_name = 'widget_' . $parsed_id['id_base'];
/** /**
* If a previously-sanitized instance is provided, populate the input vars * If a previously-sanitized instance is provided, populate the input vars
* with its values so that the widget update callback will read this instance * with its values so that the widget update callback will read this instance
*/ */
$added_input_vars = array(); $added_input_vars = array();
if ( ! empty( $_POST['sanitized_widget_setting'] ) ) { if ( ! empty( $_POST['sanitized_widget_setting'] ) ) {
$sanitized_widget_setting = json_decode( self::get_post_value( 'sanitized_widget_setting' ), true ); $sanitized_widget_setting = json_decode( self::get_post_value( 'sanitized_widget_setting' ), true );
if ( empty( $sanitized_widget_setting ) ) { if ( empty( $sanitized_widget_setting ) ) {
throw new Widget_Customizer_Exception( 'Malformed sanitized_widget_setting' ); $options_transaction->rollback();
} return new WP_Error( 'malformed_data', 'Malformed sanitized_widget_setting' );
$instance = self::sanitize_widget_instance( $sanitized_widget_setting ); }
if ( is_null( $instance ) ) {
throw new Widget_Customizer_Exception( 'Unsanitary sanitized_widget_setting' ); $instance = self::sanitize_widget_instance( $sanitized_widget_setting );
} if ( is_null( $instance ) ) {
if ( ! is_null( $parsed_id['number'] ) ) { $options_transaction->rollback();
$value = array(); return new WP_Error( 'unsanitary_data', 'Unsanitary sanitized_widget_setting' );
$value[$parsed_id['number']] = $instance; }
$key = 'widget-' . $parsed_id['id_base'];
if ( ! is_null( $parsed_id['number'] ) ) {
$value = array();
$value[$parsed_id['number']] = $instance;
$key = 'widget-' . $parsed_id['id_base'];
$_REQUEST[$key] = $_POST[$key] = wp_slash( $value );
$added_input_vars[] = $key;
} else {
foreach ( $instance as $key => $value ) {
$_REQUEST[$key] = $_POST[$key] = wp_slash( $value ); $_REQUEST[$key] = $_POST[$key] = wp_slash( $value );
$added_input_vars[] = $key; $added_input_vars[] = $key;
} else {
foreach ( $instance as $key => $value ) {
$_REQUEST[$key] = $_POST[$key] = wp_slash( $value );
$added_input_vars[] = $key;
}
} }
} }
/**
* Invoke the widget update callback
*/
foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
if ( $name === $parsed_id['id_base'] && is_callable( $control['callback'] ) ) {
ob_start();
call_user_func_array( $control['callback'], $control['params'] );
ob_end_clean();
break;
}
}
// Clean up any input vars that were manually added
foreach ( $added_input_vars as $key ) {
unset( $_POST[$key] );
unset( $_REQUEST[$key] );
}
/**
* Make sure the expected option was updated
*/
if ( 0 !== $options_transaction->count() ) {
if ( count( $options_transaction->options ) > 1 ) {
throw new Widget_Customizer_Exception( sprintf( 'Widget %1$s unexpectedly updated more than one option.', $widget_id ) );
}
$updated_option_name = key( $options_transaction->options );
if ( $updated_option_name !== $option_name ) {
throw new Widget_Customizer_Exception( sprintf( 'Widget %1$s updated option "%2$s", but expected "%3$s".', $widget_id, $updated_option_name, $option_name ) );
}
}
/**
* Obtain the widget control with the updated instance in place
*/
ob_start();
$form = $wp_registered_widget_controls[$widget_id];
if ( $form ) {
call_user_func_array( $form['callback'], $form['params'] );
}
$form = ob_get_clean();
/**
* Obtain the widget instance
*/
$option = get_option( $option_name );
if ( null !== $parsed_id['number'] ) {
$instance = $option[$parsed_id['number']];
} else {
$instance = $option;
}
$options_transaction->rollback();
return compact( 'instance', 'form' );
} }
catch ( Exception $e ) {
$options_transaction->rollback(); /**
throw $e; * Invoke the widget update callback
*/
foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
if ( $name === $parsed_id['id_base'] && is_callable( $control['callback'] ) ) {
ob_start();
call_user_func_array( $control['callback'], $control['params'] );
ob_end_clean();
break;
}
} }
// Clean up any input vars that were manually added
foreach ( $added_input_vars as $key ) {
unset( $_POST[$key] );
unset( $_REQUEST[$key] );
}
/**
* Make sure the expected option was updated
*/
if ( 0 !== $options_transaction->count() ) {
if ( count( $options_transaction->options ) > 1 ) {
$options_transaction->rollback();
return new WP_Error( 'unexpected_update', 'Widget unexpectedly updated more than one option.' );
}
$updated_option_name = key( $options_transaction->options );
if ( $updated_option_name !== $option_name ) {
$options_transaction->rollback();
return new WP_Error( 'wrong_option', sprintf( 'Widget updated option "%1$s", but expected "%2$s".', $updated_option_name, $option_name ) );
}
}
/**
* Obtain the widget control with the updated instance in place
*/
ob_start();
$form = $wp_registered_widget_controls[$widget_id];
if ( $form ) {
call_user_func_array( $form['callback'], $form['params'] );
}
$form = ob_get_clean();
/**
* Obtain the widget instance
*/
$option = get_option( $option_name );
if ( null !== $parsed_id['number'] ) {
$instance = $option[$parsed_id['number']];
} else {
$instance = $option;
}
$options_transaction->rollback();
return compact( 'instance', 'form' );
} }
/** /**
@ -1026,53 +1022,47 @@ class WP_Customize_Widgets {
* @action wp_ajax_update_widget * @action wp_ajax_update_widget
*/ */
static function wp_ajax_update_widget() { static function wp_ajax_update_widget() {
$generic_error = __( 'An error has occurred. Please reload the page and try again.' );
try { if ( ! is_user_logged_in() ) {
if ( ! check_ajax_referer( self::UPDATE_WIDGET_AJAX_ACTION, self::UPDATE_WIDGET_NONCE_POST_KEY, false ) ) { wp_die( 0 );
throw new Widget_Customizer_Exception( ( 'Nonce check failed. Reload and try again?' ) );
}
if ( ! current_user_can( 'edit_theme_options' ) ) {
throw new Widget_Customizer_Exception( ( 'Current user cannot!' ) ); // @todo translate
}
if ( ! isset( $_POST['widget-id'] ) ) {
throw new Widget_Customizer_Exception( ( 'Incomplete request' ) ); // @todo translate
}
unset( $_POST[self::UPDATE_WIDGET_NONCE_POST_KEY], $_POST['action'] );
do_action( 'load-widgets.php' );
do_action( 'widgets.php' );
do_action( 'sidebar_admin_setup' );
$widget_id = self::get_post_value( 'widget-id' );
$parsed_id = self::parse_widget_id( $widget_id );
$id_base = $parsed_id['id_base'];
if ( isset( $_POST['widget-' . $id_base] ) && is_array( $_POST['widget-' . $id_base] ) && preg_match( '/__i__|%i%/', key( $_POST['widget-' . $id_base] ) ) ) {
throw new Widget_Customizer_Exception( 'Cannot pass widget templates to create new instances; apply template vars in JS' );
}
$updated_widget = self::call_widget_update( $widget_id ); // => {instance,form}
$form = $updated_widget['form'];
$instance = self::sanitize_widget_js_instance( $updated_widget['instance'] );
wp_send_json_success( compact( 'form', 'instance' ) );
} }
catch( Exception $e ) {
if ( $e instanceof Widget_Customizer_Exception ) { check_ajax_referer( self::UPDATE_WIDGET_AJAX_ACTION, self::UPDATE_WIDGET_NONCE_POST_KEY );
$message = $e->getMessage();
} else { if ( ! current_user_can( 'edit_theme_options' ) ) {
error_log( sprintf( '%s in %s: %s', get_class( $e ), __FUNCTION__, $e->getMessage() ) ); wp_die( -1 );
$message = $generic_error;
}
wp_send_json_error( compact( 'message' ) );
} }
if ( ! isset( $_POST['widget-id'] ) ) {
wp_send_json_error();
}
unset( $_POST[self::UPDATE_WIDGET_NONCE_POST_KEY], $_POST['action'] );
do_action( 'load-widgets.php' );
do_action( 'widgets.php' );
do_action( 'sidebar_admin_setup' );
$widget_id = self::get_post_value( 'widget-id' );
$parsed_id = self::parse_widget_id( $widget_id );
$id_base = $parsed_id['id_base'];
if ( isset( $_POST['widget-' . $id_base] ) && is_array( $_POST['widget-' . $id_base] ) && preg_match( '/__i__|%i%/', key( $_POST['widget-' . $id_base] ) ) ) {
wp_send_json_error();
}
$updated_widget = self::call_widget_update( $widget_id ); // => {instance,form}
if ( is_wp_error( $updated_widget ) ) {
wp_send_json_error();
}
$form = $updated_widget['form'];
$instance = self::sanitize_widget_js_instance( $updated_widget['instance'] );
wp_send_json_success( compact( 'form', 'instance' ) );
} }
} }
class Widget_Customizer_Exception extends Exception {}
class Options_Transaction { class Options_Transaction {
/** /**
@ -1204,9 +1194,6 @@ class Options_Transaction {
else if ( 'update' === $option_operation['operation'] ) { else if ( 'update' === $option_operation['operation'] ) {
update_option( $option_operation['option_name'], $option_operation['old_value'] ); update_option( $option_operation['option_name'], $option_operation['old_value'] );
} }
else {
throw new Exception( 'Unexpected operation' );
}
} }
$this->_is_current = false; $this->_is_current = false;
} }