| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Components\Consent; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles getting, saving and removing consent based on a whitelist |
| 7 |
* |
| 8 |
* Class ConsentManager |
| 9 |
* |
| 10 |
* @package Codelight\GDPR\Components\Consent |
| 11 |
*/ |
| 12 |
class ConsentManager |
| 13 |
{ |
| 14 |
/* @var UserConsentModel */ |
| 15 |
protected $model; |
| 16 |
|
| 17 |
/* @var array */ |
| 18 |
protected $defaultConsentTypes = []; |
| 19 |
|
| 20 |
/* @var array */ |
| 21 |
protected $customConsentTypes = []; |
| 22 |
|
| 23 |
/** |
| 24 |
* ConsentManager constructor. |
| 25 |
* |
| 26 |
* @param UserConsentModel $model |
| 27 |
*/ |
| 28 |
public function __construct(UserConsentModel $model) |
| 29 |
{ |
| 30 |
$this->model = $model; |
| 31 |
|
| 32 |
add_action('init', [$this, 'registerCustomConsentTypes'], 0); |
| 33 |
add_action('init', [$this, 'registerDefaultConsentTypes'], 0); |
| 34 |
|
| 35 |
add_filter('gdpr/data-subject/data', [$this, 'getdata'], 20, 2); |
| 36 |
add_action('gdpr/data-subject/delete', [$this, 'delete']); |
| 37 |
add_action('gdpr/data-subject/anonymize', [$this, 'anonymize'], 10, 2); |
| 38 |
} |
| 39 |
|
| 40 |
public function registerDefaultConsentTypes() |
| 41 |
{ |
| 42 |
global $gdpr; |
| 43 |
|
| 44 |
/**** |
| 45 |
* privacy-policy default consent |
| 46 |
*/ |
| 47 |
$policyPageUrl = get_permalink($gdpr->Options->get('policy_page')); |
| 48 |
add_filter( 'gdpr_custom_policy_link', 'gdprfPrivacyPolicyurl' ); |
| 49 |
|
| 50 |
$policyPageUrl = apply_filters( 'gdpr_custom_policy_link',$policyPageUrl); |
| 51 |
|
| 52 |
$gdpr->Consent->register( |
| 53 |
'privacy-policy', |
| 54 |
sprintf( |
| 55 |
__('I accept the %sPrivacy Policy%s', 'gdpr-framework'), |
| 56 |
"<a href='{$policyPageUrl}' target='_blank'>", |
| 57 |
"</a>" |
| 58 |
), |
| 59 |
_x('This consent is not visible by default. If someone wishes to withdraw it, they should simply request to delete all their data.', '(Admin)', 'gdpr-framework'), |
| 60 |
false |
| 61 |
); |
| 62 |
|
| 63 |
/**** |
| 64 |
* privacy-policy do-not-sell-request form default consent |
| 65 |
*/ |
| 66 |
|
| 67 |
$this->register( |
| 68 |
'do-not-sell-info', |
| 69 |
sprintf( |
| 70 |
__( 'Do Not Sell My Data', 'gdpr-framework' ) |
| 71 |
), |
| 72 |
_x( 'I do not consent to having my information sold by ' , '(Admin)', 'gdpr-framework' ) .get_bloginfo('name'). '.', |
| 73 |
true |
| 74 |
); |
| 75 |
|
| 76 |
$this->register( |
| 77 |
'receive-communications', |
| 78 |
sprintf( |
| 79 |
__( 'Receive Communications from ', 'gdpr-framework' ) . get_bloginfo('name') |
| 80 |
), |
| 81 |
_x( 'I agree to receive other communications from ' , '(Admin)', 'gdpr-framework' ) .get_bloginfo('name'). '.', |
| 82 |
true |
| 83 |
); |
| 84 |
|
| 85 |
/**** |
| 86 |
* terms-conditions default consent |
| 87 |
*/ |
| 88 |
$termsPage = $gdpr->Options->get('terms_page'); |
| 89 |
if ($termsPage) { |
| 90 |
$termsPageUrl = get_permalink($termsPage); |
| 91 |
} else { |
| 92 |
$termsPageUrl = false; |
| 93 |
} |
| 94 |
|
| 95 |
if ($termsPageUrl) { |
| 96 |
$gdpr->Consent->register( |
| 97 |
'terms-conditions', |
| 98 |
sprintf( |
| 99 |
__('I accept the %sTerms & Conditions%s', 'gdpr-framework'), |
| 100 |
"<a href='{$termsPageUrl}' target='_blank'>", |
| 101 |
"</a>" |
| 102 |
), |
| 103 |
_x('This consent is not visible by default. If someone wishes to withdraw it, they should simply request to delete all their data.', '(Admin)', 'gdpr-framework'), |
| 104 |
false |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
$this->register( |
| 109 |
'gdpr_cookie_consent', |
| 110 |
_x( 'Site Cookie Consent', '(Admin)', 'gdpr-framework' ), |
| 111 |
_x( 'This consent is not visible by default. If someone wishes to withdraw it, they should simply request to delete all their data.', '(Admin)', 'gdpr-framework' ), |
| 112 |
false |
| 113 |
); |
| 114 |
|
| 115 |
/**** |
| 116 |
* Woocommerce Policy consent |
| 117 |
*/ |
| 118 |
if ( class_exists( 'WooCommerce' ) ) { |
| 119 |
$gdpr->Consent->register( |
| 120 |
'gdpr_woo_consent', _x('Woocommerce Policy Consent', '(Admin)', 'gdpr-framework'), |
| 121 |
_x('This consent is visible by default on woocommerce checkout page. If someone wishes to withdraw it, they should simply request to delete all their data.', '(Admin)', 'gdpr-framework'), |
| 122 |
true |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get a list of all registered consent types |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public function getConsentTypes() |
| 133 |
{ |
| 134 |
return apply_filters('gdpr/consent/types', $this->getDefaultConsentTypes() + $this->getCustomConsentTypes()); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get all consent types registered by external sources, i.e. not stored in the database |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function getDefaultConsentTypes() |
| 143 |
{ |
| 144 |
return apply_filters('gdpr/consent/types/default', $this->defaultConsentTypes); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get all consent types registered by the admin, i.e. stored in the database |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function getCustomConsentTypes() |
| 153 |
{ |
| 154 |
return apply_filters('gdpr/consent/types/custom', $this->customConsentTypes); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Register a *default* consent in the list of valid consents |
| 159 |
* |
| 160 |
* @param $consent |
| 161 |
*/ |
| 162 |
public function register($slug, $title, $description, $visible = true) |
| 163 |
{ |
| 164 |
$this->defaultConsentTypes[$slug] = [ |
| 165 |
'slug' => $slug, |
| 166 |
'title' => $title, |
| 167 |
'description' => $description, |
| 168 |
'visible' => $visible, |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Register consent types saved via WP admin |
| 174 |
*/ |
| 175 |
public function registerCustomConsentTypes() |
| 176 |
{ |
| 177 |
global $gdpr; |
| 178 |
|
| 179 |
$savedConsentTypes = $gdpr->Options->get('consent_types'); |
| 180 |
|
| 181 |
if (is_array($savedConsentTypes) && count($savedConsentTypes)) |
| 182 |
{ |
| 183 |
foreach ($savedConsentTypes as $consentType) |
| 184 |
{ |
| 185 |
/* FRAM-135 check for the slug key before the reference */ |
| 186 |
if (!empty($consentType['slug'])) { |
| 187 |
$this->customConsentTypes[$consentType['slug']] = [ |
| 188 |
'slug' => isset($consentType['slug']) ? $consentType['slug'] : '', |
| 189 |
'title' => isset($consentType['title']) ? $consentType['title'] : '', |
| 190 |
'description' => isset($consentType['description']) ? $consentType['description'] : '', |
| 191 |
'visible' => isset($consentType['visible']) ? $consentType['visible'] : '', |
| 192 |
]; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Save the given consent types to database |
| 200 |
* |
| 201 |
* @param $consentTypes |
| 202 |
*/ |
| 203 |
public function saveCustomConsentTypes($consentTypes) |
| 204 |
{ |
| 205 |
global $gdpr; |
| 206 |
|
| 207 |
$gdpr->Options->set('consent_types', $consentTypes); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Check if a consent is valid so that we don't write random stuff in the database by accident |
| 212 |
* |
| 213 |
* @param $consent |
| 214 |
* @return bool |
| 215 |
*/ |
| 216 |
public function isRegisteredConsent($consent) |
| 217 |
{ |
| 218 |
return null !== $this->getConsentTypes( $consent ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Set a consent as 'given' for the data subject |
| 223 |
* |
| 224 |
* @param $email |
| 225 |
* @param $consent |
| 226 |
*/ |
| 227 |
public function giveConsent($email, $consent, $valid_until = null) |
| 228 |
{ |
| 229 |
if ($this->isRegisteredConsent($consent)) { |
| 230 |
$validation = apply_filters('gdpr/consent/give', true, $email, $consent); |
| 231 |
|
| 232 |
// If the data subject has already given this consent, do nothing |
| 233 |
if ($this->model->given($email, $consent) || !$validation) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
$this->model->give($email, $consent, $valid_until); |
| 238 |
do_action('gdpr/consent/given', $email, $consent); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Set a consent as withdrawn for the data subject |
| 244 |
* |
| 245 |
* @param $email |
| 246 |
* @param $consent |
| 247 |
*/ |
| 248 |
public function withdrawConsent($email, $consent) |
| 249 |
{ |
| 250 |
if ($this->isRegisteredConsent($consent)) { |
| 251 |
$validation = apply_filters('gdpr/consent/withdraw', true, $email, $consent); |
| 252 |
|
| 253 |
// If the consent has never been given or if data subject has already withdrawn this consent, do nothing |
| 254 |
if (!$this->model->exists($email, $consent) || $this->model->withdrawn($email, $consent) || !$validation) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
$this->model->withdraw($email, $consent); |
| 259 |
do_action('gdpr/consent/withdrawn', $email, $consent, 'withdrawn'); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Remove consent given by subject |
| 265 |
* |
| 266 |
* @param $email |
| 267 |
* @param $consent |
| 268 |
*/ |
| 269 |
public function deleteConsent($email, $consent) |
| 270 |
{ |
| 271 |
if ($this->isRegisteredConsent($consent)) { |
| 272 |
if ($this->model->given($email, $consent)) { |
| 273 |
do_action('gdpr/consent/withdrawn', $email, $consent, 'deleted'); |
| 274 |
} |
| 275 |
|
| 276 |
$this->model->delete($email, $consent); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Withdraw and anonymize a consent |
| 282 |
* |
| 283 |
* @param $email |
| 284 |
* @param $consent |
| 285 |
* @param $anonymizedId |
| 286 |
*/ |
| 287 |
public function anonymizeConsent($email, $consent, $anonymizedId) |
| 288 |
{ |
| 289 |
if ($this->isRegisteredConsent($consent)) { |
| 290 |
if ($this->model->given($email, $consent)) { |
| 291 |
do_action('gdpr/consent/withdrawn', $email, $consent, 'anonymized'); |
| 292 |
} |
| 293 |
|
| 294 |
$this->model->anonymize($email, $consent, $anonymizedId); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Get all consent given by subject |
| 300 |
* |
| 301 |
* @param $email |
| 302 |
*/ |
| 303 |
public function getAllConsents($email) |
| 304 |
{ |
| 305 |
return $this->model->getAll($email); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Get all consent given by subject with other data |
| 310 |
* |
| 311 |
* @param $email |
| 312 |
*/ |
| 313 |
public function getAllConsentswithdetails($email) |
| 314 |
{ |
| 315 |
return $this->model->getAllwithdetails($email); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get all logs deleted for user |
| 320 |
* |
| 321 |
* @param $id |
| 322 |
*/ |
| 323 |
public function gdpr_delete_log($id) |
| 324 |
{ |
| 325 |
return $this->model->deletelog($id); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Get all user logs |
| 330 |
* |
| 331 |
* @param $email |
| 332 |
*/ |
| 333 |
public function getuserlogsData($email) |
| 334 |
{ |
| 335 |
$usefromemail = get_user_by( 'email', $email ); |
| 336 |
return $this->model->getuserlogs($usefromemail->data->ID); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Get the registered consent types and add 'given' field depending |
| 341 |
* on whether or not the user has given this particular consent |
| 342 |
* |
| 343 |
* @param $dataSubjectConsents |
| 344 |
* @return array |
| 345 |
*/ |
| 346 |
public function getConsentData($dataSubjectConsents) |
| 347 |
{ |
| 348 |
$consentTypes = $this->getConsentTypes(); |
| 349 |
$consents = []; |
| 350 |
|
| 351 |
if($dataSubjectConsents){ |
| 352 |
foreach ($dataSubjectConsents as $consent => $subjectConsentType){ |
| 353 |
$subjectConsentTypeArray[$subjectConsentType->consent] = $subjectConsentType->consent; |
| 354 |
$subjectConsentUntilArray[$subjectConsentType->consent] = $subjectConsentType->valid_until; |
| 355 |
|
| 356 |
} |
| 357 |
foreach ($consentTypes as $slug => $consentType) |
| 358 |
{ |
| 359 |
if (in_array($slug, $subjectConsentTypeArray)) |
| 360 |
{ |
| 361 |
$consents[$slug] = $consentType; |
| 362 |
$consents[$slug]['valid_until'] = $subjectConsentUntilArray[$slug]; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
return $consents; |
| 367 |
} |
| 368 |
|
| 369 |
public function getbySlugConsent($dataSubjectConsents) |
| 370 |
{ |
| 371 |
if(isset($dataSubjectConsents) && !empty($dataSubjectConsents)){ |
| 372 |
$dataSubjectConsents=sanitize_key($dataSubjectConsents); |
| 373 |
$consentTypes = $this->getConsentTypes(); |
| 374 |
foreach ($consentTypes as $slug => $consentType) |
| 375 |
{ |
| 376 |
if ($slug === $dataSubjectConsents) |
| 377 |
{ |
| 378 |
return $consentType; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Return a list of all data subjects who have given a particular consent |
| 386 |
* |
| 387 |
* @param $consent |
| 388 |
*/ |
| 389 |
public function getAllDataSubjectsByConsent($consent) |
| 390 |
{ |
| 391 |
// Todo |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Return a list of all consent with other data |
| 396 |
* |
| 397 |
* @param $data and $email |
| 398 |
*/ |
| 399 |
public function getdata(array $data, $email) |
| 400 |
{ |
| 401 |
$consents = $this->getAllConsentswithdetails($email); |
| 402 |
if(!empty($consents)) |
| 403 |
{ |
| 404 |
$title = __('Consent Information', 'gdpr'); |
| 405 |
foreach ($consents as $i => $consent) |
| 406 |
{ |
| 407 |
$data[$title][$i]['consent'] = $consent->consent; |
| 408 |
$data[$title][$i]['updated_at'] = $consent->updated_at; |
| 409 |
$data[$title][$i]['ip'] = $consent->ip; |
| 410 |
} |
| 411 |
} |
| 412 |
return $data; |
| 413 |
} |
| 414 |
/** |
| 415 |
* Withdraw and delete all consents given by a data subject |
| 416 |
* |
| 417 |
* @param $email |
| 418 |
*/ |
| 419 |
public function delete($email) |
| 420 |
{ |
| 421 |
$consents = $this->getAllConsents($email); |
| 422 |
foreach ($consents as $consent) |
| 423 |
{ |
| 424 |
$this->deleteConsent($email, $consent->consent); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Withdraw and anonymize all consents given by a data subject |
| 430 |
* |
| 431 |
* @param $email |
| 432 |
* @param $anonymizedId |
| 433 |
*/ |
| 434 |
public function anonymize($email, $anonymizedId) |
| 435 |
{ |
| 436 |
$consents = $this->getAllConsents($email); |
| 437 |
foreach ($consents as $consent) |
| 438 |
{ |
| 439 |
$this->anonymizeConsent($email, $consent->consent, $anonymizedId); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|