-
Notifications
You must be signed in to change notification settings - Fork 3k
Lazy load user capabilities in WP_User object #5098
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
base: trunk
Are you sure you want to change the base?
Changes from 6 commits
a02f807
06cbd09
8046889
43d8eb3
c95b154
ba07a49
ab9fd92
623d977
64cefd6
719028e
10df03b
8de554a
154a136
2725437
242545f
b5307f5
18c48c2
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 | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -61,7 +61,7 @@ class WP_User { | |||||||||||||||||||||||
* @var bool[] Array of key/value pairs where keys represent a capability name | ||||||||||||||||||||||||
* and boolean values represent whether the user has that capability. | ||||||||||||||||||||||||
Comment on lines
65
to
66
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. See above. Not a problem in this PR, but since we're touching it, it would be good to fix this incorrect type annotation.
Suggested change
|
||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public $caps = array(); | ||||||||||||||||||||||||
protected $caps = array(); | ||||||||||||||||||||||||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* User metadata option name. | ||||||||||||||||||||||||
|
@@ -77,7 +77,7 @@ class WP_User { | |||||||||||||||||||||||
* @since 2.0.0 | ||||||||||||||||||||||||
* @var string[] | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public $roles = array(); | ||||||||||||||||||||||||
protected $roles = array(); | ||||||||||||||||||||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* All capabilities the user has, including individual and role based. | ||||||||||||||||||||||||
|
@@ -86,7 +86,7 @@ class WP_User { | |||||||||||||||||||||||
* @var bool[] Array of key/value pairs where keys represent a capability name | ||||||||||||||||||||||||
* and boolean values represent whether the user has that capability. | ||||||||||||||||||||||||
Comment on lines
90
to
91
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. See above:
Suggested change
|
||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public $allcaps = array(); | ||||||||||||||||||||||||
protected $allcaps = array(); | ||||||||||||||||||||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* The filter context applied to user data fields. | ||||||||||||||||||||||||
|
@@ -104,6 +104,9 @@ class WP_User { | |||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
private $site_id = 0; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
private $loaded_caps = false; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* @since 3.3.0 | ||||||||||||||||||||||||
* @var array | ||||||||||||||||||||||||
|
@@ -286,6 +289,10 @@ public function __isset( $key ) { | |||||||||||||||||||||||
$key = 'ID'; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( in_array( $key, array( 'caps', 'allcaps', 'roles' ), true ) ) { | ||||||||||||||||||||||||
return isset( $this->$key ); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( isset( $this->data->$key ) ) { | ||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
@@ -319,6 +326,11 @@ public function __get( $key ) { | |||||||||||||||||||||||
return $this->ID; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( in_array( $key, array( 'caps', 'allcaps', 'roles' ), true ) ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
return $this->$key; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( isset( $this->data->$key ) ) { | ||||||||||||||||||||||||
$value = $this->data->$key; | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
|
@@ -513,6 +525,10 @@ public function get_role_caps() { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
$wp_roles = wp_roles(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( ! $this->loaded_caps ) { | ||||||||||||||||||||||||
$this->caps = $this->get_caps_data(); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Comment on lines
+538
to
+541
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 is unnecessary I think. The
Suggested change
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. The lines below checks if caps is an array.
I wanted to ensure 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. My point is that that's unnecessary to check, because it'll always be set correctly based on 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. The method 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. Hmm, that's a fair point. Could you add an inline comment to clarify this? For example:
Suggested change
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. In addition, ideally this method would also set |
||||||||||||||||||||||||
// 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' ) ); | ||||||||||||||||||||||||
|
@@ -546,6 +562,7 @@ public function add_role( $role ) { | |||||||||||||||||||||||
if ( empty( $role ) ) { | ||||||||||||||||||||||||
return; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( in_array( $role, $this->roles, true ) ) { | ||||||||||||||||||||||||
return; | ||||||||||||||||||||||||
|
@@ -575,6 +592,7 @@ public function add_role( $role ) { | |||||||||||||||||||||||
* @param string $role Role name. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public function remove_role( $role ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
if ( ! in_array( $role, $this->roles, true ) ) { | ||||||||||||||||||||||||
return; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
@@ -607,6 +625,7 @@ public function remove_role( $role ) { | |||||||||||||||||||||||
* @param string $role Role name. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public function set_role( $role ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
if ( 1 === count( $this->roles ) && current( $this->roles ) === $role ) { | ||||||||||||||||||||||||
return; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
@@ -708,6 +727,7 @@ public function update_user_level_from_caps() { | |||||||||||||||||||||||
* @param bool $grant Whether to grant capability to user. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public function add_cap( $cap, $grant = true ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
$this->caps[ $cap ] = $grant; | ||||||||||||||||||||||||
update_user_meta( $this->ID, $this->cap_key, $this->caps ); | ||||||||||||||||||||||||
$this->get_role_caps(); | ||||||||||||||||||||||||
|
@@ -722,6 +742,7 @@ public function add_cap( $cap, $grant = true ) { | |||||||||||||||||||||||
* @param string $cap Capability name. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public function remove_cap( $cap ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
if ( ! isset( $this->caps[ $cap ] ) ) { | ||||||||||||||||||||||||
return; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
@@ -774,6 +795,8 @@ public function remove_all_caps() { | |||||||||||||||||||||||
* the given capability for that object. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
public function has_cap( $cap, ...$args ) { | ||||||||||||||||||||||||
$this->load_capablity_data(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ( is_numeric( $cap ) ) { | ||||||||||||||||||||||||
_deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) ); | ||||||||||||||||||||||||
$cap = $this->translate_level_to_cap( $cap ); | ||||||||||||||||||||||||
|
@@ -876,9 +899,7 @@ public function for_site( $site_id = '' ) { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
$this->caps = $this->get_caps_data(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
$this->get_role_caps(); | ||||||||||||||||||||||||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
wp_lazyload_user_meta( array( $this->ID ) ); | ||||||||||||||||||||||||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
|
@@ -909,4 +930,17 @@ private function get_caps_data() { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
return $caps; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Load capability data. | ||||||||||||||||||||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @since 6.4.0 | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
private function load_capablity_data() { | ||||||||||||||||||||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
if ( ! $this->loaded_caps ) { | ||||||||||||||||||||||||
$this->caps = $this->get_caps_data(); | ||||||||||||||||||||||||
$this->get_role_caps(); | ||||||||||||||||||||||||
$this->loaded_caps = true; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.