|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Admin\Component\SettingsField; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Admin\Component\SettingsFieldInterface; |
|
6
|
|
|
use Leonidas\Library\Admin\Abstracts\CanBeRestrictedTrait; |
|
7
|
|
|
use Leonidas\Library\Admin\Component\SettingsField\Traits\HasSettingsFieldDataTrait; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use WebTheory\Html\Html; |
|
10
|
|
|
use WebTheory\HttpPolicy\ServerRequestPolicyInterface; |
|
11
|
|
|
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface; |
|
12
|
|
|
use WebTheory\Saveyour\Contracts\Formatting\DataFormatterInterface; |
|
13
|
|
|
use WebTheory\Saveyour\Field\Type\Text; |
|
14
|
|
|
|
|
15
|
|
|
class SettingsField implements SettingsFieldInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use CanBeRestrictedTrait; |
|
18
|
|
|
use HasSettingsFieldDataTrait; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
string $setting, |
|
22
|
|
|
string $id, |
|
23
|
|
|
string $title, |
|
24
|
|
|
string $page, |
|
25
|
|
|
string $section, |
|
26
|
|
|
?string $inputId = null, |
|
27
|
|
|
?array $args = null, |
|
28
|
|
|
?FormFieldInterface $input = null, |
|
29
|
|
|
?DataFormatterInterface $formatter = null, |
|
30
|
|
|
?ServerRequestPolicyInterface $policy = null |
|
31
|
|
|
) { |
|
32
|
|
|
$this->id = $id; |
|
33
|
|
|
$this->title = $title; |
|
34
|
|
|
$this->page = $page; |
|
35
|
|
|
$this->section = $section; |
|
36
|
|
|
$this->setting = $setting; |
|
37
|
|
|
|
|
38
|
|
|
$this->inputId = $inputId ?? $this->id; |
|
39
|
|
|
|
|
40
|
|
|
$args && $this->args = $args; |
|
41
|
|
|
$input && $this->input = $input; |
|
42
|
|
|
$formatter && $this->formatter = $formatter; |
|
43
|
|
|
$policy && $this->policy = $policy; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function renderComponent(ServerRequestInterface $request): string |
|
47
|
|
|
{ |
|
48
|
|
|
$output = ''; |
|
49
|
|
|
$settingData = $this->getSettingData(); |
|
50
|
|
|
$value = get_option($this->getSetting(), $settingData['default'] ?? null); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$output .= ($this->input ?? $this->getDefaultInput()) |
|
53
|
|
|
->setName($this->getSetting()) |
|
54
|
|
|
->setValue($this->formatValue($value)) |
|
55
|
|
|
->setId($this->getInputId()) |
|
56
|
|
|
->toHtml() . "\n"; |
|
57
|
|
|
|
|
58
|
|
|
if ($description = $settingData['description'] ?? false) { |
|
59
|
|
|
$output .= $this->renderDescription($description) . "\n"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $output; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getSettingData(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return get_registered_settings()[$this->getSetting()]; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function getDefaultInput(): FormFieldInterface |
|
71
|
|
|
{ |
|
72
|
|
|
$input = new Text(); |
|
73
|
|
|
$input->addClass('regular-text'); |
|
74
|
|
|
|
|
75
|
|
|
return $input; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function renderDescription($description) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->args['description_attr']['class'][] = 'description'; |
|
81
|
|
|
|
|
82
|
|
|
return Html::tag('p', [$this->args['description_attr']], $description); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function formatValue($value) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->formatter ? $this->formatter->formatData($value) : $value; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|