Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROD-7997: Fix member search filter results in the member directory for non-admin users #4523

Merged
merged 11 commits into from
Nov 4, 2024
Merged
5 changes: 5 additions & 0 deletions src/bp-core/bp-core-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ function bp_version_updater() {
if ( function_exists( 'bb_group_migration' ) ) {
bb_group_migration();
}

// Run migration about profile fields visibility.
if ( function_exists( 'bb_migrate_xprofile_visibility' ) ) {
bb_migrate_xprofile_visibility( true );
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/bp-members/bp-members-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,18 @@ function bp_core_map_user_registration( $user_id, $by_pass = false ) {
xprofile_set_field_data( bp_xprofile_lastname_field_id(), $user_id, $lastname );
xprofile_set_field_data( bp_xprofile_nickname_field_id(), $user_id, $nickname );

$default_field_ids = array(
bp_xprofile_firstname_field_id(),
bp_xprofile_lastname_field_id(),
bp_xprofile_nickname_field_id(),
);

// Set visibility levels for the default fields.
foreach ( $default_field_ids as $field_id ) {
$visibility = xprofile_get_field_visibility_level( $field_id, $user_id );
xprofile_set_field_visibility_level( $field_id, $user_id, $visibility );
}

bp_xprofile_update_display_name( $user_id );
}

Expand Down
67 changes: 54 additions & 13 deletions src/bp-xprofile/bp-xprofile-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2054,18 +2054,40 @@ function bp_xprofile_sync_bp_profile( $user_id ) {
return;
}

$user = get_user_by( 'id', $user_id );
$user = get_user_by( 'id', $user_id );
$visibility_levels = get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );

if ( isset( $user->first_name ) ) {
xprofile_set_field_data( bp_xprofile_firstname_field_id(), $user->ID, $user->first_name );
$firstname_field_id = bp_xprofile_firstname_field_id();
xprofile_set_field_data( $firstname_field_id, $user->ID, $user->first_name );

// Check if firstname field visibility level is not set.
if ( empty( $visibility_levels ) || empty( $visibility_levels[ $firstname_field_id ] ) ) {
$visibility = xprofile_get_field_visibility_level( $firstname_field_id, $user_id );
xprofile_set_field_visibility_level( $firstname_field_id, $user_id, $visibility );
}
}

if ( isset( $user->last_name ) ) {
xprofile_set_field_data( bp_xprofile_lastname_field_id(), $user->ID, $user->last_name );
$lastname_field_id = bp_xprofile_lastname_field_id();
xprofile_set_field_data( $lastname_field_id, $user->ID, $user->last_name );

// Check if lastname field visibility level is not set.
if ( empty( $visibility_levels ) || empty( $visibility_levels[ $lastname_field_id ] ) ) {
$visibility = xprofile_get_field_visibility_level( $lastname_field_id, $user_id );
xprofile_set_field_visibility_level( $lastname_field_id, $user_id, $visibility );
}
}

if ( isset( $user->nickname ) ) {
xprofile_set_field_data( bp_xprofile_nickname_field_id(), $user->ID, $user->nickname );
$nickname_field_id = bp_xprofile_nickname_field_id();
xprofile_set_field_data( $nickname_field_id, $user->ID, $user->nickname );

// Check if nickname field visibility level is not set.
if ( empty( $visibility_levels ) || empty( $visibility_levels[ $nickname_field_id ] ) ) {
$visibility = xprofile_get_field_visibility_level( $nickname_field_id, $user_id );
xprofile_set_field_visibility_level( $nickname_field_id, $user_id, $visibility );
}
}
}
add_action( 'profile_update', 'bp_xprofile_sync_bp_profile', 999, 1 );
Expand Down Expand Up @@ -2735,15 +2757,9 @@ function bb_migrate_xprofile_visibility( $background = false, $page = 1 ) {
$page = ! empty( $_POST['offset'] ) ? (int) ( $_POST['offset'] ) : $page;
$bp = buddypress();
$args = array(
'number' => apply_filters( 'bb_migrate_xprofile_visibility_users_number', 100 ),
'paged' => $page,
'meta_query' => array(
array(
'key' => 'bp_xprofile_visibility_levels',
'compare' => 'EXISTS',
),
),
'fields' => 'ID',
'number' => apply_filters( 'bb_migrate_xprofile_visibility_users_number', 100 ),
'paged' => $page,
'fields' => 'ID',
);

$users = get_users( $args );
Expand Down Expand Up @@ -2772,6 +2788,31 @@ function bb_migrate_xprofile_visibility( $background = false, $page = 1 ) {

foreach ( $users as $user_id ) {

$visibility_levels = get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );
if ( ! empty( $visibility_levels ) ) {
$default_field_ids = array(
bp_xprofile_firstname_field_id(),
bp_xprofile_lastname_field_id(),
bp_xprofile_nickname_field_id(),
);

foreach ( $default_field_ids as $default_field_id ) {
if ( empty( $visibility_levels[ $default_field_id ] ) ) {
$visibility_level = xprofile_get_field_visibility_level( $default_field_id, $user_id );
xprofile_set_field_visibility_level( $default_field_id, $user_id, $visibility_level );
}
}
} else {
// Get all profile field data for the user and set the visibility levels.
$profile_data = BP_XProfile_ProfileData::get_all_for_user( $user_id );
foreach ( $profile_data as $field_name => $field_data ) {
if ( ! empty( $field_data ) && ! empty( $field_data['field_id'] ) ) {
$visibility_level = xprofile_get_field_visibility_level( $field_data['field_id'], $user_id );
xprofile_set_field_visibility_level( $field_data['field_id'], $user_id, $visibility_level );
}
}
}

$visibility_levels = get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );
if ( ! empty( $visibility_levels ) ) {

Expand Down