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