@@ -4,7 +4,6 @@ |
||
4 | 4 | |
5 | 5 | use EE_Error; |
6 | 6 | use EE_Request; |
7 | -use EventEspresso\core\exceptions\ExceptionStackTraceDisplay; |
|
8 | 7 | use EventEspresso\core\exceptions\InvalidClassException; |
9 | 8 | use EventEspresso\core\exceptions\InvalidDataTypeException; |
10 | 9 | use EventEspresso\core\exceptions\InvalidEntityException; |
@@ -29,584 +29,584 @@ |
||
29 | 29 | abstract class SequentialStepFormManager |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * a simplified URL with no form related parameters |
|
34 | - * that will be used to build the form's redirect URLs |
|
35 | - * |
|
36 | - * @var string $base_url |
|
37 | - */ |
|
38 | - private $base_url = ''; |
|
39 | - |
|
40 | - /** |
|
41 | - * the key used for the URL param that denotes the current form step |
|
42 | - * defaults to 'ee-form-step' |
|
43 | - * |
|
44 | - * @var string $form_step_url_key |
|
45 | - */ |
|
46 | - private $form_step_url_key = ''; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var string $default_form_step |
|
50 | - */ |
|
51 | - private $default_form_step = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var string $form_action |
|
55 | - */ |
|
56 | - private $form_action; |
|
57 | - |
|
58 | - /** |
|
59 | - * value of one of the string constant above |
|
60 | - * |
|
61 | - * @var string $form_config |
|
62 | - */ |
|
63 | - private $form_config; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var string $progress_step_style |
|
67 | - */ |
|
68 | - private $progress_step_style = ''; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var EE_Request $request |
|
72 | - */ |
|
73 | - private $request; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var Collection $form_steps |
|
77 | - */ |
|
78 | - protected $form_steps; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var ProgressStepManager $progress_step_manager |
|
82 | - */ |
|
83 | - protected $progress_step_manager; |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * @return Collection|null |
|
88 | - */ |
|
89 | - abstract protected function getFormStepsCollection(); |
|
90 | - |
|
91 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
92 | - /** |
|
93 | - * StepsManager constructor |
|
94 | - * |
|
95 | - * @param string $base_url |
|
96 | - * @param string $default_form_step |
|
97 | - * @param string $form_action |
|
98 | - * @param string $form_config |
|
99 | - * @param EE_Request $request |
|
100 | - * @param string $progress_step_style |
|
101 | - * @throws InvalidDataTypeException |
|
102 | - * @throws InvalidArgumentException |
|
103 | - */ |
|
104 | - public function __construct( |
|
105 | - $base_url, |
|
106 | - $default_form_step, |
|
107 | - $form_action = '', |
|
108 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
109 | - $progress_step_style = 'number_bubbles', |
|
110 | - EE_Request $request |
|
111 | - ) { |
|
112 | - $this->setBaseUrl($base_url); |
|
113 | - $this->setDefaultFormStep($default_form_step); |
|
114 | - $this->setFormAction($form_action); |
|
115 | - $this->setFormConfig($form_config); |
|
116 | - $this->setProgressStepStyle($progress_step_style); |
|
117 | - $this->request = $request; |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * @return string |
|
123 | - * @throws InvalidFormHandlerException |
|
124 | - */ |
|
125 | - public function baseUrl() |
|
126 | - { |
|
127 | - if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) { |
|
128 | - add_query_arg( |
|
129 | - array($this->form_step_url_key => $this->getCurrentStep()->slug()), |
|
130 | - $this->base_url |
|
131 | - ); |
|
132 | - } |
|
133 | - return $this->base_url; |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $base_url |
|
139 | - * @throws InvalidDataTypeException |
|
140 | - * @throws InvalidArgumentException |
|
141 | - */ |
|
142 | - protected function setBaseUrl($base_url) |
|
143 | - { |
|
144 | - if (! is_string($base_url)) { |
|
145 | - throw new InvalidDataTypeException('$base_url', $base_url, 'string'); |
|
146 | - } |
|
147 | - if (empty($base_url)) { |
|
148 | - throw new InvalidArgumentException( |
|
149 | - esc_html__('The base URL can not be an empty string.', 'event_espresso') |
|
150 | - ); |
|
151 | - } |
|
152 | - $this->base_url = $base_url; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * @return string |
|
158 | - * @throws InvalidDataTypeException |
|
159 | - */ |
|
160 | - public function formStepUrlKey() |
|
161 | - { |
|
162 | - if (empty($this->form_step_url_key)) { |
|
163 | - $this->setFormStepUrlKey(); |
|
164 | - } |
|
165 | - return $this->form_step_url_key; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $form_step_url_key |
|
171 | - * @throws InvalidDataTypeException |
|
172 | - */ |
|
173 | - public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
174 | - { |
|
175 | - if (! is_string($form_step_url_key)) { |
|
176 | - throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
177 | - } |
|
178 | - $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
179 | - } |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * @return string |
|
184 | - */ |
|
185 | - public function defaultFormStep() |
|
186 | - { |
|
187 | - return $this->default_form_step; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * @param $default_form_step |
|
193 | - * @throws InvalidDataTypeException |
|
194 | - */ |
|
195 | - protected function setDefaultFormStep($default_form_step) |
|
196 | - { |
|
197 | - if (! is_string($default_form_step)) { |
|
198 | - throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string'); |
|
199 | - } |
|
200 | - $this->default_form_step = $default_form_step; |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * @return void |
|
206 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
207 | - * @throws InvalidDataTypeException |
|
208 | - */ |
|
209 | - protected function setCurrentStepFromRequest() |
|
210 | - { |
|
211 | - $current_step_slug = $this->request()->get($this->formStepUrlKey(), $this->defaultFormStep()); |
|
212 | - if (! $this->form_steps->setCurrent($current_step_slug)) { |
|
213 | - throw new InvalidIdentifierException( |
|
214 | - $current_step_slug, |
|
215 | - $this->defaultFormStep(), |
|
216 | - $message = sprintf( |
|
217 | - esc_html__( |
|
218 | - 'The "%1$s" form step could not be set.', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - $current_step_slug |
|
222 | - ) |
|
223 | - ); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * @return object|SequentialStepFormInterface |
|
230 | - * @throws InvalidFormHandlerException |
|
231 | - */ |
|
232 | - public function getCurrentStep() |
|
233 | - { |
|
234 | - if (! $this->form_steps->current() instanceof SequentialStepForm) { |
|
235 | - throw new InvalidFormHandlerException($this->form_steps->current()); |
|
236 | - } |
|
237 | - return $this->form_steps->current(); |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * @return string |
|
243 | - * @throws InvalidFormHandlerException |
|
244 | - */ |
|
245 | - public function formAction() |
|
246 | - { |
|
247 | - if (! is_string($this->form_action) || empty($this->form_action)) { |
|
248 | - $this->form_action = $this->baseUrl(); |
|
249 | - } |
|
250 | - return $this->form_action; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * @param string $form_action |
|
256 | - * @throws InvalidDataTypeException |
|
257 | - */ |
|
258 | - public function setFormAction($form_action) |
|
259 | - { |
|
260 | - if (! is_string($form_action)) { |
|
261 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
262 | - } |
|
263 | - $this->form_action = $form_action; |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @param array $form_action_args |
|
269 | - * @throws InvalidDataTypeException |
|
270 | - * @throws InvalidFormHandlerException |
|
271 | - */ |
|
272 | - public function addFormActionArgs($form_action_args = array()) |
|
273 | - { |
|
274 | - if (! is_array($form_action_args)) { |
|
275 | - throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array'); |
|
276 | - } |
|
277 | - $form_action_args = ! empty($form_action_args) |
|
278 | - ? $form_action_args |
|
279 | - : array($this->formStepUrlKey() => $this->form_steps->current()->slug()); |
|
280 | - $this->getCurrentStep()->setFormAction( |
|
281 | - add_query_arg($form_action_args, $this->formAction()) |
|
282 | - ); |
|
283 | - $this->form_action = $this->getCurrentStep()->formAction(); |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - public function formConfig() |
|
291 | - { |
|
292 | - return $this->form_config; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * @param string $form_config |
|
298 | - */ |
|
299 | - public function setFormConfig($form_config) |
|
300 | - { |
|
301 | - $this->form_config = $form_config; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * @return string |
|
307 | - */ |
|
308 | - public function progressStepStyle() |
|
309 | - { |
|
310 | - return $this->progress_step_style; |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * @param string $progress_step_style |
|
316 | - */ |
|
317 | - public function setProgressStepStyle($progress_step_style) |
|
318 | - { |
|
319 | - $this->progress_step_style = $progress_step_style; |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * @return EE_Request |
|
325 | - */ |
|
326 | - public function request() |
|
327 | - { |
|
328 | - return $this->request; |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * @return Collection|null |
|
334 | - * @throws InvalidInterfaceException |
|
335 | - */ |
|
336 | - protected function getProgressStepsCollection() |
|
337 | - { |
|
338 | - static $collection = null; |
|
339 | - if (! $collection instanceof ProgressStepCollection) { |
|
340 | - $collection = new ProgressStepCollection(); |
|
341 | - } |
|
342 | - return $collection; |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * @param Collection $progress_steps_collection |
|
348 | - * @return ProgressStepManager |
|
349 | - * @throws InvalidInterfaceException |
|
350 | - * @throws InvalidClassException |
|
351 | - * @throws InvalidDataTypeException |
|
352 | - * @throws InvalidEntityException |
|
353 | - * @throws InvalidFormHandlerException |
|
354 | - */ |
|
355 | - protected function generateProgressSteps(Collection $progress_steps_collection) |
|
356 | - { |
|
357 | - $current_step = $this->getCurrentStep(); |
|
358 | - /** @var SequentialStepForm $form_step */ |
|
359 | - foreach ($this->form_steps as $form_step) { |
|
360 | - // is this step active ? |
|
361 | - if (! $form_step->initialize()) { |
|
362 | - continue; |
|
363 | - } |
|
364 | - $progress_steps_collection->add( |
|
365 | - new ProgressStep( |
|
366 | - $form_step->order(), |
|
367 | - $form_step->slug(), |
|
368 | - $form_step->slug(), |
|
369 | - $form_step->formName() |
|
370 | - ), |
|
371 | - $form_step->slug() |
|
372 | - ); |
|
373 | - } |
|
374 | - // set collection pointer back to current step |
|
375 | - $this->form_steps->setCurrentUsingObject($current_step); |
|
376 | - return new ProgressStepManager( |
|
377 | - $this->progressStepStyle(), |
|
378 | - $this->defaultFormStep(), |
|
379 | - $this->formStepUrlKey(), |
|
380 | - $progress_steps_collection |
|
381 | - ); |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * @throws InvalidClassException |
|
387 | - * @throws InvalidDataTypeException |
|
388 | - * @throws InvalidEntityException |
|
389 | - * @throws InvalidIdentifierException |
|
390 | - * @throws InvalidInterfaceException |
|
391 | - * @throws InvalidArgumentException |
|
392 | - * @throws InvalidFormHandlerException |
|
393 | - */ |
|
394 | - public function buildForm() |
|
395 | - { |
|
396 | - $this->buildCurrentStepFormForDisplay(); |
|
397 | - } |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * @param array $form_data |
|
402 | - * @throws InvalidArgumentException |
|
403 | - * @throws InvalidClassException |
|
404 | - * @throws InvalidDataTypeException |
|
405 | - * @throws InvalidEntityException |
|
406 | - * @throws InvalidFormHandlerException |
|
407 | - * @throws InvalidIdentifierException |
|
408 | - * @throws InvalidInterfaceException |
|
409 | - */ |
|
410 | - public function processForm($form_data = array()) |
|
411 | - { |
|
412 | - $this->buildCurrentStepFormForProcessing(); |
|
413 | - $this->processCurrentStepForm($form_data); |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * @throws InvalidClassException |
|
419 | - * @throws InvalidDataTypeException |
|
420 | - * @throws InvalidEntityException |
|
421 | - * @throws InvalidInterfaceException |
|
422 | - * @throws InvalidIdentifierException |
|
423 | - * @throws InvalidArgumentException |
|
424 | - * @throws InvalidFormHandlerException |
|
425 | - */ |
|
426 | - public function buildCurrentStepFormForDisplay() |
|
427 | - { |
|
428 | - $form_step = $this->buildCurrentStepForm(); |
|
429 | - // no displayable content ? then skip straight to processing |
|
430 | - if (! $form_step->displayable()) { |
|
431 | - $this->addFormActionArgs(); |
|
432 | - $form_step->setFormAction($this->formAction()); |
|
433 | - wp_safe_redirect($form_step->formAction()); |
|
434 | - } |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - /** |
|
439 | - * @throws InvalidClassException |
|
440 | - * @throws InvalidDataTypeException |
|
441 | - * @throws InvalidEntityException |
|
442 | - * @throws InvalidInterfaceException |
|
443 | - * @throws InvalidIdentifierException |
|
444 | - * @throws InvalidArgumentException |
|
445 | - * @throws InvalidFormHandlerException |
|
446 | - */ |
|
447 | - public function buildCurrentStepFormForProcessing() |
|
448 | - { |
|
449 | - $this->buildCurrentStepForm(false); |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * @param bool $for_display |
|
455 | - * @return SequentialStepFormInterface |
|
456 | - * @throws InvalidArgumentException |
|
457 | - * @throws InvalidClassException |
|
458 | - * @throws InvalidDataTypeException |
|
459 | - * @throws InvalidEntityException |
|
460 | - * @throws InvalidFormHandlerException |
|
461 | - * @throws InvalidIdentifierException |
|
462 | - * @throws InvalidInterfaceException |
|
463 | - */ |
|
464 | - private function buildCurrentStepForm($for_display = true) |
|
465 | - { |
|
466 | - $this->form_steps = $this->getFormStepsCollection(); |
|
467 | - $this->setCurrentStepFromRequest(); |
|
468 | - $form_step = $this->getCurrentStep(); |
|
469 | - if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) { |
|
470 | - $form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso')); |
|
471 | - } |
|
472 | - if ($for_display && $form_step->displayable()) { |
|
473 | - $this->progress_step_manager = $this->generateProgressSteps( |
|
474 | - $this->getProgressStepsCollection() |
|
475 | - ); |
|
476 | - $this->progress_step_manager->setCurrentStep( |
|
477 | - $form_step->slug() |
|
478 | - ); |
|
479 | - // mark all previous progress steps as completed |
|
480 | - $this->progress_step_manager->setPreviousStepsCompleted(); |
|
481 | - $this->progress_step_manager->enqueueStylesAndScripts(); |
|
482 | - $this->addFormActionArgs(); |
|
483 | - $form_step->setFormAction($this->formAction()); |
|
484 | - } else { |
|
485 | - $form_step->setRedirectUrl($this->baseUrl()); |
|
486 | - $form_step->addRedirectArgs( |
|
487 | - array($this->formStepUrlKey() => $this->form_steps->current()->slug()) |
|
488 | - ); |
|
489 | - } |
|
490 | - $form_step->generate(); |
|
491 | - if ($for_display) { |
|
492 | - $form_step->enqueueStylesAndScripts(); |
|
493 | - } |
|
494 | - return $form_step; |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * @param bool $return_as_string |
|
500 | - * @return string |
|
501 | - * @throws InvalidFormHandlerException |
|
502 | - */ |
|
503 | - public function displayProgressSteps($return_as_string = true) |
|
504 | - { |
|
505 | - $form_step = $this->getCurrentStep(); |
|
506 | - if (! $form_step->displayable()) { |
|
507 | - return ''; |
|
508 | - } |
|
509 | - $progress_steps = apply_filters( |
|
510 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps', |
|
511 | - '' |
|
512 | - ); |
|
513 | - $progress_steps .= $this->progress_step_manager->displaySteps(); |
|
514 | - $progress_steps .= apply_filters( |
|
515 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps', |
|
516 | - '' |
|
517 | - ); |
|
518 | - if ($return_as_string) { |
|
519 | - return $progress_steps; |
|
520 | - } |
|
521 | - echo $progress_steps; |
|
522 | - return ''; |
|
523 | - } |
|
524 | - |
|
525 | - |
|
526 | - /** |
|
527 | - * @param bool $return_as_string |
|
528 | - * @return string |
|
529 | - * @throws InvalidFormHandlerException |
|
530 | - */ |
|
531 | - public function displayCurrentStepForm($return_as_string = true) |
|
532 | - { |
|
533 | - if ($return_as_string) { |
|
534 | - return $this->getCurrentStep()->display(); |
|
535 | - } |
|
536 | - echo $this->getCurrentStep()->display(); |
|
537 | - return ''; |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - /** |
|
542 | - * @param array $form_data |
|
543 | - * @return void |
|
544 | - * @throws InvalidArgumentException |
|
545 | - * @throws InvalidDataTypeException |
|
546 | - * @throws InvalidFormHandlerException |
|
547 | - */ |
|
548 | - public function processCurrentStepForm($form_data = array()) |
|
549 | - { |
|
550 | - // grab instance of current step because after calling next() below, |
|
551 | - // any calls to getCurrentStep() will return the "next" step because we advanced |
|
552 | - $current_step = $this->getCurrentStep(); |
|
553 | - try { |
|
554 | - // form processing should either throw exceptions or return true |
|
555 | - $current_step->process($form_data); |
|
556 | - } catch (Exception $e) { |
|
557 | - // something went wrong, convert the Exception to an EE_Error |
|
558 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
559 | - // prevent redirect to next step or other if exception was thrown |
|
560 | - if ($current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP |
|
561 | - || $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER |
|
562 | - ) { |
|
563 | - $current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP); |
|
564 | - } |
|
565 | - } |
|
566 | - // save notices to a transient so that when we redirect back |
|
567 | - // to the display portion for this step |
|
568 | - // those notices can be displayed |
|
569 | - EE_Error::get_notices(false, true); |
|
570 | - $this->redirectForm($current_step); |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * handles where to go to next |
|
576 | - * |
|
577 | - * @param SequentialStepFormInterface $current_step |
|
578 | - * @throws InvalidArgumentException |
|
579 | - * @throws InvalidDataTypeException |
|
580 | - * @throws InvalidFormHandlerException |
|
581 | - */ |
|
582 | - public function redirectForm(SequentialStepFormInterface $current_step) |
|
583 | - { |
|
584 | - $redirect_step = $current_step; |
|
585 | - switch ($current_step->redirectTo()) { |
|
586 | - case SequentialStepForm::REDIRECT_TO_OTHER: |
|
587 | - // going somewhere else, so just check out now |
|
588 | - wp_safe_redirect($redirect_step->redirectUrl()); |
|
589 | - exit(); |
|
590 | - break; |
|
591 | - case SequentialStepForm::REDIRECT_TO_PREV_STEP: |
|
592 | - $redirect_step = $this->form_steps->previous(); |
|
593 | - break; |
|
594 | - case SequentialStepForm::REDIRECT_TO_NEXT_STEP: |
|
595 | - $this->form_steps->next(); |
|
596 | - if ($this->form_steps->valid()) { |
|
597 | - $redirect_step = $this->form_steps->current(); |
|
598 | - } |
|
599 | - break; |
|
600 | - case SequentialStepForm::REDIRECT_TO_CURRENT_STEP: |
|
601 | - default: |
|
602 | - // $redirect_step is already set |
|
603 | - } |
|
604 | - $current_step->setRedirectUrl($this->baseUrl()); |
|
605 | - $current_step->addRedirectArgs( |
|
606 | - // use the slug for whatever step we are redirecting too |
|
607 | - array($this->formStepUrlKey() => $redirect_step->slug()) |
|
608 | - ); |
|
609 | - wp_safe_redirect($current_step->redirectUrl()); |
|
610 | - exit(); |
|
611 | - } |
|
32 | + /** |
|
33 | + * a simplified URL with no form related parameters |
|
34 | + * that will be used to build the form's redirect URLs |
|
35 | + * |
|
36 | + * @var string $base_url |
|
37 | + */ |
|
38 | + private $base_url = ''; |
|
39 | + |
|
40 | + /** |
|
41 | + * the key used for the URL param that denotes the current form step |
|
42 | + * defaults to 'ee-form-step' |
|
43 | + * |
|
44 | + * @var string $form_step_url_key |
|
45 | + */ |
|
46 | + private $form_step_url_key = ''; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var string $default_form_step |
|
50 | + */ |
|
51 | + private $default_form_step = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var string $form_action |
|
55 | + */ |
|
56 | + private $form_action; |
|
57 | + |
|
58 | + /** |
|
59 | + * value of one of the string constant above |
|
60 | + * |
|
61 | + * @var string $form_config |
|
62 | + */ |
|
63 | + private $form_config; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var string $progress_step_style |
|
67 | + */ |
|
68 | + private $progress_step_style = ''; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var EE_Request $request |
|
72 | + */ |
|
73 | + private $request; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var Collection $form_steps |
|
77 | + */ |
|
78 | + protected $form_steps; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var ProgressStepManager $progress_step_manager |
|
82 | + */ |
|
83 | + protected $progress_step_manager; |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * @return Collection|null |
|
88 | + */ |
|
89 | + abstract protected function getFormStepsCollection(); |
|
90 | + |
|
91 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
92 | + /** |
|
93 | + * StepsManager constructor |
|
94 | + * |
|
95 | + * @param string $base_url |
|
96 | + * @param string $default_form_step |
|
97 | + * @param string $form_action |
|
98 | + * @param string $form_config |
|
99 | + * @param EE_Request $request |
|
100 | + * @param string $progress_step_style |
|
101 | + * @throws InvalidDataTypeException |
|
102 | + * @throws InvalidArgumentException |
|
103 | + */ |
|
104 | + public function __construct( |
|
105 | + $base_url, |
|
106 | + $default_form_step, |
|
107 | + $form_action = '', |
|
108 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
109 | + $progress_step_style = 'number_bubbles', |
|
110 | + EE_Request $request |
|
111 | + ) { |
|
112 | + $this->setBaseUrl($base_url); |
|
113 | + $this->setDefaultFormStep($default_form_step); |
|
114 | + $this->setFormAction($form_action); |
|
115 | + $this->setFormConfig($form_config); |
|
116 | + $this->setProgressStepStyle($progress_step_style); |
|
117 | + $this->request = $request; |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * @return string |
|
123 | + * @throws InvalidFormHandlerException |
|
124 | + */ |
|
125 | + public function baseUrl() |
|
126 | + { |
|
127 | + if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) { |
|
128 | + add_query_arg( |
|
129 | + array($this->form_step_url_key => $this->getCurrentStep()->slug()), |
|
130 | + $this->base_url |
|
131 | + ); |
|
132 | + } |
|
133 | + return $this->base_url; |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $base_url |
|
139 | + * @throws InvalidDataTypeException |
|
140 | + * @throws InvalidArgumentException |
|
141 | + */ |
|
142 | + protected function setBaseUrl($base_url) |
|
143 | + { |
|
144 | + if (! is_string($base_url)) { |
|
145 | + throw new InvalidDataTypeException('$base_url', $base_url, 'string'); |
|
146 | + } |
|
147 | + if (empty($base_url)) { |
|
148 | + throw new InvalidArgumentException( |
|
149 | + esc_html__('The base URL can not be an empty string.', 'event_espresso') |
|
150 | + ); |
|
151 | + } |
|
152 | + $this->base_url = $base_url; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * @return string |
|
158 | + * @throws InvalidDataTypeException |
|
159 | + */ |
|
160 | + public function formStepUrlKey() |
|
161 | + { |
|
162 | + if (empty($this->form_step_url_key)) { |
|
163 | + $this->setFormStepUrlKey(); |
|
164 | + } |
|
165 | + return $this->form_step_url_key; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $form_step_url_key |
|
171 | + * @throws InvalidDataTypeException |
|
172 | + */ |
|
173 | + public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
174 | + { |
|
175 | + if (! is_string($form_step_url_key)) { |
|
176 | + throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
177 | + } |
|
178 | + $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
179 | + } |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * @return string |
|
184 | + */ |
|
185 | + public function defaultFormStep() |
|
186 | + { |
|
187 | + return $this->default_form_step; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * @param $default_form_step |
|
193 | + * @throws InvalidDataTypeException |
|
194 | + */ |
|
195 | + protected function setDefaultFormStep($default_form_step) |
|
196 | + { |
|
197 | + if (! is_string($default_form_step)) { |
|
198 | + throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string'); |
|
199 | + } |
|
200 | + $this->default_form_step = $default_form_step; |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * @return void |
|
206 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
207 | + * @throws InvalidDataTypeException |
|
208 | + */ |
|
209 | + protected function setCurrentStepFromRequest() |
|
210 | + { |
|
211 | + $current_step_slug = $this->request()->get($this->formStepUrlKey(), $this->defaultFormStep()); |
|
212 | + if (! $this->form_steps->setCurrent($current_step_slug)) { |
|
213 | + throw new InvalidIdentifierException( |
|
214 | + $current_step_slug, |
|
215 | + $this->defaultFormStep(), |
|
216 | + $message = sprintf( |
|
217 | + esc_html__( |
|
218 | + 'The "%1$s" form step could not be set.', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + $current_step_slug |
|
222 | + ) |
|
223 | + ); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * @return object|SequentialStepFormInterface |
|
230 | + * @throws InvalidFormHandlerException |
|
231 | + */ |
|
232 | + public function getCurrentStep() |
|
233 | + { |
|
234 | + if (! $this->form_steps->current() instanceof SequentialStepForm) { |
|
235 | + throw new InvalidFormHandlerException($this->form_steps->current()); |
|
236 | + } |
|
237 | + return $this->form_steps->current(); |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * @return string |
|
243 | + * @throws InvalidFormHandlerException |
|
244 | + */ |
|
245 | + public function formAction() |
|
246 | + { |
|
247 | + if (! is_string($this->form_action) || empty($this->form_action)) { |
|
248 | + $this->form_action = $this->baseUrl(); |
|
249 | + } |
|
250 | + return $this->form_action; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * @param string $form_action |
|
256 | + * @throws InvalidDataTypeException |
|
257 | + */ |
|
258 | + public function setFormAction($form_action) |
|
259 | + { |
|
260 | + if (! is_string($form_action)) { |
|
261 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
262 | + } |
|
263 | + $this->form_action = $form_action; |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @param array $form_action_args |
|
269 | + * @throws InvalidDataTypeException |
|
270 | + * @throws InvalidFormHandlerException |
|
271 | + */ |
|
272 | + public function addFormActionArgs($form_action_args = array()) |
|
273 | + { |
|
274 | + if (! is_array($form_action_args)) { |
|
275 | + throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array'); |
|
276 | + } |
|
277 | + $form_action_args = ! empty($form_action_args) |
|
278 | + ? $form_action_args |
|
279 | + : array($this->formStepUrlKey() => $this->form_steps->current()->slug()); |
|
280 | + $this->getCurrentStep()->setFormAction( |
|
281 | + add_query_arg($form_action_args, $this->formAction()) |
|
282 | + ); |
|
283 | + $this->form_action = $this->getCurrentStep()->formAction(); |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + public function formConfig() |
|
291 | + { |
|
292 | + return $this->form_config; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * @param string $form_config |
|
298 | + */ |
|
299 | + public function setFormConfig($form_config) |
|
300 | + { |
|
301 | + $this->form_config = $form_config; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * @return string |
|
307 | + */ |
|
308 | + public function progressStepStyle() |
|
309 | + { |
|
310 | + return $this->progress_step_style; |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * @param string $progress_step_style |
|
316 | + */ |
|
317 | + public function setProgressStepStyle($progress_step_style) |
|
318 | + { |
|
319 | + $this->progress_step_style = $progress_step_style; |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * @return EE_Request |
|
325 | + */ |
|
326 | + public function request() |
|
327 | + { |
|
328 | + return $this->request; |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * @return Collection|null |
|
334 | + * @throws InvalidInterfaceException |
|
335 | + */ |
|
336 | + protected function getProgressStepsCollection() |
|
337 | + { |
|
338 | + static $collection = null; |
|
339 | + if (! $collection instanceof ProgressStepCollection) { |
|
340 | + $collection = new ProgressStepCollection(); |
|
341 | + } |
|
342 | + return $collection; |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * @param Collection $progress_steps_collection |
|
348 | + * @return ProgressStepManager |
|
349 | + * @throws InvalidInterfaceException |
|
350 | + * @throws InvalidClassException |
|
351 | + * @throws InvalidDataTypeException |
|
352 | + * @throws InvalidEntityException |
|
353 | + * @throws InvalidFormHandlerException |
|
354 | + */ |
|
355 | + protected function generateProgressSteps(Collection $progress_steps_collection) |
|
356 | + { |
|
357 | + $current_step = $this->getCurrentStep(); |
|
358 | + /** @var SequentialStepForm $form_step */ |
|
359 | + foreach ($this->form_steps as $form_step) { |
|
360 | + // is this step active ? |
|
361 | + if (! $form_step->initialize()) { |
|
362 | + continue; |
|
363 | + } |
|
364 | + $progress_steps_collection->add( |
|
365 | + new ProgressStep( |
|
366 | + $form_step->order(), |
|
367 | + $form_step->slug(), |
|
368 | + $form_step->slug(), |
|
369 | + $form_step->formName() |
|
370 | + ), |
|
371 | + $form_step->slug() |
|
372 | + ); |
|
373 | + } |
|
374 | + // set collection pointer back to current step |
|
375 | + $this->form_steps->setCurrentUsingObject($current_step); |
|
376 | + return new ProgressStepManager( |
|
377 | + $this->progressStepStyle(), |
|
378 | + $this->defaultFormStep(), |
|
379 | + $this->formStepUrlKey(), |
|
380 | + $progress_steps_collection |
|
381 | + ); |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * @throws InvalidClassException |
|
387 | + * @throws InvalidDataTypeException |
|
388 | + * @throws InvalidEntityException |
|
389 | + * @throws InvalidIdentifierException |
|
390 | + * @throws InvalidInterfaceException |
|
391 | + * @throws InvalidArgumentException |
|
392 | + * @throws InvalidFormHandlerException |
|
393 | + */ |
|
394 | + public function buildForm() |
|
395 | + { |
|
396 | + $this->buildCurrentStepFormForDisplay(); |
|
397 | + } |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * @param array $form_data |
|
402 | + * @throws InvalidArgumentException |
|
403 | + * @throws InvalidClassException |
|
404 | + * @throws InvalidDataTypeException |
|
405 | + * @throws InvalidEntityException |
|
406 | + * @throws InvalidFormHandlerException |
|
407 | + * @throws InvalidIdentifierException |
|
408 | + * @throws InvalidInterfaceException |
|
409 | + */ |
|
410 | + public function processForm($form_data = array()) |
|
411 | + { |
|
412 | + $this->buildCurrentStepFormForProcessing(); |
|
413 | + $this->processCurrentStepForm($form_data); |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * @throws InvalidClassException |
|
419 | + * @throws InvalidDataTypeException |
|
420 | + * @throws InvalidEntityException |
|
421 | + * @throws InvalidInterfaceException |
|
422 | + * @throws InvalidIdentifierException |
|
423 | + * @throws InvalidArgumentException |
|
424 | + * @throws InvalidFormHandlerException |
|
425 | + */ |
|
426 | + public function buildCurrentStepFormForDisplay() |
|
427 | + { |
|
428 | + $form_step = $this->buildCurrentStepForm(); |
|
429 | + // no displayable content ? then skip straight to processing |
|
430 | + if (! $form_step->displayable()) { |
|
431 | + $this->addFormActionArgs(); |
|
432 | + $form_step->setFormAction($this->formAction()); |
|
433 | + wp_safe_redirect($form_step->formAction()); |
|
434 | + } |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + /** |
|
439 | + * @throws InvalidClassException |
|
440 | + * @throws InvalidDataTypeException |
|
441 | + * @throws InvalidEntityException |
|
442 | + * @throws InvalidInterfaceException |
|
443 | + * @throws InvalidIdentifierException |
|
444 | + * @throws InvalidArgumentException |
|
445 | + * @throws InvalidFormHandlerException |
|
446 | + */ |
|
447 | + public function buildCurrentStepFormForProcessing() |
|
448 | + { |
|
449 | + $this->buildCurrentStepForm(false); |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * @param bool $for_display |
|
455 | + * @return SequentialStepFormInterface |
|
456 | + * @throws InvalidArgumentException |
|
457 | + * @throws InvalidClassException |
|
458 | + * @throws InvalidDataTypeException |
|
459 | + * @throws InvalidEntityException |
|
460 | + * @throws InvalidFormHandlerException |
|
461 | + * @throws InvalidIdentifierException |
|
462 | + * @throws InvalidInterfaceException |
|
463 | + */ |
|
464 | + private function buildCurrentStepForm($for_display = true) |
|
465 | + { |
|
466 | + $this->form_steps = $this->getFormStepsCollection(); |
|
467 | + $this->setCurrentStepFromRequest(); |
|
468 | + $form_step = $this->getCurrentStep(); |
|
469 | + if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) { |
|
470 | + $form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso')); |
|
471 | + } |
|
472 | + if ($for_display && $form_step->displayable()) { |
|
473 | + $this->progress_step_manager = $this->generateProgressSteps( |
|
474 | + $this->getProgressStepsCollection() |
|
475 | + ); |
|
476 | + $this->progress_step_manager->setCurrentStep( |
|
477 | + $form_step->slug() |
|
478 | + ); |
|
479 | + // mark all previous progress steps as completed |
|
480 | + $this->progress_step_manager->setPreviousStepsCompleted(); |
|
481 | + $this->progress_step_manager->enqueueStylesAndScripts(); |
|
482 | + $this->addFormActionArgs(); |
|
483 | + $form_step->setFormAction($this->formAction()); |
|
484 | + } else { |
|
485 | + $form_step->setRedirectUrl($this->baseUrl()); |
|
486 | + $form_step->addRedirectArgs( |
|
487 | + array($this->formStepUrlKey() => $this->form_steps->current()->slug()) |
|
488 | + ); |
|
489 | + } |
|
490 | + $form_step->generate(); |
|
491 | + if ($for_display) { |
|
492 | + $form_step->enqueueStylesAndScripts(); |
|
493 | + } |
|
494 | + return $form_step; |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * @param bool $return_as_string |
|
500 | + * @return string |
|
501 | + * @throws InvalidFormHandlerException |
|
502 | + */ |
|
503 | + public function displayProgressSteps($return_as_string = true) |
|
504 | + { |
|
505 | + $form_step = $this->getCurrentStep(); |
|
506 | + if (! $form_step->displayable()) { |
|
507 | + return ''; |
|
508 | + } |
|
509 | + $progress_steps = apply_filters( |
|
510 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps', |
|
511 | + '' |
|
512 | + ); |
|
513 | + $progress_steps .= $this->progress_step_manager->displaySteps(); |
|
514 | + $progress_steps .= apply_filters( |
|
515 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps', |
|
516 | + '' |
|
517 | + ); |
|
518 | + if ($return_as_string) { |
|
519 | + return $progress_steps; |
|
520 | + } |
|
521 | + echo $progress_steps; |
|
522 | + return ''; |
|
523 | + } |
|
524 | + |
|
525 | + |
|
526 | + /** |
|
527 | + * @param bool $return_as_string |
|
528 | + * @return string |
|
529 | + * @throws InvalidFormHandlerException |
|
530 | + */ |
|
531 | + public function displayCurrentStepForm($return_as_string = true) |
|
532 | + { |
|
533 | + if ($return_as_string) { |
|
534 | + return $this->getCurrentStep()->display(); |
|
535 | + } |
|
536 | + echo $this->getCurrentStep()->display(); |
|
537 | + return ''; |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + /** |
|
542 | + * @param array $form_data |
|
543 | + * @return void |
|
544 | + * @throws InvalidArgumentException |
|
545 | + * @throws InvalidDataTypeException |
|
546 | + * @throws InvalidFormHandlerException |
|
547 | + */ |
|
548 | + public function processCurrentStepForm($form_data = array()) |
|
549 | + { |
|
550 | + // grab instance of current step because after calling next() below, |
|
551 | + // any calls to getCurrentStep() will return the "next" step because we advanced |
|
552 | + $current_step = $this->getCurrentStep(); |
|
553 | + try { |
|
554 | + // form processing should either throw exceptions or return true |
|
555 | + $current_step->process($form_data); |
|
556 | + } catch (Exception $e) { |
|
557 | + // something went wrong, convert the Exception to an EE_Error |
|
558 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
559 | + // prevent redirect to next step or other if exception was thrown |
|
560 | + if ($current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP |
|
561 | + || $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER |
|
562 | + ) { |
|
563 | + $current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP); |
|
564 | + } |
|
565 | + } |
|
566 | + // save notices to a transient so that when we redirect back |
|
567 | + // to the display portion for this step |
|
568 | + // those notices can be displayed |
|
569 | + EE_Error::get_notices(false, true); |
|
570 | + $this->redirectForm($current_step); |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * handles where to go to next |
|
576 | + * |
|
577 | + * @param SequentialStepFormInterface $current_step |
|
578 | + * @throws InvalidArgumentException |
|
579 | + * @throws InvalidDataTypeException |
|
580 | + * @throws InvalidFormHandlerException |
|
581 | + */ |
|
582 | + public function redirectForm(SequentialStepFormInterface $current_step) |
|
583 | + { |
|
584 | + $redirect_step = $current_step; |
|
585 | + switch ($current_step->redirectTo()) { |
|
586 | + case SequentialStepForm::REDIRECT_TO_OTHER: |
|
587 | + // going somewhere else, so just check out now |
|
588 | + wp_safe_redirect($redirect_step->redirectUrl()); |
|
589 | + exit(); |
|
590 | + break; |
|
591 | + case SequentialStepForm::REDIRECT_TO_PREV_STEP: |
|
592 | + $redirect_step = $this->form_steps->previous(); |
|
593 | + break; |
|
594 | + case SequentialStepForm::REDIRECT_TO_NEXT_STEP: |
|
595 | + $this->form_steps->next(); |
|
596 | + if ($this->form_steps->valid()) { |
|
597 | + $redirect_step = $this->form_steps->current(); |
|
598 | + } |
|
599 | + break; |
|
600 | + case SequentialStepForm::REDIRECT_TO_CURRENT_STEP: |
|
601 | + default: |
|
602 | + // $redirect_step is already set |
|
603 | + } |
|
604 | + $current_step->setRedirectUrl($this->baseUrl()); |
|
605 | + $current_step->addRedirectArgs( |
|
606 | + // use the slug for whatever step we are redirecting too |
|
607 | + array($this->formStepUrlKey() => $redirect_step->slug()) |
|
608 | + ); |
|
609 | + wp_safe_redirect($current_step->redirectUrl()); |
|
610 | + exit(); |
|
611 | + } |
|
612 | 612 | } |