Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/08/2014 12:33:18 PM (11 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.