Skip to:
Content

BuddyPress.org

Changeset 8082


Ignore:
Timestamp:
03/08/2014 12:33:18 PM (13 years ago)
Author:
boonebgorges
Message:

Improve behavior or bp_member_profile_data() when used outside of members loop

Our documentation suggests the use of bp_member_profile_data() to pull up
miscellaneous pieces of user profile data anywhere in a template. Technically,
this works, but the internals of the function made a few assumptions that it
would only be used in the context of a bp_has_members() loop. This would result
in needless PHP notices when using the function outside of a loop context.

This changeset introduces checks to ensure that the $members_template global
is only manipulated if it exists, making the function safe to use anywhere.

Fixes #5334

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-template.php

    r8081 r8082  
    682682                extract( $r, EXTR_SKIP );
    683683
    684                 // Populate the user if it hasn't been already.
    685                 if ( empty( $members_template->member->profile_data ) && method_exists( 'BP_XProfile_ProfileData', 'get_all_for_user' ) )
    686                         $members_template->member->profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id );
     684                // If we're in a members loop, get the data from the global
     685                if ( ! empty( $members_template->member->profile_data ) ) {
     686                        $profile_data = $members_template->member->profile_data;
     687                }
     688
     689                // Otherwise query for the data
     690                if ( empty( $profile_data ) && method_exists( 'BP_XProfile_ProfileData', 'get_all_for_user' ) ) {
     691                        $profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id );
     692                }
     693
     694                // If we're in the members loop, but the profile data has not
     695                // been loaded into the global, cache it there for later use
     696                if ( ! empty( $members_template->member ) && empty( $members_template->member->profile_data ) ) {
     697                        $members_template->member->profile_data = $profile_data;
     698                }
    687699
    688700                // Get the field data if there is data to get
    689                 if ( ! empty( $members_template->member->profile_data ) && ! empty( $members_template->member->profile_data[$field]['field_type'] ) && ! empty( $members_template->member->profile_data[$field]['field_data'] ) )
    690                         $data = xprofile_format_profile_field( $members_template->member->profile_data[$field]['field_type'], $members_template->member->profile_data[$field]['field_data'] );
     701                if ( ! empty( $profile_data ) && ! empty( $profile_data[ $field ]['field_type'] ) && ! empty( $profile_data[ $field ]['field_data'] ) )
     702                        $data = xprofile_format_profile_field( $profile_data[ $field ]['field_type'], $profile_data[ $field ]['field_data'] );
    691703
    692704                return apply_filters( 'bp_get_member_profile_data', $data );
  • trunk/tests/testcases/xprofile/functions.php

    r7887 r8082  
    496496                $this->assertNotEmpty( bp_xprofile_add_meta( $g, 'group', 'foo', 'baz' ) );
    497497        }
     498
     499        /**
     500         * @group bp_get_member_profile_data
     501         */
     502        public function test_bp_get_member_profile_data_inside_loop() {
     503                $u = $this->create_user();
     504                $g = $this->factory->xprofile_group->create();
     505                $f = $this->factory->xprofile_field->create( array(
     506                        'field_group_id' => $g,
     507                        'type' => 'textbox',
     508                        'name' => 'Neato',
     509                ) );
     510                xprofile_set_field_data( $f, $u, 'foo' );
     511
     512                if ( bp_has_members() ) : while ( bp_members() ) : bp_the_member();
     513                $found = bp_get_member_profile_data( array(
     514                        'user_id' => $u,
     515                        'field' => 'Neato',
     516                ) );
     517                endwhile; endif;
     518
     519                // Cleanup
     520                unset( $GLOBALS['members_template'] );
     521
     522                $this->assertSame( 'foo', $found );
     523        }
     524        /**
     525         * @group bp_get_member_profile_data
     526         */
     527        public function test_bp_get_member_profile_data_outside_of_loop() {
     528                $u = $this->create_user();
     529                $g = $this->factory->xprofile_group->create();
     530                $f = $this->factory->xprofile_field->create( array(
     531                        'field_group_id' => $g,
     532                        'type' => 'textbox',
     533                        'name' => 'Kewl',
     534                ) );
     535                xprofile_set_field_data( $f, $u, 'foo' );
     536
     537                $found = bp_get_member_profile_data( array(
     538                        'user_id' => $u,
     539                        'field' => 'Kewl',
     540                ) );
     541
     542                $this->assertSame( 'foo', $found );
     543        }
    498544}
Note: See TracChangeset for help on using the changeset viewer.