Customize: Fix php warning due to `WP_Customize_Manager::prepare_setting_validity_for_js()` incorrectly assuming that `WP_Error` will only ever have arrays in its `$error_data`.

* Eliminates the server mutating the a `WP_Error`'s `$error_data` to merge-in a `$from_server` flag (since it may not be an array to begin with). Instead it defers to the client to add a `fromServer` param on any `Notification` instances created from server-sent errors.
* Ensures that notifications will be re-rendered if a notification's `message` changes but the `data` and `type` remain the same.
* Adds explicit support for the `Notification` class to have a `setting` property, ensuring that the property is set whereas previously it was dropped.

Fixes #37890.
Props westonruter, dlh.

Built from https://develop.svn.wordpress.org/trunk@38513


git-svn-id: http://core.svn.wordpress.org/trunk@38454 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2016-09-02 22:35:44 +00:00
parent 2559ade0ae
commit 91f583c9af
6 changed files with 28 additions and 23 deletions

View File

@ -3461,12 +3461,13 @@
// Add notifications for invalidities. // Add notifications for invalidities.
if ( _.isObject( validity ) ) { if ( _.isObject( validity ) ) {
_.each( validity, function( params, code ) { _.each( validity, function( params, code ) {
var notification = new api.Notification( code, params ), existingNotification, needsReplacement = false; var notification, existingNotification, needsReplacement = false;
notification = new api.Notification( code, _.extend( { fromServer: true }, params ) );
// Remove existing notification if already exists for code but differs in parameters. // Remove existing notification if already exists for code but differs in parameters.
existingNotification = setting.notifications( notification.code ); existingNotification = setting.notifications( notification.code );
if ( existingNotification ) { if ( existingNotification ) {
needsReplacement = ( notification.type !== existingNotification.type ) || ! _.isEqual( notification.data, existingNotification.data ); needsReplacement = notification.type !== existingNotification.type || notification.message !== existingNotification.message || ! _.isEqual( notification.data, existingNotification.data );
} }
if ( needsReplacement ) { if ( needsReplacement ) {
setting.notifications.remove( code ); setting.notifications.remove( code );
@ -3715,7 +3716,7 @@
*/ */
api.each( function( setting ) { api.each( function( setting ) {
setting.notifications.each( function( notification ) { setting.notifications.each( function( notification ) {
if ( 'error' === notification.type && ( ! notification.data || ! notification.data.from_server ) ) { if ( 'error' === notification.type && ! notification.fromServer ) {
invalidSettings.push( setting.id ); invalidSettings.push( setting.id );
} }
} ); } );

File diff suppressed because one or more lines are too long

View File

@ -1046,17 +1046,9 @@ final class WP_Customize_Manager {
if ( is_wp_error( $validity ) ) { if ( is_wp_error( $validity ) ) {
$notification = array(); $notification = array();
foreach ( $validity->errors as $error_code => $error_messages ) { foreach ( $validity->errors as $error_code => $error_messages ) {
$error_data = $validity->get_error_data( $error_code );
if ( is_null( $error_data ) ) {
$error_data = array();
}
$error_data = array_merge(
$error_data,
array( 'from_server' => true )
);
$notification[ $error_code ] = array( $notification[ $error_code ] = array(
'message' => join( ' ', $error_messages ), 'message' => join( ' ', $error_messages ),
'data' => $error_data, 'data' => $validity->get_error_data( $error_code ),
); );
} }
return $notification; return $notification;

View File

@ -762,18 +762,30 @@ window.wp = window.wp || {};
* @augments wp.customize.Class * @augments wp.customize.Class
* @since 4.6.0 * @since 4.6.0
* *
* @param {string} code The error code. * @param {string} code - The error code.
* @param {object} params Params. * @param {object} params - Params.
* @param {string} params.message The error message. * @param {string} params.message=null - The error message.
* @param {string} [params.type=error] The notification type. * @param {string} [params.type=error] - The notification type.
* @param {*} [params.data] Any additional data. * @param {boolean} [params.fromServer=false] - Whether the notification was server-sent.
* @param {string} [params.setting=null] - The setting ID that the notification is related to.
* @param {*} [params.data=null] - Any additional data.
*/ */
api.Notification = api.Class.extend({ api.Notification = api.Class.extend({
initialize: function( code, params ) { initialize: function( code, params ) {
var _params;
this.code = code; this.code = code;
this.message = params.message; _params = _.extend(
this.type = params.type || 'error'; {
this.data = params.data || null; message: null,
type: 'error',
fromServer: false,
data: null,
setting: null
},
params
);
delete _params.code;
_.extend( this, _params );
} }
}); });

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38512'; $wp_version = '4.7-alpha-38513';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.