BP_Bbp_Gdpr_Replies

Class BP_Bbp_Gdpr_Replies

Description

Source

File: bp-core/gdpr/class-bp-bbp-gdpr-replies.php

15
16
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
class BP_Bbp_Gdpr_Replies {
 
    public $post_type = 'reply';
 
    /**
     * BP_Bbp_Gdpr_Replies constructor.
     */
    public function __construct() {
 
        add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporter' ), 10 );
        add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'erase_exporter' ), 10 );
    }
 
    /**
     * Register forum replies exporter.
     *
     * @param $exporters
     *
     * @since BuddyBoss 1.0.0
     *
     * @return mixed
     */
    function register_exporter( $exporters ) {
        $exporters['bbp-reply'] = array(
            'exporter_friendly_name' => __( 'Discussion Replies', 'buddyboss' ),
            'callback'               => array( $this, 'replies_exporter' ),
        );
 
        return $exporters;
    }
 
    /**
     * Register forum replies deletion.
     *
     * @param $erasers
     *
     * @since BuddyBoss 1.0.0
     *
     * @return mixed
     */
    function erase_exporter( $erasers ) {
        $erasers['bbp-reply'] = array(
            'eraser_friendly_name' => __( 'Forum Replies', 'buddyboss' ),
            'callback'             => array( $this, 'replies_eraser' ),
        );
 
        return $erasers;
    }
 
    /**
     * Export member created forum replies.
     *
     * @param $email_address
     * @param int $page
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array
     */
    function replies_exporter( $email_address, $page = 1 ) {
        $per_page = 500; // Limit us to avoid timing out
        $page     = (int) $page;
 
        $export_items = array();
 
        $user = get_user_by( 'email', $email_address );
        if ( false === $user ) {
            return array(
                'data' => $export_items,
                'done' => true,
            );
        }
 
        $replies_details = $this->get_replies( $user, $page, $per_page );
        $total           = isset( $replies_details['total'] ) ? $replies_details['total'] : 0;
        $replies         = isset( $replies_details['replies'] ) ? $replies_details['replies'] : array();
 
        if ( $total > 0 ) {
            foreach ( $replies as $reply ) {
                $item_id = "bbp-reply-{$reply->ID}";
 
                $group_id = 'bbp-replies';
 
                $group_label = __( 'Discussion Replies', 'buddyboss' );
 
                $permalink = get_permalink( $reply->ID );
 
                $parent_title = get_the_title( $reply->post_parent );
 
                // Plugins can add as many items in the item data array as they want
                $data = array(
                    array(
                        'name'  => __( 'Reply Author', 'buddyboss' ),
                        'value' => $user->display_name,
                    ),
                    array(
                        'name'  => __( 'Reply Author Email', 'buddyboss' ),
                        'value' => $user->user_email,
                    ),
                    array(
                        'name'  => __( 'Reply Title', 'buddyboss' ),
                        'value' => ! empty( $parent_title ) ? __( 'Reply To: ',
                                'buddyboss' ) . html_entity_decode( $parent_title ) : '',
                    ),
                    array(
                        'name'  => __( 'Reply Content', 'buddyboss' ),
                        'value' => $reply->post_content,
                    ),
                    array(
                        'name'  => __( 'Reply Date', 'buddyboss' ),
                        'value' => $reply->post_date,
                    ),
                    array(
                        'name'  => __( 'Reply URL', 'buddyboss' ),
                        'value' => $permalink,
                    ),
                );
 
                $export_items[] = array(
                    'group_id'    => $group_id,
                    'group_label' => $group_label,
                    'item_id'     => $item_id,
                    'data'        => $data,
                );
            }
        }
 
        $offset = ( $page - 1 ) * $per_page;
 
        // Tell core if we have more comments to work on still
        $done = $total < $offset;
 
        return array(
            'data' => $export_items,
            'done' => $done,
        );
    }
 
    /**
     * Get member created forum replies.
     *
     * @param $user
     * @param $page
     * @param $per_page
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array|bool
     */
    function get_replies( $user, $page, $per_page ) {
        $pp_args = array(
            'post_type'      => $this->post_type,
            'author'         => $user->ID,
            'posts_per_page' => $per_page,
            'paged'          => $page,
        );
 
        $the_query = new \WP_Query( $pp_args );
 
        if ( $the_query->have_posts() ) {
            return array( 'replies' => $the_query->posts, 'total' => $the_query->post_count );
        }
 
        return false;
    }
 
    /**
     * Delete member created forum replies.
     *
     * @param $email_address
     * @param int $page
     *
     * @since BuddyBoss 1.0.0
     *
     * @return array
     */
    function replies_eraser( $email_address, $page = 1 ) {
        $per_page = 500; // Limit us to avoid timing out
        $page     = (int) $page;
 
        $user = get_user_by( 'email', $email_address );
        if ( false === $user ) {
            return array(
                'items_removed'  => false,
                'items_retained' => false,
                'messages'       => array(),
                'done'           => true,
            );
        }
 
        $items_removed  = false;
        $items_retained = false;
        $messages       = array();
 
        $items = $this->get_replies( $user, 1, $per_page );
 
        if ( ! $items ) {
            return array(
                'items_removed'  => false,
                'items_retained' => false,
                'messages'       => array(),
                'done'           => true,
            );
        }
 
        $total   = isset( $items['total'] ) ? $items['total'] : 0;
        $replies = ! empty( $items['replies'] ) ? $items['replies'] : array();
 
        if ( $total ) {
            foreach ( (array) $replies as $reply ) {
                $attachments = get_posts( array(
                    'post_type'      => 'attachment',
                    'posts_per_page' => - 1,
                    'post_parent'    => $reply->ID,
                ) );
 
                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                        wp_delete_post( $attachment->ID, true );
                    }
                }
                wp_delete_post( $reply->ID, true );
                $items_removed = true;
            }
        }
 
        $offset = ( $page - 1 ) * $per_page;
 
        // Tell core if we have more comments to work on still
        $done = $total < $offset;
 
        return array(
            'items_removed'  => $items_removed,
            'items_retained' => $items_retained,
            'messages'       => $messages,
            'done'           => $done,
        );
    }
}

Methods

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.