PersonalDataExporter.php
3.88 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
<?php
namespace wpdFormAttr\Tools;
use wpdFormAttr\FormConst\wpdFormConst;
use wpdFormAttr\Form;
class PersonalDataExporter implements wpdFormConst {
private static $_instance = null;
private $generalOptions;
private $fields = [];
private function __construct($options) {
$this->generalOptions = $options;
$this->initFormsFields();
add_filter("wp_privacy_personal_data_exporters", [&$this, "wpdiscuzCommentsPersonalDataExport"], 13);
}
private function initFormsFields() {
$forms = get_posts(["numberposts" => -1, "post_type" => self::WPDISCUZ_FORMS_CONTENT_TYPE]);
if ($forms) {
foreach ($forms as $k => $form) {
$wpdiscuzForm = new Form($this->generalOptions, $form->ID);
$wpdiscuzForm->initFormFields();
$formFields = $wpdiscuzForm->getFormCustomFields();
if ($formFields) {
$this->fields = array_merge($this->fields, $formFields);
}
}
}
}
public function wpdiscuzCommentsPersonalDataExport($exporters) {
$exporters["wpdiscuz"] = [
"exporter_friendly_name" => esc_html__("wpDiscuz Fields Data", "wpdiscuz"),
"callback" => [&$this, "customFieldsExport"],
];
return $exporters;
}
public function customFieldsExport($email_address, $page = 1) {
$number = 500; // Limit us to avoid timing out
$page = (int) $page;
$done = true;
$export_items = [];
$doExport = apply_filters("wpdiscuz_do_export_personal_data", false);
if ($this->fields || $doExport) {
$comments = get_comments(
[
"author_email" => $email_address,
"number" => $number,
"paged" => $page,
"order_by" => "comment_ID",
"order" => "ASC",
]
);
foreach ((array) $comments as $k => $comment) {
$commentId = $comment->comment_ID;
$data = [];
$commentMeta = get_metadata("comment", $commentId);
foreach ($this->fields as $key => $field) {
if (isset($commentMeta[$key])) {
$value = $this->generateFieldData($commentMeta[$key][0]);
if (empty($value)) {
continue;
}
$data[] = [
"name" => $field["name"],
"value" => $value,
];
}
}
$data = apply_filters("wpdiscuz_privacy_personal_data_export", $data, $commentId);
if ($data) {
$export_items[] = [
"group_id" => "comments",
"group_label" => esc_html__("Comments"),
"item_id" => "comment-$commentId",
"data" => $data,
];
}
}
$done = count($comments) < $number;
}
return [
"data" => $export_items,
"done" => $done,
];
}
private function generateFieldData($data) {
$value = "";
$data = maybe_unserialize($data);
if (empty($data)) {
return "";
}
if (is_array($data)) {
$value = implode(", ", $data);
} else {
$value = $data;
}
return $value;
}
public static function getInstance($options) {
if (is_null(self::$_instance)) {
self::$_instance = new self($options);
}
return self::$_instance;
}
}