Skip to:
Content

BuddyPress.org

Changeset 12110


Ignore:
Timestamp:
05/23/2018 01:40:06 AM (7 years ago)
Author:
boonebgorges
Message:

Privacy: Data exporter for XProfile.

See #7817.

Location:
trunk
Files:
3 edited

Legend:

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

    r11803 r12110  
    7777add_filter( 'xprofile_field_options_before_save', 'bp_xprofile_sanitize_field_options' );
    7878add_filter( 'xprofile_field_default_before_save', 'bp_xprofile_sanitize_field_default' );
     79
     80// Privacy data export.
     81add_filter( 'wp_privacy_personal_data_exporters', 'bp_register_xprofile_personal_data_exporter' );
    7982
    8083/**
     
    624627    return $q;
    625628}
     629
     630/**
     631 * Register XProfile personal data exporter.
     632 *
     633 * @since 4.0.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}
  • trunk/src/bp-xprofile/bp-xprofile-functions.php

    r11886 r12110  
    13371337    }
    13381338}
     1339
     1340/**
     1341 * Finds and exports personal data associated with an email address from the XProfile tables.
     1342 *
     1343 * @since 4.0.0
     1344 *
     1345 * @param string $email_address  The userss 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        // Re-pull 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', 'buddypress' ),
     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}
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r11737 r12110  
    10751075        unset( $GLOBALS['field'] );
    10761076    }
     1077
     1078    /**
     1079     * @ticket BP7817
     1080     * @ticket BP7698
     1081     */
     1082    public function test_bp_xprofile_personal_data_exporter() {
     1083        $u = self::factory()->user->create();
     1084
     1085        $field_group_id = self::factory()->xprofile_group->create();
     1086        $f1 = self::factory()->xprofile_field->create(
     1087            array(
     1088                'field_group_id' => $field_group_id,
     1089            )
     1090        );
     1091        $f2 = self::factory()->xprofile_field->create(
     1092            array(
     1093                'field_group_id' => $field_group_id,
     1094                'type'           => 'checkbox',
     1095            )
     1096        );
     1097
     1098        $option1 = xprofile_insert_field( array(
     1099            'field_group_id' => $field_group_id,
     1100            'parent_id'      => $f2,
     1101            'type'           => 'option',
     1102            'name'           => 'Option 1',
     1103        ) );
     1104
     1105        $option2 = xprofile_insert_field( array(
     1106            'field_group_id' => $field_group_id,
     1107            'parent_id'      => $f2,
     1108            'type'           => 'option',
     1109            'name'           => 'Option 2',
     1110        ) );
     1111
     1112        $option3 = xprofile_insert_field( array(
     1113            'field_group_id' => $field_group_id,
     1114            'parent_id'      => $f2,
     1115            'type'           => 'option',
     1116            'name'           => 'Option 3',
     1117        ) );
     1118
     1119        xprofile_set_field_data( $f1, $u, 'foo' );
     1120        xprofile_set_field_data( $f2, $u, array( 'Option 1', 'Option 3' ) );
     1121
     1122        $test_user = new WP_User( $u );
     1123
     1124        $actual = bp_xprofile_personal_data_exporter( $test_user->user_email );
     1125
     1126        $this->assertTrue( $actual['done'] );
     1127
     1128        // Number of exported users.
     1129        $this->assertSame( 1, count( $actual['data'] ) );
     1130
     1131        // Number of exported user properties.
     1132        $this->assertSame( 3, count( $actual['data'][0]['data'] ) );
     1133    }
    10771134}
Note: See TracChangeset for help on using the changeset viewer.