BP_Xprofile_Export

Class BP_Xprofile_Export

Description

Source

File: bp-core/gdpr/class-bp-xprofile-export.php

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
final class BP_Xprofile_Export extends BP_Export {
 
    /**
     * Get the instance of this class.
     *
     * @return Controller|null
     */
    public static function instance() {
        static $instance = null;
 
        if ( null === $instance ) {
            $instance = new BP_Xprofile_Export();
            $instance->setup( 'bp_xprofile', __( 'User Profile Information', 'buddyboss' ) );
        }
 
        return $instance;
    }
 
    /**
     * Export member extended profile fields.
     *
     * @param $user
     * @param $page
     * @param bool $email_address
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array
     */
    function process_data( $user, $page, $email_address = false ) {
 
        if ( ! $user || is_wp_error( $user ) ) {
            return $this->response( array(), true );
        }
 
        $export_items = array();
 
        /**
         * Cover Photo & Avatar
         */
        if ( function_exists( 'bp_core_fetch_avatar' ) ) {
            $avatar = bp_core_fetch_avatar( array(
                'item_id' => $user->ID,
                'object'  => 'user',
                'type'    => 'full',
                'html'    => false,
            ) );
        }
 
        if ( function_exists( 'bp_attachments_get_attachment' ) ) {
            $cover_photo = bp_attachments_get_attachment( 'url',
                array(
                    'item_id'    => $user->ID,
                    'type'       => 'cover-image',
                    'object_dir' => 'members',
                ) );
        }
 
        if ( empty( $avatar ) || ! $avatar ) {
            $avatar = __( 'N/A', 'buddyboss' );
        }
        if ( empty( $cover_photo ) || ! $cover_photo ) {
            $cover_photo = __( 'N/A', 'buddyboss' );
        }
 
        $data[] = array( 'name' => __( 'Avatar', 'buddyboss' ), 'value' => $avatar );
        $data[] = array( 'name' => __( 'Cover Photo', 'buddyboss' ), 'value' => $cover_photo );
 
        $export_items[] = array(
            'group_id'    => "{$this->exporter_name}-cover-avatar",
            'group_label' => __( 'User Profile - Avatar & Cover Photos', 'buddyboss' ),
            'item_id'     => "{$this->exporter_name}-assets-{$user->ID}",
            'data'        => $data,
        );
 
 
        /**
         * Xprofile Fields
         */
 
        $data_items = $this->get_data( $user, $page );
 
        foreach ( $data_items['items'] as $xgroup => $items ) {
 
            $group_id    = $xgroup;
            $group_label = __( "User Profile - {$xgroup}", 'buddyboss' );
            $item_id     = "{$this->exporter_name}-{$group_id}";
 
            $data = array();
 
            foreach ( $items as $item ) {
                $val = trim( $item['value'] );
                if ( empty( $val ) ) {
                    $val = __( 'N/A', 'buddyboss' );
                }
                $data[] = array( 'name' => $item['name'], 'value' => $val );
            }
 
            $data = apply_filters( 'buddyboss_bp_gdpr_xprofile_after_data_prepare', $data, $items, $data_items );
 
            $export_items[] = array(
                'group_id'    => $group_id,
                'group_label' => $group_label,
                'item_id'     => $item_id,
                'data'        => $data,
            );
 
        }
 
 
        $done = true; // on this we are processing everything at once.
 
        return $this->response( $export_items, $done );
    }
 
    /**
     * Delete member extended profile fields.
     *
     * @param $user
     * @param $page
     * @param bool $email_address
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array
     */
    function process_erase( $user, $page, $email_address ) {
 
        global $wpdb;
 
        if ( ! $user || is_wp_error( $user ) ) {
            return $this->response_erase( array(), true );
        }
 
        $number         = $this->items_per_batch;
        $page           = (int) $page;
        $items_removed  = true;
        $items_retained = false;
 
        /**
         * Make use of buddypress default data remover.
         */
        xprofile_remove_data( $user->ID );
 
        // delete avatar
        bp_core_delete_avatar_on_user_delete( $user->ID );
 
        // delete cover photo
        bp_attachments_delete_file( array( 'item_id' => $user->ID, 'object' => 'members', 'type' => 'cover-image' ) );
 
        $done = true;
 
        return $this->response_erase( $items_removed, $done, array(), $items_retained );
    }
 
    /**
     * Returns the data & count of activities by page and user.
     *
     * @param $user
     * @param $page
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array
     */
    function get_data( $user, $page ) {
        global $wpdb, $bp;
 
        $wpdb->show_errors( false );
 
        $items = array();
 
        $xprofile_groups = bp_xprofile_get_groups();
 
        $field_table = $bp->profile->global_tables["table_name_fields"];
 
        foreach ( $xprofile_groups as $xgroup ) {
 
            $fields = $wpdb->get_results( $wpdb->prepare( "SELECT *FROM {$field_table} WHERE group_id=%d AND parent_id=0",
                $xgroup->id ) );
 
            foreach ( $fields as $key => $field ) {
                $field          = (array) $field;
                $field['value'] = xprofile_get_field_data( $field['id'], $user->ID, 'comma' );
                $fields[ $key ] = $field;
            }
            $items[ $xgroup->name ] = $fields;
        }
 
        return array(
            'total'  => 1,
            'offset' => 0,
            'items'  => $items,
        );
 
    }
 
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Methods

  • get_data — Returns the data & count of activities by page and user.
  • instance — Get the instance of this class.
  • process_data — Export member extended profile fields.
  • process_erase — Delete member extended profile fields.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.