Skip to content

Commit 439f966

Browse files
yaswanth.kumaryaswanthkumar1995
authored andcommitted
Fix view_stats capability check for multi-role users
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 #47258
1 parent 39a0718 commit 439f966

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fix view_stats capability check for users with multiple roles by checking all roles instead of only the first.

projects/packages/stats/src/class-main.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,11 @@ public static function map_meta_caps( $caps, $cap, $user_id ) {
127127
$user = new WP_User( $user_id );
128128
// WordPress 6.9 introduced lazy-loading of some WP_User properties, including `roles`.
129129
// It also made said properties protected, so we can't modify keys directly.
130-
$user_roles = $user->roles;
131-
$user_role = array_shift( $user_roles ); // Work with the copy
130+
$user_roles = (array) $user->roles;
132131
$stats_roles = Options::get_option( 'roles' );
133132

134-
// Is the users role in the available stats roles?
135-
if ( is_array( $stats_roles ) && in_array( $user_role, $stats_roles, true ) ) {
133+
// Is any of the user's roles in the available stats roles?
134+
if ( is_array( $stats_roles ) && ! empty( array_intersect( $user_roles, $stats_roles ) ) ) {
136135
$caps = array( 'read' );
137136
}
138137
}

projects/packages/stats/tests/php/Main_Test.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,25 @@ public function test_view_stats_meta_mapping() {
178178
$this->assertTrue( user_can( $dummy_user_id, 'view_stats' ) );
179179
}
180180

181+
/**
182+
* Test Main::map_meta_caps with multi-role user where admin is not the first role.
183+
*/
184+
public function test_view_stats_meta_mapping_multi_role() {
185+
$dummy_user_id = wp_insert_user(
186+
array(
187+
'user_login' => 'dummy_multirole',
188+
'user_pass' => 'password',
189+
'role' => 'subscriber',
190+
)
191+
);
192+
193+
// Add administrator as a second role.
194+
$user = new \WP_User( $dummy_user_id );
195+
$user->add_role( 'administrator' );
196+
197+
$this->assertTrue( user_can( $dummy_user_id, 'view_stats' ) );
198+
}
199+
181200
/**
182201
* Test Main::should_track
183202
*/

0 commit comments

Comments
 (0)