-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.php
667 lines (309 loc) · 14.2 KB
/
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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Plugin Settings API Implementation
*/
/**
* Globalize Plugin options
*/
global $cbnet_macn_options;
$cbnet_macn_options = cbnet_macn_get_options();
/**
* Get Plugin options
*/
function cbnet_macn_get_options() {
return wp_parse_args( get_option( 'plugin_cbnet_macn_options', array() ), cbnet_macn_get_option_defaults() );
}
/**
* Get Plugin option defaults
*/
function cbnet_macn_get_option_defaults() {
$defaults = array(
'site_admin' => true,
'all_administrators' => false,
'all_editors' => false,
'all_authors' => false,
'all_contributors' => false,
'all_subscribers' => false,
'additional_emails' => false,
'notify_for_registered_users' => true,
'notify_for_comment_moderation' => false
);
return apply_filters( 'cbnet_macn_option_defaults', $defaults );
}
/**
* Get Plugin option parameters
*/
function cbnet_macn_get_option_parameters() {
$defaults = cbnet_macn_get_option_defaults();
$parameters = array(
'site_admin' => array(
'name' => 'site_admin',
'title' => __( 'Site Admin', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to the site admin email address', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['site_admin']
),
'all_administrators' => array(
'name' => 'all_administrators',
'title' => __( 'All Administrators', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to all Administrators', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['all_administrators']
),
'all_editors' => array(
'name' => 'all_editors',
'title' => __( 'All Editors', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to all Editors', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['all_editors']
),
'all_authors' => array(
'name' => 'all_authors',
'title' => __( 'All Authors', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to all Authors', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['all_authors']
),
'all_contributors' => array(
'name' => 'all_contributors',
'title' => __( 'All Contributors', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to all Contributors', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['all_contributors']
),
'all_subscribers' => array(
'name' => 'all_subscribers',
'title' => __( 'All Subscribers', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to all Subscribers', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['all_subscribers']
),
'additional_emails' => array(
'name' => 'additional_emails',
'title' => __( 'Additional Email Addresses', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send comment notification emails to these additional email addresses', 'cbnet-multi-author-comment-notification' ),
'type' => 'text',
'sanitize' => 'emailarray',
'default' => $defaults['additional_emails']
),
'notify_for_registered_users' => array(
'name' => 'notify_for_registered_users',
'title' => __( 'Registered-User Comments', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send notification emails for comments from registered users', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['notify_for_registered_users']
),
'notify_for_comment_moderation' => array(
'name' => 'notify_for_comment_moderation',
'title' => __( 'Moderated Comment Notification', 'cbnet-multi-author-comment-notification' ),
'description' => __( 'Send notification emails for comments held for moderation', 'cbnet-multi-author-comment-notification' ),
'type' => 'checkbox',
'default' => $defaults['notify_for_comment_moderation']
),
);
return apply_filters( 'cbnet_macn_option_parameters', $parameters );
}
/**
* Register Plugin Settings
*/
function cbnet_macn_register_settings() {
/**
* Register Favicon setting
*
* Registers Favicon setting as
* part of core General settings
*/
register_setting( 'discussion', 'plugin_cbnet_macn_options', 'cbnet_macn_validate_settings' );
/**
* Add settings section to Settings -> Discussion
*/
add_settings_section( 'cbnet-multi-author-comment-notification', __( 'cbnet Multi-Author Comment Notification Settings', 'cbnet-multi-author-comment-notification' ), 'cbnet_macn_settings_section', 'discussion' );
/**
* Discussion settings section callback
*/
function cbnet_macn_settings_section() {
echo '<p>' . __( 'Configure cbnet Multi-Author Comment Notification settings here. These settings will only apply if the "Email me whenever" setting is enabled for either comment notification or comment modeation.', 'cbnet-multi-author-comment-notification' ) . '</p>';
}
/**
* Add user roles setting field
*
* Adds setting fields to
* Settings -> Discussion
*/
add_settings_field( 'cbnet_macn_user_roles', '<label for="cbnet_macn_user_roles">' . __( 'User Roles' , 'cbnet-multi-author-comment-notification' ) . '</label>', 'cbnet_macn_settings_field_user_roles', 'discussion', 'cbnet-multi-author-comment-notification' );
/**
* User roles setting fields callback
*/
function cbnet_macn_settings_field_user_roles() {
global $cbnet_macn_options;
$option_parameters = cbnet_macn_get_option_parameters();
?>
<p>
<input type="checkbox" name="plugin_cbnet_macn_options[site_admin]" value="true" <?php checked( true == $cbnet_macn_options['site_admin'] ); ?>>
<?php echo $option_parameters['site_admin']['description']; ?>
<br />
<input type="checkbox" name="plugin_cbnet_macn_options[all_administrators]" value="true" <?php checked( true == $cbnet_macn_options['all_administrators'] ); ?>>
<?php echo $option_parameters['all_administrators']['description']; ?>
<br />
<input type="checkbox" name="plugin_cbnet_macn_options[all_editors]" value="true" <?php checked( true == $cbnet_macn_options['all_editors'] ); ?>>
<?php echo $option_parameters['all_editors']['description']; ?>
<br />
<input type="checkbox" name="plugin_cbnet_macn_options[all_authors]" value="true" <?php checked( true == $cbnet_macn_options['all_authors'] ); ?>>
<?php echo $option_parameters['all_authors']['description']; ?>
<br />
<input type="checkbox" name="plugin_cbnet_macn_options[all_contributors]" value="true" <?php checked( true == $cbnet_macn_options['all_contributors'] ); ?>>
<?php echo $option_parameters['all_contributors']['description']; ?>
<br />
<input type="checkbox" name="plugin_cbnet_macn_options[all_subscribers]" value="true" <?php checked( true == $cbnet_macn_options['all_subscribers'] ); ?>>
<?php echo $option_parameters['all_subscribers']['description']; ?>
<br />
</p>
<?php
}
/**
* Add additional emails setting field
*
* Adds setting field to
* Settings -> Discussion
*/
add_settings_field( 'cbnet_macn_additional_emails', '<label for="cbnet_macn_additional_emails">' . __( 'Additional Emails' , 'cbnet-multi-author-comment-notification' ) . '</label>', 'cbnet_macn_settings_field_additional_emails', 'discussion', 'cbnet-multi-author-comment-notification' );
/**
* Additional emails setting fields callback
*/
function cbnet_macn_settings_field_additional_emails() {
global $cbnet_macn_options;
$option_parameters = cbnet_macn_get_option_parameters();
$additional_emails = ( is_array( $cbnet_macn_options['additional_emails'] ) ? implode( $cbnet_macn_options['additional_emails'], ',' ) : '' );
?>
<p>
<input type="text" size="80" name="plugin_cbnet_macn_options[additional_emails]" value="<?php echo esc_attr( $additional_emails ); ?>" />
<br />
<?php echo $option_parameters['additional_emails']['description']; ?>
</p>
<?php
}
/**
* Add Miscellaneous setting field
*
* Adds setting field to
* Settings -> Discussion
*/
add_settings_field( 'cbnet_macn_misc', '<label for="cbnet_macn_misc">' . __( 'Miscellaneous' , 'cbnet-multi-author-comment-notification' ) . '</label>', 'cbnet_macn_settings_field_misc', 'discussion', 'cbnet-multi-author-comment-notification' );
/**
* Miscellaneous setting fields callback
*/
function cbnet_macn_settings_field_misc() {
global $cbnet_macn_options;
$option_parameters = cbnet_macn_get_option_parameters();
?>
<p>
<input type="checkbox" name="plugin_cbnet_macn_options[notify_for_registered_users]" value="true" <?php checked( true == $cbnet_macn_options['notify_for_registered_users'] ); ?>>
<?php echo $option_parameters['notify_for_registered_users']['description']; ?>
</p>
<p>
<input type="checkbox" name="plugin_cbnet_macn_options[notify_for_comment_moderation]" value="true" <?php checked( true == $cbnet_macn_options['notify_for_comment_moderation'] ); ?>>
<?php echo $option_parameters['notify_for_comment_moderation']['description']; ?>
</p>
<?php
}
}
add_action( 'admin_init', 'cbnet_macn_register_settings' );
/**
* Plugin register_setting() sanitize callback
*
* Validate and whitelist user-input data before updating Plugin
* Options in the database. Only whitelisted options are passed
* back to the database, and user-input data for all whitelisted
* options are sanitized.
*
* @link http://codex.wordpress.org/Data_Validation Codex Reference: Data Validation
*
* @param array $input Raw user-input data submitted via the Plugin Settings page
* @return array $input Sanitized user-input data passed to the database
*/
function cbnet_macn_validate_settings( $input ) {
// This is the "whitelist": current settings
global $cbnet_macn_options;
$valid_input = $cbnet_macn_options;
// Get the array of option parameters
$option_parameters = cbnet_macn_get_option_parameters();
// Get the array of option defaults
$option_defaults = cbnet_macn_get_option_defaults();
// Determine what type of submit was input
$submittype = ( ! empty( $input['reset'] ) ? 'reset' : 'submit' );
// Loop through each setting
foreach ( $option_defaults as $setting => $value ) {
// If submit, validate/sanitize $input
if ( 'submit' == $submittype ) {
// Get the setting details from the defaults array
$optiondetails = $option_parameters[$setting];
// Get the array of valid options, if applicable
$valid_options = ( isset( $optiondetails['valid_options'] ) ? $optiondetails['valid_options'] : false );
// Validate checkbox fields
if ( 'checkbox' == $optiondetails['type'] ) {
// If input value is set and is true, return true; otherwise return false
$valid_input[$setting] = ( ( isset( $input[$setting] ) && true == $input[$setting] ) ? true : false );
}
// Validate radio button fields
else if ( 'radio' == $optiondetails['type'] ) {
// Only update setting if input value is in the list of valid options
$valid_input[$setting] = ( array_key_exists( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate select fields
else if ( 'select' == $optiondetails['type'] ) {
// Only update setting if input value is in the list of valid options
$valid_input[$setting] = ( in_array( $input[$setting], $valid_options ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate text input and textarea fields
else if ( ( 'text' == $optiondetails['type'] || 'textarea' == $optiondetails['type'] ) ) {
// Validate no-HTML content
if ( 'nohtml' == $optiondetails['sanitize'] ) {
// Pass input data through the wp_filter_nohtml_kses filter
$valid_input[$setting] = wp_filter_nohtml_kses( $input[$setting] );
}
// Validate HTML content
if ( 'html' == $optiondetails['sanitize'] ) {
// Pass input data through the wp_filter_kses filter
$valid_input[$setting] = wp_filter_kses( $input[$setting] );
}
// Validate integer content
if ( 'integer' == $optiondetails['sanitize'] ) {
// Verify value is an integer
$valid_input[$setting] = ( is_int( (int) $input[$setting] ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate RGB content
if ( 'rgb' == $optiondetails['sanitize'] ) {
// Verify value is an integer
$valid_input[$setting] = ( ( is_int( (int) $input[$setting] ) && 0 <= (int) $input[$setting] && 255 >= (int) $input[$setting] ) ? $input[$setting] : $valid_input[$setting] );
}
// Validate Email Array content
if ( 'emailarray' == $optiondetails['sanitize'] ) {
// Create array
if ( ! is_array( $input[$setting] ) && false == strpos( $input[$setting], ',' ) ) {
$input_emails = array( $input[$setting] );
} else {
$input_emails = ( '' != $input[$setting] ? explode( ',', $input[$setting] ) : false );
}
// Verify values are valid email addresses
if ( false != $input_emails ) {
foreach ( $input_emails as $email ) {
$email = trim( $email );
if ( ! is_email( $email ) ) { unset( $input_emails[$email] ); }
}
}
$valid_input[$setting] = ( ! empty( $input_emails ) ? $input_emails : false );
}
}
}
// If reset, reset defaults
elseif ( 'reset' == $submittype ) {
// Set $setting to the default value
$valid_input[$setting] = $option_defaults[$setting];
}
// Delete transients
delete_site_transient( 'cbnet_macn_moderation_email_addresses' );
delete_site_transient( 'cbnet_macn_notification_email_addresses' );
}
return $valid_input;
}
?>