Skip to:
Content

BuddyPress.org

Ticket #7817: 7817.diff

File 7817.diff, 4.7 KB (added by boonebgorges, 7 years ago)
  • src/bp-xprofile/bp-xprofile-filters.php

    diff --git a/src/bp-xprofile/bp-xprofile-filters.php b/src/bp-xprofile/bp-xprofile-filters.php
    index dd3f7c567..9ba0a9ccc 100644
    a b add_filter( 'xprofile_field_can_delete_before_save', 'absint' ); 
    7777add_filter( 'xprofile_field_options_before_save', 'bp_xprofile_sanitize_field_options' );
    7878add_filter( 'xprofile_field_default_before_save', 'bp_xprofile_sanitize_field_default' );
    7979
     80// Privacy data export.
     81add_filter( 'wp_privacy_personal_data_exporters', 'bp_register_xprofile_personal_data_exporter' );
     82
    8083/**
    8184 * Sanitize each field option name for saving to the database.
    8285 *
    function bp_xprofile_filter_meta_query( $q ) { 
    623626
    624627        return $q;
    625628}
     629
     630/**
     631 * Register XProfile personal data exporter.
     632 *
     633 * @since 3.1.0
     634 *
     635 * @param array $exporters  An array of personal data exporters.
     636 * @return array An array of personal data exporters.
     637 */
     638function bp_register_xprofile_personal_data_exporter( $exporters ) {
     639        $exporters['buddypress-xprofile'] = array(
     640                'exporter_friendly_name' => __( 'BuddyPress XProfile Data', 'buddypress' ),
     641                'callback'               => 'bp_xprofile_personal_data_exporter',
     642        );
     643
     644        return $exporters;
     645}
  • src/bp-xprofile/bp-xprofile-functions.php

    diff --git a/src/bp-xprofile/bp-xprofile-functions.php b/src/bp-xprofile/bp-xprofile-functions.php
    index 3344465ec..4d5c8ed5a 100644
    a b function bp_xprofile_maybe_format_datebox_post_data( $field_id ) { 
    13361336                }
    13371337        }
    13381338}
     1339
     1340/**
     1341 * Finds and exports personal data associated with an email address from the XProfile tables.
     1342 *
     1343 * @since 3.1.0
     1344 *
     1345 * @param string $email_address  The users email address.
     1346 * @return array An array of personal data.
     1347 */
     1348function bp_xprofile_personal_data_exporter( $email_address ) {
     1349        $email_address = trim( $email_address );
     1350
     1351        $data_to_export = array();
     1352
     1353        $user = get_user_by( 'email', $email_address );
     1354
     1355        if ( ! $user ) {
     1356                return array(
     1357                        'data' => array(),
     1358                        'done' => true,
     1359                );
     1360        }
     1361
     1362        $user_data_to_export = array();
     1363
     1364        $user_profile_data = BP_XProfile_ProfileData::get_all_for_user( $user->ID );
     1365        foreach ( $user_profile_data as $field_name => $field ) {
     1366                // Skip non-array fields, which don't belong to XProfile.
     1367                if ( ! is_array( $field ) ) {
     1368                        continue;
     1369                }
     1370
     1371                // Repull the data so that BuddyPress formats and sanitizes properly.
     1372                $value = xprofile_get_field_data( $field['field_id'], $user->ID, 'comma' );
     1373                $user_data_to_export[] = array(
     1374                        'name'  => $field_name,
     1375                        'value' => $value,
     1376                );
     1377        }
     1378
     1379        $data_to_export[] = array(
     1380                'group_id'    => 'bp_xprofile',
     1381                'group_label' => __( 'Extended Profile Data' ),
     1382                'item_id'     => "bp-xprofile-{$user->ID}",
     1383                'data'        => $user_data_to_export,
     1384        );
     1385
     1386        return array(
     1387                'data' => $data_to_export,
     1388                'done' => true,
     1389        );
     1390}
  • tests/phpunit/testcases/xprofile/functions.php

    diff --git a/tests/phpunit/testcases/xprofile/functions.php b/tests/phpunit/testcases/xprofile/functions.php
    index 15e9955db..8c8838f85 100644
    a b Bar!'; 
    10741074                $this->assertRegExp( $regex, $output );
    10751075                unset( $GLOBALS['field'] );
    10761076        }
     1077
     1078        public function test_bp_xprofile_personal_data_exporter() {
     1079                $u = self::factory()->user->create();
     1080
     1081                $field_group_id = self::factory()->xprofile_group->create();
     1082                $f1 = self::factory()->xprofile_field->create(
     1083                        array(
     1084                                'field_group_id' => $field_group_id,
     1085                        )
     1086                );
     1087                $f2 = self::factory()->xprofile_field->create(
     1088                        array(
     1089                                'field_group_id' => $field_group_id,
     1090                                'type'           => 'checkbox',
     1091                        )
     1092                );
     1093
     1094                $option1 = xprofile_insert_field( array(
     1095                        'field_group_id' => $field_group_id,
     1096                        'parent_id'      => $f2,
     1097                        'type'           => 'option',
     1098                        'name'           => 'Option 1',
     1099                ) );
     1100
     1101                $option2 = xprofile_insert_field( array(
     1102                        'field_group_id' => $field_group_id,
     1103                        'parent_id'      => $f2,
     1104                        'type'           => 'option',
     1105                        'name'           => 'Option 2',
     1106                ) );
     1107
     1108                $option3 = xprofile_insert_field( array(
     1109                        'field_group_id' => $field_group_id,
     1110                        'parent_id'      => $f2,
     1111                        'type'           => 'option',
     1112                        'name'           => 'Option 3',
     1113                ) );
     1114
     1115                xprofile_set_field_data( $f1, $u, 'foo' );
     1116                xprofile_set_field_data( $f2, $u, array( 'Option 1', 'Option 3' ) );
     1117
     1118                $test_user = new WP_User( $u );
     1119
     1120                $actual = bp_xprofile_personal_data_exporter( $test_user->user_email );
     1121
     1122                $this->assertTrue( $actual['done'] );
     1123
     1124                // Number of exported users.
     1125                $this->assertSame( 1, count( $actual['data'] ) );
     1126
     1127                // Number of exported user properties.
     1128                $this->assertSame( 3, count( $actual['data'][0]['data'] ) );
     1129        }
    10771130}