| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Components\PrivacyToolsPage; |
| 4 |
|
| 5 |
use Codelight\GDPR\DataSubject\DataSubject; |
| 6 |
use Codelight\GDPR\DataSubject\DataSubjectAuthenticator; |
| 7 |
use Codelight\GDPR\DataSubject\DataSubjectIdentificator; |
| 8 |
use Codelight\GDPR\DataSubject\DataSubjectManager; |
| 9 |
use Codelight\GDPR\DataSubject\DataExporter; |
| 10 |
use Codelight\GDPR\Components\Consent\UserConsentModel; |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle the data page on front-end |
| 14 |
* |
| 15 |
* Class DataPageController |
| 16 |
* |
| 17 |
* @package Codelight\GDPR\Components\DataPage |
| 18 |
*/ |
| 19 |
class PrivacyToolsPageController { |
| 20 |
|
| 21 |
/* @var DataSubjectAuthenticator */ |
| 22 |
protected $dataSubjectAuthenticator; |
| 23 |
|
| 24 |
/* @var DataSubjectIdentificator */ |
| 25 |
protected $dataSubjectIdentificator; |
| 26 |
|
| 27 |
/* @var DataSubjectManager */ |
| 28 |
protected $dataSubjectManager; |
| 29 |
|
| 30 |
protected $UserConsentModel; |
| 31 |
|
| 32 |
protected $dataExporter; |
| 33 |
|
| 34 |
/** |
| 35 |
* DataPageController constructor. |
| 36 |
* |
| 37 |
* @param DataSubjectIdentificator $dataSubjectIdentificator |
| 38 |
* @param DataSubjectManager $dataSubjectManager |
| 39 |
*/ |
| 40 |
public function __construct( |
| 41 |
DataSubjectAuthenticator $dataSubjectAuthenticator, |
| 42 |
DataSubjectIdentificator $dataSubjectIdentificator, |
| 43 |
DataSubjectManager $dataSubjectManager, |
| 44 |
DataExporter $dataExporter, |
| 45 |
UserConsentModel $UserConsentModel |
| 46 |
) { |
| 47 |
$this->dataSubjectAuthenticator = $dataSubjectAuthenticator; |
| 48 |
$this->dataSubjectIdentificator = $dataSubjectIdentificator; |
| 49 |
$this->dataSubjectManager = $dataSubjectManager; |
| 50 |
$this->dataExporter = $dataExporter; |
| 51 |
|
| 52 |
$this->UserConsentModel = $UserConsentModel; |
| 53 |
|
| 54 |
if ( ! gdpr( 'options' )->get( 'enable' ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$this->setup(); |
| 59 |
} |
| 60 |
|
| 61 |
protected function setup() { |
| 62 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 63 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_donotsell' ) ); |
| 64 |
|
| 65 |
// Listen to 'identify' action and send an email |
| 66 |
add_action( 'gdpr/frontend/action/identify', array( $this, 'sendIdentificationEmail' ) ); |
| 67 |
|
| 68 |
add_action( 'gdpr/frontend/privacy-tools-page/content', array( $this, 'renderConsentForm' ), 10, 2 ); |
| 69 |
add_action( 'gdpr/frontend/privacy-tools-page/content', array( $this, 'renderExportForm' ), 20, 2 ); |
| 70 |
add_action( 'gdpr/frontend/privacy-tools-page/content', array( $this, 'renderDeleteForm' ), 30, 2 ); |
| 71 |
|
| 72 |
add_action( 'gdpr/frontend/privacy-tools-page/action/withdraw_consent', array( $this, 'withdrawConsent' ), 10, 2 ); |
| 73 |
add_action( 'gdpr/frontend/privacy-tools-page/action/export', array( $this, 'export' ), 10, 2 ); |
| 74 |
add_action( 'gdpr/frontend/privacy-tools-page/action/forget', array( $this, 'forget' ), 10, 2 ); |
| 75 |
add_action( 'wp_ajax_donot_sell_save_post', array( $this, 'donot_sell_save_post' ) ); |
| 76 |
add_action( 'wp_ajax_nopriv_donot_sell_save_post', array( $this, 'donot_sell_save_post' ) ); |
| 77 |
add_action( 'wp_ajax_nopriv_validation_privacysafe', array( $this, 'validation_privacysafe' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
public function enqueue_donotsell() { |
| 81 |
global $gdpr; |
| 82 |
wp_enqueue_script( |
| 83 |
'donot-sell-form', |
| 84 |
$gdpr->PluginUrl . 'assets/js/gdpr-donotsell.js', |
| 85 |
array( 'jquery' ), |
| 86 |
GDPR_FRAMEWORK_VERSION, |
| 87 |
true |
| 88 |
); |
| 89 |
wp_localize_script( |
| 90 |
'donot-sell-form', |
| 91 |
'localized_donot_sell_form', |
| 92 |
array( |
| 93 |
'admin_donot_sell_ajax_url' => admin_url( 'admin-ajax.php' ), |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
public function enqueue() { |
| 99 |
global $gdpr; |
| 100 |
if ( ! gdpr( 'options' )->get( 'enable_stylesheet' ) || ! is_page( gdpr( 'options' )->get( 'tools_page' ) ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
wp_enqueue_style( |
| 105 |
'gdpr-framework-privacy-tools', |
| 106 |
$gdpr->PluginUrl . 'assets/privacy-tools.css' |
| 107 |
); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public function validation_privacysafe() { |
| 112 |
return true; |
| 113 |
exit; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* If the given email address exists as a data subject, send an authentication email to that address |
| 118 |
*/ |
| 119 |
public function sendIdentificationEmail() { |
| 120 |
// Additional safety check |
| 121 |
if ( ! is_email( $_REQUEST['email'] ) ) { |
| 122 |
$this->redirect( array( 'gdpr_notice' => 'invalid_email' ) ); |
| 123 |
} else { |
| 124 |
$requested_email = sanitize_email( $_REQUEST['email'] ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( $this->dataSubjectIdentificator->isDataSubject( $requested_email ) ) { |
| 128 |
$this->dataSubjectIdentificator->sendIdentificationEmail( $requested_email ); |
| 129 |
} else { |
| 130 |
$user = get_user_by( 'email', $requested_email ); |
| 131 |
if (empty($user)) { |
| 132 |
$this->redirect( array( 'gdpr_notice' => 'unregistered_user' ) ); |
| 133 |
} else { |
| 134 |
$this->dataSubjectIdentificator->sendNoDataFoundEmail( $requested_email ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$this->redirect( array( 'gdpr_notice' => 'email_sent' ) ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Render the page contents. |
| 143 |
* This is only called via the shortcode. |
| 144 |
*/ |
| 145 |
public function render() { |
| 146 |
$dataSubject = $this->dataSubjectAuthenticator->authenticate(); |
| 147 |
$this->renderNotices(); |
| 148 |
|
| 149 |
if ( $dataSubject ) { |
| 150 |
$this->renderPrivacyTools( $dataSubject ); |
| 151 |
} else { |
| 152 |
$this->renderIdentificationForm(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Display notices to the user. |
| 158 |
* The contents of the notices are currently hardcoded inside the template. |
| 159 |
*/ |
| 160 |
protected function renderNotices() { |
| 161 |
if ( ! isset( $_REQUEST['gdpr_notice'] ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
echo gdpr( 'view' )->render( 'privacy-tools/notices' ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Render the contents of the identification form |
| 170 |
*/ |
| 171 |
protected function renderIdentificationForm() { |
| 172 |
$nonce = wp_create_nonce( 'gdpr/frontend/action/identify' ); |
| 173 |
// FRAM-144 Fix reference of an undefined variable 'notices' |
| 174 |
if (!isset($notices)) { |
| 175 |
$notices = "NOTICES PLACEHOLDER"; |
| 176 |
} |
| 177 |
echo gdpr( 'view' )->render( 'privacy-tools/form-identify', compact( 'nonce', 'notices' ) ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Render the contents of the Privacy Tools page |
| 182 |
* |
| 183 |
* @param DataSubject $dataSubject |
| 184 |
*/ |
| 185 |
protected function renderPrivacyTools( DataSubject $dataSubject ) { |
| 186 |
$email = $dataSubject->getEmail(); |
| 187 |
echo gdpr( 'view' )->render( 'privacy-tools/privacy-tools', compact( 'dataSubject', 'email' ) ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Render the form that allows withdrawing consent |
| 192 |
* |
| 193 |
* @param DataSubject $dataSubject |
| 194 |
*/ |
| 195 |
public function renderConsentForm( DataSubject $dataSubject ) { |
| 196 |
$consentData = $dataSubject->getVisibleConsentData(); |
| 197 |
if ( $consentData ) { |
| 198 |
foreach ( $consentData as &$item ) { |
| 199 |
$item['withdraw_url'] = add_query_arg( |
| 200 |
array( |
| 201 |
'gdpr_action' => 'withdraw_consent', |
| 202 |
'gdpr_nonce' => wp_create_nonce( 'gdpr/frontend/privacy-tools-page/action/withdraw_consent' ), |
| 203 |
'email' => $dataSubject->getEmail(), |
| 204 |
'consent' => $item['slug'], |
| 205 |
) |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$consentInfo = wpautop( gdpr( 'options' )->get( 'consent_info' ) ); |
| 211 |
|
| 212 |
echo gdpr( 'view' )->render( |
| 213 |
'privacy-tools/form-consent', |
| 214 |
compact( 'consentData', 'consentInfo' ) |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Render the form that allows the data subject to export their data |
| 220 |
* |
| 221 |
* @param DataSubject $dataSubject |
| 222 |
*/ |
| 223 |
public function renderExportForm( DataSubject $dataSubject ) { |
| 224 |
$email = $dataSubject->getEmail(); |
| 225 |
$nonce = wp_create_nonce( 'gdpr/frontend/privacy-tools-page/action/export' ); |
| 226 |
|
| 227 |
echo gdpr( 'view' )->render( |
| 228 |
'privacy-tools/form-export', |
| 229 |
compact( 'email', 'nonce' ) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Render the form that allows the data subject to delete their data |
| 235 |
* |
| 236 |
* @param DataSubject $dataSubject |
| 237 |
*/ |
| 238 |
public function renderDeleteForm( DataSubject $dataSubject ) { |
| 239 |
// Let's not allow admins to delete themselves |
| 240 |
if ( current_user_can( 'manage_options' ) ) { |
| 241 |
echo gdpr( 'view' )->render( 'privacy-tools/notice-admin-role' ); |
| 242 |
return; |
| 243 |
} |
| 244 |
$email = $dataSubject->getEmail(); |
| 245 |
$gdpr_user = get_user_by( 'email', $email ); |
| 246 |
if ( isset( $gdpr_user->data->ID ) ) { |
| 247 |
if ( user_can( $gdpr_user->data->ID, 'manage_options' ) ) { |
| 248 |
echo gdpr( 'view' )->render( 'privacy-tools/notice-admin-role' ); |
| 249 |
return; |
| 250 |
} |
| 251 |
} |
| 252 |
$action = 'forget'; |
| 253 |
$nonce = wp_create_nonce( 'gdpr/frontend/privacy-tools-page/action/forget' ); |
| 254 |
$user = wp_get_current_user(); |
| 255 |
echo gdpr( 'view' )->render( |
| 256 |
'privacy-tools/form-delete', |
| 257 |
compact( 'action', 'email', 'nonce' ) |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Withdraw the consent |
| 263 |
* |
| 264 |
* @param DataSubject $dataSubject |
| 265 |
*/ |
| 266 |
public function withdrawConsent( DataSubject $dataSubject ) { |
| 267 |
$consent = sanitize_key( $_REQUEST['consent'] ); |
| 268 |
$dataSubject->withdrawConsent( $consent ); |
| 269 |
$this->redirect( array( 'gdpr_notice' => 'consent_withdrawn' ) ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Trigger the export action. |
| 274 |
* |
| 275 |
* @param DataSubject $dataSubject |
| 276 |
*/ |
| 277 |
public function export( DataSubject $dataSubject ) { |
| 278 |
$format = sanitize_key( $_REQUEST['gdpr_format'] ); |
| 279 |
$data = $dataSubject->export( $format ); |
| 280 |
|
| 281 |
if ( ! is_null( $data ) ) { |
| 282 |
// If there is data, download it |
| 283 |
$this->dataExporter->export( $data, $dataSubject, $format ); |
| 284 |
} else { |
| 285 |
// If there's no data, then show notification that your request has been sent. |
| 286 |
$this->redirect( array( 'gdpr_notice' => 'request_sent' ) ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Trigger the forget action. |
| 292 |
* |
| 293 |
* @param DataSubject $dataSubject |
| 294 |
*/ |
| 295 |
public function forget( DataSubject $dataSubject ) { |
| 296 |
$deleted = $dataSubject->forget(); |
| 297 |
|
| 298 |
if ( $deleted ) { |
| 299 |
$this->dataSubjectAuthenticator->deleteSession(); |
| 300 |
$this->redirect( array( 'gdpr_notice' => 'data_deleted' ) ); |
| 301 |
} else { |
| 302 |
// If request was sent to admin, then show notification |
| 303 |
$this->redirect( array( 'gdpr_notice' => 'request_sent' ) ); |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Redirect the visitor to an appropriate location |
| 310 |
* |
| 311 |
* @param array $args |
| 312 |
* @param null $baseUrl |
| 313 |
*/ |
| 314 |
protected function redirect( $args = array(), $baseUrl = null ) { |
| 315 |
if ( ! $baseUrl ) { |
| 316 |
// If custom tools page URL is set |
| 317 |
if ( gdpr( 'options' )->get( 'custom_tools_page' ) ) { |
| 318 |
$privacyToolsUrl = gdpr( 'options' )->get( 'custom_tools_page' ); |
| 319 |
$baseUrl = apply_filters( 'redirect_after_gdpr_submit', $privacyToolsUrl ); |
| 320 |
} else { |
| 321 |
$privacyToolsUrl = gdpr( 'options' )->get( 'tools_page' ); |
| 322 |
$baseUrl = $privacyToolsUrl ? get_permalink( $privacyToolsUrl ) : home_url(); |
| 323 |
$baseUrl = apply_filters( 'redirect_after_gdpr_submit', $baseUrl ); |
| 324 |
} |
| 325 |
// Avoid infinite loop redirect |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
wp_safe_redirect( add_query_arg( $args, $baseUrl ) ); |
| 330 |
exit; |
| 331 |
} |
| 332 |
|
| 333 |
public function gdpr_get_formatted_billing_name_and_address( $user_id ) { |
| 334 |
$address = get_user_meta( $user_id, 'billing_address_1', true ) . ' '; |
| 335 |
$address .= get_user_meta( $user_id, 'billing_address_2', true ) . ' '; |
| 336 |
$address .= get_user_meta( $user_id, 'billing_city', true ) . ' '; |
| 337 |
$address .= get_user_meta( $user_id, 'billing_state', true ) . ' '; |
| 338 |
$address .= get_user_meta( $user_id, 'billing_postcode', true ) . ' '; |
| 339 |
$address .= get_user_meta( $user_id, 'billing_country', true ) . ' '; |
| 340 |
return $address; |
| 341 |
} |
| 342 |
public function donot_sell_save_post() { |
| 343 |
if ( ! empty( $_POST['form_data'] ) ) { |
| 344 |
$authorname = ''; |
| 345 |
parse_str( $_POST['form_data'], $form_data ); |
| 346 |
if ( is_user_logged_in() ) { |
| 347 |
// your code for logged in user |
| 348 |
$current_user = wp_get_current_user(); |
| 349 |
$authorname = esc_html( $current_user->user_login ); |
| 350 |
} |
| 351 |
$postarr = array( |
| 352 |
'ID' => '', // If ID stays empty the post will be created. |
| 353 |
'post_author' => $authorname, |
| 354 |
'post_title' => sanitize_email( $form_data['donotsell_email'] ), |
| 355 |
'post_status' => 'publish', |
| 356 |
'post_type' => 'donotsellrequests', |
| 357 |
); |
| 358 |
$new_post = wp_insert_post( |
| 359 |
$postarr, |
| 360 |
true |
| 361 |
); |
| 362 |
$post_id = intval( $new_post ); |
| 363 |
if ( $post_id ) { |
| 364 |
add_post_meta( $post_id, 'donotsell_first_name', sanitize_text_field( $form_data['donotsell_first_name'] ) ); |
| 365 |
add_post_meta( $post_id, 'donotsell_last_name', sanitize_text_field( $form_data['donotsell_last_name'] ) ); |
| 366 |
add_post_meta( $post_id, 'donotsell_consent', sanitize_text_field( $form_data['donotsell_consent'] ) ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( ! empty( $form_data['donotsell_consent'] ) && $form_data['donotsell_consent'] ) { |
| 370 |
$email = sanitize_email( $form_data['donotsell_email'] ); |
| 371 |
$consent = 'do-not-sell-info'; |
| 372 |
$output = $this->UserConsentModel->give( $email, $consent, $valid_until = null ); |
| 373 |
$this->UserConsentModel->give( $email, 'receive-communications', $valid_until = null ); |
| 374 |
} |
| 375 |
// Post was not created/updated, so let's output the error message. |
| 376 |
if ( is_wp_error( $new_post ) ) { |
| 377 |
$r['error'] = $new_post->get_error_message(); |
| 378 |
|
| 379 |
echo json_encode( $r ); |
| 380 |
|
| 381 |
exit; |
| 382 |
} |
| 383 |
|
| 384 |
// Gets post info in array format as it's easier to debug via console if needed. |
| 385 |
$post_array = get_post( $post_id, ARRAY_A ); |
| 386 |
|
| 387 |
if ( $post_array ) { |
| 388 |
$r['donotsellrequests'] = $post_array; |
| 389 |
} |
| 390 |
|
| 391 |
echo json_encode( $r ); |
| 392 |
} |
| 393 |
exit; |
| 394 |
} |
| 395 |
} |
| 396 |
|