1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Field; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Form; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partial; |
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Session; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
15
|
|
|
|
16
|
|
|
class SiteReviewsForm |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $args; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $errors; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $message; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $required; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $values; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void|string |
45
|
|
|
*/ |
46
|
|
|
public function build( array $args = [] ) |
47
|
|
|
{ |
48
|
|
|
$this->args = $args; |
49
|
|
|
$this->errors = glsr( Session::class )->get( $args['id'].'errors', [], true ); |
50
|
|
|
$this->message = glsr( Session::class )->get( $args['id'].'message', '', true ); |
51
|
|
|
$this->required = glsr( OptionManager::class )->get( 'settings.submissions.required', [] ); |
52
|
|
|
$this->values = glsr( Session::class )->get( $args['id'].'values', [], true ); |
53
|
|
|
$fields = array_reduce( $this->getFields(), function( $carry, $field ) { |
54
|
|
|
return $carry.$field; |
55
|
|
|
}); |
56
|
|
|
return glsr( Template::class )->build( 'templates/reviews-form', [ |
57
|
|
|
'context' => [ |
58
|
|
|
'class' => $this->getClass(), |
59
|
|
|
'fields' => $fields, |
60
|
|
|
'id' => $this->args['id'], |
61
|
|
|
'response' => $this->buildResponse(), |
62
|
|
|
'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
63
|
|
|
], |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void|string |
69
|
|
|
*/ |
70
|
|
|
protected function buildRecaptcha() |
71
|
|
|
{ |
72
|
|
|
$integration = glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' ); |
73
|
|
|
$recaptchaMethod = glsr( Helper::class )->buildMethodName( $integration, 'getRecaptcha' ); |
74
|
|
|
if( method_exists( $this, $recaptchaMethod )) { |
75
|
|
|
return $this->$recaptchaMethod(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function buildResponse() |
83
|
|
|
{ |
84
|
|
|
$classes = !empty( $this->errors ) |
85
|
|
|
? 'glsr-has-errors' |
86
|
|
|
: ''; |
87
|
|
|
return glsr( Template::class )->build( 'templates/form/response', [ |
88
|
|
|
'context' => [ |
89
|
|
|
'class' => $classes, |
90
|
|
|
'message' => wpautop( $this->message ), |
|
|
|
|
91
|
|
|
], |
92
|
|
|
'has_errors' => !empty( $this->errors ), |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function buildSubmitButton() |
100
|
|
|
{ |
101
|
|
|
return glsr( Template::class )->build( 'templates/form/submit-button', [ |
102
|
|
|
'context' => [ |
103
|
|
|
'text' => __( 'Submit your review', 'site-reviews' ), |
104
|
|
|
], |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getClass() |
112
|
|
|
{ |
113
|
|
|
return trim( 'glsr-form glsr-'.glsr( Style::class )->get().' '.$this->args['class'] ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function getFields() |
120
|
|
|
{ |
121
|
|
|
$fields = array_merge( |
122
|
|
|
$this->getHiddenFields(), |
123
|
|
|
[$this->getHoneypotField()], |
124
|
|
|
$this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' )) |
125
|
|
|
); |
126
|
|
|
return $fields; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function getHiddenFields() |
133
|
|
|
{ |
134
|
|
|
$fields = [[ |
135
|
|
|
'name' => 'action', |
136
|
|
|
'value' => 'submit-review', |
137
|
|
|
],[ |
138
|
|
|
'name' => 'assign_to', |
139
|
|
|
'value' => $this->args['assign_to'], |
140
|
|
|
],[ |
141
|
|
|
'name' => 'category', |
142
|
|
|
'value' => $this->args['category'], |
143
|
|
|
],[ |
144
|
|
|
'name' => 'excluded', |
145
|
|
|
'value' => $this->args['excluded'], // @todo should default to "[]" |
146
|
|
|
],[ |
147
|
|
|
'name' => 'form_id', |
148
|
|
|
'value' => $this->args['id'], |
149
|
|
|
],[ |
150
|
|
|
'name' => 'nonce', |
151
|
|
|
'value' => wp_create_nonce( 'submit-review' ), |
152
|
|
|
],[ |
153
|
|
|
'name' => 'referer', |
154
|
|
|
'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' )), |
155
|
|
|
]]; |
156
|
|
|
return array_map( function( $field ) { |
157
|
|
|
return new Field( wp_parse_args( $field, ['type' => 'hidden'] )); |
158
|
|
|
}, $fields ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return Field |
163
|
|
|
*/ |
164
|
|
|
protected function getHoneypotField() |
165
|
|
|
{ |
166
|
|
|
return new Field([ |
167
|
|
|
'name' => 'gotcha', |
168
|
|
|
'type' => 'honeypot', |
169
|
|
|
]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function getRecaptchaCustom() |
176
|
|
|
{ |
177
|
|
|
return glsr( Builder::class )->div([ |
178
|
|
|
'class' => 'glsr-recaptcha-holder', |
179
|
|
|
'data-badge' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.position' )), |
180
|
|
|
'data-sitekey' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.key' )), |
181
|
|
|
'data-size' => 'invisible', |
182
|
|
|
]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
protected function getRecaptchaInvisibleRecaptcha() |
189
|
|
|
{ |
190
|
|
|
ob_start(); |
191
|
|
|
do_action( 'google_invre_render_widget_action' ); |
192
|
|
|
$html = ob_get_clean(); |
193
|
|
|
return glsr( Builder::class )->div( $html, [ |
194
|
|
|
'class' => 'glsr-recaptcha-holder', |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
protected function normalizeFieldErrors( Field &$field ) |
202
|
|
|
{ |
203
|
|
|
if( !array_key_exists( $field->field['path'], $this->errors ))return; |
204
|
|
|
$field->field['errors'] = $this->errors[$field->field['path']]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
protected function normalizeFieldRequired( Field &$field ) |
211
|
|
|
{ |
212
|
|
|
if( !in_array( $field->field['path'], $this->required ))return; |
213
|
|
|
$field->field['required'] = true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
protected function normalizeFields( $fields ) |
220
|
|
|
{ |
221
|
|
|
foreach( $fields as &$field ) { |
222
|
|
|
$field->field['is_public'] = true; |
223
|
|
|
$this->normalizeFieldErrors( $field ); |
224
|
|
|
$this->normalizeFieldRequired( $field ); |
225
|
|
|
$this->normalizeFieldValue( $field ); |
226
|
|
|
} |
227
|
|
|
return $fields; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
protected function normalizeFieldValue( Field &$field ) |
234
|
|
|
{ |
235
|
|
|
if( !array_key_exists( $field->field['path'], $this->values ))return; |
236
|
|
|
if( in_array( $field->field['type'], ['radio', 'checkbox'] )) { |
237
|
|
|
$field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
238
|
|
|
} |
239
|
|
|
else { |
240
|
|
|
$field->field['value'] = $this->values[$field->field['path']]; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|