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