| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\DataSubject; |
| 4 |
|
| 5 |
use Codelight\GDPR\Components\Consent\ConsentManager; |
| 6 |
|
| 7 |
/** |
| 8 |
* A data subject is any person whose data we are storing. |
| 9 |
* A data subject might or might not have a user account on this site. |
| 10 |
* |
| 11 |
* Class DataSubject |
| 12 |
* |
| 13 |
* @package Codelight\GDPR\DataSubject |
| 14 |
*/ |
| 15 |
class DataSubject { |
| 16 |
|
| 17 |
/* @var string */ |
| 18 |
protected $email; |
| 19 |
|
| 20 |
/* @var \WP_User|null */ |
| 21 |
protected $user = null; |
| 22 |
|
| 23 |
/* @var ConsentManager */ |
| 24 |
protected $consentManager; |
| 25 |
|
| 26 |
/* @var array */ |
| 27 |
protected $consents = array(); |
| 28 |
|
| 29 |
protected $dataRepository; |
| 30 |
|
| 31 |
/** |
| 32 |
* DataSubject constructor. |
| 33 |
* |
| 34 |
* @param $email |
| 35 |
* @param \WP_User|null $user |
| 36 |
*/ |
| 37 |
public function __construct( $email, \WP_User $user = null, ConsentManager $consentManager ) { |
| 38 |
$this->email = $email; |
| 39 |
$this->user = $user; |
| 40 |
$this->consentManager = $consentManager; |
| 41 |
$this->dataRepository = new DataRepository( $email ); |
| 42 |
|
| 43 |
$this->consents = $this->consentManager->getAllConsents( $this->email ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getEmail() { |
| 50 |
return $this->email; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
public function hasUser() { |
| 57 |
return $this->getUser() ? true : false; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @return null|\WP_User |
| 62 |
*/ |
| 63 |
public function getUser() { |
| 64 |
return $this->user; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @return int|null |
| 69 |
*/ |
| 70 |
public function getUserId() { |
| 71 |
if ( $this->hasUser() ) { |
| 72 |
return $this->getUser()->ID; |
| 73 |
} |
| 74 |
|
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @delete logs |
| 80 |
*/ |
| 81 |
public function gdpr_delete_log( $id ) { |
| 82 |
return $this->consentManager->gdpr_delete_log( $id ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if the data subject has consented to something specific |
| 87 |
*/ |
| 88 |
public function hasConsented( $consent ) { |
| 89 |
return in_array( $consent, $this->consents ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get all consent given by the data subject |
| 94 |
*/ |
| 95 |
public function getConsents() { |
| 96 |
return $this->consents; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get a list of all consents intersected with the data subjects consents |
| 101 |
*/ |
| 102 |
public function getConsentData() { |
| 103 |
return $this->consentManager->getConsentData( $this->consents ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get a list of user logs |
| 108 |
*/ |
| 109 |
public function getuserlogsData() { |
| 110 |
return $this->consentManager->getuserlogsData( $this->email ); |
| 111 |
} |
| 112 |
/** |
| 113 |
* Get a list of all visible consents |
| 114 |
*/ |
| 115 |
public function getVisibleConsentData() { |
| 116 |
if ( $this->getConsentData() ) { |
| 117 |
|
| 118 |
return array_filter( |
| 119 |
$this->getConsentData(), |
| 120 |
function ( $item ) { |
| 121 |
return $item['visible']; |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
} |
| 126 |
return array(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Save given consent to data subject |
| 131 |
* |
| 132 |
* @param $consent |
| 133 |
*/ |
| 134 |
public function giveConsent( $consent, $valid_until = null ) { |
| 135 |
$this->consentManager->giveConsent( $this->email, $consent, $valid_until ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Remove given consent from data subject |
| 140 |
* |
| 141 |
* @param $consent |
| 142 |
*/ |
| 143 |
public function withdrawConsent( $consent ) { |
| 144 |
$this->consentManager->withdrawConsent( $this->email, $consent ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check if there's any data stored about this data subject at all |
| 149 |
*/ |
| 150 |
public function hasData() { |
| 151 |
return ! empty( $this->getData() ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Just get the data subjects data, without sending emails or anything. |
| 156 |
* Applies 'gdpr/data-subject/data' filter. |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function getData() { |
| 161 |
return $this->dataRepository->getData( $this->getEmail() ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Export the data subject's data as per admin configuration. |
| 166 |
* Applies 'gdpr/data-subject/data' filter. |
| 167 |
* |
| 168 |
* @return array|null |
| 169 |
*/ |
| 170 |
public function export( $format, $force = false ) { |
| 171 |
return $this->dataRepository->export( $this->getEmail(), $format, $force ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Forget the data subject as per admin configuration. |
| 176 |
* Triggers 'gdpr/data-subject/anonymize' or 'gdpr/data-subject/delete' action. |
| 177 |
* |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
public function forget( $forceAction = null ) { |
| 181 |
return $this->dataRepository->forget( $this->getEmail(), $forceAction ); |
| 182 |
} |
| 183 |
} |
| 184 |
|