-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.php
More file actions
396 lines (329 loc) · 11 KB
/
form.php
File metadata and controls
396 lines (329 loc) · 11 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace BestProject\Wordpress;
use BestProject\Wordpress\Object;
defined('ABSPATH') or die;
/**
* Form class.
*/
class Form
{
use Object;
/**
* Name of this form.
*
* @var String
*/
protected $name;
/**
* List of form fields.
*
* @var Array
*/
protected $fields;
/**
* Form method.
*
* @var String
*/
protected $method;
/**
* Array of form tag attributes.
*
* @var Array
*/
protected $attributes;
/**
* Recipient for this form data. It accepts a valid email, name of a form field
* that holds the email or a PostType instance that should hold the data.
* It also accepts array of those values.
*
* Form is processing the recipients and its input on construct if $recipient is not empty.
*
* @var Mixed
*/
protected $recipient;
/**
* Holds list of form errors (mostly validation).
*
* @var Array
*/
protected $errors = array();
/**
* Subject of an e-mail if there are e-mail recipipients for this form.
*
* @var String
*/
protected $mail_subject = '';
/**
* Sender e-mail if there are e-mail recipients for form.
*
* @var String
*/
protected $mail_sender = '';
/**
* Name of the sender if there are e-mail recipients for form.
*
* @var String
*/
protected $mail_sender_name = '';
/**
* Create new Form instance.
*
* @param String $name Name of this form.
* @param Array $fields List of form fields.
* @param Mixed $recipient Recipient for this form data. It accepts a valid email, name of a form field that holds the email or a PostType instance that should hold the data. It also accepts array of those values.
* @param String $mail_subject Subject for a mail if there are e-mail recipients in this form.
* @param String $mail_sender_name Name of a sender if there are e-mail recipients in this form.
*/
public function __construct($name, Array $fields, $recipient = array(),
$mail_subject = '', $mail_sender = '',
$mail_sender_name = '')
{
// Add fields using its name as array key so we can access them easly later
foreach ($fields AS $field) {
$this->fields[$field->get('name')] = $field;
}
// Set default params
$this->set('name', $name);
$this->set('recipient', $recipient);
$this->set('mail_subject', $mail_subject);
$this->set('mail_sender', $mail_sender);
$this->set('mail_sender_name', $mail_sender_name);
// Set basic form attributes
$this->setAttribute('method', 'post');
$this->setAttribute('id', 'form-'.$name);
$this->setAttribute('action',
filter_input(INPUT_SERVER, 'REQUEST_URI').'#form-'.$name);
}
/**
* Get a render list of form attributes like name, action, method.
*/
public function renderFormAttributes()
{
$html = '';
foreach ($this->attributes AS $attribute => $value) {
$html .= $attribute.'="'.htmlentities($value).'" ';
}
return trim($html);
}
/**
* Set a form tag attribute and return form instance for chaining.
*
* @param String $name Attribute name.
* @param String $value Attribute value.
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
* Return selected form attribute.
*
* @param String $name Attribute name to return.
* @param Mixed $default Default value.
* @return String
*/
public function getAttribute($name, $default = '')
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
} else {
return $default;
}
}
/**
* Render a selected form field.
*
* @param String $name Name of a field to render.
* @return String
*/
public function renderField($name)
{
if (isset($this->fields[$name])) {
return $this->fields[$name]->render();
} else {
if (defined('WP_DEBUG') AND WP_DEBUG) {
throw new \Exception('Field '.$name.' was not found in this form.',
500);
}
}
}
/**
* Does this form validate? Check each field if it validates.
*
* @return Boolean
*/
public function validate()
{
// Validate each form field.
$validates = true;
foreach ($this->fields AS $field) {
// Get validation result
$validate_result = $field->validate();
// If this field did not validate add its error message to form errors.
if ($validate_result !== true) {
$this->errors[] = $validate_result;
$validates = false;
}
}
return $validates;
}
/**
* Return errors list.
*
* @return Array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Process form input. Returns:
* TRUE: If every recipient was processed correctly.
* FALSE: If there was a single error while processing the recipients.
* NULL: If there is no data to process.
*/
public function processInput()
{
// Is there anything to process
if (strtoupper($this->attributes['method']) == 'POST' AND empty($_POST)) {
return;
} elseif (strtoupper($this->attributes['method']) == 'GET' AND empty($_GET)) {
return;
}
// Check if form validates
if (!$this->validate()) {
return false;
}
// Holding recipient processing results
$results = array();
// If there is a list providing recipients
if (is_array($this->recipient)) {
// Process each recipient
foreach ($this->recipient AS $recipient) {
$results[] = $this->processRecipient($recipient);
}
// its a single recipient
} else {
$results[] = $this->processRecipient($this->recipient);
}
// Was there any error while processing the recipients list?
if (in_array(false, $results)) {
return false;
// All recipients processed without an error
} elseif (!empty($results)) {
return true;
}
}
/**
* Process selected recipient.
*
* @param $recipient
*/
private function processRecipient($recipient)
{
// Its a PostType
$className = '\\BestProject\\Wordpress\\PostType';
if (is_object($recipient) AND ( $recipient instanceof $className)) {
// TODO: Process PostType as a recipient
//
// Is an object but wrong type
} elseif (is_object($recipient)) {
throw new \Exception('Wrong object provided as a form recipient.');
// String provided
} elseif (is_string($recipient)) {
$recipient = (string) $recipient;
// Is this a valid e-mail
$is_valid_email = !(filter_var($recipient, FILTER_VALIDATE_EMAIL) === false);
// Is this not a valid email but a valid form field name that holds an e-mail
if (!$is_valid_email AND isset($this->fields[$recipient]) AND ! (filter_var($this->fields[$recipient]->getSaveData(),
FILTER_VALIDATE_EMAIL) === false)) {
$recipient = $this->fields[$recipient]->getSaveData();
$is_valid_email = true;
} elseif (!$is_valid_email) {
$is_valid_email = false;
}
// Send e-mail
if ($is_valid_email AND $this->sendEmail($recipient)) {
return true;
} else {
$this->errors[] = Language::_('FORM_COULD_NOT_SEND_EMAIL');
return false;
}
}
}
/**
* Send e-mail with form input to the selected e-mail address.
*
* @param String $recipient_email Recipient e-mail address.
* @return Boolean
*/
private function sendEmail($recipient_email)
{
// Generate an e-mail template file
$path = get_template_directory().'/template-parts/emails/'.$this->name.'.php';
// Default e-mail options
$mail_subject = $this->mail_subject;
$mail_sender = $this->mail_sender;
$mail_sender_name = $this->mail_sender_name;
$mail_recipient = $recipient_email;
// If e-mail template file exists
if (file_exists($path)) {
// Generate e-mail body
ob_start();
require $path;
$mail_body = ob_get_clean();
// There is no e-mail template. Use default view
} else {
// List of field types that should not be processed
$hide_fields = array(
'\\BestProject\Wordpress\\Form\\Field\\Submit',
'\\BestProject\Wordpress\\Form\\Field\\Button',
);
$mail_body = '';
/* @var $field Field */
foreach ($this->fields AS $field) {
if (!in_array(get_class($field), $hide_fields)) {
$mail_body .= '<div>';
$mail_body .= '<h4>'.$field->get('label').'</h4>';
$mail_body .= $field->getSaveData();
$mail_body .= '</div>'."\n";
}
}
}
// If any of required parameters is missing, return failure
if (empty($mail_subject) OR empty($mail_sender) OR empty($mail_sender_name) OR empty($mail_body)) {
return false;
// We have all the data, so send the message
} else {
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
// Add sender name if provided
if (!empty($mail_sender_name)) {
$headers[] = 'From: '.$mail_sender_name.' <'.$mail_sender.'>';
// No sender name, just set an e-mail address
} else {
$headers[] = 'From: '.$mail_sender;
}
// Send email and return its
return mail($mail_recipient, $mail_subject, $mail_body,
implode("\r\n", $headers));
}
return true;
}
/**
* Returns an instance of a selected form field.
*
* @param String $name Name of a field to return.
* @return Field|Boolean
*/
public function &getField($name)
{
// If field with this name was set in this form, return its reference.
if (isset($this->fields[$name])) {
return $this->fields[$name];
}
return false;
}
}