-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathclass-gdpr.php
More file actions
executable file
·706 lines (623 loc) · 25.1 KB
/
class-gdpr.php
File metadata and controls
executable file
·706 lines (623 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
<?php
/**
* The file that defines the core plugin class
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @link https://trewknowledge.com
* @since 1.0.0
*
* @package GDPR
* @subpackage includes
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
/**
* The core plugin class.
*
* This is used to define internationalization, admin-specific hooks, and
* public-facing site hooks.
*
* Also maintains the unique identifier of this plugin as well as the current
* version of the plugin.
*
* @since 1.0.0
* @package GDPR
* @subpackage includes
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
class GDPR {
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The current version of the plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
public function __construct() {
if ( defined( 'GDPR_VERSION' ) ) {
$this->version = GDPR_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'gdpr';
$this->load_dependencies();
$this->define_common_hooks();
$this->define_admin_hooks();
$this->define_public_hooks();
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) or ( defined( 'DOING_CRON' ) && DOING_CRON ) or ( defined( 'DOING_AJAX' ) && DOING_AJAX ) or ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) {
return;
}
}
/**
* Load the required dependencies for this plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access private
*/
private function load_dependencies() {
/**
* The class responsible for adding help tabs.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gdpr-help.php';
/**
* The class responsible logging user actions.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gdpr-audit-log.php';
/**
* The class responsible for defining the requests section of the plugin.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gdpr-requests.php';
/**
* The class responsible for defining the admin facing requests section of the plugin.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-gdpr-requests-admin.php';
/**
* The class responsible for defining the admin facing requests section of the plugin.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-gdpr-requests-public.php';
/**
* The class responsible for locating the email templates and sending emails.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gdpr-email.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-gdpr-admin.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-gdpr-public.php';
/**
* The class responsible for defining compatibility to olp php versions.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/compatibility-functions.php';
}
/**
* Define the locale for this plugin for internationalization.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
public function set_locale() {
load_plugin_textdomain(
'gdpr',
false,
plugin_dir_url( dirname( __FILE__ ) ) . 'languages/'
);
}
/**
* Register all of the common hooks.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access private
*/
private function define_common_hooks() {
add_action( 'wp_ajax_gdpr_generate_data_export', array( $this, 'export_data' ) );
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access private
*/
private function define_admin_hooks() {
$plugin_admin = new GDPR_Admin( $this->get_plugin_name(), $this->get_version() );
$requests_admin = new GDPR_Requests_Admin( $this->get_plugin_name(), $this->get_version() );
$requests = new GDPR_Requests( $this->get_plugin_name(), $this->get_version() );
$plugin_emails = new GDPR_Email();
$woo_add_to_registration = get_option( 'gdpr_add_consent_checkboxes_registration', false );
$woo_add_to_checkout = get_option( 'gdpr_add_consent_checkboxes_checkout', false );
add_filter( 'nonce_user_logged_out', array( $this, 'woo_nonce_fix' ), 100, 2 );
add_action( 'plugins_loaded', array( $this, 'set_locale' ) );
add_action( 'bp_account_details_fields', array( __CLASS__, 'consent_checkboxes' ) );
if ( $woo_add_to_registration ) {
add_action( 'woocommerce_register_form', array( __CLASS__, 'consent_checkboxes' ) );
}
if ( $woo_add_to_checkout ) {
add_action( 'woocommerce_checkout_update_user_meta', array( $plugin_admin, 'woocommerce_checkout_save_consent' ), 10, 2 );
add_filter( 'woocommerce_checkout_fields', array( $plugin_admin, 'woocommerce_consent_checkboxes' ) );
}
add_filter( 'manage_users_custom_column', array( $plugin_admin, 'add_consents_to_consents_column' ), 10, 3 );
add_filter( 'manage_users_columns', array( $plugin_admin, 'add_consents_column_to_user_table' ) );
add_filter( 'manage_users_sortable_columns', array( $plugin_admin, 'sort_consents_column_from_user_table' ) );
add_action( 'pre_get_users', array( $plugin_admin, 'sort_logic_for_consents_from_user_table' ) );
add_action( 'show_user_profile', array( $plugin_admin, 'edit_user_profile' ) );
add_action( 'personal_options_update', array( $plugin_admin, 'user_profile_update' ) );
add_action( 'admin_notices', array( $plugin_admin, 'policy_updated_notice' ) );
add_action( 'admin_notices', array( $plugin_admin, 'version_check_notice' ) );
add_action( 'admin_notices', array( $plugin_admin, 'review_settings_after_v2_notice' ) );
add_action( 'upgrader_process_complete', array( $plugin_admin, 'upgrade_completed' ), 10, 2 );
add_action( 'wp_ajax_ignore_policy_update', array( $plugin_admin, 'ignore_policy_update' ) );
add_action( 'wp_ajax_seek_consent', array( $plugin_admin, 'seek_consent' ) );
add_action( 'publish_page', array( $plugin_admin, 'policy_updated' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_scripts' ) );
add_action( 'admin_menu', array( $plugin_admin, 'add_menu' ) );
add_action( 'admin_init', array( $plugin_admin, 'register_settings' ) );
add_action( 'register_form', array( __CLASS__, 'consent_checkboxes' ) );
add_action( 'registration_errors', array( $plugin_admin, 'registration_errors' ), 10, 3 );
add_action( 'user_register', array( __CLASS__, 'save_user_consent_on_registration' ) );
add_action( 'wp_ajax_gdpr_access_data', array( $plugin_admin, 'access_data' ) );
add_action( 'wp_ajax_gdpr_audit_log', array( $plugin_admin, 'audit_log' ) );
add_action( 'admin_post_gdpr_data_breach', array( $plugin_admin, 'send_data_breach_confirmation_email' ) );
add_action( 'clean_gdpr_data_breach_request', array( $plugin_admin, 'clean_data_breach_request' ), 10, 2 ); // CRON JOB
add_action( 'admin_post_gdpr_delete_user', array( $requests_admin, 'delete_user' ) );
add_action( 'admin_post_gdpr_cancel_request', array( $requests_admin, 'cancel_request' ) );
add_action( 'admin_post_gdpr_add_to_deletion_requests', array( $requests_admin, 'add_to_deletion_requests' ) );
add_action( 'admin_post_gdpr_mark_resolved', array( $requests_admin, 'mark_resolved' ) );
add_action( 'wp_ajax_gdpr_anonymize_comments', array( $requests_admin, 'anonymize_comments' ) );
add_action( 'wp_ajax_gdpr_reassign_content', array( $requests_admin, 'reassign_content' ) );
// CRON JOBS
add_action( 'clean_gdpr_requests', array( $requests, 'clean_requests' ) );
add_action( 'clean_gdpr_user_request_key', array( $requests, 'clean_user_request_key' ), 10, 2 );
add_action( 'send_data_breach_emails', array( $plugin_emails, 'send_data_breach_emails' ), 10, 2 );
}
/**
* Fixes nonce manipulation made by Woocommerce.
* @param int $user_id The user id.
* @param string $action The nonce Action.
* @return int The user id.
*/
function woo_nonce_fix( $user_id, $action ) {
if ( ( 0 !== $user_id ) && $action && ( false !== strpos( $action, 'gdpr-' ) ) ) {
$user_id = 0;
}
return $user_id;
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @access private
*/
private function define_public_hooks() {
$plugin_public = new GDPR_Public( $this->get_plugin_name(), $this->get_version() );
$requests_public = new GDPR_Requests_Public( $this->get_plugin_name(), $this->get_version() );
add_action( 'wp_enqueue_scripts', array( $plugin_public, 'enqueue_styles' ) );
add_action( 'wp_enqueue_scripts', array( $plugin_public, 'enqueue_scripts' ) );
add_action( 'wp_footer', array( $plugin_public, 'overlay' ) );
add_action( 'wp_footer', array( $plugin_public, 'privacy_bar' ) );
add_action( 'wp_footer', array( $plugin_public, 'is_consent_needed' ) );
add_action( 'wp_footer', array( $plugin_public, 'privacy_preferences_modal' ) );
add_action( 'wp_footer', array( $plugin_public, 'confirmation_screens' ) );
add_action( 'wp_ajax_disagree_with_terms', array( $plugin_public, 'logout' ) );
add_action( 'wp_ajax_agree_with_terms', array( $plugin_public, 'agree_with_terms' ) );
add_action( 'wp_ajax_gdpr_update_privacy_preferences', array( $plugin_public, 'update_privacy_preferences' ) );
add_action( 'wp_ajax_nopriv_gdpr_update_privacy_preferences', array( $plugin_public, 'update_privacy_preferences' ) );
add_action( 'wp_ajax_agree_with_new_policies', array( $plugin_public, 'agree_with_new_policies' ) );
add_action( 'wp_ajax_nopriv_agree_with_new_policies', array( $plugin_public, 'agree_with_new_policies' ) );
add_action( 'wp', array( $requests_public, 'request_confirmed' ) );
add_action( 'wp_ajax_gdpr_send_request_email', array( $requests_public, 'send_request_email' ) );
add_action( 'wp_ajax_nopriv_gdpr_send_request_email', array( $requests_public, 'send_request_email' ) );
}
/**
* Checks in an array if a value is found using LIKE instead of =.
* @since 1.4.3
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @return Bool
*/
public static function similar_in_array( $needle, $haystack ) {
foreach ( $haystack as $value ) {
if ( stripos( strtolower( $value ), strtolower( $needle ) ) !== false ) {
return true;
}
}
return false;
}
/**
* Save the extra fields on a successful registration.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @param int $user_id The user ID.
*/
public static function save_user_consent_on_registration( $user_id ) {
GDPR_Audit_Log::log( $user_id, esc_html__( 'User registered to the site.', 'gdpr' ) );
if ( isset( $_POST['user_consents'] ) && is_array( $_POST['user_consents'] ) ) { // phpcs:ignore
$consents = array_map( 'sanitize_text_field', array_keys( wp_unslash( $_POST['user_consents'] ) ) ); // phpcs:ignore
foreach ( $consents as $consent ) {
/* translators: Name of consent */
GDPR_Audit_Log::log( $user_id, sprintf( esc_html__( 'User gave explicit consent to %s', 'gdpr' ), $consent ) );
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
add_user_attribute( $user_id, 'gdpr_consents', $consent );
} else {
add_user_meta( $user_id, 'gdpr_consents', $consent );
}
}
}
}
/**
* Returns the consent checkboxes to be used across the site.
* @since 1.2.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
public static function get_consent_checkboxes( $consent_key = false ) {
$consent_types = get_option( 'gdpr_consent_types', array() );
if ( empty( $consent_types ) ) {
return;
}
$sent_extras = ( isset( $_POST['user_consents'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['user_consents'] ) ) : array(); // phpcs:ignore
$allowed_html = array(
'a' => array(
'href' => true,
'title' => true,
'target' => true,
),
);
if ( $consent_key ) {
$consent_types = array_filter(
$consent_types, function( $key ) use ( $consent_key ) {
return $key === $consent_key;
}, ARRAY_FILTER_USE_KEY
);
}
ob_start();
foreach ( $consent_types as $key => $consent ) {
$required = ( isset( $consent['policy-page'] ) && $consent['policy-page'] ) ? 'required' : '';
$checked = ( isset( $sent_extras[ $key ] ) ) ? checked( $sent_extras[ $key ], 1, false ) : '';
echo '<p>' .
'<label class="gdpr-label">' .
'<input type="checkbox" name="user_consents[' . esc_attr( $key ) . ']" id="' . esc_attr( $key ) . '-consent" value="1" ' . esc_html( $required ) . ' ' . esc_html( $checked ) . '>' .
wp_kses( $consent['registration'], $allowed_html ) . '</label>' .
'</p>';
}
return ob_get_clean();
}
/**
* Renders consent checkboxes to be used across the site.
* @since 1.1.4
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
public static function consent_checkboxes( $consent_key = false ) {
echo self::get_consent_checkboxes( $consent_key ); // phpcs:ignore
}
/**
* Get user meta for exporting.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @static
* @param int $user_id The user ID.
* @return array The user meta minus not important metas.
*/
static function get_user_meta( $user_id ) {
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
$usermeta = get_user_attribute( $user_id );
} else {
$usermeta = get_user_meta( $user_id );
}
$remove_metadata = array(
'nickname',
'first_name',
'last_name',
'description',
'rich_editing',
'syntax_highlighting',
'comment_shortcuts',
'admin_color',
'use_ssl',
'show_admin_bar_front',
'wp_capabilities',
'wp_user_level',
'gdpr_consents',
'gdpr_audit_log',
'dismissed_wp_pointers',
'gdpr_delete_key',
'gdpr_rectify_key',
'gdpr_complaint_key',
'gdpr_export-data_key',
);
return array_diff_key( $usermeta, array_flip( $remove_metadata ) );
}
/**
* Generates the export in JSON or XML formats.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @static
* @param string $email The user email.
* @param string $format Either XML or JSON.
* @return string Returns the file as string.
*/
static function generate_export( $email, $format ) {
$email = sanitize_email( $email );
$user = get_user_by( 'email', $email );
if ( ! $user instanceof WP_User ) {
return false;
}
$usermeta = self::get_user_meta( $user->ID );
$comments = get_comments(
array(
'author_email' => $user->user_email,
'include_unapproved' => true,
)
);
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
$user_consents = get_user_attribute( $user->ID, 'gdpr_consents' );
} else {
$user_consents = get_user_meta( $user->ID, 'gdpr_consents' );
}
$extra_content = apply_filters( 'gdpr_export_data_extra_tables', '', $email );
switch ( strtolower( $format ) ) {
case 'json':
$metadata = array();
foreach ( $usermeta as $k => $v ) {
$metadata[ $k ] = array();
foreach ( $v as $value ) {
if ( is_serialized( $value ) ) {
$metadata[ $k ][] = maybe_unserialize( $value );
} else {
$metadata[ $k ] = $value;
}
}
}
$comments_array = array();
if ( ! empty( $comments ) ) {
foreach ( $comments as $k => $v ) {
$comments_array[ $k ] = array(
'comment_author' => $v->comment_author,
'comment_author_email' => $v->comment_author_email,
'comment_author_url' => $v->comment_author_url,
'comment_author_IP' => $v->comment_author_IP,
'comment_date' => $v->comment_date,
'comment_agent' => $v->comment_agent,
'comment_content' => $v->comment_content,
);
}
}
$json = array(
'Personal Information' => array(
'Username' => $user->user_login,
'First name' => $user->first_name,
'Last name' => $user->last_name,
'Email' => $user->user_email,
'Nickname' => $user->nickname,
'Display name' => $user->display_name,
'Description' => $user->description,
'Website' => $user->user_url,
),
'Consents' => $user_consents,
'Metadata' => $metadata,
'Comments' => $comments_array,
);
if ( $extra_content ) {
$json[ $extra_content['name'] ] = $extra_content['content'];
}
return wp_json_encode( $json );
break;
case 'md':
case 'markdown':
# code...
break;
default: // XML
$dom = new DomDocument( '1.0', 'ISO-8859-1' );
$data_wrapper = $dom->createElement( 'Data' );
$dom->appendChild( $data_wrapper );
$personal_info = $dom->createElement( 'Personal_Information' );
$data_wrapper->appendChild( $personal_info );
$personal_info->appendChild( $dom->createElement( 'Username', $user->user_login ) );
$personal_info->appendChild( $dom->createElement( 'First_Name', $user->first_name ) );
$personal_info->appendChild( $dom->createElement( 'Last_Name', $user->last_name ) );
$personal_info->appendChild( $dom->createElement( 'Email', $user->user_email ) );
$personal_info->appendChild( $dom->createElement( 'Nickname', $user->nickname ) );
$personal_info->appendChild( $dom->createElement( 'Display_Name', $user->display_name ) );
$personal_info->appendChild( $dom->createElement( 'Description', $user->description ) );
$personal_info->appendChild( $dom->createElement( 'Website', $user->user_url ) );
if ( ! empty( $user_consents ) ) {
$consents = $dom->createElement( 'Consents' );
$data_wrapper->appendChild( $consents );
foreach ( $user_consents as $consent_item ) {
$consents->appendChild( $dom->createElement( 'consent', $consent_item ) );
}
}
if ( ! empty( $comments ) ) {
$comments_node = $dom->createElement( 'Comments' );
$data_wrapper->appendChild( $comments_node );
foreach ( $comments as $k => $v ) {
$single_comment = $dom->createElement( 'Comment' );
$comments_node->appendChild( $single_comment );
$single_comment->appendChild( $dom->createElement( 'comment_author', htmlspecialchars( $v->comment_author ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_author_email', htmlspecialchars( $v->comment_author_email ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_author_url', htmlspecialchars( $v->comment_author_url ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_author_IP', htmlspecialchars( $v->comment_author_IP ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_date', htmlspecialchars( $v->comment_date ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_agent', htmlspecialchars( $v->comment_agent ) ) );
$single_comment->appendChild( $dom->createElement( 'comment_content', htmlspecialchars( $v->comment_content ) ) );
}
}
$meta_data = $dom->createElement( 'Metadata' );
$data_wrapper->appendChild( $meta_data );
foreach ( $usermeta as $k => $v ) {
$k = is_numeric( substr( $k, 0, 1 ) ) ? '_' . $k : $k;
$key = $dom->createElement( htmlspecialchars( $k ) );
$meta_data->appendChild( $key );
foreach ( $v as $value ) {
$key->appendChild( $dom->createElement( 'item', htmlspecialchars( $value ) ) );
}
}
if ( $extra_content ) {
$extra = $dom->createElement( $extra_content['name'] );
$data_wrapper->appendChild( $extra );
foreach ( $extra_content['content'] as $key => $obj ) {
$item = $extra->appendChild( $dom->createElement( 'item' ) );
foreach ( $obj as $k => $value ) {
$item->appendChild( $dom->createElement( $k, ( is_object( $value ) || is_array( $value ) ) ? wp_json_encode( (array) $value ) : $value ) );
}
}
}
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
return $dom->saveXML();
break;
}
return false;
}
/**
* Export the generated export file.
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
*/
function export_data() {
if ( ! isset( $_POST['nonce'], $_POST['email'], $_POST['type'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ) ), 'gdpr-export-data' ) ) { // phpcs:ignore
wp_send_json_error();
}
$type = sanitize_text_field( wp_unslash( $_POST['type'] ) ); // phpcs:ignore
$email = sanitize_email( wp_unslash( $_POST['email'] ) ); // phpcs:ignore
$user = get_user_by( 'email', $email );
if ( ! $user instanceof WP_User ) {
wp_send_json_error();
}
$export = self::generate_export( $email, $type );
if ( $export ) {
wp_send_json_success( $export );
}
wp_send_json_error();
}
/**
* Save a consent to the user meta.
* @since 1.1.4
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @param integer $user_id The user ID.
* @param string $consent The consent ID.
* @return void
*/
public static function save_consent( $user_id, $consent ) {
$registered_consent = get_option( 'gdpr_consent_types', array() );
if ( empty( $registered_consent ) ) {
return false;
}
$consent_ids = array_keys( $registered_consent );
$consent = sanitize_text_field( wp_unslash( $consent ) );
$user = get_user_by( 'ID', $user_id );
if ( ! $user ) {
return false;
}
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
$user_consent = get_user_attribute( $user_id, 'gdpr_consents' );
} else {
$user_consent = get_user_meta( $user_id, 'gdpr_consents' );
}
if ( in_array( $consent, $consent_ids, true ) && ! in_array( $consent, $user_consent, true ) ) {
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
add_user_attribute( $user_id, 'gdpr_consents', $consent );
} else {
add_user_meta( $user_id, 'gdpr_consents', $consent );
}
$user_consent[] = $consent;
setcookie( 'gdpr_consent_types', wp_json_encode( $user_consent ), time() + YEAR_IN_SECONDS, '/' ); // phpcs:ignore
return true;
}
return false;
}
/**
* Remove a user consent.
* @since 1.1.4
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @param integer $user_id The user ID.
* @param string $consent The consent ID.
* @return void
*/
public static function remove_consent( $user_id, $consent ) {
$user = get_user_by( 'ID', $user_id );
if ( ! $user ) {
return false;
}
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
$user_consent = get_user_attribute( $user_id, 'gdpr_consents' );
} else {
$user_consent = get_user_meta( $user_id, 'gdpr_consents' );
}
$consent = sanitize_text_field( wp_unslash( $consent ) );
$key = array_search( $consent, $user_consent, true );
if ( false !== $key ) {
if ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) {
delete_user_attribute( $user_id, 'gdpr_consents', $consent );
} else {
delete_user_meta( $user_id, 'gdpr_consents', $consent );
}
unset( $user_consent[ $key ] );
setcookie( 'gdpr_consent_types', wp_json_encode( $user_consent ), time() + YEAR_IN_SECONDS, '/' ); // phpcs:ignore
return true;
}
return false;
}
/**
* Generates a random 6 digit pin.
* This pin is necessary to use with the audit log files.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @static
* @param integer $length Number of digits.
* @return string Returns the generated pin
*/
public static function generate_pin( $length = 6 ) {
$bytes = openssl_random_pseudo_bytes( $length / 2 );
return strtoupper( bin2hex( $bytes ) );
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @return string The name of the plugin.
*/
public function get_plugin_name() {
return $this->plugin_name;
}
/**
* Retrieve the version number of the plugin.
*
* @since 1.0.0
* @author Fernando Claussen <fernandoclaussen@gmail.com>
* @return string The version number of the plugin.
*/
public function get_version() {
return $this->version;
}
}