Code Modernisation: Introduce the spread operator in capabilities.php.

Rather than relying `func_get_args()` to retrieve arbitrary function arguments, we can now use the spread operator to assign them directly to a variable.

Props jrf.
See #47678.

Built from https://develop.svn.wordpress.org/trunk@45622


git-svn-id: http://core.svn.wordpress.org/trunk@45433 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2019-07-11 23:46:56 +00:00
parent ff723a2fba
commit 5d658427a8
2 changed files with 10 additions and 23 deletions

View File

@ -33,8 +33,7 @@
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return array Actual capabilities for meta capability.
*/
function map_meta_cap( $cap, $user_id ) {
$args = array_slice( func_get_args(), 2 );
function map_meta_cap( $cap, $user_id, ...$args ) {
$caps = array();
switch ( $cap ) {
@ -649,17 +648,14 @@ function map_meta_cap( $cap, $user_id ) {
* @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
* passed, whether the current user has the given meta capability for the given object.
*/
function current_user_can( $capability ) {
function current_user_can( $capability, ...$args ) {
$current_user = wp_get_current_user();
if ( empty( $current_user ) ) {
return false;
}
$args = array_slice( func_get_args(), 1 );
$args = array_merge( array( $capability ), $args );
return call_user_func_array( array( $current_user, 'has_cap' ), $args );
return $current_user->has_cap( $capability, ...$args );
}
/**
@ -682,7 +678,7 @@ function current_user_can( $capability ) {
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability.
*/
function current_user_can_for_blog( $blog_id, $capability ) {
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();
@ -694,10 +690,7 @@ function current_user_can_for_blog( $blog_id, $capability ) {
return false;
}
$args = array_slice( func_get_args(), 2 );
$args = array_merge( array( $capability ), $args );
$can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
$can = $current_user->has_cap( $capability, ...$args );
if ( $switched ) {
restore_current_blog();
@ -726,7 +719,7 @@ function current_user_can_for_blog( $blog_id, $capability ) {
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the post author has the given capability.
*/
function author_can( $post, $capability ) {
function author_can( $post, $capability, ...$args ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
@ -738,10 +731,7 @@ function author_can( $post, $capability ) {
return false;
}
$args = array_slice( func_get_args(), 2 );
$args = array_merge( array( $capability ), $args );
return call_user_func_array( array( $author, 'has_cap' ), $args );
return $author->has_cap( $capability, ...$args );
}
/**
@ -764,7 +754,7 @@ function author_can( $post, $capability ) {
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability.
*/
function user_can( $user, $capability ) {
function user_can( $user, $capability, ...$args ) {
if ( ! is_object( $user ) ) {
$user = get_userdata( $user );
}
@ -773,10 +763,7 @@ function user_can( $user, $capability ) {
return false;
}
$args = array_slice( func_get_args(), 2 );
$args = array_merge( array( $capability ), $args );
return call_user_func_array( array( $user, 'has_cap' ), $args );
return $user->has_cap( $capability, ...$args );
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-45621';
$wp_version = '5.3-alpha-45622';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.