Make WordPress Core

Changeset 31190


Ignore:
Timestamp:
01/16/2015 01:48:36 AM (10 years ago)
Author:
wonderboymusic
Message:

In WP_User, ->get_role_caps() and ->update_user_level_from_caps() must be called inside ->add_cap() and ->remove_cap() after updating user meta. ->has_cap() checks are currently failing directly after calling ->add_cap().

Adds unit test.

Props rachelbaker.
Fixes #28374.

Location:
trunk
Files:
2 edited

Legend:

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

    r31188 r31190  
    935935        $this->caps[$cap] = $grant;
    936936        update_user_meta( $this->ID, $this->cap_key, $this->caps );
     937        $this->get_role_caps();
     938        $this->update_user_level_from_caps();
    937939    }
    938940
     
    946948     */
    947949    public function remove_cap( $cap ) {
    948         if ( ! isset( $this->caps[$cap] ) )
     950        if ( ! isset( $this->caps[ $cap ] ) ) {
    949951            return;
    950         unset( $this->caps[$cap] );
     952        }
     953        unset( $this->caps[ $cap ] );
    951954        update_user_meta( $this->ID, $this->cap_key, $this->caps );
     955        $this->get_role_caps();
     956        $this->update_user_level_from_caps();
    952957    }
    953958
  • trunk/tests/phpunit/tests/user/capabilities.php

    r27390 r31190  
    695695        wp_set_current_user( $old_uid );
    696696    }
     697
     698    /**
     699     * @ticket 28374
     700     */
     701    function test_current_user_edit_caps() {
     702        $user = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
     703        wp_set_current_user( $user->ID );
     704
     705        $user->add_cap( 'publish_posts' );
     706        $user->add_cap( 'publish_pages' );
     707        $this->assertTrue( $user->has_cap( 'publish_posts' ) );
     708        $this->assertTrue( $user->has_cap( 'publish_pages' ) );
     709
     710        $user->remove_cap( 'publish_pages' );
     711        $this->assertFalse( $user->has_cap( 'publish_pages' ) );
     712    }
    697713}
Note: See TracChangeset for help on using the changeset viewer.