Make WordPress Core

Ticket #20824: capabilities.php.patch

File capabilities.php.patch, 3.8 KB (added by rodrigosprimo, 11 years ago)

Implementation of WP_Users->has_role() and current_user_has_role()

  • src/wp-includes/capabilities.php

     
    819819        }
    820820
    821821        /**
     822         * Whether user has role.
     823         *
     824         * @since 3.7.0
     825         * @access public
     826         *
     827         * @param string $role Role name to search.
     828         * @return bool True, if user has role; false, if user does not have role.
     829         */
     830        function has_role( $role ) {
     831                if ( in_array($role, $this->roles) ) {
     832                        return true;
     833                }
     834               
     835                return false;
     836        }
     837
     838        /**
    822839         * Choose the maximum level the user has.
    823840         *
    824841         * Will compare the level from the $item parameter against the $max
     
    906923        }
    907924
    908925        /**
    909          * Whether user has capability or role name.
     926         * Whether user has capability.
    910927         *
    911          * This is useful for looking up whether the user has a specific role
    912          * assigned to the user. The second optional parameter can also be used to
     928         * The second optional parameter can also be used to
    913929         * check for capabilities against a specific object, such as a post or user.
    914930         *
    915931         * @since 2.0.0
    916932         * @access public
    917933         *
    918          * @param string|int $cap Capability or role name to search.
     934         * @param string|int $cap Capability name to search.
    919935         * @return bool True, if user has capability; false, if user does not have capability.
    920936         */
    921937        function has_cap( $cap ) {
     
    12681284}
    12691285
    12701286/**
    1271  * Whether current user has capability or role.
     1287 * Whether current user has capability.
    12721288 *
    12731289 * @since 2.0.0
    12741290 *
    1275  * @param string $capability Capability or role name.
     1291 * @param string $capability Capability name.
    12761292 * @return bool
    12771293 */
    12781294function current_user_can( $capability ) {
     
    12881304}
    12891305
    12901306/**
    1291  * Whether current user has a capability or role for a given blog.
     1307 * Whether current user has a capability for a given blog.
    12921308 *
    12931309 * @since 3.0.0
    12941310 *
    12951311 * @param int $blog_id Blog ID
    1296  * @param string $capability Capability or role name.
     1312 * @param string $capability Capability.
    12971313 * @return bool
    12981314 */
    12991315function current_user_can_for_blog( $blog_id, $capability ) {
     
    13171333}
    13181334
    13191335/**
    1320  * Whether author of supplied post has capability or role.
     1336 * Whether author of supplied post has capability.
    13211337 *
    13221338 * @since 2.9.0
    13231339 *
    13241340 * @param int|object $post Post ID or post object.
    1325  * @param string $capability Capability or role name.
     1341 * @param string $capability Capability.
    13261342 * @return bool
    13271343 */
    13281344function author_can( $post, $capability ) {
     
    13411357}
    13421358
    13431359/**
    1344  * Whether a particular user has capability or role.
     1360 * Whether a particular user has capability.
    13451361 *
    13461362 * @since 3.1.0
    13471363 *
    13481364 * @param int|object $user User ID or object.
    1349  * @param string $capability Capability or role name.
     1365 * @param string $capability Capability.
    13501366 * @return bool
    13511367 */
    13521368function user_can( $user, $capability ) {
     
    13631379}
    13641380
    13651381/**
     1382 * Whether current user has role.
     1383 *
     1384 * @since 3.7.0
     1385 *
     1386 * @param string $role role name.
     1387 * @return bool
     1388 */
     1389function current_user_has_role( $role ) {
     1390        $current_user = wp_get_current_user();
     1391
     1392        if ( empty( $current_user ) )
     1393                return false;
     1394       
     1395        return $current_user->has_role( $role );
     1396}
     1397
     1398/**
    13661399 * Retrieve role object.
    13671400 *
    13681401 * @see WP_Roles::get_role() Uses method to retrieve role object.
  • tests/phpunit/tests/user/capabilities.php

     
    667667
    668668                wp_set_current_user( $old_uid );
    669669        }
     670       
     671        function test_current_user_has_role() {
     672                $user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     673                wp_set_current_user( $user->ID );
     674               
     675                $this->assertTrue(current_user_has_role('administrator'));
     676                $this->assertFalse(current_user_has_role('subscriber'));
     677        }
    670678}