Skip to content

Refactor WP_User capabilities handling:#10309

Open
jonnynews wants to merge 1 commit intoWordPress:trunkfrom
spacedmonkey:fix/wp_author-3
Open

Refactor WP_User capabilities handling:#10309
jonnynews wants to merge 1 commit intoWordPress:trunkfrom
spacedmonkey:fix/wp_author-3

Conversation

@jonnynews
Copy link
Copy Markdown

  • Made caps, roles, and allcaps public to improve access flexibility.
  • Introduced $short_init to enable optimized initialization for use cases where full capability and role loading is unnecessary.
  • Removed redundant lazy-loading logic for these properties, simplifying the class structure.
  • Adjusted documentation for affected properties and methods.

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

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

- Made `caps`, `roles`, and `allcaps` public to improve access flexibility.
- Introduced `$short_init` to enable optimized initialization for use cases where full capability and role loading is unnecessary.
- Removed redundant lazy-loading logic for these properties, simplifying the class structure.
- Adjusted documentation for affected properties and methods.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 16, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey, westonruter, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

* @param bool $short_init Optional. Whether to skip initializing capabilities and roles.
*/
public function init( $data, $site_id = 0 ) {
public function init( $data, $site_id = 0, $short_init = false ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This $short_init arg isn't used anywhere?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, init() isn't being called anywhere with this argument provided, right?

*/
private static $back_compat_keys;

/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
/**

@peterwilsoncc
Copy link
Copy Markdown
Contributor

@jonnynews There's a couple of failing unit tests, are you able to look at those.

To ensure the caps are available when using get_object_vars(), are you able to add this to tests/phpunit/tests/user/capabilities.php. The data provider already exists.

/**
 * Ensure get_object_vars includes allcaps.
 *
 * @ticket 58001
 *
 * @dataProvider data_single_site_roles_to_check
 */
public function test_get_object_vars_includes_roles( $role ) {
	$user_id        = self::$users[ $role ]->ID;
	$user           = new WP_User( $user_id );
	$vars           = get_object_vars( $user );
	$primitive_caps = $this->getPrimitiveCapsAndRoles();

	$expected = array();
	foreach ( $primitive_caps as $cap => $roles ) {
		if ( in_array( $role, $roles, true ) ) {
			$expected[] = $cap;
		}
	}

	$this->assertArrayHasKey( 'allcaps', $vars, 'WP_User object vars should include allcaps key.' );

	$actual = $vars['allcaps'];
	$actual = array_keys( $actual );

	// Remove special cases.
	$special_cases = array(
		// Role names.
		'administrator',
		'editor',
		'author',
		'subscriber',
		'contributor',

		// Granted via `user_has_cap`.
		'resume_plugins',
		'resume_themes',
		'view_site_health_checks',

		// Other capabilities.
		'manage_links',
		'unfiltered_upload',
	);

	$actual   = array_diff( $actual, $special_cases );
	$expected = array_diff( $expected, $special_cases );
	$this->assertSameSets( $expected, $actual, "User with the {$role} role should have the correct primitive capabilities in allcaps." );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants