Make WordPress Core

Changeset 60828


Ignore:
Timestamp:
09/30/2025 05:03:15 PM (5 weeks ago)
Author:
desrosj
Message:

Grouped backports for the 5.8 branch.

  • REST API: Increase the specificity of capability checks for collections when the edit context is in use.
  • Menus: Prevent HTML in menu item titles from being rendered unexpectedly.

Merges [60814], [60815], [60816] to the 5.8 branch.

Props andraganescu, desrosj, ehti, hurayraiit, iandunn, joehoyle, johnbillion, jorbin, mnelson4, noisysocks, peterwilsoncc, phillsav, rmccue, timothyblynjacobs, vortfu, westonruter , whyisjake, zieladam.

Location:
branches/5.8
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/js/_enqueues/lib/nav-menu.js

    r51010 r60828  
    872872         *
    873873         * @since 5.8.0
    874          */ 
     874         */
    875875        attachBulkSelectButtonListeners : function() {
    876876            var that = this;
     
    891891         *
    892892         * @since 5.8.0
    893          */ 
     893         */
    894894        enableBulkSelection : function() {
    895895            var checkbox = $( '#menu-to-edit .menu-item-checkbox' );
     
    908908         *
    909909         * @since 5.8.0
    910          */ 
     910         */
    911911        disableBulkSelection : function() {
    912912            var checkbox = $( '#menu-to-edit .menu-item-checkbox' );
     
    932932         *
    933933         * @since 5.8.0
    934          */ 
     934         */
    935935        attachMenuCheckBoxListeners : function() {
    936936            var that = this;
     
    945945         *
    946946         * @since 5.8.0
    947          */ 
     947         */
    948948        attachMenuItemDeleteButton : function() {
    949949            var that = this;
     
    986986         *
    987987         * @since 5.8.0
    988          */ 
     988         */
    989989        attachPendingMenuItemsListForDeletion : function() {
    990990            $( '#post-body-content' ).on( 'change', '.menu-item-checkbox', function() {
     
    10051005
    10061006                if ( this.checked === true ) {
    1007                     $( '#pending-menu-items-to-delete ul' ).append(
    1008                         '<li data-menu-item-id="' + menuItemID + '">' +
    1009                             '<span class="pending-menu-item-name">' + menuItemName + '</span> ' +
    1010                             '<span class="pending-menu-item-type">(' + menuItemType + ')</span>' +
    1011                             '<span class="separator"></span>' +
    1012                         '</li>'
    1013                     );
     1007                    var $li = $( '<li>', { 'data-menu-item-id': menuItemID } );
     1008                    $li.append( $( '<span>', {
     1009                        'class': 'pending-menu-item-name',
     1010                        text: menuItemName
     1011                    } ) );
     1012                    $li.append( ' ' );
     1013                    $li.append( $( '<span>', {
     1014                        'class': 'pending-menu-item-type',
     1015                        text: '(' + menuItemType + ')'
     1016                    } ) );
     1017                    $li.append( $( '<span>', { 'class': 'separator' } ) );
     1018                    $( '#pending-menu-items-to-delete ul' ).append( $li );
    10141019                }
    10151020
     
    10231028         *
    10241029         * @since 5.8.0
    1025          */ 
     1030         */
    10261031        setBulkDeleteCheckboxStatus : function() {
    10271032            var that = this;
     
    10471052         *
    10481053         * @since 5.8.0
    1049          */ 
     1054         */
    10501055        setRemoveSelectedButtonStatus : function() {
    10511056            var button = $( '.menu-items-delete' );
     
    13981403
    13991404        eventOnClickMenuSave : function() {
    1400             var locs = '',
    1401             menuName = $('#menu-name'),
    1402             menuNameVal = menuName.val();
     1405            var menuName = $('#menu-name'),
     1406                menuNameVal = menuName.val();
    14031407
    14041408            // Cancel and warn if invalid menu name.
     
    14081412            }
    14091413            // Copy menu theme locations.
     1414            // Note: This appears to be dead code since #nav-menu-theme-locations no longer exists, perhaps removed in r32842.
     1415            var $updateNavMenu = $('#update-nav-menu');
    14101416            $('#nav-menu-theme-locations select').each(function() {
    1411                 locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />';
    1412             });
    1413             $('#update-nav-menu').append( locs );
     1417                $updateNavMenu.append(
     1418                    $( '<input>', {
     1419                        type: 'hidden',
     1420                        name: this.name,
     1421                        value: $( this ).val()
     1422                    } )
     1423                );
     1424            });
    14141425            // Update menu item position data.
    14151426            api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
     
    14541465
    14551466            if( ! $items.length ) {
    1456                 $('.categorychecklist', panel).html( '<li><p>' + wp.i18n.__( 'No results found.' ) + '</p></li>' );
     1467                var li = $( '<li>' );
     1468                var p = $( '<p>', { text: wp.i18n.__( 'No results found.' ) } );
     1469                li.append( p );
     1470                $('.categorychecklist', panel).empty().append( li );
    14571471                $( '.spinner', panel ).removeClass( 'is-active' );
    14581472                wrapper.addClass( 'has-no-menu-item' );
  • branches/5.8/src/js/_enqueues/wp/customize/nav-menus.js

    r50547 r60828  
    527527            }
    528528
    529             this.currentMenuControl.addItemToMenu( menu_item.attributes );
     529            // Leave the title as empty to reuse the original title as a placeholder if set.
     530            var nav_menu_item = Object.assign( {}, menu_item.attributes );
     531            if ( nav_menu_item.title === nav_menu_item.original_title ) {
     532                nav_menu_item.title = '';
     533            }
     534
     535            this.currentMenuControl.addItemToMenu( nav_menu_item );
    530536
    531537            $( menuitemTpl ).find( '.menu-item-handle' ).addClass( 'item-added' );
     
    30213027                {
    30223028                    nav_menu_term_id: menuControl.params.menu_id,
    3023                     original_title: item.title,
    30243029                    position: position
    30253030                }
  • branches/5.8/src/wp-includes/class-wp-customize-nav-menus.php

    r51047 r60828  
    191191            } elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) {
    192192                // Add a post type archive link.
     193                $title   = $post_type->labels->archives;
    193194                $items[] = array(
    194                     'id'         => $object . '-archive',
    195                     'title'      => $post_type->labels->archives,
    196                     'type'       => 'post_type_archive',
    197                     'type_label' => __( 'Post Type Archive' ),
    198                     'object'     => $object,
    199                     'url'        => get_post_type_archive_link( $object ),
     195                    'id'             => $object . '-archive',
     196                    'title'          => $title,
     197                    'original_title' => $title,
     198                    'type'           => 'post_type_archive',
     199                    'type_label'     => __( 'Post Type Archive' ),
     200                    'object'         => $object,
     201                    'url'            => get_post_type_archive_link( $object ),
    200202                );
    201203            }
     
    244246                }
    245247
     248                $title   = html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
    246249                $items[] = array(
    247                     'id'         => "post-{$post->ID}",
    248                     'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    249                     'type'       => 'post_type',
    250                     'type_label' => $post_type_label,
    251                     'object'     => $post->post_type,
    252                     'object_id'  => (int) $post->ID,
    253                     'url'        => get_permalink( (int) $post->ID ),
     250                    'id'             => "post-{$post->ID}",
     251                    'title'          => $title,
     252                    'original_title' => $title,
     253                    'type'           => 'post_type',
     254                    'type_label'     => $post_type_label,
     255                    'object'         => $post->post_type,
     256                    'object_id'      => (int) $post->ID,
     257                    'url'            => get_permalink( (int) $post->ID ),
    254258                );
    255259            }
     
    276280
    277281            foreach ( $terms as $term ) {
     282                $title   = html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) );
    278283                $items[] = array(
    279                     'id'         => "term-{$term->term_id}",
    280                     'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    281                     'type'       => 'taxonomy',
    282                     'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
    283                     'object'     => $term->taxonomy,
    284                     'object_id'  => (int) $term->term_id,
    285                     'url'        => get_term_link( (int) $term->term_id, $term->taxonomy ),
     284                    'id'             => "term-{$term->term_id}",
     285                    'title'          => $title,
     286                    'original_title' => $title,
     287                    'type'           => 'taxonomy',
     288                    'type_label'     => get_taxonomy( $term->taxonomy )->labels->singular_name,
     289                    'object'         => $term->taxonomy,
     290                    'object_id'      => (int) $term->term_id,
     291                    'url'            => get_term_link( (int) $term->term_id, $term->taxonomy ),
    286292                );
    287293            }
  • branches/5.8/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php

    r49624 r60828  
    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,
     
    212211     */
    213212    public function value() {
     213        $type_label = null;
    214214        if ( $this->is_previewed && get_current_blog_id() === $this->_previewed_blog_id ) {
    215215            $undefined  = new stdClass(); // Symbol.
     
    220220            } else {
    221221                $value = $post_value;
    222             }
    223             if ( ! empty( $value ) && empty( $value['original_title'] ) ) {
    224                 $value['original_title'] = $this->get_original_title( (object) $value );
    225222            }
    226223        } elseif ( isset( $this->value ) ) {
     
    235232                    $is_title_empty = empty( $post->post_title );
    236233                    $value          = (array) wp_setup_nav_menu_item( $post );
     234                    if ( isset( $value['type_label'] ) ) {
     235                        $type_label = $value['type_label'];
     236                    }
    237237                    if ( $is_title_empty ) {
    238238                        $value['title'] = '';
     
    251251        }
    252252
    253         if ( ! empty( $value ) && empty( $value['type_label'] ) ) {
    254             $value['type_label'] = $this->get_type_label( (object) $value );
     253        // These properties are read-only and are part of the setting for use in the Customizer UI.
     254        if ( is_array( $value ) ) {
     255            $value_obj               = (object) $value;
     256            $value['type_label']     = isset( $type_label ) ? $type_label : $this->get_type_label( $value_obj );
     257            $value['original_title'] = $this->get_original_title( $value_obj );
    255258        }
    256259
     
    259262
    260263    /**
     264     * Prepares the value for editing on the client.
     265     *
     266     * @since 6.8.3
     267     *
     268     * @return array|false Value prepared for the client.
     269     */
     270    public function js_value() {
     271        $value = parent::js_value();
     272        if ( is_array( $value ) && isset( $value['original_title'] ) ) {
     273            // Decode entities for the sake of displaying the original title as a placeholder.
     274            $value['original_title'] = html_entity_decode( $value['original_title'], ENT_QUOTES, get_bloginfo( 'charset' ) );
     275        }
     276        return $value;
     277    }
     278
     279    /**
    261280     * Get original title.
    262281     *
     
    264283     *
    265284     * @param object $item Nav menu item.
    266      * @return string The original title.
     285     * @return string The original title, without entity decoding.
    267286     */
    268287    protected function get_original_title( $item ) {
     
    290309            }
    291310        }
    292         $original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
    293311        return $original_title;
    294312    }
     
    346364            $this->value['status'] = $this->value['post_status'];
    347365            unset( $this->value['post_status'] );
    348         }
    349 
    350         if ( ! isset( $this->value['original_title'] ) ) {
    351             $this->value['original_title'] = $this->get_original_title( (object) $this->value );
    352366        }
    353367
     
    596610        unset( $item->position );
    597611
    598         if ( empty( $item->original_title ) ) {
    599             $item->original_title = $this->get_original_title( $item );
    600         }
    601612        if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
    602             $item->title = $item->original_title;
     613            $item->title = $item->original_title; // This is NOT entity-decoded. It comes from self::get_original_title().
    603614        }
    604615        if ( $item->title ) {
     
    654665     *
    655666     * @since 4.3.0
    656      *
    657      * @param array $menu_item_value The value to sanitize.
     667     * @since 5.9.0 Renamed `$menu_item_value` to `$value` for PHP 8 named parameter support.
     668     *
     669     * @param array|false $value The menu item value to sanitize.
    658670     * @return array|false|null|WP_Error Null or WP_Error if an input isn't valid. False if it is marked for deletion.
    659671     *                                   Otherwise the sanitized value.
     
    709721        }
    710722
    711         $menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );
    712 
    713723        // Apply the same filters as when calling wp_insert_post().
    714724
  • branches/5.8/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r51295 r60828  
    361361
    362362        foreach ( $query_result as $post ) {
    363             if ( ! $this->check_read_permission( $post ) ) {
     363            if ( 'edit' === $request['context'] ) {
     364                $permission = $this->check_update_permission( $post );
     365            } else {
     366                $permission = $this->check_read_permission( $post );
     367            }
     368
     369            if ( ! $permission ) {
    364370                continue;
    365371            }
  • branches/5.8/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r54548 r60828  
    331331
    332332        foreach ( $query_result as $term ) {
     333            if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) {
     334                continue;
     335            }
     336
    333337            $data       = $this->prepare_item_for_response( $term, $request );
    334338            $response[] = $this->prepare_response_for_collection( $data );
  • branches/5.8/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r56884 r60828  
    202202            return new WP_Error(
    203203                'rest_forbidden_context',
    204                 __( 'Sorry, you are not allowed to list users.' ),
     204                __( 'Sorry, you are not allowed to edit users.' ),
    205205                array( 'status' => rest_authorization_required_code() )
    206206            );
     
    325325
    326326        foreach ( $query->results as $user ) {
     327            if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
     328                continue;
     329            }
     330
    327331            $data    = $this->prepare_item_for_response( $user, $request );
    328332            $users[] = $this->prepare_response_for_collection( $data );
     
    424428        }
    425429
    426         if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
    427             return new WP_Error(
    428                 'rest_user_cannot_view',
    429                 __( 'Sorry, you are not allowed to list users.' ),
     430        if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
     431            return new WP_Error(
     432                'rest_forbidden_context',
     433                __( 'Sorry, you are not allowed to edit this user.' ),
    430434                array( 'status' => rest_authorization_required_code() )
    431435            );
    432         } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) {
     436        }
     437
     438        if ( ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) && ! count_user_posts( $user->ID, $types ) ) {
    433439            return new WP_Error(
    434440                'rest_user_cannot_view',
     
    10251031        }
    10261032
    1027         if ( in_array( 'roles', $fields, true ) ) {
     1033        if ( in_array( 'roles', $fields, true ) && ( current_user_can( 'list_users' ) || current_user_can( 'edit_user', $user->ID ) ) ) {
    10281034            // Defensively call array_values() to ensure an array is returned.
    10291035            $data['roles'] = array_values( $user->roles );
  • branches/5.8/tests/phpunit/tests/customize/nav-menu-item-setting.php

    r49624 r60828  
    9090            'xfn'              => '',
    9191            'status'           => 'publish',
    92             'original_title'   => '',
    9392            'nav_menu_term_id' => 0,
    9493            '_invalid'         => false,
     
    582581            'xfn'              => 'hello  inject',
    583582            'status'           => 'draft',
    584             'original_title'   => 'Hi',
     583            'original_title'   => 'Hi<script>unfilteredHtml()</script>',
    585584            'nav_menu_term_id' => 0,
    586585        );
  • branches/5.8/tests/phpunit/tests/customize/nav-menus.php

    r49108 r60828  
    169169        // Expected menu item array.
    170170        $expected = array(
    171             'id'         => "post-{$post_id}",
    172             'title'      => 'Post Title',
    173             'type'       => 'post_type',
    174             'type_label' => 'Post',
    175             'object'     => 'post',
    176             'object_id'  => (int) $post_id,
    177             'url'        => get_permalink( (int) $post_id ),
     171            'id'             => "post-{$post_id}",
     172            'title'          => 'Post Title',
     173            'original_title' => 'Post Title',
     174            'type'           => 'post_type',
     175            'type_label'     => 'Post',
     176            'object'         => 'post',
     177            'object_id'      => (int) $post_id,
     178            'url'            => get_permalink( (int) $post_id ),
    178179        );
    179180
     
    201202        // Expected menu item array.
    202203        $expected = array(
    203             'id'         => "post-{$page_id}",
    204             'title'      => 'Page Title',
    205             'type'       => 'post_type',
    206             'type_label' => 'Page',
    207             'object'     => 'page',
    208             'object_id'  => (int) $page_id,
    209             'url'        => get_permalink( (int) $page_id ),
     204            'id'             => "post-{$page_id}",
     205            'title'          => 'Page Title',
     206            'original_title' => 'Page Title',
     207            'type'           => 'post_type',
     208            'type_label'     => 'Page',
     209            'object'         => 'page',
     210            'object_id'      => (int) $page_id,
     211            'url'            => get_permalink( (int) $page_id ),
    210212        );
    211213
     
    227229        // Expected menu item array.
    228230        $expected = array(
    229             'id'         => "post-{$post_id}",
    230             'title'      => 'Post Title',
    231             'type'       => 'post_type',
    232             'type_label' => 'Post',
    233             'object'     => 'post',
    234             'object_id'  => (int) $post_id,
    235             'url'        => get_permalink( (int) $post_id ),
     231            'id'             => "post-{$post_id}",
     232            'title'          => 'Post Title',
     233            'original_title' => 'Post Title',
     234            'type'           => 'post_type',
     235            'type_label'     => 'Post',
     236            'object'         => 'post',
     237            'object_id'      => (int) $post_id,
     238            'url'            => get_permalink( (int) $post_id ),
    236239        );
    237240
     
    253256        // Expected menu item array.
    254257        $expected = array(
    255             'id'         => "term-{$term_id}",
    256             'title'      => 'Term Title',
    257             'type'       => 'taxonomy',
    258             'type_label' => 'Category',
    259             'object'     => 'category',
    260             'object_id'  => (int) $term_id,
    261             'url'        => get_term_link( (int) $term_id, 'category' ),
     258            'id'             => "term-{$term_id}",
     259            'title'          => 'Term Title',
     260            'original_title' => 'Term Title',
     261            'type'           => 'taxonomy',
     262            'type_label'     => 'Category',
     263            'object'         => 'category',
     264            'object_id'      => (int) $term_id,
     265            'url'            => get_term_link( (int) $term_id, 'category' ),
    262266        );
    263267
  • branches/5.8/tests/phpunit/tests/rest-api/rest-users-controller.php

    r49603 r60828  
    11271127        $request->set_param( 'context', 'edit' );
    11281128        $response = rest_get_server()->dispatch( $request );
    1129         $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 );
     1129        $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
    11301130    }
    11311131
Note: See TracChangeset for help on using the changeset viewer.