diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index 320f892781..631df0b57c 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -3461,12 +3461,13 @@ // Add notifications for invalidities. if ( _.isObject( validity ) ) { _.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. existingNotification = setting.notifications( notification.code ); 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 ) { setting.notifications.remove( code ); @@ -3715,7 +3716,7 @@ */ api.each( function( setting ) { 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 ); } } ); diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 5ee3b6d90d..668989b574 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -1046,17 +1046,9 @@ final class WP_Customize_Manager { if ( is_wp_error( $validity ) ) { $notification = array(); 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( 'message' => join( ' ', $error_messages ), - 'data' => $error_data, + 'data' => $validity->get_error_data( $error_code ), ); } return $notification; diff --git a/src/wp-includes/js/customize-base.js b/src/wp-includes/js/customize-base.js index e59f926594..20bb74a199 100644 --- a/src/wp-includes/js/customize-base.js +++ b/src/wp-includes/js/customize-base.js @@ -762,18 +762,30 @@ window.wp = window.wp || {}; * @augments wp.customize.Class * @since 4.6.0 * - * @param {string} code The error code. - * @param {object} params Params. - * @param {string} params.message The error message. - * @param {string} [params.type=error] The notification type. - * @param {*} [params.data] Any additional data. + * @param {string} code - The error code. + * @param {object} params - Params. + * @param {string} params.message=null - The error message. + * @param {string} [params.type=error] - The notification type. + * @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({ initialize: function( code, params ) { + var _params; this.code = code; - this.message = params.message; - this.type = params.type || 'error'; - this.data = params.data || null; + _params = _.extend( + { + message: null, + type: 'error', + fromServer: false, + data: null, + setting: null + }, + params + ); + delete _params.code; + _.extend( this, _params ); } }); diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 011bd8de2d..b8fc4b5e46 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -303,8 +303,8 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { function test_prepare_setting_validity_for_js() { $this->assertTrue( $this->manager->prepare_setting_validity_for_js( true ) ); $error = new WP_Error(); - $error->add( 'bad_letter', 'Bad letter' ); - $error->add( 'bad_letter', 'Bad letra' ); + $error->add( 'bad_letter', 'Bad letter', 'A' ); + $error->add( 'bad_letter', 'Bad letra', 123 ); $error->add( 'bad_number', 'Bad number', array( 'number' => 123 ) ); $validity = $this->manager->prepare_setting_validity_for_js( $error ); $this->assertInternalType( 'array', $validity ); @@ -313,7 +313,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertInternalType( 'array', $validity[ $code ] ); $this->assertEquals( join( ' ', $messages ), $validity[ $code ]['message'] ); $this->assertArrayHasKey( 'data', $validity[ $code ] ); - $this->assertArrayHasKey( 'from_server', $validity[ $code ]['data'] ); + $this->assertEquals( $validity[ $code ]['data'], $error->get_error_data( $code ) ); } $this->assertArrayHasKey( 'number', $validity['bad_number']['data'] ); $this->assertEquals( 123, $validity['bad_number']['data']['number'] ); diff --git a/tests/qunit/wp-admin/js/customize-base.js b/tests/qunit/wp-admin/js/customize-base.js index b7aca0d114..8ba7b2ce7f 100644 --- a/tests/qunit/wp-admin/js/customize-base.js +++ b/tests/qunit/wp-admin/js/customize-base.js @@ -164,12 +164,16 @@ jQuery( function( $ ) { var notification = new wp.customize.Notification( 'mycode', { 'message': 'Hello World', 'type': 'update', + 'setting': 'blogname', + 'fromServer': true, 'data': { 'foo': 'bar' } } ); assert.equal( 'mycode', notification.code ); assert.equal( 'Hello World', notification.message ); assert.equal( 'update', notification.type ); + assert.equal( 'blogname', notification.setting ); + assert.equal( true, notification.fromServer ); assert.deepEqual( { 'foo': 'bar' }, notification.data ); notification = new wp.customize.Notification( 'mycode2', {