Make WordPress Core


Ignore:
Timestamp:
11/11/2025 12:51:01 PM (3 months ago)
Author:
SergeyBiryukov
Message:

Users: Initialize the WP_User::$roles property as a sequential array.

Previously, if any roles were filtered out via array_filter() when assigning the WP_User::$roles property in WP_User::get_role_caps(), the resulting array could contain non-sequential numeric keys, which would then cause it to be treated as an object when JSON-encoded, e.g. in wp_localize_script(), instead of a proper array, breaking client-side expectations.

This commit ensures that the WP_User::$roles property is always treated as an array.

Follow-up to [2703], [2793], [22118].

Props haruncpi, peterwilsoncc, SirLouen, getsyash, wildworks, johnjamesjacoby, SergeyBiryukov.
Fixes #63427.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user.php

    r61037 r61210  
    516516        $wp_roles = wp_roles();
    517517
    518         // Filter out caps that are not role names and assign to $this->roles.
     518        // Select caps that are role names and assign to $this->roles.
    519519        if ( is_array( $this->caps ) ) {
    520             $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
     520            $this->roles = array();
     521
     522            foreach ( $this->caps as $key => $value ) {
     523                if ( $wp_roles->is_role( $key ) ) {
     524                    $this->roles[] = $key;
     525                }
     526            }
    521527        }
    522528
Note: See TracChangeset for help on using the changeset viewer.