| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\DataSubject; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class DataRepository |
| 7 |
* |
| 8 |
* @package Codelight\GDPR\DataSubject |
| 9 |
*/ |
| 10 |
class DataRepository |
| 11 |
{ |
| 12 |
protected $email; |
| 13 |
|
| 14 |
/** |
| 15 |
* DataRepository constructor. |
| 16 |
* |
| 17 |
* @param $email |
| 18 |
*/ |
| 19 |
public function __construct($email) |
| 20 |
{ |
| 21 |
$this->email = $email; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Export all stored data. Triggers 'gdpr/data-subject/data' filter. |
| 26 |
*/ |
| 27 |
public function getData($email) |
| 28 |
{ |
| 29 |
return apply_filters('gdpr/data-subject/data', [], $email); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Trigger the configured 'export' action |
| 34 |
* |
| 35 |
* @param $email |
| 36 |
* @return array|null |
| 37 |
*/ |
| 38 |
public function export($email, $format, $force = false) |
| 39 |
{ |
| 40 |
global $gdpr; |
| 41 |
$action = $gdpr->Options->get('export_action'); |
| 42 |
$data = null; |
| 43 |
|
| 44 |
if ($force) { |
| 45 |
$action = 'download'; |
| 46 |
} |
| 47 |
|
| 48 |
switch($action) { |
| 49 |
case 'download': |
| 50 |
$data = $this->getData($email); |
| 51 |
break; |
| 52 |
case 'download_and_notify': |
| 53 |
$data = $this->getData($email); |
| 54 |
$this->notifyExportAction($email, $format); |
| 55 |
break; |
| 56 |
case 'notify': |
| 57 |
$this->notifyExportRequest($email, $format); |
| 58 |
break; |
| 59 |
default: |
| 60 |
$this->notifyExportRequest($email, $format); |
| 61 |
break; |
| 62 |
} |
| 63 |
|
| 64 |
return $data; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Trigger the configured 'forget' action |
| 69 |
* |
| 70 |
* @param $email |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public function forget($email, $forceAction = null) |
| 75 |
{ |
| 76 |
global $gdpr; |
| 77 |
$action = $gdpr->Options->get('delete_action'); |
| 78 |
|
| 79 |
if ($forceAction) { |
| 80 |
$action = $forceAction; |
| 81 |
} |
| 82 |
|
| 83 |
switch($action) { |
| 84 |
case 'delete': |
| 85 |
$this->delete($email); |
| 86 |
return true; |
| 87 |
case 'delete_and_notify': |
| 88 |
$userId = $this->delete($email); |
| 89 |
$this->notifyForgetAction($email, $userId); |
| 90 |
return true; |
| 91 |
case 'anonymize': |
| 92 |
$this->anonymize($email); |
| 93 |
return true; |
| 94 |
case 'anonymize_and_notify': |
| 95 |
$userId = $this->anonymize($email); |
| 96 |
$this->notifyForgetAction($email, $userId); |
| 97 |
return true; |
| 98 |
case 'notify': |
| 99 |
$this->notifyForgetRequest($email); |
| 100 |
return false; |
| 101 |
default: |
| 102 |
$this->notifyForgetRequest($email); |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param $email |
| 109 |
*/ |
| 110 |
protected function anonymize($email) |
| 111 |
{ |
| 112 |
$userId = null; |
| 113 |
|
| 114 |
if (email_exists($email)) { |
| 115 |
$userId = get_user_by('email', $email)->ID; |
| 116 |
} |
| 117 |
|
| 118 |
$anonymizedId = wp_generate_password(12, false); |
| 119 |
do_action('gdpr/data-subject/anonymize', $email, $anonymizedId, $userId); |
| 120 |
|
| 121 |
return $userId; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param $email |
| 126 |
*/ |
| 127 |
protected function delete($email) |
| 128 |
{ |
| 129 |
$userId = null; |
| 130 |
|
| 131 |
if (email_exists($email)) { |
| 132 |
$userId = get_user_by('email', $email)->ID; |
| 133 |
} |
| 134 |
|
| 135 |
do_action('gdpr/data-subject/delete', $email, $userId); |
| 136 |
|
| 137 |
return $userId; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param $email |
| 142 |
*/ |
| 143 |
protected function notifyExportAction($email, $format) |
| 144 |
{ |
| 145 |
global $gdpr; |
| 146 |
gdpr('helpers')->mail( |
| 147 |
$gdpr->Options->get('export_action_email'), |
| 148 |
__("Data exported", 'gdpr-framework'), |
| 149 |
gdpr('view')->render('email/action-export', compact('email', 'format')), |
| 150 |
['Content-Type: text/html; charset=UTF-8'] |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param $email |
| 156 |
*/ |
| 157 |
protected function notifyExportRequest($email, $format) |
| 158 |
{ |
| 159 |
global $gdpr; |
| 160 |
$adminTabLink = esc_url(gdpr('helpers')->getAdminUrl('&gdpr-tab=data-subject&search=' . $email)); |
| 161 |
|
| 162 |
gdpr('helpers')->mail( |
| 163 |
$gdpr->Options->get('export_action_email'), |
| 164 |
__("Data export request", 'gdpr-framework'), |
| 165 |
gdpr('view')->render('email/request-export', compact('email', 'format', 'adminTabLink')), |
| 166 |
['Content-Type: text/html; charset=UTF-8'] |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param $email |
| 172 |
*/ |
| 173 |
protected function notifyForgetAction($email, $userId = null) |
| 174 |
{ |
| 175 |
global $gdpr; |
| 176 |
gdpr('helpers')->mail( |
| 177 |
$gdpr->Options->get('delete_action_email'), |
| 178 |
__("Data removed", 'gdpr-framework'), |
| 179 |
gdpr('view')->render('email/action-forget', compact('email', 'userId')), |
| 180 |
['Content-Type: text/html; charset=UTF-8'] |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param $email |
| 186 |
*/ |
| 187 |
protected function notifyForgetRequest($email) |
| 188 |
{ |
| 189 |
global $gdpr; |
| 190 |
$adminTabLink = esc_url(gdpr('helpers')->getAdminUrl('&gdpr-tab=data-subject&search=' . $email)); |
| 191 |
|
| 192 |
gdpr('helpers')->mail( |
| 193 |
$gdpr->Options->get('delete_action_email'), |
| 194 |
__("Data removal request", 'gdpr-framework'), |
| 195 |
gdpr('view')->render('email/request-forget', compact('email', 'adminTabLink')), |
| 196 |
['Content-Type: text/html; charset=UTF-8'] |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|