PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.0
1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / privacy / privacy-data.php

privacy-data.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.6.0, at inc/privacy/privacy-data.php

371 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Privacy Data — WordPress personal-data export/erase integration.
4 *
5 * Registers SureDonation with WordPress Tools → Export/Erase Personal Data so a
6 * donor's data can be exported or erased on request. Erasure honors the Privacy
7 * settings' Minimum Data Retention Period (full erase — no per-field retention):
8 * donations still inside the retention window are retained; older ones are fully
9 * anonymized.
10 *
11 * @package SureDonation
12 * @since 1.2.0
13 */
14
15 namespace SureDonation\Inc\Privacy;
16
17 use SureDonation\Inc\Database\Tables\Donations;
18 use SureDonation\Inc\Database\Tables\Donors;
19 use SureDonation\Inc\Helper;
20 use SureDonation\Inc\Pdf\Receipt_Generator;
21 use SureDonation\Inc\Traits\Get_Instance;
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit; // Exit if accessed directly.
25 }
26
27 /**
28 * Privacy_Data class.
29 *
30 * @since 1.2.0
31 */
32 class Privacy_Data {
33 use Get_Instance;
34
35 /**
36 * Constructor — register the exporter + eraser with WordPress.
37 *
38 * @since 1.2.0
39 */
40 public function __construct() {
41 add_filter( 'wp_privacy_personal_data_exporters', [ $this, 'register_exporters' ] );
42 add_filter( 'wp_privacy_personal_data_erasers', [ $this, 'register_erasers' ] );
43 }
44
45 /**
46 * Register the SureDonation personal-data exporter.
47 *
48 * @since 1.2.0
49 * @param array<string, mixed> $exporters Registered exporters.
50 * @return array<string, mixed>
51 */
52 public function register_exporters( $exporters ) {
53 if ( ! is_array( $exporters ) ) {
54 $exporters = [];
55 }
56 $exporters['suredonation'] = [
57 'exporter_friendly_name' => __( 'SureDonation Donations', 'suredonation' ),
58 'callback' => [ $this, 'export' ],
59 ];
60 return $exporters;
61 }
62
63 /**
64 * Register the SureDonation personal-data eraser.
65 *
66 * @since 1.2.0
67 * @param array<string, mixed> $erasers Registered erasers.
68 * @return array<string, mixed>
69 */
70 public function register_erasers( $erasers ) {
71 if ( ! is_array( $erasers ) ) {
72 $erasers = [];
73 }
74 $erasers['suredonation'] = [
75 'eraser_friendly_name' => __( 'SureDonation Donations', 'suredonation' ),
76 'callback' => [ $this, 'erase' ],
77 ];
78 return $erasers;
79 }
80
81 /**
82 * Export a donor's personal data (donor profile + their donations).
83 *
84 * Paginated per the WordPress exporter contract: the donor profile is emitted on
85 * the first page and donations are returned in fixed-size batches, so a donor with
86 * many donations doesn't load them all into one response.
87 *
88 * @since 1.2.0
89 * @param string $email_address The email being exported.
90 * @param int $page 1-based page number supplied by WordPress.
91 * @return array{data: array<int, array<string, mixed>>, done: bool}
92 */
93 public function export( $email_address, $page = 1 ) {
94 $email = sanitize_email( (string) $email_address );
95 $page = max( 1, (int) $page );
96 $batch = 100;
97 $export = [];
98
99 // Donor profile is emitted once, on the first page.
100 $donor = 1 === $page ? Donors::get_by_email( $email ) : null;
101 if ( is_array( $donor ) && ! empty( $donor ) ) {
102 $export[] = [
103 'group_id' => 'suredonation-donor',
104 'group_label' => __( 'SureDonation Donor', 'suredonation' ),
105 'item_id' => 'suredonation-donor-' . absint( Helper::get_string_value( $donor['id'] ?? 0 ) ),
106 'data' => self::name_value_rows(
107 [
108 __( 'Name', 'suredonation' ) => $donor['name'] ?? '',
109 __( 'Email', 'suredonation' ) => $donor['email'] ?? '',
110 __( 'Phone', 'suredonation' ) => $donor['phone'] ?? '',
111 __( 'Company', 'suredonation' ) => $donor['company'] ?? '',
112 __( 'Address', 'suredonation' ) => $donor['address'] ?? '',
113 ]
114 ),
115 ];
116 }
117
118 $donations = Donations::get_by_donor_email( $email, $batch, ( $page - 1 ) * $batch );
119 $donations = is_array( $donations ) ? $donations : [];
120 foreach ( $donations as $donation ) {
121 if ( ! is_array( $donation ) ) {
122 continue;
123 }
124
125 // Mirrors the eraser's field set — everything the eraser treats as donor
126 // PII (incl. ip / user-agent / referrer) must also be disclosed on export.
127 $rows = [
128 __( 'Amount', 'suredonation' ) => $donation['amount'] ?? '',
129 __( 'Currency', 'suredonation' ) => $donation['currency'] ?? '',
130 __( 'Status', 'suredonation' ) => $donation['status'] ?? '',
131 __( 'Date', 'suredonation' ) => $donation['created_at'] ?? '',
132 __( 'Donor Name', 'suredonation' ) => $donation['donor_name'] ?? '',
133 __( 'Donor Email', 'suredonation' ) => $donation['donor_email'] ?? '',
134 __( 'Donor Phone', 'suredonation' ) => $donation['donor_phone'] ?? '',
135 __( 'Comment', 'suredonation' ) => $donation['donor_comment'] ?? '',
136 __( 'IP Address', 'suredonation' ) => $donation['ip_address'] ?? '',
137 __( 'User Agent', 'suredonation' ) => $donation['user_agent'] ?? '',
138 __( 'Referrer', 'suredonation' ) => $donation['referer_url'] ?? '',
139 ];
140
141 // Include the extra submitted form fields (label => value) if present.
142 $fields = isset( $donation['donation_data']['fields'] ) && is_array( $donation['donation_data']['fields'] ) ? $donation['donation_data']['fields'] : [];
143 foreach ( $fields as $field ) {
144 if ( ! is_array( $field ) || ! isset( $field['label'] ) ) {
145 continue;
146 }
147 // Deduplicate repeated labels ("Label", "Label (2)", …) — a duplicate
148 // key would silently overwrite the earlier field's value in the export.
149 $label = Helper::get_string_value( $field['label'] );
150 $unique = $label;
151 $suffix = 2;
152 while ( array_key_exists( $unique, $rows ) ) {
153 $unique = $label . ' (' . $suffix . ')';
154 ++$suffix;
155 }
156 $rows[ $unique ] = $field['value'] ?? '';
157 }
158
159 $export[] = [
160 'group_id' => 'suredonation-donations',
161 'group_label' => __( 'SureDonation Donations', 'suredonation' ),
162 'item_id' => 'suredonation-donation-' . absint( Helper::get_string_value( $donation['id'] ?? 0 ) ),
163 'data' => self::name_value_rows( $rows ),
164 ];
165 }
166
167 // Done once a page returns fewer donations than the batch size.
168 $done = count( $donations ) < $batch;
169
170 if ( $done ) {
171 /**
172 * Fires when a personal-data export including SureDonation data completes.
173 *
174 * @since 1.2.0
175 */
176 do_action( 'suredonation_privacy_data_exported' );
177 }
178
179 return [
180 'data' => $export,
181 'done' => $done,
182 ];
183 }
184
185 /**
186 * Erase a donor's personal data, honoring the retention period.
187 *
188 * Donations still inside the Minimum Data Retention Period are retained; older
189 * donations are fully anonymized. The donor profile is anonymized only when all
190 * of their donations were erased (none retained).
191 *
192 * Handled in a single pass (not paginated like export): the donor-profile decision
193 * needs to know whether *any* donation across the whole set was retained, which
194 * can't be determined from one page in isolation.
195 *
196 * @since 1.2.0
197 * @param string $email_address The email being erased.
198 * @param int $page Page number (unused — all data handled at once).
199 * @return array{items_removed: bool, items_retained: bool, messages: array<int, string>, done: bool}
200 */
201 public function erase( $email_address, $page = 1 ) {
202 unset( $page );
203 $email = sanitize_email( (string) $email_address );
204 $items_removed = false;
205 $items_retained = false;
206 $erase_failed = false;
207 $messages = [];
208
209 $donations = Donations::get_by_donor_email( $email );
210 foreach ( is_array( $donations ) ? $donations : [] as $donation ) {
211 if ( ! is_array( $donation ) || ! isset( $donation['id'] ) ) {
212 continue;
213 }
214
215 if ( ! Privacy_Settings::is_donation_erasable( Helper::get_string_value( $donation['created_at'] ?? '' ) ) ) {
216 $items_retained = true;
217 continue;
218 }
219
220 // Strip the personal-data keys from donation_data, keep the rest
221 // (e.g. refunds/notes are operational records, not donor PII).
222 $donation_data = isset( $donation['donation_data'] ) && is_array( $donation['donation_data'] ) ? $donation['donation_data'] : [];
223 unset( $donation_data['fields'] );
224
225 // The receipt PDF is generated from the donor's name/email/address —
226 // erasure must remove the file from disk, not just the DB columns.
227 $receipt_deleted = Receipt_Generator::delete_receipt( Helper::get_string_value( $donation['receipt_pdf_url'] ?? '' ) );
228
229 // Besides the direct PII columns, clear pseudonymous identifiers that
230 // re-link the record to the donor at the payment provider (Stripe
231 // customer/subscription ids) and the gateway log (can embed the donor's
232 // email). transaction_id and subscription_status are kept deliberately:
233 // the transaction reference is required for financial reconciliation /
234 // refund handling and identifies the payment, not the person; the
235 // status is a non-identifying enum.
236 $anonymized = [
237 'donor_name' => wp_privacy_anonymize_data( 'text', Helper::get_string_value( $donation['donor_name'] ?? '' ) ),
238 'donor_email' => wp_privacy_anonymize_data( 'email', $email ),
239 'donor_phone' => '',
240 'donor_comment' => '',
241 'ip_address' => wp_privacy_anonymize_data( 'ip', Helper::get_string_value( $donation['ip_address'] ?? '' ) ),
242 'user_agent' => '',
243 'referer_url' => '',
244 'donation_data' => $donation_data,
245 'customer_id' => '',
246 'subscription_id' => '',
247 'parent_subscription_id' => 0,
248 'log' => '',
249 ];
250
251 // Clear the receipt pointer only when the file is actually gone —
252 // otherwise keep it so a retried erasure can still find the file.
253 if ( $receipt_deleted ) {
254 $anonymized['receipt_pdf_url'] = '';
255 }
256
257 $updated = Donations::update( absint( Helper::get_string_value( $donation['id'] ) ), $anonymized );
258
259 // Donations::update() returns int|false — only report data as removed
260 // when the write actually succeeded; a confirmed erasure for data still
261 // in the database would be a silent compliance failure.
262 if ( false !== $updated && $receipt_deleted ) {
263 $items_removed = true;
264 } else {
265 $erase_failed = true;
266 }
267 }
268
269 // Anonymize the donor profile only when nothing is being retained for them
270 // (and nothing failed to erase — a failed donation write means PII may still
271 // reference this donor). All-or-nothing by design (Charitable-style model):
272 // while any donation is retained, its row still carries the donor's identity,
273 // so scrubbing only the profile would not reduce what is held; the profile is
274 // erased together with the last retained donation once the window lapses.
275 $donor = Donors::get_by_email( $email );
276 if ( is_array( $donor ) && ! empty( $donor ) && ! $items_retained && ! $erase_failed ) {
277 $donor_id = absint( Helper::get_string_value( $donor['id'] ?? 0 ) );
278 if ( $donor_id > 0 ) {
279 Donors::clear_stripe_customer_id_by_email( $email );
280
281 // Clear the per-account Stripe customer identifiers added with
282 // multi-account support: the donor_data map (folded into the
283 // anonymization write below) and the per-account user-meta cache.
284 $donor_data = isset( $donor['donor_data'] ) && is_array( $donor['donor_data'] ) ? $donor['donor_data'] : [];
285 unset( $donor_data['stripe_customers'] );
286
287 $wp_user = get_user_by( 'email', $email );
288 if ( $wp_user instanceof \WP_User ) {
289 delete_user_meta( $wp_user->ID, '_stripe_customer_id' );
290 global $wpdb;
291 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- One-off GDPR erase of prefixed per-account customer-id meta; $wpdb->usermeta is trusted.
292 $meta_keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key LIKE %s", $wp_user->ID, $wpdb->esc_like( '_suredonation_stripe_customer_id_' ) . '%' ) );
293 if ( is_array( $meta_keys ) ) {
294 foreach ( $meta_keys as $meta_key ) {
295 delete_user_meta( $wp_user->ID, (string) $meta_key );
296 }
297 }
298 }
299
300 $updated = Donors::update(
301 $donor_id,
302 [
303 // Keep the email unique per donor to respect the UNIQUE column.
304 'email' => 'deleted-' . $donor_id . '@site.invalid',
305 'name' => wp_privacy_anonymize_data( 'text', Helper::get_string_value( $donor['name'] ?? '' ) ),
306 'phone' => '',
307 'company' => '',
308 'address' => '',
309 'donor_data' => $donor_data,
310 ]
311 );
312
313 // Same int|false contract as the donation updates above.
314 if ( false !== $updated ) {
315 $items_removed = true;
316 } else {
317 $erase_failed = true;
318 }
319 }
320 }
321
322 if ( $items_retained ) {
323 $messages[] = __( 'Some donation data was retained because it falls within the configured data retention period.', 'suredonation' );
324 }
325
326 if ( $erase_failed ) {
327 $messages[] = __( 'Some SureDonation data could not be erased due to a database or filesystem error. Please retry or contact the site administrator.', 'suredonation' );
328 }
329
330 /**
331 * Fires when a personal-data erasure request has been processed for SureDonation data.
332 *
333 * @since 1.2.0
334 * @param array{items_removed: bool, items_retained: bool, erase_failed: bool} $outcome Erasure outcome flags.
335 */
336 do_action(
337 'suredonation_privacy_data_erased',
338 [
339 'items_removed' => $items_removed,
340 'items_retained' => $items_retained,
341 'erase_failed' => $erase_failed,
342 ]
343 );
344
345 return [
346 'items_removed' => $items_removed,
347 'items_retained' => $items_retained,
348 'messages' => $messages,
349 'done' => true,
350 ];
351 }
352
353 /**
354 * Convert a label => value map into the WordPress exporter row shape.
355 *
356 * @since 1.2.0
357 * @param array<string, mixed> $map Label => value pairs.
358 * @return array<int, array{name: string, value: string}>
359 */
360 private static function name_value_rows( $map ) {
361 $rows = [];
362 foreach ( $map as $label => $value ) {
363 $rows[] = [
364 'name' => (string) $label,
365 'value' => Helper::get_string_value( $value ),
366 ];
367 }
368 return $rows;
369 }
370 }
371