| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Components\Consent; |
| 4 |
|
| 5 |
use Codelight\GDPR\Admin\AdminTab; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle rendering and saving the Consent tab on GDPR Options page |
| 9 |
* |
| 10 |
* Class AdminTabConsent |
| 11 |
* @package Codelight\GDPR\Components\Consent |
| 12 |
*/ |
| 13 |
class AdminTabConsent extends AdminTab |
| 14 |
{ |
| 15 |
/* @var string */ |
| 16 |
protected $slug = 'consent'; |
| 17 |
|
| 18 |
/* @var ConsentManager */ |
| 19 |
protected $consentManager; |
| 20 |
|
| 21 |
/** |
| 22 |
* AdminTabConsent constructor. |
| 23 |
* |
| 24 |
* @param ConsentManager $consentManager |
| 25 |
*/ |
| 26 |
public function __construct(ConsentManager $consentManager) |
| 27 |
{ |
| 28 |
$this->consentManager = $consentManager; |
| 29 |
|
| 30 |
$this->title = _x('Consent', '(Admin)', 'gdpr-framework'); |
| 31 |
|
| 32 |
// If we don't register the settings, WP will not allow this page to be submitted |
| 33 |
$this->registerSetting('consent_types'); |
| 34 |
$this->registerSetting('consent_info'); |
| 35 |
$this->registerSetting('gdpr_consent_until_display'); |
| 36 |
|
| 37 |
$this->renderErrors(); |
| 38 |
|
| 39 |
// Register handler for this action |
| 40 |
add_action('gdpr/admin/action/update_consent_data', [$this, 'updateConsentData']); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Initialize tab contents and register hooks |
| 45 |
*/ |
| 46 |
public function init() |
| 47 |
{ |
| 48 |
$this->registerSettingSection( |
| 49 |
'gdpr_section_consent', |
| 50 |
_x('Consent', '(Admin)', 'gdpr-framework'), |
| 51 |
[$this, 'renderConsentForm'] |
| 52 |
); |
| 53 |
$this->registerSettingSection( |
| 54 |
'gdpr_section_consent_until', |
| 55 |
_x('Additional Settings', '(Admin)', 'gdpr-framework'), |
| 56 |
[$this, 'renderConsentUntil'] |
| 57 |
); |
| 58 |
$this->registerSettingField( |
| 59 |
'gdpr_consent_until_display', |
| 60 |
_x( 'Display Consent Calendar', '(Admin)', 'gdpr-framework' ), |
| 61 |
array( $this, 'consent_until_display' ), |
| 62 |
'gdpr_section_consent_until' |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Render the contents of the registered section |
| 68 |
*/ |
| 69 |
public function renderConsentForm() |
| 70 |
{ |
| 71 |
global $gdpr; |
| 72 |
$consentInfo = $gdpr->Options->get('consent_info'); |
| 73 |
|
| 74 |
if (is_null($consentInfo)) { |
| 75 |
$consentInfo = $this->getDefaultConsentInfo(); |
| 76 |
} elseif (!$consentInfo) { |
| 77 |
$consentInfo = ''; |
| 78 |
} |
| 79 |
|
| 80 |
$nonce = wp_create_nonce("gdpr/admin/action/update_consent_data"); |
| 81 |
$defaultConsentTypes = $this->consentManager->getDefaultConsentTypes(); |
| 82 |
$customConsentTypes = $this->consentManager->getCustomConsentTypes(); |
| 83 |
|
| 84 |
if (defined('ICL_LANGUAGE_CODE')) { |
| 85 |
$prefix = ICL_LANGUAGE_CODE . '_'; |
| 86 |
} else { |
| 87 |
$prefix = ''; |
| 88 |
} |
| 89 |
|
| 90 |
echo gdpr('view')->render('admin/consent', compact('nonce', 'customConsentTypes', 'defaultConsentTypes', 'consentInfo', 'prefix')); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Save the submitted consent types |
| 95 |
*/ |
| 96 |
public function updateConsentData() |
| 97 |
{ |
| 98 |
global $gdpr; |
| 99 |
// Update additional information |
| 100 |
if (isset($_POST['gdpr_consent_info'])) { |
| 101 |
$gdpr->Options->set('consent_info', wp_unslash($_POST['gdpr_consent_info'])); |
| 102 |
} |
| 103 |
|
| 104 |
// Update consent types |
| 105 |
if (isset($_POST['gdpr_consent_types']) && is_array($_POST['gdpr_consent_types'])) { |
| 106 |
$consentTypes = $_POST['gdpr_consent_types']; |
| 107 |
} else { |
| 108 |
$consentTypes = []; |
| 109 |
} |
| 110 |
|
| 111 |
// Strip slashes which WP adds automatically |
| 112 |
if (count($consentTypes)) { |
| 113 |
foreach ($consentTypes as &$type) { |
| 114 |
foreach ($type as $key => $item) { |
| 115 |
if (is_array($item)) { |
| 116 |
$type[$key] = array_map('wp_unslash', $item); |
| 117 |
} else { |
| 118 |
$type[$key] = wp_unslash($item); |
| 119 |
} |
| 120 |
|
| 121 |
if ('visible' === $key) { |
| 122 |
$type[$key] = 1; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$errors = []; |
| 129 |
|
| 130 |
if (!empty($consentTypes)) { |
| 131 |
//$errors = $this->validate($consentTypes); |
| 132 |
} |
| 133 |
|
| 134 |
if (!count($errors)) { |
| 135 |
$this->consentManager->saveCustomConsentTypes($consentTypes); |
| 136 |
} else { |
| 137 |
$errorQuery = http_build_query($errors); |
| 138 |
wp_safe_redirect(gdpr('helpers')->getAdminUrl('&gdpr-tab=consent&') . $errorQuery); |
| 139 |
exit; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
protected function validate($consentTypes) |
| 144 |
{ |
| 145 |
$errors = []; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
foreach ($consentTypes as $consentType) { |
| 150 |
if (empty($consentType['slug'])) { |
| 151 |
$errors['errors[]'] = 'slug-empty'; |
| 152 |
} |
| 153 |
|
| 154 |
if (!preg_match('/^[A-Za-z0-9_-]+$/', $consentType['slug'])) { |
| 155 |
$errors['errors[]'] = 'slug-invalid'; |
| 156 |
} |
| 157 |
|
| 158 |
if (empty($consentType['title'])) { |
| 159 |
$errors['errors[]'] = 'title-empty'; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $errors; |
| 164 |
} |
| 165 |
|
| 166 |
public function renderErrors() |
| 167 |
{ |
| 168 |
if (isset($_GET['errors']) && count($_GET['errors'])) { |
| 169 |
|
| 170 |
foreach ($_GET['errors'] as $error) { |
| 171 |
if ('slug-empty' === $error) { |
| 172 |
$message = _x("Consent slug is a required field!", '(Admin)', 'gdpr-framework'); |
| 173 |
gdpr('admin-error')->add('admin/notices/error', compact('message')); |
| 174 |
} |
| 175 |
|
| 176 |
if ('slug-invalid' === $error) { |
| 177 |
$message = _x("You may only use alphanumeric characters, dash and underscore in the consent slug field.", '(Admin)', 'gdpr-framework'); |
| 178 |
gdpr('admin-error')->add('admin/notices/error', compact('message')); |
| 179 |
} |
| 180 |
|
| 181 |
if ('title-empty' === $error) { |
| 182 |
$message = _x("Consent title is a required field!", '(Admin)', 'gdpr-framework'); |
| 183 |
gdpr('admin-error')->add('admin/notices/error', compact('message')); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function getDefaultConsentInfo() |
| 193 |
{ |
| 194 |
// this is merely a one time initializtion, this string can be changed in the Admin Consent tab and is saved into the database with |
| 195 |
// the key of gdpr_consent_info |
| 196 |
return __('To use this website, you accepted our Privacy Policy. If you wish to withdraw your acceptance, please use the "Delete my data" button below.', 'gdpr-framework'); |
| 197 |
} |
| 198 |
public function renderConsentUntil() { |
| 199 |
echo '<p>' . __('Enable this feature to allow users to submit a time limit on how many months their consent is given for their coments and registration.', 'gdpr-framework') . '</p>'; |
| 200 |
} |
| 201 |
|
| 202 |
public function consent_until_display(){ |
| 203 |
if ( get_option( 'gdpr_consent_until_display' ) === '1' ) { |
| 204 |
$checked = get_option( 'gdpr_consent_until_display' ); |
| 205 |
}else{ |
| 206 |
$checked = 0; |
| 207 |
} |
| 208 |
echo gdpr( 'view' )->render( 'admin/consent/enable-consent-until' , compact( 'checked' ) ); |
| 209 |
} |
| 210 |
} |
| 211 |
|