PluginProbe
The GDPR Framework By Data443 / 2.1.0
The GDPR Framework By Data443 v2.1.0
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Installer / InstallerRouter.php

InstallerRouter.php in The GDPR Framework By Data443 2.1.0, at src/Installer/InstallerRouter.php

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