-
Notifications
You must be signed in to change notification settings - Fork 4
/
OptionsSection.php
153 lines (133 loc) · 4.22 KB
/
OptionsSection.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
151
152
153
<?php
namespace Laraish\Options;
use Laraish\Contracts\Options\OptionsPage as OptionsPageContract;
use Laraish\Contracts\Options\OptionsSection as OptionsSectionContract;
use Laraish\Contracts\Options\OptionsField as OptionsFieldContract;
use Laraish\Support\Traits\ClassHelper;
class OptionsSection implements OptionsSectionContract
{
use ClassHelper;
/**
* ID of the section.
* @var string
*/
private $id;
/**
* Title of the section.
* @var string
*/
private $title;
/**
* The capability required for this section to be displayed to the current user.
* @var string
*/
private $capability = 'manage_options';
/**
* Description of the section.
* @var string
*/
private $description;
/**
* settings fields in the section.
* @var array
*/
private $fields;
/**
* Function that fills the section with the desired content. The function should echo its output.
* @var callable
*/
private $renderFunction;
/**
* OptionsSection constructor.
*
* @param array $configs
*/
public function __construct(array $configs)
{
$requiredOptions = ['id', 'title'];
$acceptableOptions = array_merge($requiredOptions, ['description', 'renderFunction', 'fields', 'capability']);
$this->convertMapToProperties($configs, $acceptableOptions, $requiredOptions, function ($option) {
return "The option `$option` must be defined when instantiate the class `" . static::class . "`.";
});
}
/**
* Register the section to a specific page.
*
* @param OptionsPageContract|string $optionsPage menu-slug of a page or a OptionsPage object
* @param string $optionGroup The option-group to be used.
* @param string $optionName The option-group to be used.
* @param bool $hook Determine if call register functions in appropriate hook or not.
*
* @return void
*/
final public function register($optionsPage, $optionGroup, $optionName, $hook = true)
{
// check user capabilities
if ( ! current_user_can($this->capability())) {
return;
}
$page = $optionsPage instanceof OptionsPageContract ? $optionsPage->menuSlug() : $optionsPage;
$register = function ($page, OptionsSectionContract $section, $optionGroup, $optionName) {
add_settings_section($section->id(), $section->title(), [$section, 'render'], $page);
if (isset($this->fields)) {
foreach ($this->fields as &$field) {
if ( ! $field instanceof OptionsFieldContract) {
$field = new OptionsField($field);
}
$field->register($page, $section->id(), $optionGroup, $optionName, false);
}
}
};
if ($hook) {
add_action('admin_init', function () use ($page, $register, $optionGroup, $optionName) {
$register($page, $this, $optionGroup, $optionName);
});
} else {
$register($page, $this, $optionGroup, $optionName);
}
}
/**
* The id of the section.
* @return string
*/
final public function id()
{
return $this->id;
}
/**
* Title of the section.
* @return string
*/
final public function title()
{
return $this->title;
}
/**
* The capability required for this field to be displayed to the current user.
* @return string
*/
public function capability()
{
return $this->capability;
}
/**
* The description of the section.
* @return string
*/
final public function description()
{
return $this->description;
}
/**
* Function that fills the section with the desired content. The function should echo its output.
* @return void
*/
public function render()
{
if (isset($this->renderFunction)) {
call_user_func_array($this->renderFunction, [$this]);
} elseif ($this->description()) {
echo '<p>' . $this->description() . '</p>';
}
}
}