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/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 );
Note: See TracChangeset for help on using the changeset viewer.