WordPress.org

Make WordPress Core

Changeset 38513


Ignore:
Timestamp:
09/02/2016 10:34:48 PM (21 months ago)
Author:
westonruter
Message:

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.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/customize-controls.js

    r38492 r38513  
    34623462                if ( _.isObject( validity ) ) {
    34633463                    _.each( validity, function( params, code ) {
    3464                         var notification = new api.Notification( code, params ), existingNotification, needsReplacement = false;
     3464                        var notification, existingNotification, needsReplacement = false;
     3465                        notification = new api.Notification( code, _.extend( { fromServer: true }, params ) );
    34653466
    34663467                        // Remove existing notification if already exists for code but differs in parameters.
    34673468                        existingNotification = setting.notifications( notification.code );
    34683469                        if ( existingNotification ) {
    3469                             needsReplacement = ( notification.type !== existingNotification.type ) || ! _.isEqual( notification.data, existingNotification.data );
     3470                            needsReplacement = notification.type !== existingNotification.type || notification.message !== existingNotification.message || ! _.isEqual( notification.data, existingNotification.data );
    34703471                        }
    34713472                        if ( needsReplacement ) {
     
    37163717                    api.each( function( setting ) {
    37173718                        setting.notifications.each( function( notification ) {
    3718                             if ( 'error' === notification.type && ( ! notification.data || ! notification.data.from_server ) ) {
     3719                            if ( 'error' === notification.type && ! notification.fromServer ) {
    37193720                                invalidSettings.push( setting.id );
    37203721                            }
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r38470 r38513  
    10471047            $notification = array();
    10481048            foreach ( $validity->errors as $error_code => $error_messages ) {
    1049                 $error_data = $validity->get_error_data( $error_code );
    1050                 if ( is_null( $error_data ) ) {
    1051                     $error_data = array();
    1052                 }
    1053                 $error_data = array_merge(
    1054                     $error_data,
    1055                     array( 'from_server' => true )
    1056                 );
    10571049                $notification[ $error_code ] = array(
    10581050                    'message' => join( ' ', $error_messages ),
    1059                     'data' => $error_data,
     1051                    'data' => $validity->get_error_data( $error_code ),
    10601052                );
    10611053            }
  • trunk/src/wp-includes/js/customize-base.js

    r37476 r38513  
    763763     * @since 4.6.0
    764764     *
    765      * @param {string} code                The error code.
    766      * @param {object} params              Params.
    767      * @param {string} params.message      The error message.
    768      * @param {string} [params.type=error] The notification type.
    769      * @param {*}      [params.data]       Any additional data.
     765     * @param {string}  code - The error code.
     766     * @param {object}  params - Params.
     767     * @param {string}  params.message=null - The error message.
     768     * @param {string}  [params.type=error] - The notification type.
     769     * @param {boolean} [params.fromServer=false] - Whether the notification was server-sent.
     770     * @param {string}  [params.setting=null] - The setting ID that the notification is related to.
     771     * @param {*}       [params.data=null] - Any additional data.
    770772     */
    771773    api.Notification = api.Class.extend({
    772774        initialize: function( code, params ) {
     775            var _params;
    773776            this.code = code;
    774             this.message = params.message;
    775             this.type = params.type || 'error';
    776             this.data = params.data || null;
     777            _params = _.extend(
     778                {
     779                    message: null,
     780                    type: 'error',
     781                    fromServer: false,
     782                    data: null,
     783                    setting: null
     784                },
     785                params
     786            );
     787            delete _params.code;
     788            _.extend( this, _params );
    777789        }
    778790    });
  • trunk/tests/phpunit/tests/customize/manager.php

    r37942 r38513  
    304304        $this->assertTrue( $this->manager->prepare_setting_validity_for_js( true ) );
    305305        $error = new WP_Error();
    306         $error->add( 'bad_letter', 'Bad letter' );
    307         $error->add( 'bad_letter', 'Bad letra' );
     306        $error->add( 'bad_letter', 'Bad letter', 'A' );
     307        $error->add( 'bad_letter', 'Bad letra', 123 );
    308308        $error->add( 'bad_number', 'Bad number', array( 'number' => 123 ) );
    309309        $validity = $this->manager->prepare_setting_validity_for_js( $error );
     
    314314            $this->assertEquals( join( ' ', $messages ), $validity[ $code ]['message'] );
    315315            $this->assertArrayHasKey( 'data', $validity[ $code ] );
    316             $this->assertArrayHasKey( 'from_server', $validity[ $code ]['data'] );
     316            $this->assertEquals( $validity[ $code ]['data'], $error->get_error_data( $code ) );
    317317        }
    318318        $this->assertArrayHasKey( 'number', $validity['bad_number']['data'] );
  • trunk/tests/qunit/wp-admin/js/customize-base.js

    r37476 r38513  
    165165            'message': 'Hello World',
    166166            'type': 'update',
     167            'setting': 'blogname',
     168            'fromServer': true,
    167169            'data': { 'foo': 'bar' }
    168170        } );
     
    171173        assert.equal( 'Hello World', notification.message );
    172174        assert.equal( 'update', notification.type );
     175        assert.equal( 'blogname', notification.setting );
     176        assert.equal( true, notification.fromServer );
    173177        assert.deepEqual( { 'foo': 'bar' }, notification.data );
    174178
Note: See TracChangeset for help on using the changeset viewer.