| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Installer; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle redirecting and routing the installer steps |
| 9 |
* |
| 10 |
* Class InstallerRouter |
| 11 |
* |
| 12 |
* @package Codelight\GDPR\Installer |
| 13 |
*/ |
| 14 |
class InstallerRouter |
| 15 |
{ |
| 16 |
protected $steps; |
| 17 |
|
| 18 |
/** |
| 19 |
* Set up the router |
| 20 |
* |
| 21 |
* @param array $steps |
| 22 |
*/ |
| 23 |
public function __construct(array $steps) |
| 24 |
{ |
| 25 |
$this->steps = $steps; |
| 26 |
|
| 27 |
if (isset($_GET['gdpr-step'])) { |
| 28 |
add_action('admin_init', [$this, 'route']); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Do the magic |
| 34 |
*/ |
| 35 |
public function route() |
| 36 |
{ |
| 37 |
/* @var $step InstallerStepInterface */ |
| 38 |
$step = $this->findStep($_GET['gdpr-step']); |
| 39 |
|
| 40 |
if (!$step) { |
| 41 |
trigger_error("Step {$_GET['gdpr-step']} not found!", E_USER_ERROR); |
| 42 |
} |
| 43 |
|
| 44 |
if ('POST' === $_SERVER['REQUEST_METHOD'] && isset($_POST['gdpr-installer'])) { |
| 45 |
|
| 46 |
// Handle the previous step button |
| 47 |
if ('previous' === esc_html($_POST['gdpr-installer'])) { |
| 48 |
$this->setCurrentStep($this->getPreviousStep($step)->getSlug()); |
| 49 |
wp_safe_redirect($this->getPreviousStep($step)->getUrl(), 302); |
| 50 |
exit; |
| 51 |
} |
| 52 |
|
| 53 |
// Handle form submission |
| 54 |
if ('next' === esc_html($_POST['gdpr-installer'])) { |
| 55 |
|
| 56 |
if (!$step->validateNonce()) { |
| 57 |
wp_safe_redirect($step->getUrl() . '&gdpr-error=nonce', 302); |
| 58 |
exit; |
| 59 |
} |
| 60 |
|
| 61 |
// Handle successful validation |
| 62 |
if ($step->validate()) { |
| 63 |
$step->submit(); |
| 64 |
$nextStep = $this->getNextStep($step); |
| 65 |
if ($nextStep) { |
| 66 |
$this->setCurrentStep($nextStep->getSlug()); |
| 67 |
wp_safe_redirect($nextStep->getUrl(), 302); |
| 68 |
exit; |
| 69 |
} else { |
| 70 |
// If no next step then we're done |
| 71 |
wp_safe_redirect(admin_url(), 302); |
| 72 |
exit; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Handle form submission with failed validation |
| 77 |
wp_safe_redirect($step->getUrl() . '&gdpr-error=' . $step->getErrors(), 302); |
| 78 |
exit; |
| 79 |
} |
| 80 |
|
| 81 |
trigger_error('Installer action not defined!', E_USER_NOTICE); |
| 82 |
|
| 83 |
} else { |
| 84 |
|
| 85 |
// Run wizard page step |
| 86 |
if ('wizard' === $step->getType()) { |
| 87 |
ob_start(); |
| 88 |
$step->run(); |
| 89 |
exit; |
| 90 |
} |
| 91 |
|
| 92 |
// Run regular admin page step |
| 93 |
$step->run(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Find a step by slug |
| 99 |
* |
| 100 |
* @param $slug |
| 101 |
* @return InstallerStepInterface|bool |
| 102 |
*/ |
| 103 |
public function findStep($slug) |
| 104 |
{ |
| 105 |
foreach ($this->steps as $i => $step) { |
| 106 |
if ($slug === $step->getSlug()) { |
| 107 |
return $step; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Find the index of a step by its slug |
| 116 |
* |
| 117 |
* @param $slug |
| 118 |
* @return bool|int|string |
| 119 |
*/ |
| 120 |
protected function findStepIndex($slug) |
| 121 |
{ |
| 122 |
foreach ($this->steps as $i => $step) { |
| 123 |
if ($slug === $step->getSlug()) { |
| 124 |
return $i; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get the URL of the previous step or false if it's the first step |
| 133 |
* |
| 134 |
* @param InstallerStepInterface $step |
| 135 |
* @return InstallerStepInterface|bool |
| 136 |
*/ |
| 137 |
public function getPreviousStep(InstallerStepInterface $step) |
| 138 |
{ |
| 139 |
$currentStepNumber = $this->findStepIndex($step->getSlug()); |
| 140 |
|
| 141 |
if (false === $currentStepNumber or 0 === $currentStepNumber) { |
| 142 |
$prevStep = false; |
| 143 |
} else { |
| 144 |
$prevStep = $this->steps[$currentStepNumber - 1]; |
| 145 |
} |
| 146 |
|
| 147 |
return apply_filters('gdpr/installer/prev-step', $prevStep, $currentStepNumber); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the URL of the next step or admin dashboard if it's the last step |
| 152 |
* |
| 153 |
* @param InstallerStepInterface $step |
| 154 |
* @return InstallerStepInterface|bool |
| 155 |
*/ |
| 156 |
public function getNextStep(InstallerStepInterface $step) |
| 157 |
{ |
| 158 |
$currentStepNumber = $this->findStepIndex($step->getSlug()); |
| 159 |
|
| 160 |
if (false === $currentStepNumber or count($this->steps) === $currentStepNumber + 1) { |
| 161 |
$nextStep = false; |
| 162 |
} else { |
| 163 |
$nextStep = $this->steps[$currentStepNumber + 1]; |
| 164 |
} |
| 165 |
|
| 166 |
return apply_filters('gdpr/installer/next-step-url', $nextStep, $currentStepNumber); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Save the current step slug in the database to enable continue functionality |
| 171 |
* |
| 172 |
* @param $steps |
| 173 |
*/ |
| 174 |
protected function setCurrentStep($slug) |
| 175 |
{ |
| 176 |
global $gdpr; |
| 177 |
$gdpr->Options->set('installer_step', $slug); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Check if we are currently on any of the installer steps |
| 182 |
* |
| 183 |
* @return array|bool |
| 184 |
*/ |
| 185 |
public function isInstallerStep() |
| 186 |
{ |
| 187 |
return isset($_GET['gdpr-step']) && $this->findStep($_GET['gdpr-step']); |
| 188 |
} |
| 189 |
} |