Skip to:
Content

BuddyPress.org

Changeset 13121


Ignore:
Timestamp:
10/09/2021 05:13:00 PM (5 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

See #8568 (trunk)

Location:
trunk
Files:
2 edited

Legend:

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

    r13108 r13121  
    816816 *
    817817 * @since 1.0.0
    818  *
    819  * @param int $user_id ID of the user to sync.
     818 * @since 9.2.0 Adds the $args arguments to catch hook's additional arguments.
     819 *
     820 * @param int   $user_id ID of the user to sync.
     821 * @param array $args    Hook's additional arguments.
    820822 * @return bool
    821823 */
    822 function xprofile_sync_wp_profile( $user_id = 0 ) {
     824function xprofile_sync_wp_profile( $user_id = 0, ...$args ) {
    823825
    824826        // Bail if profile syncing is disabled.
     
    835837        }
    836838
    837         $fullname = xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );
     839        $fullname_field_id = (int) bp_xprofile_fullname_field_id();
     840        $usermeta          = array();
     841        $userdata          = array();
     842
     843        if ( isset( $args[1]['meta'] ) ) {
     844                $usermeta = $args[1]['meta'];
     845        } elseif ( isset( $args[3] ) ) {
     846                $usermeta = $args[3];
     847        }
     848
     849        if ( isset( $usermeta['profile_field_ids'] ) ) {
     850                $xprofile_fields = wp_parse_id_list( $usermeta['profile_field_ids'] );
     851                $xprofile_fields = array_diff( $xprofile_fields, array( $fullname_field_id ) );
     852
     853                foreach ( $xprofile_fields as $xprofile_field_id ) {
     854                        $field_type = bp_xprofile_get_field_type( $xprofile_field_id );
     855
     856                        $field_key = 'field_' . $xprofile_field_id;
     857                        if ( isset( $field_type->wp_user_key ) && isset( $usermeta[ $field_key ] ) && $usermeta[ $field_key ] ) {
     858                                $userdata[ $field_type->wp_user_key ] = $usermeta[ $field_key ];
     859                        }
     860                }
     861        }
     862
     863        $fullname = xprofile_get_field_data( $fullname_field_id, $user_id );
    838864        $space    = strpos( $fullname, ' ' );
    839865
    840866        if ( false === $space ) {
    841                 $firstname = $fullname;
    842                 $lastname = '';
     867                if ( ! isset( $userdata['first_name'] ) ) {
     868                        $userdata['first_name'] = $fullname;
     869                }
     870
     871                if ( ! isset( $userdata['last_name'] ) ) {
     872                        $userdata['last_name'] = '';
     873                }
    843874        } else {
    844                 $firstname = substr( $fullname, 0, $space );
    845                 $lastname = trim( substr( $fullname, $space, strlen( $fullname ) ) );
     875                if ( ! isset( $userdata['first_name'] ) ) {
     876                        $userdata['first_name'] = substr( $fullname, 0, $space );
     877                }
     878
     879                if ( ! isset( $userdata['last_name'] ) ) {
     880                        $userdata['last_name'] = trim( substr( $fullname, $space, strlen( $fullname ) ) );
     881                }
    846882        }
    847883
    848884        bp_update_user_meta( $user_id, 'nickname',   $fullname  );
    849         bp_update_user_meta( $user_id, 'first_name', $firstname );
    850         bp_update_user_meta( $user_id, 'last_name',  $lastname  );
     885        bp_update_user_meta( $user_id, 'first_name', $userdata['first_name'] );
     886        bp_update_user_meta( $user_id, 'last_name',  $userdata['last_name']  );
    851887
    852888        wp_update_user( array( 'ID' => $user_id, 'display_name' => $fullname ) );
    853889}
    854 add_action( 'bp_core_signup_user',      'xprofile_sync_wp_profile' );
    855 add_action( 'bp_core_activated_user',   'xprofile_sync_wp_profile' );
     890add_action( 'bp_core_signup_user', 'xprofile_sync_wp_profile', 10, 5 );
     891add_action( 'bp_core_activated_user', 'xprofile_sync_wp_profile', 10, 3 );
    856892
    857893/**
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r12605 r13121  
    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.