Skip to:
Content

BuddyPress.org

Changeset 13122


Ignore:
Timestamp:
10/09/2021 05:20:55 PM (3 years 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)

Location:
branches/9.0
Files:
2 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/**
  • branches/9.0/tests/phpunit/testcases/xprofile/functions.php

    r12605 r13122  
    12071207        $this->assertSame( 'foo', xprofile_get_field_data( $f1, $u ) );
    12081208    }
     1209
     1210    /**
     1211     * @ticket BP8568
     1212     */
     1213    public function test_xprofile_sync_wp_profile_signup_with_wp_first_and_last_name_fields() {
     1214        add_filter( 'bp_disable_profile_sync', '__return_true' );
     1215
     1216        $u = self::factory()->user->create_and_get(
     1217            array(
     1218                'user_login' => 'foobar',
     1219                'user_email' => 'foo@bar.email',
     1220            )
     1221        );
     1222
     1223        $field_fn = self::factory()->xprofile_field->create(
     1224            array(
     1225                'field_group_id' => 1,
     1226                'type'           => 'wp-textbox',
     1227                'name'           => 'WP First Name',
     1228            )
     1229        );
     1230
     1231        // Set the WP User Key.
     1232        bp_xprofile_update_meta( $field_fn, 'field', 'wp_user_key', 'first_name' );
     1233
     1234        $field_ln = self::factory()->xprofile_field->create(
     1235            array(
     1236                'field_group_id' => 1,
     1237                'type'           => 'wp-textbox',
     1238                'name'           => 'WP Last Name',
     1239            )
     1240        );
     1241
     1242        // Set the WP User Key.
     1243        bp_xprofile_update_meta( $field_ln, 'field', 'wp_user_key', 'last_name' );
     1244
     1245        $field_n  = bp_xprofile_fullname_field_id();
     1246        $usermeta = array(
     1247            'field_' . $field_n  => 'foobar',
     1248            'field_' . $field_fn => 'Foo',
     1249            'field_' . $field_ln => 'Bar',
     1250            'profile_field_ids'  => $field_n . ',' . $field_fn . ',' . $field_ln,
     1251        );
     1252
     1253        remove_filter( 'bp_disable_profile_sync', '__return_true' );
     1254
     1255        // simulates do_action( 'bp_core_signup_user', $user_id, $user_login, $user_password, $user_email, $usermeta );
     1256        xprofile_sync_wp_profile( $u->ID, $u->user_login, $u->user_pass, $u->user_email, $usermeta );
     1257
     1258        $updated_u = get_user_by( 'id', $u->ID );
     1259
     1260        $this->assertEquals( 'Foo', $updated_u->first_name );
     1261        $this->assertEquals( 'Bar', $updated_u->last_name );
     1262    }
     1263
     1264    /**
     1265     * @ticket BP8568
     1266     */
     1267    public function test_xprofile_sync_wp_profile_signup_without_wp_first_and_last_name_fields() {
     1268        add_filter( 'bp_disable_profile_sync', '__return_true' );
     1269
     1270        $u = self::factory()->user->create_and_get(
     1271            array(
     1272                'user_login' => 'foobar',
     1273                'user_email' => 'foo@bar.email',
     1274            )
     1275        );
     1276
     1277        $field_n  = bp_xprofile_fullname_field_id();
     1278        $usermeta = array(
     1279            'field_' . $field_n  => 'foobar',
     1280            'profile_field_ids'  => $field_n,
     1281        );
     1282
     1283        remove_filter( 'bp_disable_profile_sync', '__return_true' );
     1284
     1285        // simulates do_action( 'bp_core_signup_user', $user_id, $user_login, $user_password, $user_email, $usermeta );
     1286        xprofile_sync_wp_profile( $u->ID, $u->user_login, $u->user_pass, $u->user_email, $usermeta );
     1287
     1288        $updated_u = get_user_by( 'id', $u->ID );
     1289
     1290        $this->assertEquals( 'foobar', $updated_u->first_name );
     1291        $this->assertEquals( '', $updated_u->last_name );
     1292    }
     1293
     1294    /**
     1295     * @ticket BP8568
     1296     */
     1297    public function test_xprofile_sync_wp_profile_activate_signup_with_wp_first_and_last_name_fields() {
     1298        add_filter( 'bp_disable_profile_sync', '__return_true' );
     1299
     1300        $u = self::factory()->user->create_and_get(
     1301            array(
     1302                'user_login' => 'barfoo',
     1303                'user_email' => 'bar@foo.email',
     1304            )
     1305        );
     1306
     1307        $field_fn = self::factory()->xprofile_field->create(
     1308            array(
     1309                'field_group_id' => 1,
     1310                'type'           => 'wp-textbox',
     1311                'name'           => 'WP First Name',
     1312            )
     1313        );
     1314
     1315        // Set the WP User Key.
     1316        bp_xprofile_update_meta( $field_fn, 'field', 'wp_user_key', 'first_name' );
     1317
     1318        $field_ln = self::factory()->xprofile_field->create(
     1319            array(
     1320                'field_group_id' => 1,
     1321                'type'           => 'wp-textbox',
     1322                'name'           => 'WP Last Name',
     1323            )
     1324        );
     1325
     1326        // Set the WP User Key.
     1327        bp_xprofile_update_meta( $field_ln, 'field', 'wp_user_key', 'last_name' );
     1328
     1329        $field_n  = bp_xprofile_fullname_field_id();
     1330
     1331        $user = array(
     1332            'user_id'  => $u->ID,
     1333            'password' => $u->user_pass,
     1334            'meta'     => array(
     1335                'field_' . $field_n  => 'barfoo',
     1336                'field_' . $field_fn => 'Bar',
     1337                'field_' . $field_ln => 'Foo',
     1338                'profile_field_ids'  => $field_n . ',' . $field_fn . ',' . $field_ln,
     1339            ),
     1340        );
     1341
     1342        remove_filter( 'bp_disable_profile_sync', '__return_true' );
     1343
     1344        // simulates do_action( 'bp_core_activated_user', $user_id, $key, $user );
     1345        xprofile_sync_wp_profile( $u->ID, 'randomkey', $user );
     1346
     1347        $updated_u = get_user_by( 'id', $u->ID );
     1348
     1349        $this->assertEquals( 'Bar', $updated_u->first_name );
     1350        $this->assertEquals( 'Foo', $updated_u->last_name );
     1351    }
     1352
     1353    /**
     1354     * @ticket BP8568
     1355     */
     1356    public function test_xprofile_sync_wp_profile_activate_signup_without_wp_first_and_last_name_fields() {
     1357        add_filter( 'bp_disable_profile_sync', '__return_true' );
     1358
     1359        $u = self::factory()->user->create_and_get(
     1360            array(
     1361                'user_login' => 'barfoo',
     1362                'user_email' => 'bar@foo.email',
     1363            )
     1364        );
     1365
     1366        $field_n  = bp_xprofile_fullname_field_id();
     1367
     1368        $user = array(
     1369            'user_id'  => $u->ID,
     1370            'password' => $u->user_pass,
     1371            'meta'     => array(
     1372                'field_' . $field_n  => 'barfoo',
     1373                'profile_field_ids'  => $field_n,
     1374            ),
     1375        );
     1376
     1377        remove_filter( 'bp_disable_profile_sync', '__return_true' );
     1378
     1379        // simulates do_action( 'bp_core_activated_user', $user_id, $key, $user );
     1380        xprofile_sync_wp_profile( $u->ID, 'randomkey', $user );
     1381
     1382        $updated_u = get_user_by( 'id', $u->ID );
     1383
     1384        $this->assertEquals( 'barfoo', $updated_u->first_name );
     1385        $this->assertEquals( '', $updated_u->last_name );
     1386    }
    12091387}
Note: See TracChangeset for help on using the changeset viewer.