Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#62959 new enhancement

Ensure wp_roles() returns an instance of WP_Roles

Reported by: marian1's profile marian1 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version:
Component: Role/Capability Keywords: has-patch
Focuses: Cc:

Description

Currently, wp_roles() only checks whether the global variable $wp_roles is set before returning it. As global variables can be overridden, it should verify that $wp_roles is indeed a WP_Roles object to ensure that wp_roles() consistently returns a WP_Roles instance.

Refer to wp_scripts() and wp_styles() for the correct approach.

See #62562 for another example where the type of a global variable is not properly checked.

Change History (1)

This ticket was mentioned in PR #8315 on WordPress/wordpress-develop by @marian1.


4 weeks ago
#1

  • Keywords has-patch added

The original implementation of wp_roles() only checked whether the global variable $wp_roles was set (isset) before returning it. This could lead to issues if $wp_roles was set to a non-WP_Roles value, resulting in potential errors or unexpected behaviour.

The fix introduces an additional check to ensure that $wp_roles is an instance of WP_Roles. If it is not, a new WP_Roles object is instantiated, ensuring that wp_roles() always returns a valid WP_Roles instance.

Trac ticket: https://core.trac.wordpress.org/ticket/62959

Note: See TracTickets for help on using tickets.