-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
263 lines (238 loc) · 9.16 KB
/
settings.php
File metadata and controls
263 lines (238 loc) · 9.16 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
<?php
/**
* Custom option and settings:
* - callback functions
*/
/**
* initlab_field_text_template callback function.
*
* WordPress has magic interaction with the following keys: label_for, class.
* - the "label_for" key value is used for the "for" attribute of the <label>.
* - the "class" key value is used for the "class" attribute of the <tr> containing the field.
* Note: you can add custom key value pairs to be used inside your callbacks.
*
* @param array $args
* @param string $size
*/
function initlab_field_text_template($args, $size = 24) {
// Get the value of the setting we've registered with register_setting()
$options = get_option('initlab_options', []);
?>
<input type="text" size="<?php echo strval($size); ?>" id="<?php echo esc_attr($args['label_for']); ?>" name="initlab_options[<?php echo esc_attr($args['label_for']); ?>]" value="<?php echo esc_attr(array_key_exists($args['label_for'], $options) ? $options[$args['label_for']] : '')?>">
<?php
}
/**
* Mailman token section callback function.
*
* @param array $args The settings array, defining title, id, callback.
*/
function initlab_section_mailman_token_callback($args) {
?>
<p><?php _e('Please input the correct settings so the [initlab_mailman_token] shortcode can operate properly.', 'initlab-addons'); ?></p>
<?php
}
/**
* initlab_field_mailman_secret callback function.
*
* WordPress has magic interaction with the following keys: label_for, class.
* - the "label_for" key value is used for the "for" attribute of the <label>.
* - the "class" key value is used for the "class" attribute of the <tr> containing the field.
* Note: you can add custom key value pairs to be used inside your callbacks.
*
* @param array $args
*/
function initlab_field_mailman_version_cb($args) {
// Get the value of the setting we've registered with register_setting()
$options = get_option('initlab_options', []);
?>
<select id="<?php echo esc_attr($args['label_for']); ?>" name="initlab_options[<?php echo esc_attr($args['label_for']); ?>]">
<?php
foreach ($args['options'] as $value => $label) {
?>
<option value="<?php echo esc_attr($value)?>" <?php echo array_key_exists($args['label_for'], $options) ? selected($options[$args['label_for']], $value, false) : ''; ?>>
<?php esc_html_e($label, 'initlab-addons'); ?>
</option>
<?php
}
?>
</select>
<?php
}
function initlab_field_mailman_secret_cb($args) {
initlab_field_text_template($args, 64);
}
/**
* Discord section callback function.
*
* @param array $args The settings array, defining title, id, callback.
*/
function initlab_section_discord_callback($args) {
?>
<p><?php _e('In order to integrate with Discord, you need a bot that\'s already added to your server with the <strong>MANAGE_EVENTS</strong> permission.', 'initlab-addons'); ?></p>
<?php
}
function initlab_field_discord_guild_id_cb($args) {
initlab_field_text_template($args, 24);
}
function initlab_field_discord_bot_token_cb($args) {
initlab_field_text_template($args, 96);
}
function initlab_field_discord_stage_channel_id_cb($args) {
initlab_field_text_template($args, 24);
}
function initlab_field_discord_invite_url_cb($args) {
initlab_field_text_template($args, 32);
}
/**
* custom option and settings
*/
function initlab_settings_init() {
// Register a new setting for "initlab" page.
register_setting('initlab', 'initlab_options');
// Register a new section in the "initlab" page.
add_settings_section(
'initlab_section_mailman_token',
__('Mailman token shortcode settings', 'initlab-addons'),
'initlab_section_mailman_token_callback',
'initlab'
);
// Register a new field in the "initlab_section_mailman_token" section, inside the "initlab" page.
add_settings_field(
'initlab_field_mailman_version', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('Mailman version', 'initlab-addons'),
'initlab_field_mailman_version_cb',
'initlab',
'initlab_section_mailman_token',
[
'label_for' => 'mailman_version',
'options' => [
'2.1.16' => '2.1.16 - 2.1.20',
'2.1.21' => '2.1.21 - 2.1.26',
'2.1.27' => '2.1.27 - 2.1.29',
'2.1.30' => '2.1.30+',
],
]
);
// Register a new field in the "initlab_section_mailman_token" section, inside the "initlab" page.
add_settings_field(
'initlab_field_mailman_secret', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('SUBSCRIBE_FORM_SECRET', 'initlab-addons'),
'initlab_field_mailman_secret_cb',
'initlab',
'initlab_section_mailman_token',
[
'label_for' => 'mailman_secret',
]
);
// Register a new section in the "initlab" page.
add_settings_section(
'initlab_section_discord',
__('Discord settings', 'initlab-addons'),
'initlab_section_discord_callback',
'initlab'
);
// Register a new field in the "initlab_section_discord" section, inside the "initlab" page.
add_settings_field(
'initlab_field_discord_guild_id', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('Guild ID (Server ID)', 'initlab-addons'),
'initlab_field_discord_guild_id_cb',
'initlab',
'initlab_section_discord',
[
'label_for' => 'discord_guild_id',
]
);
// Register a new field in the "initlab_section_discord" section, inside the "initlab" page.
add_settings_field(
'initlab_field_discord_bot_token', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('Bot token', 'initlab-addons'),
'initlab_field_discord_bot_token_cb',
'initlab',
'initlab_section_discord',
[
'label_for' => 'discord_bot_token',
]
);
// Register a new field in the "initlab_section_discord" section, inside the "initlab" page.
add_settings_field(
'initlab_field_discord_stage_channel_id', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('Stage channel ID', 'initlab-addons'),
'initlab_field_discord_stage_channel_id_cb',
'initlab',
'initlab_section_discord',
[
'label_for' => 'discord_stage_channel_id',
]
);
// Register a new field in the "initlab_section_discord" section, inside the "initlab" page.
add_settings_field(
'initlab_field_discord_invite_url', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__('Invite URL', 'initlab-addons'),
'initlab_field_discord_invite_url_cb',
'initlab',
'initlab_section_discord',
[
'label_for' => 'discord_invite_url',
]
);
}
/**
* Register our initlab_settings_init to the admin_init action hook.
*/
add_action('admin_init', 'initlab_settings_init');
/**
* Top level menu callback function
*/
function initlab_options_page_html() {
// check user capabilities
if (!current_user_can('manage_options')) {
return;
}
// add error/update messages
// check if the user have submitted the settings
// WordPress will add the "settings-updated" $_GET parameter to the url
if (isset($_GET['settings-updated'])) {
// add settings saved message with the class of "updated"
add_settings_error('initlab_messages', 'initlab_message', __('Settings saved.', 'initlab-addons'), 'updated');
}
// show error/update messages
settings_errors('initlab_messages');
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "initlab"
settings_fields('initlab');
// output setting sections and their fields
// (sections are registered for "initlab", each field is registered to a specific section)
do_settings_sections('initlab');
// output save settings button
submit_button();
?>
</form>
</div>
<?php
}
/**
* Add the top level menu page.
*/
function initlab_options_page() {
add_menu_page(
__('init Lab Options', 'initlab-addons'),
__('init Lab', 'initlab-addons'),
'manage_options',
'initlab',
'initlab_options_page_html'
);
}
/**
* Register our initlab_options_page to the admin_menu action hook.
*/
add_action('admin_menu', 'initlab_options_page');