Skip to:
Content

BuddyPress.org

Ticket #7225: 7225.01.patch

File 7225.01.patch, 1.5 KB (added by dcavins, 3 years ago)

Filter member types to make sure they're registered.

  • src/bp-members/bp-members-functions.php

    diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
    index 8ee5a6d..a3b911e 100644
    function bp_get_member_type( $user_id, $single = true ) { 
    27502750                $types = bp_get_object_terms( $user_id, 'bp_member_type' );
    27512751
    27522752                if ( ! is_wp_error( $types ) ) {
     2753                        // Filter out currently unregistered member types.
     2754                        foreach ( $types as $key => $mtype ) {
     2755                                if ( ! bp_get_member_type_object( $mtype->name ) ) {
     2756                                        unset( $types[$key] );
     2757                                }
     2758                        }
     2759
    27532760                        $types = wp_list_pluck( $types, 'name' );
    27542761                        wp_cache_set( $user_id, $types, 'bp_member_member_type' );
    27552762                }
  • tests/phpunit/testcases/members/types.php

    diff --git tests/phpunit/testcases/members/types.php tests/phpunit/testcases/members/types.php
    index 91dccd0..9170d3e 100644
    class BP_Tests_Members_Types extends BP_UnitTestCase { 
    392392                $types = bp_get_member_type( $u1, false );
    393393                $this->assertEqualSets( array( 'bar', 'foo' ), $types );
    394394        }
     395
     396        public function test_bp_get_member_type_should_not_return_unregistered_types() {
     397                $u1 = $this->factory->user->create();
     398                bp_register_member_type( 'foo' );
     399                bp_set_member_type( $u1, 'foo' );
     400
     401                // Directly set a type that hasn't been registered.
     402                bp_set_object_terms( $u1, 'ugh', 'bp_member_type', true );
     403
     404                $type = bp_get_member_type( $u1, false );
     405                $this->assertEquals( array( 'foo' ), $type );
     406        }
    395407}