-
Notifications
You must be signed in to change notification settings - Fork 2
/
hectane_settings.php
195 lines (176 loc) · 5.06 KB
/
hectane_settings.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
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
<?php
/**
* Manage settings for the plugin including the page that allows the connection
* settings to be configured.
*/
class HectaneSettings {
/**
* Default values for all options.
*/
private static $defaults = array(
'host' => 'localhost',
'port' => '8025',
'tls' => '0',
'tls_ignore' => '0',
'username' => '',
'password' => '',
);
/**
* Obtain the current value for the specified option.
*/
public static function get($name) {
$o = get_option('hectane_settings');
return isset($o[$name]) ? $o[$name] : self::$defaults[$name];
}
/**
* Initialize the plugin by setting up hooks.
*/
public function __construct() {
add_action('admin_init', array($this, 'register_settings'));
add_action('admin_menu', array($this, 'add_options_page'));
}
/**
* Register the section and individual settings.
*/
public function register_settings() {
// Connection settings
add_settings_section(
'hectane_connection_settings',
'Connection Settings',
array($this, 'connection_settings_callback'),
'hectane'
);
add_settings_field(
'host',
'Host',
array($this, 'text_field_callback'),
'hectane',
'hectane_connection_settings',
array('host')
);
add_settings_field(
'port',
'Port',
array($this, 'text_field_callback'),
'hectane',
'hectane_connection_settings',
array('port')
);
add_settings_field(
'tls',
'Use TLS',
array($this, 'checkbox_field_callback'),
'hectane',
'hectane_connection_settings',
array('tls')
);
// Authentication settings
add_settings_section(
'hectane_auth_settings',
'Authentication Settings',
array($this, 'auth_settings_callback'),
'hectane'
);
add_settings_field(
'username',
'Username',
array($this, 'text_field_callback'),
'hectane',
'hectane_auth_settings',
array('username')
);
add_settings_field(
'password',
'Password',
array($this, 'text_field_callback'),
'hectane',
'hectane_auth_settings',
array('password')
);
// Behavior settings
add_settings_section(
'hectane_behavior_settings',
'Behavior Settings',
array($this, 'behavior_settings_callback'),
'hectane'
);
add_settings_field(
'tls_ignore',
'Ignore TLS errors',
array($this, 'checkbox_field_callback'),
'hectane',
'hectane_behavior_settings',
array('tls_ignore')
);
register_setting(
'hectane_settings',
'hectane_settings'
);
}
/**
* Render the label for the connection section.
*/
public function connection_settings_callback() {
echo '<p>These settings are used to connect to the Hectane client.</p>';
}
/**
* Render the label for the auth section.
*/
public function auth_settings_callback() {
echo '<p>These settings are used to authenticate with the Hectane server.</p>';
}
/**
* Render the label for the behavior section.
*/
public function behavior_settings_callback() {
echo '<p>These settings are used to adjust behavior during transmission.</p>';
}
/**
* Display the specified input field.
*/
public function text_field_callback($args) {
printf(
'<input type="text" id="%s" name="hectane_settings[%s]" value="%s">',
esc_attr($args[0]),
esc_attr($args[0]),
esc_attr(self::get($args[0]))
);
}
/**
* Display the specified checkbox field.
*/
public function checkbox_field_callback($args) {
printf(
'<input type="checkbox" id="%s" name="hectane_settings[%s]" %s>',
esc_attr($args[0]),
esc_attr($args[0]),
self::get($args[0]) ? 'checked="checked"' : ''
);
}
/**
* Add the options page to the admin menu.
*/
public function add_options_page() {
add_options_page(
'Hectane Options',
'Hectane',
'manage_options',
'hectane',
array($this, 'display_options_page')
);
}
/**
* Display the page containing the options form.
*/
public function display_options_page() {
echo '<div class="wrap">';
echo '<h2>Hectane Options</h2>';
echo '<form method="post" action="options.php">';
settings_fields('hectane_settings');
do_settings_sections('hectane');
submit_button();
echo '</form>';
echo '</div>';
}
}
?>