PluginProbe
The GDPR Framework By Data443 / trunk
The GDPR Framework By Data443 vtrunk
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Modules / ContactForm7 / Flamingo.php

Flamingo.php in The GDPR Framework By Data443 trunk, at src/Modules/ContactForm7/Flamingo.php

171 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\Modules\ContactForm7;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 class Flamingo
8 {
9 public function __construct()
10 {
11 add_filter('wpcf7_editor_panels', [$this, 'addCF7Tab']);
12 add_action('wpcf7_save_contact_form', [$this, 'saveCF7Tab'], 10, 3);
13 add_action( 'wpcf7_admin_notices', [$this, 'addInformation'] );
14
15 add_filter('gdpr/data-subject/data', [$this, 'getExportData'], 20, 2);
16 add_action('gdpr/data-subject/delete', [$this, 'deleteEntries']);
17 add_action('gdpr/data-subject/anonymize', [$this, 'deleteEntries']);
18 }
19
20 public function addCF7Tab($tabs)
21 {
22 $tabs['privacy-panel'] = [
23 'title' => __('Privacy', 'gdpr-framework'),
24 'callback' => [$this, 'renderPrivacyTab'],
25 ];
26
27 return $tabs;
28 }
29
30 public function renderPrivacyTab(\WPCF7_ContactForm $form)
31 {
32 $enabled = get_post_meta($form->id(), 'gdpr_cf7_enabled', true) ? get_post_meta($form->id(), 'gdpr_cf7_enabled', true) : '';
33 $emailField = get_post_meta($form->id(), 'gdpr_cf7_email_field', true) ? get_post_meta($form->id(), 'gdpr_cf7_email_field', true) : '';
34
35 echo gdpr('view')->render(
36 'modules/contact-form-7/form-privacy-tab',
37 compact('enabled', 'emailField')
38 );
39 }
40
41 public function saveCF7Tab(\WPCF7_ContactForm $contactForm, $args, $context)
42 {
43 if(isset($_POST['post_ID']) && $_POST['post_ID'])
44 {
45 $contact_form_id = filter_var($_POST['post_ID'], FILTER_SANITIZE_NUMBER_INT);
46 if (isset($_POST['gdpr_cf7_enabled'])) {
47 $sanitized_gdpr_cf7_enabled = filter_var($_POST['gdpr_cf7_enabled'], FILTER_SANITIZE_NUMBER_INT);
48 update_post_meta($contact_form_id, 'gdpr_cf7_enabled', $sanitized_gdpr_cf7_enabled);
49 }
50
51 if (isset($_POST['gdpr_cf7_email_field']) && !empty($_POST['gdpr_cf7_email_field'])) {
52 $sanitized_gdpr_cf7_email_field = filter_var($_POST['gdpr_cf7_email_field'], FILTER_SANITIZE_STRING);
53 update_post_meta($contact_form_id, 'gdpr_cf7_email_field', $sanitized_gdpr_cf7_email_field);
54 }
55 }
56 }
57
58 public function getExportData(array $data, $email)
59 {
60 $entries = $this->getEntriesByEmail($email);
61
62 if (!count($entries)) {
63 return $data;
64 }
65
66 foreach ($entries as $i => $message) {
67 $title = __('Form submissions: ', 'gdpr') . ucfirst($message->channel);
68
69 if (count($message->fields)) {
70 foreach ($message->fields as $key => $value) {
71 $data[$title][$i][$key] = $value;
72 }
73 }
74
75 if (count($message->consent)) {
76 foreach ($message->consent as $key => $value) {
77 $data[$title][$i][$key] = $value;
78 }
79 }
80
81 $data[$title][$i]['date'] = $message->date;
82 $data[$title][$i]['ip'] = $message->meta['remote_ip'];
83 $data[$title][$i]['user_agent'] = $message->meta['user_agent'];
84 $data[$title][$i]['url'] = $message->meta['post_url'];
85 }
86
87 return $data;
88 }
89
90 public function getEntriesByEmail($email)
91 {
92 $forms = $this->getValidForms();
93
94 if (!count($forms)) {
95 return [];
96 }
97
98 $entries = [];
99
100 foreach ($forms as $form) {
101 /* @var $form \WPCF7_ContactForm */
102 $messages = \Flamingo_Inbound_Message::find([
103 'posts_per_page' => -1,
104 'channel' => get_post_field( 'post_name', $form->id()),
105 ]);
106
107 if (!count($messages)) {
108 continue;
109 }
110
111 $emailField = get_post_meta($form->id(), 'gdpr_cf7_email_field', true);
112
113 if (!$emailField) {
114 continue;
115 }
116
117 foreach ($messages as $message) {
118 if ($email === $message->fields[$emailField]) {
119 $entries[] = $message;
120 }
121 }
122 }
123
124 return $entries;
125 }
126
127 public function getValidForms()
128 {
129 return \WPCF7_ContactForm::find([
130 'meta_query' => [
131 [
132 'key' => 'gdpr_cf7_enabled',
133 'value' => '1',
134 ],
135 ],
136 ]);
137 }
138
139 public function deleteEntries($email)
140 {
141 $entries = $this->getEntriesByEmail($email);
142
143 if (count($entries)) {
144 foreach ($entries as $i => $message) {
145 if ($message->id) {
146 $message->delete();
147 }
148 }
149 }
150
151 $contacts = \Flamingo_Contact::search_by_email($email);
152 if (count($contacts)) {
153 foreach ($contacts as $i => $contactId) {
154 (new \Flamingo_Contact($contactId))->delete();
155 }
156 }
157
158 }
159
160 public function addInformation()
161 {
162 $gdpr_notice_heading = _x("Do you want form to be GDPR compliance.", "(Admin)", "gdpr-framework");
163 $gdpr_notice_message = _x("You have installed flamingo, To make this GDPR compliance in individual contact form's privacy tab check the checkbox for include data to be search on Privacy tool.", "(Admin)", "gdpr-framework");
164
165 echo "<div class='welcome-gdpr-notice'>";
166 echo "<h3>".$gdpr_notice_heading."</h3>";
167 echo "<p>".$gdpr_notice_message."</p>";
168 echo "</div>";
169 }
170
171 }