-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
273 lines (241 loc) · 7.41 KB
/
dashboard.php
File metadata and controls
273 lines (241 loc) · 7.41 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
264
265
266
267
268
269
270
271
272
273
<?php
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
/**
* Add menu page
*/
function sucf_add_admin_menu() {
add_menu_page(
'Simple Contact Form',
'Contact Form',
'manage_options',
'sucf-settings',
'sucf_render_settings_page',
'dashicons-email',
58
);
}
add_action('admin_menu', 'sucf_add_admin_menu');
/**
* Render settings page
*/
function sucf_render_settings_page() {
?>
<div class="wrap">
<h1>Simple Contact Form Settings</h1>
<div class="sucf-shortcode-note" style="padding: 15px; background: #f1f5f9; border-left: 4px solid #0073aa; margin-bottom: 20px; border-radius: 3px;">
<strong>Shortcode:</strong> <code>[sucf_form]</code><br>
You can use this shortcode on any page, post, or widget to display the contact form.
Optional attributes: <code>show_name, show_email, show_phone, show_message, button_text, layout, columns, gap</code>
</div>
<form method="post" action="options.php">
<?php
settings_fields('sucf_settings_group');
do_settings_sections('sucf_settings_page');
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register settings
*/
function sucf_register_settings() {
register_setting(
'sucf_settings_group',
'sucf_settings',
'sucf_sanitize_settings'
);
/* ---------------- Email Section ---------------- */
add_settings_section(
'sucf_email_section',
'Email & Recipient',
'__return_false',
'sucf_settings_page'
);
add_settings_field(
'recipient_user',
'Send To',
'sucf_recipient_user_field',
'sucf_settings_page',
'sucf_email_section'
);
add_settings_field(
'admin_phone',
'Admin Phone Number',
'sucf_admin_phone_field',
'sucf_settings_page',
'sucf_email_section'
);
add_settings_field(
'from_name',
'From Name',
'sucf_from_name_field',
'sucf_settings_page',
'sucf_email_section'
);
add_settings_field(
'reply_to',
'Reply-To Email',
'sucf_reply_to_field',
'sucf_settings_page',
'sucf_email_section'
);
/* ---------------- Messages Section ---------------- */
add_settings_section(
'sucf_messages_section',
'Messages',
'__return_false',
'sucf_settings_page'
);
add_settings_field(
'success_message',
'Success Message',
'sucf_success_message_field',
'sucf_settings_page',
'sucf_messages_section'
);
add_settings_field(
'error_message',
'Error Message',
'sucf_error_message_field',
'sucf_settings_page',
'sucf_messages_section'
);
/* ---------------- Spam Section ---------------- */
add_settings_section(
'sucf_spam_section',
'Spam Protection',
'__return_false',
'sucf_settings_page'
);
add_settings_field(
'enable_honeypot',
'Enable Honeypot',
'sucf_honeypot_field',
'sucf_settings_page',
'sucf_spam_section'
);
/* ---------------- Logging ---------------- */
add_settings_section(
'sucf_logging_section',
'Logging',
'__return_false',
'sucf_settings_page'
);
add_settings_field(
'enable_logging',
'Enable Submission Logging',
'sucf_logging_field',
'sucf_settings_page',
'sucf_logging_section'
);
}
add_action('admin_init', 'sucf_register_settings');
/* =====================================================
FIELD RENDERERS
===================================================== */
function sucf_recipient_user_field() {
$options = get_option('sucf_settings', []);
$admins = get_users(['role' => 'administrator']);
?>
<select name="sucf_settings[recipient_user]">
<option value="">Default Site Admin Email</option>
<?php foreach ($admins as $admin): ?>
<option value="<?php echo esc_attr($admin->ID); ?>"
<?php selected($options['recipient_user'] ?? '', $admin->ID); ?>>
<?php echo esc_html($admin->display_name . ' (' . $admin->user_email . ')'); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description">Select which admin receives all form messages.</p>
<?php
}
function sucf_admin_phone_field() {
$options = get_option('sucf_settings', []);
?>
<input type="text"
name="sucf_settings[admin_phone]"
value="<?php echo esc_attr($options['admin_phone'] ?? ''); ?>"
class="regular-text">
<p class="description">Optional phone number for internal reference.</p>
<?php
}
function sucf_from_name_field() {
$options = get_option('sucf_settings', []);
?>
<input type="text"
name="sucf_settings[from_name]"
value="<?php echo esc_attr($options['from_name'] ?? get_bloginfo('name')); ?>"
class="regular-text">
<?php
}
function sucf_reply_to_field() {
$options = get_option('sucf_settings', []);
?>
<input type="email"
name="sucf_settings[reply_to]"
value="<?php echo esc_attr($options['reply_to'] ?? ''); ?>"
class="regular-text">
<?php
}
function sucf_success_message_field() {
$options = get_option('sucf_settings', []);
?>
<input type="text"
name="sucf_settings[success_message]"
value="<?php echo esc_attr($options['success_message'] ?? 'Thank you! Your message has been sent.'); ?>"
class="regular-text">
<?php
}
function sucf_error_message_field() {
$options = get_option('sucf_settings', []);
?>
<input type="text"
name="sucf_settings[error_message]"
value="<?php echo esc_attr($options['error_message'] ?? 'Something went wrong. Please try again.'); ?>"
class="regular-text">
<?php
}
function sucf_honeypot_field() {
$options = get_option('sucf_settings', []);
?>
<label>
<input type="checkbox"
name="sucf_settings[enable_honeypot]"
value="1"
<?php checked(1, $options['enable_honeypot'] ?? 0); ?>>
Enable invisible honeypot protection
</label>
<?php
}
function sucf_logging_field() {
$options = get_option('sucf_settings', []);
?>
<label>
<input type="checkbox"
name="sucf_settings[enable_logging]"
value="1"
<?php checked(1, $options['enable_logging'] ?? 0); ?>>
Save submissions in database
</label>
<?php
}
/* =====================================================
SANITIZATION
===================================================== */
function sucf_sanitize_settings($input) {
return [
'recipient_user' => absint($input['recipient_user'] ?? 0),
'admin_phone' => sanitize_text_field($input['admin_phone'] ?? ''),
'from_name' => sanitize_text_field($input['from_name'] ?? ''),
'reply_to' => sanitize_email($input['reply_to'] ?? ''),
'success_message' => sanitize_text_field($input['success_message'] ?? ''),
'error_message' => sanitize_text_field($input['error_message'] ?? ''),
'enable_honeypot' => !empty($input['enable_honeypot']) ? 1 : 0,
'enable_logging' => !empty($input['enable_logging']) ? 1 : 0,
];
}