|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the WPSymfonyForm plugin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LIN3S\WPSymfonyForm\Ajax; |
|
13
|
|
|
|
|
14
|
|
|
use LIN3S\WPSymfonyForm\Controller\AjaxController; |
|
15
|
|
|
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Form submit ajax class. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Gorka Laucirica <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class FormSubmitAjax |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The form wrapper registry. |
|
26
|
|
|
* |
|
27
|
|
|
* @var FormWrapperRegistry |
|
28
|
|
|
*/ |
|
29
|
|
|
private $formWrapperRegistry; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry |
|
35
|
|
|
* @param string $action Action used by WordPress to identify this AJAX request |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(FormWrapperRegistry $formWrapperRegistry, $action = 'form_submit') |
|
38
|
|
|
{ |
|
39
|
|
|
add_action('wp_ajax_nopriv_' . $action, [$this, 'ajax']); |
|
|
|
|
|
|
40
|
|
|
add_action('wp_ajax_' . $action, [$this, 'ajax']); |
|
|
|
|
|
|
41
|
|
|
$this->formWrapperRegistry = $formWrapperRegistry; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Call to be executed on AJAX request. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function ajax() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
try { |
|
50
|
|
|
unset($_POST['action']); |
|
51
|
|
|
if (count($_POST) !== 1) { |
|
52
|
|
|
throw new \InvalidArgumentException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$formType = key($_POST); |
|
56
|
|
|
$formWrapper = $this->formWrapperRegistry->get($formType); |
|
57
|
|
|
echo(new AjaxController())->ajaxAction($formWrapper); |
|
58
|
|
|
die(); |
|
|
|
|
|
|
59
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
60
|
|
|
echo json_encode(['errors' => ['unknown form_type']]); |
|
61
|
|
|
http_response_code(400); |
|
62
|
|
|
die(); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|