|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\FieldContract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
7
|
|
|
|
|
8
|
|
|
class MetaboxForm extends Form |
|
9
|
|
|
{ |
|
10
|
|
|
public Review $review; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(Review $review, array $args = []) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->review = $review; |
|
15
|
|
|
parent::__construct($args, array_merge( |
|
16
|
|
|
$review->toArray(), |
|
17
|
|
|
$review->custom()->toArray(), |
|
18
|
|
|
)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This builds all fields but it does not build the form element. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function build(): string |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->buildFields(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function config(): array |
|
30
|
|
|
{ |
|
31
|
|
|
$config = glsr()->config('forms/metabox-fields'); |
|
32
|
|
|
if (2 > count(glsr()->retrieveAs('array', 'review_types'))) { |
|
33
|
|
|
unset($config['type']); |
|
34
|
|
|
} |
|
35
|
|
|
foreach ($config as $key => $values) { |
|
36
|
|
|
$value = $this->session->values[$key] ?? ''; |
|
37
|
|
|
if (is_array($value)) { |
|
38
|
|
|
$value = wp_json_encode($value); |
|
39
|
|
|
} |
|
40
|
|
|
$config[$key] = wp_parse_args($values, [ |
|
41
|
|
|
'data-value' => esc_attr($value), |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
return $config; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function fieldClass(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return MetaboxField::class; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function buildFields(): string |
|
53
|
|
|
{ |
|
54
|
|
|
$fields = []; |
|
55
|
|
|
foreach ($this->hidden() as $field) { |
|
56
|
|
|
$fields[] = $field->build(); |
|
57
|
|
|
} |
|
58
|
|
|
foreach ($this->visible() as $field) { |
|
59
|
|
|
$fields[] = $field->build(); |
|
60
|
|
|
} |
|
61
|
|
|
$rendered = implode("\n", $fields); |
|
62
|
|
|
return $rendered; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Normalize the field with the form's session data. |
|
67
|
|
|
* Any normalization that is not specific to the form or session data |
|
68
|
|
|
* should be done in the field itself. |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function normalizeField(FieldContract $field): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->normalizeFieldChecked($field); |
|
73
|
|
|
$this->normalizeFieldDisabled($field); |
|
74
|
|
|
$this->normalizeFieldValue($field); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the disabled attribute of the field from the \WP_Screen action. |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function normalizeFieldDisabled(FieldContract $field): void |
|
81
|
|
|
{ |
|
82
|
|
|
$field->disabled = 'add' !== glsr_current_screen()->action |
|
83
|
|
|
&& !wp_doing_ajax(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function signForm(): void |
|
87
|
|
|
{ |
|
88
|
|
|
// the form does not need to be signed. |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|