WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/03/2015 08:46:48 PM (7 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-includes/class-wp-customize-setting.php

    r32893 r33071  
    11831183                    $this->update_status = 'error';
    11841184                    $this->update_error  = new WP_Error( 'nav_menu_setting_failure' );
     1185                    return;
    11851186                }
    11861187
     
    12081209                    $this->update_status = 'error';
    12091210                    $this->update_error  = new WP_Error( 'nav_menu_item_setting_failure' );
     1211                    return;
    12101212                }
    12111213
     
    16121614        $value['auto_add']    = ! empty( $value['auto_add'] );
    16131615
     1616        if ( '' === $value['name'] ) {
     1617            $value['name'] = _x( '(unnamed)', 'Missing menu name.' );
     1618        }
     1619
    16141620        /** This filter is documented in wp-includes/class-wp-customize-setting.php */
    16151621        return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
     
    16701676            // Insert or update menu.
    16711677            $menu_data = wp_array_slice_assoc( $value, array( 'description', 'parent' ) );
    1672             if ( isset( $value['name'] ) ) {
    1673                 $menu_data['menu-name'] = $value['name'];
    1674             }
    1675 
    1676             $r = wp_update_nav_menu_object( $is_placeholder ? 0 : $this->term_id, $menu_data );
     1678            $menu_data['menu-name'] = $value['name'];
     1679
     1680            $menu_id = $is_placeholder ? 0 : $this->term_id;
     1681            $r = wp_update_nav_menu_object( $menu_id, $menu_data );
     1682            $original_name = $menu_data['menu-name'];
     1683            $name_conflict_suffix = 1;
     1684            while ( is_wp_error( $r ) && 'menu_exists' === $r->get_error_code() ) {
     1685                $name_conflict_suffix += 1;
     1686                /* translators: 1: original menu name, 2: duplicate count */
     1687                $menu_data['menu-name'] = sprintf( __( '%1$s (%2$d)' ), $original_name, $name_conflict_suffix );
     1688                $r = wp_update_nav_menu_object( $menu_id, $menu_data );
     1689            }
     1690
    16771691            if ( is_wp_error( $r ) ) {
    16781692                $this->update_status = 'error';
     
    17651779            'error'            => $this->update_error ? $this->update_error->get_error_code() : null,
    17661780            'status'           => $this->update_status,
     1781            'saved_value'      => 'deleted' === $this->update_status ? null : $this->value(),
    17671782        );
    17681783
Note: See TracChangeset for help on using the changeset viewer.