Skip to content

Fix view_stats capability check for users with multiple roles#47991

Open
yaswanthkumar1995 wants to merge 1 commit intoAutomattic:trunkfrom
yaswanthkumar1995:fix/47258-stats-view-multi-role
Open

Fix view_stats capability check for users with multiple roles#47991
yaswanthkumar1995 wants to merge 1 commit intoAutomattic:trunkfrom
yaswanthkumar1995:fix/47258-stats-view-multi-role

Conversation

@yaswanthkumar1995
Copy link
Copy Markdown
Contributor

Fixes #47258

Proposed changes

  • Fixed view_stats meta-capability mapping to check all user roles instead of only the first one.
  • Replaced array_shift() + in_array() with array_intersect() so multi-role users (e.g. customer + administrator) are correctly granted stats access.
  • Added a test for multi-role users where administrator is not the first role.

Other information

The bug occurs because array_shift($user_roles) only returns the first role in the array. For users who were assigned a non-admin role before being promoted to administrator, the first role (e.g. customer) isn't in the stats allowlist, so they're denied access.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

  • Create a test user with subscriber role
  • Add administrator as a second role: $user->add_role('administrator')
  • Verify user_can($user_id, 'view_stats') returns true
  • Previously this would return false because only the first role (subscriber) was checked

The view_stats meta-cap mapping only checked the first role in the
user's roles array via array_shift(). Users with multiple roles (e.g.
customer + administrator) could be denied stats access if their first
role wasn't in the stats roles allowlist.

Use array_intersect() to check all user roles against the allowed
stats roles instead of only the first.

Fixes Automattic#47258
@yaswanthkumar1995
Copy link
Copy Markdown
Contributor Author

/assign

@yaswanthkumar1995
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


@github-actions github-actions bot added the OSS Citizen This Pull Request was opened by an Open Source contributor. label Apr 8, 2026
@jeherve jeherve requested a review from a team April 8, 2026 07:31
@jeherve jeherve added Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. [Pri] Normal [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. labels Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. OSS Citizen This Pull Request was opened by an Open Source contributor. [Package] Stats Data [Pri] Normal [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jetpack Stats view_stats meta-cap mapping only checks first user role, admins with multiple roles may be unable to view stats

2 participants