-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceProvider.php
150 lines (138 loc) · 5.03 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Ademti\FormProtection;
use Statamic\Facades\Form;
use Statamic\Forms\Tags;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;
use function __;
class ServiceProvider extends AddonServiceProvider
{
/**
* @return void
*/
public function bootAddon()
{
$this->addSettingToFormForm();
$this->addClassToProtectedForms();
$this->addNoscriptToProtectedForms();
$this->addJavascriptToProtectedForms();
}
/**
* Add our settings to the "Edit form" form.
*
* @return void
*/
private function addSettingToFormForm()
{
Form::appendConfigFields(
'*',
__('Form protection'),
[
'afp_dw_protection' => [
'display' => __('Protect with "David Walsh" javascript technique'),
'type' => 'toggle',
'instructions' => __('Enable to automatically generate an input on submission and check for its presence on validation. Rejects any forms where the input is not present. Requires all form submissions to have Javascript enabled.'),
],
'afp_dw_noscript_location' => [
'display' => __('Where to show "Javascript" requirement'),
'instructions' => __( 'Choose whether to output a <noscript> element in the form advising users that javascript is required, and where it should be output.'),
'type' => 'select',
'options' => [
[
'key' => 'start',
'value' => __('Start of form'),
],
[
'key' => 'end',
'value' => __('End of form'),
],
[
'key' => 'hidden',
'value' => __('Hidden')
],
],
'default' => 'start',
]
]
);
}
/**
* @return void
*/
private function addClassToProtectedForms()
{
$serviceProvider = $this;
Tags::hook('attrs', function (array $payload, $next) use ($serviceProvider) {
if ( ! $serviceProvider->isFormProtected($payload)) {
return $next($payload);
}
if ( ! empty($payload['attrs']['class'])) {
$payload['attrs']['class'] .= ' afp-form';
} else {
$payload['attrs']['class'] = 'afp-form';
}
return $next($payload);
});
}
/**
* @return void
*/
private function addNoscriptToProtectedForms()
{
$serviceProvider = $this;
Tags::hook('after-open', function (array $payload, $next) use ($serviceProvider) {
if ( ! $serviceProvider->isFormProtected($payload)) {
return $next($payload);
}
$location = $payload['data']['form_config']['afp_dw_noscript_location'] ?? 'start';
if ($location !== 'start') {
return $next($payload);
}
$payload['html'] .= <<<HTML
<noscript><div class="afp-noscript-container"><p><strong>Please enable JavaScript in your browser to complete this form.</strong></p></div></noscript>
HTML;
return $next($payload);
});
Tags::hook('before-close', function (array $payload, $next) use ($serviceProvider) {
if ( ! $serviceProvider->isFormProtected($payload)) {
return $next($payload);
}
$location = $payload['data']['form_config']['afp_dw_noscript_location'] ?? 'start';
if ($location !== 'end') {
return $next($payload);
}
$payload['html'] .= <<<HTML
<noscript><div class="afp-noscript-container"><p><strong>Please enable JavaScript in your browser to complete this form.</strong></p></div></noscript>
HTML;
return $next($payload);
});
}
/**
* @return void
*/
private function addJavascriptToProtectedForms()
{
$serviceProvider = $this;
Tags::hook('before-close', function (array $payload, $next) use ($serviceProvider) {
if ( ! $serviceProvider->isFormProtected($payload)) {
return $next($payload);
}
$scripts = Statamic::availableScripts(request());
if (isset($scripts['afp_dw_script'])) {
return $next($payload);
}
$payload['html'] .= '<script>' . file_get_contents(__DIR__ . '/../js/afp_dw.js') . '</script>';
return $next($payload);
});
}
/**
* @param array $payload
*
* @return bool
*/
public function isFormProtected(array $payload)
{
return isset($payload['data']['form_config']['afp_dw_protection']) &&
$payload['data']['form_config']['afp_dw_protection'];
}
}