| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Components\WordpressUser\Controllers; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
use Codelight\GDPR\DataSubject\DataExporter; |
| 8 |
use Codelight\GDPR\DataSubject\DataSubject; |
| 9 |
use Codelight\GDPR\DataSubject\DataSubjectAuthenticator; |
| 10 |
|
| 11 |
/** |
| 12 |
* Handles Users > Privacy Tools page |
| 13 |
* |
| 14 |
* Class DashboardDataPageController |
| 15 |
* |
| 16 |
* @package Codelight\GDPR\Modules\WordpressUser\Controllers |
| 17 |
*/ |
| 18 |
class DashboardDataPageController |
| 19 |
{ |
| 20 |
protected $dataExporter; |
| 21 |
protected $dataSubjectAuthenticator; |
| 22 |
|
| 23 |
/** |
| 24 |
* DashboardDataPageController constructor. |
| 25 |
* |
| 26 |
* @param DataExporter $dataExporter |
| 27 |
*/ |
| 28 |
public function __construct(DataExporter $dataExporter, DataSubjectAuthenticator $dataSubjectAuthenticator) |
| 29 |
{ |
| 30 |
$this->dataExporter = $dataExporter; |
| 31 |
$this->dataSubjectAuthenticator = $dataSubjectAuthenticator; |
| 32 |
|
| 33 |
add_action('gdpr/dashboard/privacy-tools/content', [$this, 'renderHeader'], 10); |
| 34 |
add_action('gdpr/dashboard/privacy-tools/content', [$this, 'renderConsentForm'], 20); |
| 35 |
add_action('gdpr/dashboard/privacy-tools/content', [$this, 'renderExportForm'], 30); |
| 36 |
add_action('gdpr/dashboard/privacy-tools/content', [$this, 'renderDeleteForm'], 40); |
| 37 |
|
| 38 |
add_action('gdpr/dashboard/privacy-tools/action/withdraw_consent', [$this, 'withdrawConsent']); |
| 39 |
add_action('gdpr/dashboard/privacy-tools/action/export', [$this, 'export']); |
| 40 |
add_action('gdpr/dashboard/privacy-tools/action/forget', [$this, 'forget']); |
| 41 |
|
| 42 |
add_action('admin_notices', [$this, 'renderAdminNotices']); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Render success notices via admin_notice action |
| 47 |
*/ |
| 48 |
public function renderAdminNotices() |
| 49 |
{ |
| 50 |
if ('profile_page_gdpr-profile' !== get_current_screen()->base) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if (!isset($_REQUEST['gdpr_notice'])) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ('request_sent' === sanitize_key($_REQUEST['gdpr_notice'])) { |
| 59 |
$message = __('We have received your request and will reply within 30 days.', 'gdpr-framework'); |
| 60 |
$class = 'notice notice-success'; |
| 61 |
} |
| 62 |
|
| 63 |
if ('consent_withdrawn' === sanitize_key($_REQUEST['gdpr_notice'])) { |
| 64 |
$message = __('Consent withdrawn.', 'gdpr-framework'); |
| 65 |
$class = 'notice notice-success'; |
| 66 |
} |
| 67 |
|
| 68 |
echo gdpr('view')->render('admin/notice', compact('message', 'class')); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Render page header |
| 73 |
*/ |
| 74 |
public function renderHeader() |
| 75 |
{ |
| 76 |
echo gdpr('view')->render( |
| 77 |
"modules/wordpress-user/dashboard/data-page/header" |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Render the consent form |
| 83 |
* |
| 84 |
* @param DataSubject $dataSubject |
| 85 |
*/ |
| 86 |
public function renderConsentForm(DataSubject $dataSubject) |
| 87 |
{ |
| 88 |
global $gdpr; |
| 89 |
$consentData = $dataSubject->getVisibleConsentData(); |
| 90 |
|
| 91 |
foreach ($consentData as &$item) { |
| 92 |
$item['withdraw_url'] = add_query_arg([ |
| 93 |
'gdpr_action' => 'withdraw_consent', |
| 94 |
'gdpr_nonce' => wp_create_nonce("gdpr/dashboard/privacy-tools/action/withdraw_consent"), |
| 95 |
'email' => $dataSubject->getEmail(), |
| 96 |
'consent' => $item['slug'], |
| 97 |
]); |
| 98 |
} |
| 99 |
|
| 100 |
$consentInfo = wpautop($gdpr->Options->get('consent_info')); |
| 101 |
|
| 102 |
echo gdpr('view')->render( |
| 103 |
"modules/wordpress-user/dashboard/data-page/form-consent", |
| 104 |
compact('consentData', 'consentInfo') |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Render the buttons that allow exporting data |
| 110 |
*/ |
| 111 |
public function renderExportForm() |
| 112 |
{ |
| 113 |
$exportHTMLUrl = add_query_arg([ |
| 114 |
'gdpr_action' => 'export', |
| 115 |
'gdpr_format' => 'html', |
| 116 |
'gdpr_nonce' => wp_create_nonce("gdpr/dashboard/privacy-tools/action/export"), |
| 117 |
]); |
| 118 |
|
| 119 |
$exportJSONUrl = add_query_arg([ |
| 120 |
'gdpr_action' => 'export', |
| 121 |
'gdpr_format' => 'json', |
| 122 |
'gdpr_nonce' => wp_create_nonce("gdpr/dashboard/privacy-tools/action/export"), |
| 123 |
]); |
| 124 |
|
| 125 |
echo gdpr('view')->render( |
| 126 |
"modules/wordpress-user/dashboard/form-export", |
| 127 |
compact('exportHTMLUrl', 'exportJSONUrl') |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render the delete data button |
| 133 |
*/ |
| 134 |
public function renderDeleteForm() |
| 135 |
{ |
| 136 |
$showDelete = !current_user_can('manage_options'); |
| 137 |
$url = add_query_arg([ |
| 138 |
'gdpr_action' => 'forget', |
| 139 |
'gdpr_nonce' => wp_create_nonce("gdpr/dashboard/privacy-tools/action/forget"), |
| 140 |
]); |
| 141 |
|
| 142 |
echo gdpr('view')->render( |
| 143 |
"modules/wordpress-user/dashboard/data-page/form-delete", |
| 144 |
compact('url', 'showDelete') |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param DataSubject $dataSubject |
| 150 |
*/ |
| 151 |
public function withdrawConsent(DataSubject $dataSubject) |
| 152 |
{ |
| 153 |
$consent = sanitize_key($_REQUEST['consent']); |
| 154 |
$dataSubject->withdrawConsent($consent); |
| 155 |
$this->redirect(['gdpr_notice' => 'consent_withdrawn']); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param DataSubject $dataSubject |
| 160 |
*/ |
| 161 |
public function export(DataSubject $dataSubject) |
| 162 |
{ |
| 163 |
$gdpr_format = sanitize_key($_REQUEST['gdpr_format']); |
| 164 |
$data = $dataSubject->export($gdpr_format); |
| 165 |
|
| 166 |
if (!is_null($data)) { |
| 167 |
// If there is data, download it |
| 168 |
$this->dataExporter->export($data, $dataSubject, $gdpr_format); |
| 169 |
} else { |
| 170 |
// If there's no data, then show notification that your request has been sent. |
| 171 |
$this->redirect(['gdpr_notice' => 'request_sent']); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param DataSubject $dataSubject |
| 177 |
*/ |
| 178 |
public function forget(DataSubject $dataSubject) |
| 179 |
{ |
| 180 |
$status = $dataSubject->forget(); |
| 181 |
|
| 182 |
if (!$status) { |
| 183 |
$this->redirect(['gdpr_notice' => 'request_sent']); |
| 184 |
} else { |
| 185 |
$this->dataSubjectAuthenticator->deleteSession(); |
| 186 |
$this->redirect([], '/'); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Redirect the visitor to an appropriate location |
| 192 |
* |
| 193 |
* @param array $args |
| 194 |
* @param null $baseUrl |
| 195 |
*/ |
| 196 |
protected function redirect($args = [], $baseUrl = null) |
| 197 |
{ |
| 198 |
if (!$baseUrl) { |
| 199 |
$baseUrl = gdpr('helpers')->getDashboardDataPageUrl(); |
| 200 |
} |
| 201 |
|
| 202 |
wp_safe_redirect(add_query_arg($args, $baseUrl)); |
| 203 |
exit; |
| 204 |
} |
| 205 |
} |
| 206 |
|