WordPress.org

Make WordPress Core

Changeset 45622


Ignore:
Timestamp:
07/11/2019 11:45:22 PM (5 months ago)
Author:
pento
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/capabilities.php

    r45590 r45622  
    3434 * @return array Actual capabilities for meta capability.
    3535 */
    36 function map_meta_cap( $cap, $user_id ) {
    37     $args = array_slice( func_get_args(), 2 );
     36function map_meta_cap( $cap, $user_id, ...$args ) {
    3837    $caps = array();
    3938
     
    650649 *              passed, whether the current user has the given meta capability for the given object.
    651650 */
    652 function current_user_can( $capability ) {
     651function current_user_can( $capability, ...$args ) {
    653652    $current_user = wp_get_current_user();
    654653
     
    657656    }
    658657
    659     $args = array_slice( func_get_args(), 1 );
    660     $args = array_merge( array( $capability ), $args );
    661 
    662     return call_user_func_array( array( $current_user, 'has_cap' ), $args );
     658    return $current_user->has_cap( $capability, ...$args );
    663659}
    664660
     
    683679 * @return bool Whether the user has the given capability.
    684680 */
    685 function current_user_can_for_blog( $blog_id, $capability ) {
     681function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
    686682    $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
    687683
     
    695691    }
    696692
    697     $args = array_slice( func_get_args(), 2 );
    698     $args = array_merge( array( $capability ), $args );
    699 
    700     $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
     693    $can = $current_user->has_cap( $capability, ...$args );
    701694
    702695    if ( $switched ) {
     
    727720 * @return bool Whether the post author has the given capability.
    728721 */
    729 function author_can( $post, $capability ) {
     722function author_can( $post, $capability, ...$args ) {
    730723    $post = get_post( $post );
    731724    if ( ! $post ) {
     
    739732    }
    740733
    741     $args = array_slice( func_get_args(), 2 );
    742     $args = array_merge( array( $capability ), $args );
    743 
    744     return call_user_func_array( array( $author, 'has_cap' ), $args );
     734    return $author->has_cap( $capability, ...$args );
    745735}
    746736
     
    765755 * @return bool Whether the user has the given capability.
    766756 */
    767 function user_can( $user, $capability ) {
     757function user_can( $user, $capability, ...$args ) {
    768758    if ( ! is_object( $user ) ) {
    769759        $user = get_userdata( $user );
     
    774764    }
    775765
    776     $args = array_slice( func_get_args(), 2 );
    777     $args = array_merge( array( $capability ), $args );
    778 
    779     return call_user_func_array( array( $user, 'has_cap' ), $args );
     766    return $user->has_cap( $capability, ...$args );
    780767}
    781768
Note: See TracChangeset for help on using the changeset viewer.