Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/09/2021 05:20:55 PM (3 months ago)
Author:
imath
Message:

xProfile: prevent the Name field to override WP Field Types on signup

When the BP xProfile Base Name field synchronization with the WordPress Display Name field is on, we need to make sure values of potential first and/or last name fields using the xProfile WP Field Type are not overridden by this synchronization.

Props needle

Fixes #8568 (branch 9.0)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/9.0/src/bp-xprofile/bp-xprofile-functions.php

    r13012 r13122  
    809809 *
    810810 * @since 1.0.0
    811  *
    812  * @param int $user_id ID of the user to sync.
     811 * @since 9.2.0 Adds the $args arguments to catch hook's additional arguments.
     812 *
     813 * @param int   $user_id ID of the user to sync.
     814 * @param array $args    Hook's additional arguments.
    813815 * @return bool
    814816 */
    815 function xprofile_sync_wp_profile( $user_id = 0 ) {
     817function xprofile_sync_wp_profile( $user_id = 0, ...$args ) {
    816818
    817819    // Bail if profile syncing is disabled.
     
    828830    }
    829831
    830     $fullname = xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );
     832    $fullname_field_id = (int) bp_xprofile_fullname_field_id();
     833    $usermeta          = array();
     834    $userdata          = array();
     835
     836    if ( isset( $args[1]['meta'] ) ) {
     837        $usermeta = $args[1]['meta'];
     838    } elseif ( isset( $args[3] ) ) {
     839        $usermeta = $args[3];
     840    }
     841
     842    if ( isset( $usermeta['profile_field_ids'] ) ) {
     843        $xprofile_fields = wp_parse_id_list( $usermeta['profile_field_ids'] );
     844        $xprofile_fields = array_diff( $xprofile_fields, array( $fullname_field_id ) );
     845
     846        foreach ( $xprofile_fields as $xprofile_field_id ) {
     847            $field_type = bp_xprofile_get_field_type( $xprofile_field_id );
     848
     849            $field_key = 'field_' . $xprofile_field_id;
     850            if ( isset( $field_type->wp_user_key ) && isset( $usermeta[ $field_key ] ) && $usermeta[ $field_key ] ) {
     851                $userdata[ $field_type->wp_user_key ] = $usermeta[ $field_key ];
     852            }
     853        }
     854    }
     855
     856    $fullname = xprofile_get_field_data( $fullname_field_id, $user_id );
    831857    $space    = strpos( $fullname, ' ' );
    832858
    833859    if ( false === $space ) {
    834         $firstname = $fullname;
    835         $lastname = '';
     860        if ( ! isset( $userdata['first_name'] ) ) {
     861            $userdata['first_name'] = $fullname;
     862        }
     863
     864        if ( ! isset( $userdata['last_name'] ) ) {
     865            $userdata['last_name'] = '';
     866        }
    836867    } else {
    837         $firstname = substr( $fullname, 0, $space );
    838         $lastname = trim( substr( $fullname, $space, strlen( $fullname ) ) );
     868        if ( ! isset( $userdata['first_name'] ) ) {
     869            $userdata['first_name'] = substr( $fullname, 0, $space );
     870        }
     871
     872        if ( ! isset( $userdata['last_name'] ) ) {
     873            $userdata['last_name'] = trim( substr( $fullname, $space, strlen( $fullname ) ) );
     874        }
    839875    }
    840876
    841877    bp_update_user_meta( $user_id, 'nickname',   $fullname  );
    842     bp_update_user_meta( $user_id, 'first_name', $firstname );
    843     bp_update_user_meta( $user_id, 'last_name',  $lastname  );
     878    bp_update_user_meta( $user_id, 'first_name', $userdata['first_name'] );
     879    bp_update_user_meta( $user_id, 'last_name',  $userdata['last_name']  );
    844880
    845881    wp_update_user( array( 'ID' => $user_id, 'display_name' => $fullname ) );
    846882}
    847 add_action( 'bp_core_signup_user',      'xprofile_sync_wp_profile' );
    848 add_action( 'bp_core_activated_user',   'xprofile_sync_wp_profile' );
     883add_action( 'bp_core_signup_user', 'xprofile_sync_wp_profile', 10, 5 );
     884add_action( 'bp_core_activated_user', 'xprofile_sync_wp_profile', 10, 3 );
    849885
    850886/**
Note: See TracChangeset for help on using the changeset viewer.