Make WordPress Core


Ignore:
Timestamp:
07/03/2015 08:46:48 PM (9 years ago)
Author:
westonruter
Message:

Customizer: Fix saving menus with empty names or names that are already used.

Adds validation for initially-supplied nav menu name, blocking empty names from being supplied. If later an empty name is supplied and the nav menu is saved, the name "(unnamed)" will be supplied instead and supplied back to the client. If a name is supplied for the menu which is currently used by another menu, then the name conflict is resolved by adding a numerical counter similar to how post_name conflicts are resolved. Includes unit tests.

Fixes #32760.

File:
1 edited

Legend:

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

    r33070 r33071  
    834834                button.attr( 'aria-expanded', 'false' );
    835835                content.slideUp( 'fast' );
     836                content.find( '.menu-name-field' ).removeClass( 'invalid' );
    836837            }
    837838        }
     
    870871                }
    871872                menuId = matches[1];
    872                 option = new Option( setting().name, menuId );
     873                option = new Option( displayNavMenuName( setting().name ), menuId );
    873874                control.container.find( 'select' ).append( option );
    874875            });
     
    896897                    control.container.find( 'option[value=' + menuId + ']' ).remove();
    897898                } else {
    898                     control.container.find( 'option[value=' + menuId + ']' ).text( setting().name );
     899                    control.container.find( 'option[value=' + menuId + ']' ).text( displayNavMenuName( setting().name ) );
    899900                }
    900901            });
     
    16061607        ready: function() {
    16071608            var control = this,
    1608                 menuId = control.params.menu_id;
     1609                menuId = control.params.menu_id,
     1610                menu = control.setting(),
     1611                name;
    16091612
    16101613            if ( 'undefined' === typeof this.params.menu_id ) {
     
    16371640
    16381641            // Add menu to Custom Menu widgets.
    1639             if ( control.setting() ) {
     1642            if ( menu ) {
     1643                name = displayNavMenuName( menu.name );
     1644
    16401645                api.control.each( function( widgetControl ) {
    16411646                    if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
     
    16441649                    var select = widgetControl.container.find( 'select' );
    16451650                    if ( select.find( 'option[value=' + String( menuId ) + ']' ).length === 0 ) {
    1646                         select.append( new Option( control.setting().name, menuId ) );
     1651                        select.append( new Option( name, menuId ) );
    16471652                    }
    16481653                } );
    1649                 $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( control.setting().name, menuId ) );
     1654                $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( name, menuId ) );
    16501655            }
    16511656        },
     
    16781683
    16791684            control.setting.bind( function( to ) {
     1685                var name;
    16801686                if ( false === to ) {
    16811687                    control._handleDeletion();
    16821688                } else {
    16831689                    // Update names in the Custom Menu widgets.
     1690                    name = displayNavMenuName( to.name );
    16841691                    api.control.each( function( widgetControl ) {
    16851692                        if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
     
    16871694                        }
    16881695                        var select = widgetControl.container.find( 'select' );
    1689                         select.find( 'option[value=' + String( menuId ) + ']' ).text( to.name );
     1696                        select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
    16901697                    });
    1691                     $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( to.name );
     1698                    $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( name );
    16921699                }
    16931700            } );
     
    18481855                        container.find( '.theme-location-set' ).hide();
    18491856                    } else {
    1850                         container.find( '.theme-location-set' ).show().find( 'span' ).text( menuSetting().name );
     1857                        container.find( '.theme-location-set' ).show().find( 'span' ).text( displayNavMenuName( menuSetting().name ) );
    18511858                    }
    18521859                };
     
    18801887                }
    18811888
    1882                 // Empty names are not allowed (will not be saved), don't update to one.
    1883                 if ( menu.name ) {
    1884                     var section = control.container.closest( '.accordion-section' ),
    1885                         menuId = control.params.menu_id,
    1886                         controlTitle = section.find( '.accordion-section-title' ),
    1887                         sectionTitle = section.find( '.customize-section-title h3' ),
    1888                         location = section.find( '.menu-in-location' ),
    1889                         action = sectionTitle.find( '.customize-action' );
    1890 
    1891                     // Update the control title
    1892                     controlTitle.text( menu.name );
    1893                     if ( location.length ) {
    1894                         location.appendTo( controlTitle );
     1889                var section = control.container.closest( '.accordion-section' ),
     1890                    menuId = control.params.menu_id,
     1891                    controlTitle = section.find( '.accordion-section-title' ),
     1892                    sectionTitle = section.find( '.customize-section-title h3' ),
     1893                    location = section.find( '.menu-in-location' ),
     1894                    action = sectionTitle.find( '.customize-action' ),
     1895                    name = displayNavMenuName( menu.name );
     1896
     1897                // Update the control title
     1898                controlTitle.text( name );
     1899                if ( location.length ) {
     1900                    location.appendTo( controlTitle );
     1901                }
     1902
     1903                // Update the section title
     1904                sectionTitle.text( name );
     1905                if ( action.length ) {
     1906                    action.prependTo( sectionTitle );
     1907                }
     1908
     1909                // Update the nav menu name in location selects.
     1910                api.control.each( function( control ) {
     1911                    if ( /^nav_menu_locations\[/.test( control.id ) ) {
     1912                        control.container.find( 'option[value=' + menuId + ']' ).text( name );
    18951913                    }
    1896 
    1897                     // Update the section title
    1898                     sectionTitle.text( menu.name );
    1899                     if ( action.length ) {
    1900                         action.prependTo( sectionTitle );
     1914                } );
     1915
     1916                // Update the nav menu name in all location checkboxes.
     1917                section.find( '.customize-control-checkbox input' ).each( function() {
     1918                    if ( $( this ).prop( 'checked' ) ) {
     1919                        $( '.current-menu-location-name-' + $( this ).data( 'location-id' ) ).text( name );
    19011920                    }
    1902 
    1903                     // Update the nav menu name in location selects.
    1904                     api.control.each( function( control ) {
    1905                         if ( /^nav_menu_locations\[/.test( control.id ) ) {
    1906                             control.container.find( 'option[value=' + menuId + ']' ).text( menu.name );
    1907                         }
    1908                     } );
    1909 
    1910                     // Update the nav menu name in all location checkboxes.
    1911                     section.find( '.customize-control-checkbox input' ).each( function() {
    1912                         if ( $( this ).prop( 'checked' ) ) {
    1913                             $( '.current-menu-location-name-' + $( this ).data( 'location-id' ) ).text( menu.name );
    1914                         }
    1915                     } );
    1916                 }
     1921                } );
    19171922            } );
    19181923        },
     
    21522157        /**
    21532158         * Create the new menu with the name supplied.
    2154          *
    2155          * @returns {boolean}
    21562159         */
    21572160        submit: function() {
     
    21642167                customizeId,
    21652168                placeholderId = api.Menus.generatePlaceholderAutoIncrementId();
     2169
     2170            if ( ! name ) {
     2171                nameInput.addClass( 'invalid' );
     2172                nameInput.focus();
     2173                return;
     2174            }
    21662175
    21672176            customizeId = 'nav_menu[' + String( placeholderId ) + ']';
     
    21902199                    id: customizeId,
    21912200                    panel: 'nav_menus',
    2192                     title: name,
     2201                    title: displayNavMenuName( name ),
    21932202                    customizeAction: api.Menus.data.l10n.customizingMenus,
    21942203                    type: 'nav_menu',
     
    22012210            // Clear name field.
    22022211            nameInput.val( '' );
     2212            nameInput.removeClass( 'invalid' );
    22032213
    22042214            wp.a11y.speak( api.Menus.data.l10n.menuAdded );
     
    22702280
    22712281        _( data.nav_menu_updates ).each(function( update ) {
    2272             var oldCustomizeId, newCustomizeId, oldSetting, newSetting, settingValue, oldSection, newSection;
     2282            var oldCustomizeId, newCustomizeId, customizeId, oldSetting, newSetting, setting, settingValue, oldSection, newSection, wasSaved;
    22732283            if ( 'inserted' === update.status ) {
    22742284                if ( ! update.previous_term_id ) {
     
    22922302                    throw new Error( 'Did not expect setting to be empty (deleted).' );
    22932303                }
    2294                 settingValue = _.clone( settingValue );
     2304                settingValue = $.extend( _.clone( settingValue ), update.saved_value );
    22952305
    22962306                insertedMenuIdMapping[ update.previous_term_id ] = update.term_id;
     
    23502360
    23512361                // @todo Update the Custom Menu selects, ensuring the newly-inserted IDs are used for any that have selected a placeholder menu.
     2362            } else if ( 'updated' === update.status ) {
     2363                customizeId = 'nav_menu[' + String( update.term_id ) + ']';
     2364                if ( ! api.has( customizeId ) ) {
     2365                    throw new Error( 'Expected setting to exist: ' + customizeId );
     2366                }
     2367
     2368                // Make sure the setting gets updated with its sanitized server value (specifically the conflict-resolved name).
     2369                setting = api( customizeId );
     2370                if ( ! _.isEqual( update.saved_value, setting.get() ) ) {
     2371                    wasSaved = api.state( 'saved' ).get();
     2372                    setting.set( update.saved_value );
     2373                    setting._dirty = false;
     2374                    api.state( 'saved' ).set( wasSaved );
     2375                }
    23522376            }
    23532377        } );
     
    24972521    }
    24982522
     2523    /**
     2524     * Apply sanitize_text_field()-like logic to the supplied name, returning a
     2525     * "unnammed" fallback string if the name is then empty.
     2526     *
     2527     * @param {string} name
     2528     * @returns {string}
     2529     */
     2530    function displayNavMenuName( name ) {
     2531        name = $( '<div>' ).text( name ).html(); // Emulate esc_html() which is used in wp-admin/nav-menus.php.
     2532        name = $.trim( name );
     2533        return name || api.Menus.data.l10n.unnamed;
     2534    }
     2535
    24992536})( wp.customize, wp, jQuery );
Note: See TracChangeset for help on using the changeset viewer.