mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Roles/Caps: Return same result from current_user_can and user_can().
Ensure `current_user_can()` and `user_can()` return the same results for logged out users. For core capabilities this changes `user_can( 0, 'exist' )` to return `true` rather than `false` in line with `current_user_can( 'exist' )` for logged out users. Convert `current_user_can()` and `current_user_can_for_blog()` to wrapper functions ultimately calling `user_can()`. Add anonymous user to primitive capability checks as appropriate. Convert `Tests_User_Capabilities::test_other_caps_for_all_roles()` to use a data provider and add tests to check whether user exists in the database (`WP_User::exists()`) as that intentionally differs from the `exist` capability. Props jjj, johnbillion, peterwilsoncc, SergeyBiryukov, TimothyBlynJacobs. Fixes #52076. git-svn-id: https://develop.svn.wordpress.org/trunk@50490 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -679,6 +679,7 @@ function map_meta_cap( $cap, $user_id, ...$args ) {
|
||||
* @since 2.0.0
|
||||
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
|
||||
* by adding it to the function signature.
|
||||
* @since 5.8.0 Converted to wrapper for the user_can() function.
|
||||
*
|
||||
* @see WP_User::has_cap()
|
||||
* @see map_meta_cap()
|
||||
@@ -689,13 +690,7 @@ function map_meta_cap( $cap, $user_id, ...$args ) {
|
||||
* passed, whether the current user has the given meta capability for the given object.
|
||||
*/
|
||||
function current_user_can( $capability, ...$args ) {
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
if ( empty( $current_user ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $current_user->has_cap( $capability, ...$args );
|
||||
return user_can( wp_get_current_user(), $capability, ...$args );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -714,6 +709,7 @@ function current_user_can( $capability, ...$args ) {
|
||||
* @since 3.0.0
|
||||
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
|
||||
* by adding it to the function signature.
|
||||
* @since 5.8.0 Wraps current_user_can() after switching to blog.
|
||||
*
|
||||
* @param int $blog_id Site ID.
|
||||
* @param string $capability Capability name.
|
||||
@@ -723,16 +719,7 @@ function current_user_can( $capability, ...$args ) {
|
||||
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
|
||||
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
|
||||
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
if ( empty( $current_user ) ) {
|
||||
if ( $switched ) {
|
||||
restore_current_blog();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
$can = $current_user->has_cap( $capability, ...$args );
|
||||
$can = current_user_can( $capability, ...$args );
|
||||
|
||||
if ( $switched ) {
|
||||
restore_current_blog();
|
||||
@@ -805,8 +792,10 @@ function user_can( $user, $capability, ...$args ) {
|
||||
$user = get_userdata( $user );
|
||||
}
|
||||
|
||||
if ( ! $user || ! $user->exists() ) {
|
||||
return false;
|
||||
if ( empty( $user ) ) {
|
||||
// User is logged out, create anonymous user object.
|
||||
$user = new WP_User( 0 );
|
||||
$user->init( new stdClass );
|
||||
}
|
||||
|
||||
return $user->has_cap( $capability, ...$args );
|
||||
|
||||
Reference in New Issue
Block a user