| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Installer; |
| 4 |
|
| 5 |
use Codelight\GDPR\Admin\AdminTabGeneral; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle all installation activities |
| 9 |
* |
| 10 |
* Class Installer |
| 11 |
* |
| 12 |
* @package Codelight\GDPR\Installer |
| 13 |
*/ |
| 14 |
class Installer |
| 15 |
{ |
| 16 |
protected $adminTab; |
| 17 |
|
| 18 |
/* @var array */ |
| 19 |
protected $defaultSteps = [ |
| 20 |
'Codelight\GDPR\Installer\Steps\Welcome', |
| 21 |
'Codelight\GDPR\Installer\Steps\Disclaimer', |
| 22 |
'Codelight\GDPR\Installer\Steps\ConfigurationPages', |
| 23 |
'Codelight\GDPR\Installer\Steps\ConfigurationSettings', |
| 24 |
'Codelight\GDPR\Installer\Steps\PolicySettings', |
| 25 |
'Codelight\GDPR\Installer\Steps\PolicyContents', |
| 26 |
'Codelight\GDPR\Installer\Steps\Consent', |
| 27 |
'Codelight\GDPR\Installer\Steps\Integrations', |
| 28 |
'Codelight\GDPR\Installer\Steps\PrivacySafe', |
| 29 |
'Codelight\GDPR\Installer\Steps\Finish', |
| 30 |
]; |
| 31 |
|
| 32 |
/* @var array */ |
| 33 |
protected $steps = []; |
| 34 |
|
| 35 |
/* @var InstallerWizard */ |
| 36 |
protected $wizard; |
| 37 |
|
| 38 |
/* @var InstallerRouter */ |
| 39 |
protected $router; |
| 40 |
|
| 41 |
/** |
| 42 |
* Check if the installer is enabled and ensure the user has correct permissions to run it |
| 43 |
*/ |
| 44 |
public function __construct(AdminTabGeneral $adminTab) |
| 45 |
{ |
| 46 |
if (!$this->isInstallerEnabled()) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (!$this->userHasPermissions()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$this->adminTab = $adminTab; |
| 55 |
|
| 56 |
$this->maybeDisplayDisclaimer(); |
| 57 |
$this->setupHooks(); |
| 58 |
|
| 59 |
if (!$this->isInstalled()) { |
| 60 |
$this->setupSteps(); |
| 61 |
$this->runInstaller(); |
| 62 |
} |
| 63 |
|
| 64 |
if(!$this->isPrivcySafeNotified()) { |
| 65 |
$this->run_privacysafe_promo(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Setup actions and admin tab components |
| 71 |
*/ |
| 72 |
protected function setupHooks() |
| 73 |
{ |
| 74 |
add_action('admin_init', [$this, 'setupAdminGeneralTabButtons'], 0); |
| 75 |
|
| 76 |
add_action('gdpr/admin/action/accept_disclaimer', [$this, 'acceptDisclaimer']); |
| 77 |
|
| 78 |
add_action('gdpr/admin/action/restart_wizard', [$this, 'restartWizard']); |
| 79 |
|
| 80 |
add_action('gdpr/admin/action/auto_install', [$this, 'autoInstall']); |
| 81 |
add_action('gdpr/admin/action/skip_install', [$this, 'skipInstall']); |
| 82 |
add_action('gdpr/admin/action/skip_notice', [$this, 'skipNotice']); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
protected function runInstaller() |
| 87 |
{ |
| 88 |
$this->wizard = new InstallerWizard; |
| 89 |
$this->router = new InstallerRouter($this->steps); |
| 90 |
|
| 91 |
// If we're currently on one of the installer steps, let the router handle it |
| 92 |
if ($this->router->isInstallerStep()) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if ($this->getCurrentStepSlug()) { |
| 97 |
// If the current step is set, display continue notice |
| 98 |
$step = $this->router->findStep($this->getCurrentStepSlug()); |
| 99 |
// If step doesn't exist, then it means the step slugs have changed. Do nothing. |
| 100 |
if (!$step) { |
| 101 |
return; |
| 102 |
} |
| 103 |
$this->displayContinueNotice($step->getUrl()); |
| 104 |
} else { |
| 105 |
// If the current step is not set, it means the installer hasn't been started yet |
| 106 |
$this->displayWelcomeNotice(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* If the admin has not accepted the disclaimer, render it |
| 112 |
*/ |
| 113 |
public function maybeDisplayDisclaimer() |
| 114 |
{ |
| 115 |
global $gdpr; |
| 116 |
if (!$gdpr->Options->get('plugin_disclaimer_accepted') && (isset($_GET['page']) && 'privacy' === $_GET['page'])) { |
| 117 |
$acceptUrl = add_query_arg([ |
| 118 |
'gdpr_action' => 'accept_disclaimer', |
| 119 |
'gdpr_nonce' => wp_create_nonce('gdpr/admin/action/accept_disclaimer'), |
| 120 |
]); |
| 121 |
|
| 122 |
gdpr('admin-notice')->add('admin/notices/disclaimer', compact('acceptUrl')); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Mark the disclaimer as accepted |
| 128 |
*/ |
| 129 |
public function acceptDisclaimer() |
| 130 |
{ |
| 131 |
global $gdpr; |
| 132 |
$gdpr->Options->set('plugin_disclaimer_accepted', 'yes'); |
| 133 |
wp_safe_redirect(gdpr('helpers')->getAdminUrl()); |
| 134 |
exit; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Display installer section in admin page |
| 139 |
*/ |
| 140 |
public function setupAdminGeneralTabButtons() |
| 141 |
{ |
| 142 |
/** |
| 143 |
* Display wizard buttons |
| 144 |
*/ |
| 145 |
$this->adminTab->registerSettingSection( |
| 146 |
'gdpr-section-wizard', |
| 147 |
_x('Setup Wizard', '(Admin)', 'gdpr-framework'), |
| 148 |
[$this, 'renderWizardButtons'] |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Render the installer section |
| 154 |
*/ |
| 155 |
public function renderWizardButtons() |
| 156 |
{ |
| 157 |
$restartUrl = add_query_arg([ |
| 158 |
'gdpr_action' => 'restart_wizard', |
| 159 |
'gdpr_nonce' => wp_create_nonce("gdpr/admin/action/restart_wizard"), |
| 160 |
]); |
| 161 |
|
| 162 |
echo gdpr('view')->render( |
| 163 |
'admin/wizard-buttons', |
| 164 |
compact('restartUrl') |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Restart and redirect to first step |
| 170 |
*/ |
| 171 |
public function restartWizard() |
| 172 |
{ |
| 173 |
global $gdpr; |
| 174 |
$gdpr->Options->delete('installer_step'); |
| 175 |
$gdpr->Options->delete('is_installed'); |
| 176 |
$gdpr->Options->delete('is_privacysafe_notified'); |
| 177 |
|
| 178 |
wp_safe_redirect(self_admin_url()); |
| 179 |
exit; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Allow plugins to modify the steps |
| 184 |
*/ |
| 185 |
protected function setupSteps() |
| 186 |
{ |
| 187 |
$steps = apply_filters('gdpr/installer/steps', $this->defaultSteps); |
| 188 |
|
| 189 |
foreach ($steps as $index => $step) { |
| 190 |
$this->steps[$index] = new $step; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* The installer can be disabled by filter. |
| 196 |
* Check if it's enabled |
| 197 |
* |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
protected function isInstallerEnabled() |
| 201 |
{ |
| 202 |
return apply_filters('gdpr/installer/enabled', true); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Check if the current user has correct permissions to run the installer |
| 207 |
* |
| 208 |
* @return bool |
| 209 |
*/ |
| 210 |
protected function userHasPermissions() |
| 211 |
{ |
| 212 |
return current_user_can(apply_filters('gdpr/installer/permissions', 'manage_options')); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Check if the installer is already ran |
| 217 |
* |
| 218 |
* @return bool |
| 219 |
*/ |
| 220 |
protected function isInstalled() |
| 221 |
{ |
| 222 |
global $gdpr; |
| 223 |
return $gdpr->Options->get('is_installed'); |
| 224 |
} |
| 225 |
/** |
| 226 |
* Check if the Privacy Safe notice is already ran |
| 227 |
* |
| 228 |
* @return bool |
| 229 |
*/ |
| 230 |
protected function isPrivcySafeNotified() |
| 231 |
{ |
| 232 |
global $gdpr; |
| 233 |
return $gdpr->Options->get('is_privacysafe_notified'); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
public function getCurrentStepSlug() |
| 240 |
{ |
| 241 |
global $gdpr; |
| 242 |
return $gdpr->Options->get('installer_step'); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Render an admin notice that will display the welcome message |
| 247 |
*/ |
| 248 |
protected function displayWelcomeNotice() |
| 249 |
{ |
| 250 |
global $gdpr; |
| 251 |
// Make sure we display the notice only to admins |
| 252 |
if (!current_user_can(apply_filters('gdpr/capability', 'manage_options'))) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
$installerUrl = $this->steps[0]->getUrl(); |
| 257 |
$autoInstallUrl = add_query_arg([ |
| 258 |
'gdpr_action' => 'auto_install', |
| 259 |
'gdpr_nonce' => wp_create_nonce("gdpr/admin/action/auto_install"), |
| 260 |
]); |
| 261 |
$skipUrl = add_query_arg([ |
| 262 |
'gdpr_action' => 'skip_install', |
| 263 |
'gdpr_nonce' => wp_create_nonce("gdpr/admin/action/skip_install"), |
| 264 |
]); |
| 265 |
|
| 266 |
gdpr('admin-notice')->add( |
| 267 |
'installer/welcome-notice', |
| 268 |
compact('installerUrl', 'autoInstallUrl', 'skipUrl') |
| 269 |
); |
| 270 |
|
| 271 |
|
| 272 |
} |
| 273 |
/** |
| 274 |
* |
| 275 |
* |
| 276 |
*/ |
| 277 |
public function run_privacysafe_promo() { |
| 278 |
global $gdpr; |
| 279 |
$skipNoticeUrl = add_query_arg([ |
| 280 |
'gdpr_action' => 'skip_notice', |
| 281 |
'gdpr_nonce' => wp_create_nonce("gdpr/admin/action/skip_notice"), |
| 282 |
]); |
| 283 |
// Make sure we display the notice only to admins |
| 284 |
$gdpr->PrivacySafe->add( |
| 285 |
'installer/privacy-safe-notice', |
| 286 |
compact('skipNoticeUrl') |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Render an admin notice that will display the continue button |
| 292 |
* |
| 293 |
* @param $url |
| 294 |
*/ |
| 295 |
protected function displayContinueNotice($url) |
| 296 |
{ |
| 297 |
global $gdpr; |
| 298 |
// Make sure we display the notice only to admins |
| 299 |
if (!current_user_can(apply_filters('gdpr/capability', 'manage_options'))) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
$skipUrl = add_query_arg([ |
| 304 |
'gdpr_action' => 'skip_install', |
| 305 |
'gdpr_nonce' => wp_create_nonce("gdpr/admin/action/skip_install"), |
| 306 |
]); |
| 307 |
|
| 308 |
gdpr('admin-notice')->add('installer/continue-notice', ['buttonUrl' => $url, 'skipUrl' => $skipUrl]); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Automatically create pages for Privacy Policy and set the corresponding options |
| 313 |
*/ |
| 314 |
public function autoInstall() |
| 315 |
{ |
| 316 |
global $gdpr; |
| 317 |
$policyPageId = wp_insert_post([ |
| 318 |
'post_title' => __('Privacy Policy', 'gdpr-framework'), |
| 319 |
'post_type' => 'page', |
| 320 |
'post_status' => 'publish', |
| 321 |
]); |
| 322 |
|
| 323 |
$gdpr->Options->set('policy_page', $policyPageId); |
| 324 |
|
| 325 |
$toolsPageId = wp_insert_post([ |
| 326 |
'post_content' => '<!-- wp:shortcode -->[gdpr_privacy_tools]<!-- /wp:shortcode -->', |
| 327 |
'post_title' => __('Privacy Tools', 'gdpr-framework'), |
| 328 |
'post_type' => 'page', |
| 329 |
'post_status' => 'publish', |
| 330 |
]); |
| 331 |
$gdpr->Options->set('tools_page', $toolsPageId); |
| 332 |
|
| 333 |
// Woocommerce compatibility - automatically add their terms page |
| 334 |
if (get_option('woocommerce_terms_page_id')) { |
| 335 |
$gdpr->Options->set('terms_page', get_option('woocommerce_terms_page_id')); |
| 336 |
} |
| 337 |
|
| 338 |
$gdpr->Options->set('is_installed', 'yes'); |
| 339 |
wp_safe_redirect(gdpr('helpers')->getAdminUrl('&gdpr-tab=privacy-policy&gdpr-notice=autoinstall')); |
| 340 |
exit; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Do nothing, but mark the installer as completed |
| 345 |
*/ |
| 346 |
public function skipInstall() |
| 347 |
{ |
| 348 |
global $gdpr; |
| 349 |
$gdpr->Options->set('is_installed', 'yes'); |
| 350 |
wp_safe_redirect(gdpr('helpers')->getAdminUrl()); |
| 351 |
exit; |
| 352 |
} |
| 353 |
/** |
| 354 |
* Do nothing, but mark the Privacy Notice as completed |
| 355 |
*/ |
| 356 |
public function skipNotice() |
| 357 |
{ |
| 358 |
global $gdpr; |
| 359 |
$gdpr->Options->set('is_privacysafe_notified', 'yes'); |
| 360 |
wp_safe_redirect(gdpr('helpers')->getAdminUrl()); |
| 361 |
exit; |
| 362 |
} |
| 363 |
} |
| 364 |
|