WordPress.org

Make WordPress Core

Ticket #36376: for36376.diff

File for36376.diff, 2.3 KB (added by bamadesigner, 3 years ago)

Patch for ticket 36376

  • src/wp-includes/class-wp-user.php

     
    486486         *
    487487         * @return array List of all capabilities for the user.
    488488         */
    489         public function get_role_caps() {
     489         public function get_role_caps() {
    490490                $switch_site = false;
    491491                if ( is_multisite() && $this->site_id != get_current_blog_id() ) {
    492492                        $switch_site = true;
     
    494494                        switch_to_blog( $this->site_id );
    495495                }
    496496
    497                 $wp_roles = wp_roles();
     497                $wp_roles = wp_roles();
    498498
    499                 // Filter out caps that are not role names and assign to $this->roles.
    500                 if ( is_array( $this->caps ) ) {
    501                         $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
     499                // Filter out caps that are not role names and assign to $this->roles.
     500                if ( is_array( $this->caps ) ) {
     501                        $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
    502502                }
    503503
    504                 // Build $allcaps from role caps, overlay user's $caps.
    505                 $this->allcaps = array();
    506                 foreach ( (array) $this->roles as $role ) {
    507                         $the_role      = $wp_roles->get_role( $role );
    508                         $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
    509                 }
    510                 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
     504                // Build $allcaps from role caps, overlay user's $caps.
     505                $this->allcaps = array();
     506                foreach ( (array) $this->roles as $role ) {
     507                        $the_role = $wp_roles->get_role( $role );
    511508
     509                        /*
     510                         * Merge role capabilities.
     511                         * Have to use this approach, instead of array_merge,
     512                         * to make sure a "0" in one role doesn't overwrite a "1" on another.
     513                         */
     514                        foreach( (array) $the_role->capabilities as $cap => $value ) {
     515                                if ( ! isset( $this->allcaps[ $cap ] ) || ( ! $this->allcaps[ $cap ] && $value ) ) {
     516                                        $this->allcaps[ $cap ] = $value;
     517                                }
     518                        }
     519                }
     520
     521                // Merge role capabilities.
     522                foreach( (array) $this->caps as $cap => $value ) {
     523                        if ( ! isset( $this->allcaps[ $cap ] ) || ( ! $this->allcaps[ $cap ] && $value ) ) {
     524                                $this->allcaps[ $cap ] = $value;
     525                        }
     526                }
     527
    512528                if ( $switch_site ) {
    513529                        restore_current_blog();
    514530                }
    515531
    516                 return $this->allcaps;
    517         }
     532                return $this->allcaps;
     533        }
    518534
    519535        /**
    520536         * Add role to user.