Make WordPress Core

Changeset 60815


Ignore:
Timestamp:
09/30/2025 04:26:44 PM (2 months ago)
Author:
johnbillion
Message:

Menus: Prevent HTML in menu item titles from being rendered unexpectedly.

Props audrasjb, desrosj, johnbillion, jorbin, phillsav, vortfu, westonruter

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/lib/nav-menu.js

    r59950 r60815  
    299299                        $.each( parentDropdowns, function() {
    300300                            var parentDropdown = $( this ),
    301                                 $html = '',
    302                                 $selected = '',
    303                                 currentItemID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-db-id' ).val(),
    304                                 currentparentID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-parent-id' ).val(),
     301                                currentItemID = parseInt( parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-db-id' ).val() ),
     302                                currentParentID = parseInt( parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-parent-id' ).val() ),
    305303                                currentItem = parentDropdown.closest( 'li.menu-item' ),
    306304                                currentMenuItemChild = currentItem.childMenuItems(),
    307                                 excludeMenuItem = [ currentItemID ];
     305                                excludeMenuItem =  /** @type {number[]} */ [ currentItemID ];
     306
     307                            parentDropdown.empty();
    308308
    309309                            if ( currentMenuItemChild.length > 0 ) {
    310310                                $.each( currentMenuItemChild, function(){
    311311                                    var childItem = $(this),
    312                                         childID = childItem.find( '.menu-item-data-db-id' ).val();
     312                                        childID = parseInt( childItem.find( '.menu-item-data-db-id' ).val() );
    313313
    314314                                    excludeMenuItem.push( childID );
     
    316316                            }
    317317
    318                             if ( currentparentID == 0 ) {
    319                                 $selected = 'selected';
    320                             }
    321 
    322                             $html += '<option ' + $selected + ' value="0">' + wp.i18n._x( 'No Parent', 'menu item without a parent in navigation menu' ) + '</option>';
     318                            parentDropdown.append(
     319                                $( '<option>', {
     320                                    value: '0',
     321                                    selected: currentParentID === 0,
     322                                    text: wp.i18n._x( 'No Parent', 'menu item without a parent in navigation menu' ),
     323                                } )
     324                            );
    323325
    324326                            $.each( menuItems, function() {
    325327                                var menuItem = $(this),
    326                                 $selected = '',
    327                                 menuID = menuItem.find( '.menu-item-data-db-id' ).val(),
     328                                menuID = parseInt( menuItem.find( '.menu-item-data-db-id' ).val() ),
    328329                                menuTitle = menuItem.find( '.edit-menu-item-title' ).val();
    329330
    330331                                if ( ! excludeMenuItem.includes( menuID ) ) {
    331                                     if ( currentparentID == menuID ) {
    332                                         $selected = 'selected';
    333                                     }
    334                                     $html += '<option ' + $selected + ' value="' + menuID + '">' + menuTitle + '</option>';
     332                                    parentDropdown.append(
     333                                        $( '<option>', {
     334                                            value: menuID.toString(),
     335                                            selected: currentParentID === menuID,
     336                                            text: menuTitle,
     337                                        } )
     338                                    );
    335339                                }
    336340                            });
    337 
    338                             parentDropdown.html( $html );
    339341                        });
    340342                       
     
    350352                                menuItem = orderDropdown.closest( 'li.menu-item' ).first(),
    351353                                depth = menuItem.menuItemDepth(),
    352                                 isPrimaryMenuItem = ( 0 === depth ),
    353                                 $html = '',
    354                                 $selected = '';
     354                                isPrimaryMenuItem = ( 0 === depth );
     355
     356                            orderDropdown.empty();
    355357
    356358                            if ( isPrimaryMenuItem ) {
     
    361363
    362364                                for ( let i = 1; i < totalMenuItems + 1; i++ ) {
    363                                     $selected = '';
    364                                     if ( i == itemPosition ) {
    365                                         $selected = 'selected';
    366                                     }
    367                                     var itemString = wp.i18n.sprintf(
     365                                    var itemString = wp.i18n.sprintf(
    368366                                        /* translators: 1: The current menu item number, 2: The total number of menu items. */
    369367                                        wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ),
     
    371369                                        totalMenuItems
    372370                                    );
    373                                     $html += '<option ' + $selected + ' value="' + i + '">' + itemString + '</option>';
     371                                    orderDropdown.append(
     372                                        $( '<option>', {
     373                                            selected: i === itemPosition,
     374                                            value: i.toString(),
     375                                            text: itemString,
     376                                        } )
     377                                    );
    374378                                }
    375379
     
    383387
    384388                                for ( let i = 1; i < totalSubMenuItems + 1; i++ ) {
    385                                     $selected = '';
    386                                     if ( i == itemPosition ) {
    387                                         $selected = 'selected';
    388                                     }
    389                                     var submenuString = wp.i18n.sprintf(
     389                                    var submenuString = wp.i18n.sprintf(
    390390                                        /* translators: 1: The current submenu item number, 2: The total number of submenu items. */
    391391                                        wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ),
     
    393393                                        totalSubMenuItems
    394394                                    );
    395                                     $html += '<option ' + $selected + ' value="' + i + '">' + submenuString + '</option>';
     395                                    orderDropdown.append(
     396                                        $( '<option>', {
     397                                            selected: i === itemPosition,
     398                                            value: i.toString(),
     399                                            text: submenuString,
     400                                        } )
     401                                    );
    396402                                }
    397403
    398404                            }
    399 
    400                             orderDropdown.html( $html );
    401405                        });
    402406                       
     
    12911295
    12921296                if ( this.checked === true ) {
    1293                     $( '#pending-menu-items-to-delete ul' ).append(
    1294                         '<li data-menu-item-id="' + menuItemID + '">' +
    1295                             '<span class="pending-menu-item-name">' + menuItemName + '</span> ' +
    1296                             '<span class="pending-menu-item-type">(' + menuItemType + ')</span>' +
    1297                             '<span class="separator"></span>' +
    1298                         '</li>'
    1299                     );
     1297                    const $li = $( '<li>', { 'data-menu-item-id': menuItemID } );
     1298                    $li.append( $( '<span>', {
     1299                        'class': 'pending-menu-item-name',
     1300                        text: menuItemName
     1301                    } ) );
     1302                    $li.append( ' ' );
     1303                    $li.append( $( '<span>', {
     1304                        'class': 'pending-menu-item-type',
     1305                        text: '(' + menuItemType + ')',
     1306                    } ) );
     1307                    $li.append( $( '<span>', { 'class': 'separator' } ) );
     1308                    $( '#pending-menu-items-to-delete ul' ).append( $li );
    13001309                }
    13011310
     
    17001709
    17011710        eventOnClickMenuSave : function() {
    1702             var locs = '',
    1703             menuName = $('#menu-name'),
    1704             menuNameVal = menuName.val();
     1711            var menuName = $('#menu-name'),
     1712                menuNameVal = menuName.val();
    17051713
    17061714            // Cancel and warn if invalid menu name.
     
    17101718            }
    17111719            // Copy menu theme locations.
     1720            // Note: This appears to be dead code since #nav-menu-theme-locations no longer exists, perhaps removed in r32842.
     1721            var $updateNavMenu = $('#update-nav-menu');
    17121722            $('#nav-menu-theme-locations select').each(function() {
    1713                 locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />';
    1714             });
    1715             $('#update-nav-menu').append( locs );
     1723                $updateNavMenu.append(
     1724                    $( '<input>', {
     1725                        type: 'hidden',
     1726                        name: this.name,
     1727                        value: $( this ).val(),
     1728                    } )
     1729                );
     1730            });
    17161731            // Update menu item position data.
    17171732            api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
     
    17561771
    17571772            if( ! $items.length ) {
    1758                 $('.categorychecklist', panel).html( '<li><p>' + wp.i18n.__( 'No results found.' ) + '</p></li>' );
     1773                const li = $( '<li>' );
     1774                const p = $( '<p>', { text: wp.i18n.__( 'No results found.' ) } );
     1775                li.append( p );
     1776                $('.categorychecklist', panel).empty().append( li );
    17591777                $( '.spinner', panel ).removeClass( 'is-active' );
    17601778                wrapper.addClass( 'has-no-menu-item' );
  • trunk/src/js/_enqueues/wp/customize/nav-menus.js

    r60715 r60815  
    530530            }
    531531
    532             this.currentMenuControl.addItemToMenu( menu_item.attributes );
     532            // Leave the title as empty to reuse the original title as a placeholder if set.
     533            var nav_menu_item = Object.assign( {}, menu_item.attributes );
     534            if ( nav_menu_item.title === nav_menu_item.original_title ) {
     535                nav_menu_item.title = '';
     536            }
     537
     538            this.currentMenuControl.addItemToMenu( nav_menu_item );
    533539
    534540            $( menuitemTpl ).find( '.menu-item-handle' ).addClass( 'item-added' );
     
    31433149                {
    31443150                    nav_menu_term_id: menuControl.params.menu_id,
    3145                     original_title: item.title,
    31463151                    position: position
    31473152                }
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r60715 r60815  
    192192            } elseif ( 'post' !== $object_name && 0 === $page && $post_type->has_archive ) {
    193193                // Add a post type archive link.
     194                $title   = $post_type->labels->archives;
    194195                $items[] = array(
    195                     'id'         => $object_name . '-archive',
    196                     'title'      => $post_type->labels->archives,
    197                     'type'       => 'post_type_archive',
    198                     'type_label' => __( 'Post Type Archive' ),
    199                     'object'     => $object_name,
    200                     'url'        => get_post_type_archive_link( $object_name ),
     196                    'id'             => $object_name . '-archive',
     197                    'title'          => $title,
     198                    'original_title' => $title,
     199                    'type'           => 'post_type_archive',
     200                    'type_label'     => __( 'Post Type Archive' ),
     201                    'object'         => $object_name,
     202                    'url'            => get_post_type_archive_link( $object_name ),
    201203                );
    202204            }
     
    245247                }
    246248
     249                $title   = html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
    247250                $items[] = array(
    248                     'id'         => "post-{$post->ID}",
    249                     'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    250                     'type'       => 'post_type',
    251                     'type_label' => $post_type_label,
    252                     'object'     => $post->post_type,
    253                     'object_id'  => (int) $post->ID,
    254                     'url'        => get_permalink( (int) $post->ID ),
     251                    'id'             => "post-{$post->ID}",
     252                    'title'          => $title,
     253                    'original_title' => $title,
     254                    'type'           => 'post_type',
     255                    'type_label'     => $post_type_label,
     256                    'object'         => $post->post_type,
     257                    'object_id'      => (int) $post->ID,
     258                    'url'            => get_permalink( (int) $post->ID ),
    255259                );
    256260            }
     
    277281
    278282            foreach ( $terms as $term ) {
     283                $title   = html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) );
    279284                $items[] = array(
    280                     'id'         => "term-{$term->term_id}",
    281                     'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    282                     'type'       => 'taxonomy',
    283                     'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
    284                     'object'     => $term->taxonomy,
    285                     'object_id'  => (int) $term->term_id,
    286                     'url'        => get_term_link( (int) $term->term_id, $term->taxonomy ),
     285                    'id'             => "term-{$term->term_id}",
     286                    'title'          => $title,
     287                    'original_title' => $title,
     288                    'type'           => 'taxonomy',
     289                    'type_label'     => get_taxonomy( $term->taxonomy )->labels->singular_name,
     290                    'object'         => $term->taxonomy,
     291                    'object_id'      => (int) $term->term_id,
     292                    'url'            => get_term_link( (int) $term->term_id, $term->taxonomy ),
    287293                );
    288294            }
  • trunk/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php

    r56548 r60815  
    5757        'xfn'              => '',
    5858        'status'           => 'publish',
    59         'original_title'   => '',
    6059        'nav_menu_term_id' => 0, // This will be supplied as the $menu_id arg for wp_update_nav_menu_item().
    6160        '_invalid'         => false,
     
    211210     */
    212211    public function value() {
     212        $type_label = null;
    213213        if ( $this->is_previewed && get_current_blog_id() === $this->_previewed_blog_id ) {
    214214            $undefined  = new stdClass(); // Symbol.
     
    219219            } else {
    220220                $value = $post_value;
    221             }
    222             if ( ! empty( $value ) && empty( $value['original_title'] ) ) {
    223                 $value['original_title'] = $this->get_original_title( (object) $value );
    224221            }
    225222        } elseif ( isset( $this->value ) ) {
     
    234231                    $is_title_empty = empty( $post->post_title );
    235232                    $value          = (array) wp_setup_nav_menu_item( $post );
     233                    if ( isset( $value['type_label'] ) ) {
     234                        $type_label = $value['type_label'];
     235                    }
    236236                    if ( $is_title_empty ) {
    237237                        $value['title'] = '';
     
    250250        }
    251251
    252         if ( ! empty( $value ) && empty( $value['type_label'] ) ) {
    253             $value['type_label'] = $this->get_type_label( (object) $value );
     252        // These properties are read-only and are part of the setting for use in the Customizer UI.
     253        if ( is_array( $value ) ) {
     254            $value_obj               = (object) $value;
     255            $value['type_label']     = isset( $type_label ) ? $type_label : $this->get_type_label( $value_obj );
     256            $value['original_title'] = $this->get_original_title( $value_obj );
    254257        }
    255258
     
    258261
    259262    /**
     263     * Prepares the value for editing on the client.
     264     *
     265     * @since 6.8.3
     266     *
     267     * @return array|false Value prepared for the client.
     268     */
     269    public function js_value() {
     270        $value = parent::js_value();
     271        if ( is_array( $value ) && isset( $value['original_title'] ) ) {
     272            // Decode entities for the sake of displaying the original title as a placeholder.
     273            $value['original_title'] = html_entity_decode( $value['original_title'], ENT_QUOTES, get_bloginfo( 'charset' ) );
     274        }
     275        return $value;
     276    }
     277
     278    /**
    260279     * Get original title.
    261280     *
     
    263282     *
    264283     * @param object $item Nav menu item.
    265      * @return string The original title.
     284     * @return string The original title, without entity decoding.
    266285     */
    267286    protected function get_original_title( $item ) {
     
    289308            }
    290309        }
    291         $original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
    292310        return $original_title;
    293311    }
     
    345363            $this->value['status'] = $this->value['post_status'];
    346364            unset( $this->value['post_status'] );
    347         }
    348 
    349         if ( ! isset( $this->value['original_title'] ) ) {
    350             $this->value['original_title'] = $this->get_original_title( (object) $this->value );
    351365        }
    352366
     
    595609        unset( $item->position );
    596610
    597         if ( empty( $item->original_title ) ) {
    598             $item->original_title = $this->get_original_title( $item );
    599         }
    600611        if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
    601             $item->title = $item->original_title;
     612            $item->title = $item->original_title; // This is NOT entity-decoded. It comes from self::get_original_title().
    602613        }
    603614        if ( $item->title ) {
     
    655666     * @since 5.9.0 Renamed `$menu_item_value` to `$value` for PHP 8 named parameter support.
    656667     *
    657      * @param array $value The menu item value to sanitize.
     668     * @param array|false $value The menu item value to sanitize.
    658669     * @return array|false|null|WP_Error Null or WP_Error if an input isn't valid. False if it is marked for deletion.
    659670     *                                   Otherwise the sanitized value.
     
    712723        }
    713724
    714         $menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );
    715 
    716725        // Apply the same filters as when calling wp_insert_post().
    717726
  • trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php

    r60253 r60815  
    106106            'xfn'              => '',
    107107            'status'           => 'publish',
    108             'original_title'   => '',
    109108            'nav_menu_term_id' => 0,
    110109            '_invalid'         => false,
     
    174173            0,
    175174            array(
    176                 'menu-item-type'      => 'post_type',
    177                 'menu-item-object'    => 'post',
    178                 'menu-item-object-id' => $post_id,
    179                 'menu-item-title'     => $item_title,
    180                 'menu-item-status'    => 'publish',
     175            'menu-item-type'      => 'post_type',
     176            'menu-item-object'    => 'post',
     177            'menu-item-object-id' => $post_id,
     178            'menu-item-title'     => $item_title,
     179            'menu-item-status'    => 'publish',
    181180            )
    182181        );
     
    200199            $other_menu_id,
    201200            $item_id,
    202             array(
     201                array(
    203202                'menu-item-title' => 'Hola',
    204203            )
     
    598597            'xfn'              => 'hello  inject',
    599598            'status'           => 'draft',
    600             'original_title'   => 'Hi',
     599            'original_title'   => 'Hi<script>unfilteredHtml()</script>',
    601600            'nav_menu_term_id' => 0,
    602601        );
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r60253 r60815  
    185185        // Expected menu item array.
    186186        $expected = array(
    187             'id'         => "post-{$post_id}",
    188             'title'      => 'Post Title',
    189             'type'       => 'post_type',
    190             'type_label' => 'Post',
    191             'object'     => 'post',
    192             'object_id'  => (int) $post_id,
    193             'url'        => get_permalink( (int) $post_id ),
     187            'id'             => "post-{$post_id}",
     188            'title'          => 'Post Title',
     189            'original_title' => 'Post Title',
     190            'type'           => 'post_type',
     191            'type_label'     => 'Post',
     192            'object'         => 'post',
     193            'object_id'      => (int) $post_id,
     194            'url'            => get_permalink( (int) $post_id ),
    194195        );
    195196
     
    217218        // Expected menu item array.
    218219        $expected = array(
    219             'id'         => "post-{$page_id}",
    220             'title'      => 'Page Title',
    221             'type'       => 'post_type',
    222             'type_label' => 'Page',
    223             'object'     => 'page',
    224             'object_id'  => (int) $page_id,
    225             'url'        => get_permalink( (int) $page_id ),
     220            'id'             => "post-{$page_id}",
     221            'title'          => 'Page Title',
     222            'original_title' => 'Page Title',
     223            'type'           => 'post_type',
     224            'type_label'     => 'Page',
     225            'object'         => 'page',
     226            'object_id'      => (int) $page_id,
     227            'url'            => get_permalink( (int) $page_id ),
    226228        );
    227229
     
    243245        // Expected menu item array.
    244246        $expected = array(
    245             'id'         => "post-{$post_id}",
    246             'title'      => 'Post Title',
    247             'type'       => 'post_type',
    248             'type_label' => 'Post',
    249             'object'     => 'post',
    250             'object_id'  => (int) $post_id,
    251             'url'        => get_permalink( (int) $post_id ),
     247            'id'             => "post-{$post_id}",
     248            'title'          => 'Post Title',
     249            'original_title' => 'Post Title',
     250            'type'           => 'post_type',
     251            'type_label'     => 'Post',
     252            'object'         => 'post',
     253            'object_id'      => (int) $post_id,
     254            'url'            => get_permalink( (int) $post_id ),
    252255        );
    253256
     
    269272        // Expected menu item array.
    270273        $expected = array(
    271             'id'         => "term-{$term_id}",
    272             'title'      => 'Term Title',
    273             'type'       => 'taxonomy',
    274             'type_label' => 'Category',
    275             'object'     => 'category',
    276             'object_id'  => (int) $term_id,
    277             'url'        => get_term_link( (int) $term_id, 'category' ),
     274            'id'             => "term-{$term_id}",
     275            'title'          => 'Term Title',
     276            'original_title' => 'Term Title',
     277            'type'           => 'taxonomy',
     278            'type_label'     => 'Category',
     279            'object'         => 'category',
     280            'object_id'      => (int) $term_id,
     281            'url'            => get_term_link( (int) $term_id, 'category' ),
    278282        );
    279283
     
    786790        $this->assertNotEmpty( $post_types );
    787791
    788         foreach ( $post_types as $type ) {
    789             $this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
    790             $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
    791             $this->assertStringContainsString( 'data-type="post_type"', $template );
    792             $this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
    793             $this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
     792            foreach ( $post_types as $type ) {
     793                $this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
     794                $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
     795                $this->assertStringContainsString( 'data-type="post_type"', $template );
     796                $this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
     797                $this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
    794798        }
    795799
     
    798802        $this->assertNotEmpty( $taxonomies );
    799803
    800         foreach ( $taxonomies as $tax ) {
    801             $this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
    802             $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
    803             $this->assertStringContainsString( 'data-type="taxonomy"', $template );
    804             $this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
    805             $this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
     804            foreach ( $taxonomies as $tax ) {
     805                $this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
     806                $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
     807                $this->assertStringContainsString( 'data-type="taxonomy"', $template );
     808                $this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
     809                $this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
    806810        }
    807811
Note: See TracChangeset for help on using the changeset viewer.