-
Notifications
You must be signed in to change notification settings - Fork 3.3k
User roles property should always be an array, but they sometimes become an object in localized data #8790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User roles property should always be an array, but they sometimes become an object in localized data #8790
Changes from 7 commits
4e32ecf
6e53e6d
3958cca
db42295
70a0ad2
d2cb3e0
2fe2fe8
7fcc427
a0753b1
7c6213f
01fba79
9323c3e
39fb17b
3c3f459
be680ac
8e155fd
cc0c584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,7 +517,12 @@ public function get_role_caps() { | |
|
|
||
| // Filter out caps that are not role names and assign to $this->roles. | ||
| if ( is_array( $this->caps ) ) { | ||
| $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); | ||
| $this->roles = array(); | ||
| foreach ( $this->caps as $cap => $granted ) { | ||
| if ( $wp_roles->is_role( $cap ) ) { | ||
| $this->roles[] = $cap; | ||
| } | ||
| } | ||
|
Comment on lines
+520
to
+525
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified to use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the very first commit of this PR, I used an However, that approach had a downside — chaining multiple array functions such as Following feedback from @SirLouen , I replaced the chained array operations with a simple foreach loop. This improves code performance, reduces time complexity, and makes the logic easier to read and maintain. You can see the related discussion in the commit linked above.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there numbers on the performance effect?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a simpler approach can make the code a bit faster and cleaner, why not use it? Even small improvements like this contribute to making WordPress overall a little more performant. In the previous version, with the addition of
A simple foreach loop, by contrast:
|
||
| } | ||
|
|
||
| // Build $allcaps from role caps, overlay user's $caps. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1931,6 +1931,44 @@ public function test_send_confirmation_on_profile_email_html_entities_decoded() | |
| $this->assertStringNotContainsString( ''Test' blog's "name" has <html entities> &', $email->subject, 'Email subject does contains HTML entities' ); | ||
| } | ||
|
|
||
| /** | ||
| * Check array is a sequential. | ||
| * | ||
| * @param array $arr array. | ||
haruncpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @ticket 63427 | ||
| * | ||
haruncpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @return bool | ||
| */ | ||
| private function is_sequential( array $arr ) { | ||
| return array_keys( $arr ) === range( 0, count( $arr ) - 1 ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the `roles` property is an sequential array. | ||
| * | ||
| * @ticket 63427 | ||
| * | ||
| * @return void | ||
haruncpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public function test_user_roles_property_is_sequential_array() { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case method is not from earlier code—it was specifically written for this PR and is directly related to ticket #63427. It’s safe and will not cause any breakage.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh my bad, they are actually unit tests 🤣 I was reading this part this morning on my phone and I completely mixed up |
||
| $user = new WP_User( self::$author_id ); | ||
| $this->assertTrue( $this->is_sequential( $user->roles ) ); | ||
haruncpi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $user->remove_role( 'author' ); | ||
| $this->assertIsArray( $user->roles ); | ||
| $this->assertSame( array(), $user->roles ); | ||
|
|
||
| $user->add_role( 'author' ); | ||
| $this->assertSame( array( 'author' ), $user->roles ); | ||
| $this->assertTrue( $this->is_sequential( $user->roles ) ); | ||
|
|
||
| $user->add_role( 'custom_role' ); | ||
| $user->add_role( 'subscriber' ); | ||
| $this->assertSame( array( 'author', 'subscriber' ), $user->roles ); | ||
| $this->assertTrue( $this->is_sequential( $user->roles ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 42564 | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.