-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordpress.php
More file actions
459 lines (408 loc) · 10.4 KB
/
Copy pathwordpress.php
File metadata and controls
459 lines (408 loc) · 10.4 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* Wordpress settings and options page.
*
* TODO: Would also like to include the following settings:
* page picker for the donate form plugin.
* page picker for landing page.
*/
require_once WP_PLUGIN_DIR . '/sdf/message.php';
require_once WP_PLUGIN_DIR . '/sdf/Stripe.php';
require_once WP_PLUGIN_DIR . '/sdf/Salesforce.php';
function sdf_activate() {
if(wp_next_scheduled('sdf_bimonthly_hook') == false) {
wp_schedule_event(
current_time('timestamp'), // timestamp
'bimonthly', // recurrence
'sdf_bimonthly_hook' // hook
);
}
}
function sdf_register_settings() {
// Stripe stuff
register_setting(
'sdf', // option group name
'stripe_api_secret_key', // option name
'sdf_stripe_secret_sanitize' // option sanitization callback
);
register_setting(
'sdf',
'stripe_api_public_key',
'sdf_string_setting_sanitize'
);
add_settings_section(
'sdf_stripe_api', // id attribute of tags ??
'Stripe', // section title
'sdf_stripe_api_section_print', // callback to print section content
'spark-form-admin' // page slug to work on
);
// Salesforce stuff
register_setting(
'sdf',
'salesforce_username',
'sdf_string_setting_sanitize'
);
register_setting(
'sdf',
'salesforce_password',
'sdf_sf_password_validate'
);
register_setting(
'sdf',
'salesforce_token',
'sdf_salesforce_api_check'
);
add_settings_section(
'sdf_salesforce_api',
'Salesforce',
'sdf_salesforce_section_print',
'spark-form-admin'
);
// email security
register_setting(
'sdf',
'alert_email_list',
'sdf_validate_settings_emails'
);
register_setting(
'sdf',
'sf_email_reply_to',
'sdf_validate_settings_emails'
);
add_settings_section(
'sdf_alert_email',
'Alert Emails',
'sdf_email_section_print',
'spark-form-admin'
);
// Donation page address and contact email
register_setting(
'sdf',
'spark_address',
'sdf_string_no_sanitize'
);
register_setting(
'sdf',
'spark_contact_email',
'sdf_validate_settings_emails'
);
add_settings_section(
'sdf_spark_details',
'Spark Details',
'sdf_spark_details_print',
'spark-form-admin'
);
}
// unregister the settings when deactivated
function sdf_deactivate() {
unregister_setting(
'sdf', // option group
'stripe_api_secret_key', // option name
'sdf_stripe_secret_sanitize' // sanitize callback.
);
unregister_setting(
'sdf',
'stripe_api_public_key',
'sdf_stripe_secret_sanitize'
);
unregister_setting(
'sdf',
'salesforce_username',
'sdf_string_setting_sanitize'
);
unregister_setting(
'sdf',
'salesforce_password',
'sdf_string_setting_sanitize'
);
unregister_setting(
'sdf',
'salesforce_token',
'sdf_salesforce_api_check'
);
unregister_setting(
'sdf',
'alert_email_list',
'sdf_validate_settings_emails'
);
unregister_setting(
'sdf',
'sf_email_reply_to',
'sdf_validate_settings_emails'
);
unregister_setting(
'sdf',
'spark_address',
'sdf_string_no_sanitize'
);
unregister_setting(
'sdf',
'spark_contact_email',
'sdf_validate_settings_emails'
);
wp_clear_scheduled_hook(
'sdf_bimonthly_hook'
);
}
function sdf_options_page() { ?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Spark Donation Form</h2>
<form method="post" action="options.php">
<?php
settings_fields('sdf');
do_settings_sections('spark-form-admin');
submit_button();
?>
</form>
</div>
<?php }
// ****************************************************************************
// Options page
function sdf_stripe_api_section_print() { ?>
<p>Enter your API keys.<br>Ensure that your public and private keys match,
whether in live mode, or test mode.</p>
<?php sdf_print_stripe_api_settings_form();
}
function sdf_salesforce_section_print() { ?>
<p>Enter your username, password, and the security token.<br>If you reset
your password, you will need to <a href='https://na3.salesforce.com/_ui/sy
stem/security/ResetApiTokenEdit?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DP
ersonalInfo&setupid=ResetApiToken'>reset your security token too.</a></p>
<?php sdf_print_salesforce_settings_form();
}
function sdf_email_section_print() { ?>
<p>Set the email addresses that receive alert emails, and the reply-to
address for outgoing mails.</p>
<?php sdf_print_email_settings_form();
}
function sdf_spark_details_print() { ?>
<p>Set the Spark SF contact address and contact email.</p>
<?php sdf_print_spark_details_form();
}
function sdf_print_email_settings_form() { ?>
<table class="form-table">
<tr valign="top">
<th scope="row">
Notification addresses:<br>
<span class="note">(Separate emails with a comma and space)</span>
</th>
<td>
<input class="sdf-wide"
type="text"
id="alert_email_list"
name="alert_email_list"
value="<?php echo esc_attr(get_option('alert_email_list')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">
Reply-to:<br>
<span class="note">(Required)</span>
</th>
<td>
<input class="sdf-wide"
type="text"
id="sf_email_reply_to"
name="sf_email_reply_to"
value="<?php echo esc_attr(get_option('sf_email_reply_to')); ?>" />
</td>
</tr>
</table>
<?php }
function sdf_print_stripe_api_settings_form() { ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Stripe secret key:</th>
<td>
<input class="sdf-wide"
type="text"
id="stripe_api_secret_key"
name="stripe_api_secret_key"
value="<?php echo esc_attr(get_option('stripe_api_secret_key')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Stripe public key:</th>
<td>
<input class="sdf-wide"
type="text"
id="stripe_api_public_key"
name="stripe_api_public_key"
value="<?php echo esc_attr(get_option('stripe_api_public_key')); ?>" />
</td>
</tr>
</table>
<?php }
function sdf_print_salesforce_settings_form() { ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Salesforce username:</th>
<td>
<input class="sdf-wide"
type="text"
id="salesforce_username"
name="salesforce_username"
value="<?php echo esc_attr(get_option('salesforce_username')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Salesforce password:</th>
<td>
<input class="sdf-wide"
type="password"
id="salesforce_password"
name="salesforce_password"
value="<?php echo sdf_password_dummy('salesforce_password'); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Salesforce token:</th>
<td>
<input class="sdf-wide"
type="text"
id="salesforce_token"
name="salesforce_token"
value="<?php echo esc_attr(get_option('salesforce_token')); ?>" />
</td>
</tr>
</table>
<?php }
function sdf_print_spark_details_form() { ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Spark's mailing address</th>
<td>
<textarea class="sdf-wide"
id="spark_address"
name="spark_address"
rows="3"><?php echo esc_attr(get_option('spark_address')); ?></textarea>
</td>
</tr>
<tr valign="top">
<th scope="row">Spark's contact email</th>
<td>
<input class="sdf-wide"
type="text"
id="spark_contact_email"
name="spark_contact_email"
value="<?php echo esc_attr(get_option('spark_contact_email')); ?>" />
</td>
</tr>
</table>
<?php }
// Test the given stripe credential
function sdf_stripe_secret_sanitize($input) {
if(strlen($input)) {
\SDF\Stripe::api($input);
try {
$count = \Stripe\Plan::all()->count;
} catch(\Stripe\Error $e) {
$message = $e->getJsonBody();
$message = $message['error']['message'];
add_settings_error(
'stripe_api_secret_key', // id, or slug, of the pertinent setting
'stripe_api_secret_key_auth_error', // id or slug of the error itself
$message,
'error' // message type, since this function actually handles everything including updates
);
}
}
return $input;
}
// Similarly, test the salesforce credentials
// Actually, only test the token.
function sdf_salesforce_api_check($input) {
if(strlen($input) > 0) {
$input = sdf_string_setting_sanitize($input);
if(get_option('salesforce_username')
&& get_option('salesforce_password')) {
try {
\SDF\Salesforce::api($input);
} catch(Exception $e) {
$message = '<span id="source">Salesforce error:</span> '
. htmlspecialchars($e->faultstring);
add_settings_error(
'salesforce_token',
'salesforce_token_auth_error',
$message,
'error'
);
}
}
}
return $input;
}
function sdf_string_setting_sanitize($input) {
return trim(sanitize_text_field($input));
}
function sdf_string_no_sanitize($input) {
return $input;
}
// This function prevents overwriting the saved password
// with dummy data
// It needs to be specific to salesforce because we need to get
// the specific option 'salesforce_password',
// and there's no way to pass another parameter
function sdf_sf_password_validate($input) {
if($input == sdf_password_dummy('salesforce_password')) {
return get_option('salesforce_password');
} else {
return trim($input);
}
}
function sdf_validate_settings_emails($input) {
// can receive a list or one email.
$list = explode(", ", $input);
foreach($list as $email) {
if(strlen($email)) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = '<span id="source">Check this email address:</span> '
. htmlspecialchars($email);
add_settings_error(
'email_alert',
'email_value_error',
$message,
'error'
);
}
}
}
if(count($list) == 1) {
return $input;
} else {
return implode(', ', $list);
}
}
function sdf_create_menu() {
$page = add_options_page(
'Spark Form Settings', // the options page html title tag contents
'Spark Donation Form', // settings menu link title
'manage_options', // capability requirement
'spark-form-admin', // options page slug
'sdf_options_page' // callback to print markup
);
add_action('admin_print_styles-' . $page, 'sdf_enqueue_admin_styles' );
}
function sdf_enqueue_admin_styles() {
wp_enqueue_style(
'sdf_admin_css', // style handle
plugins_url('sdf/css/admin.css') // href
);
}
// If there is a value in the database, we output a dummy value
// Or if there is no value in the database, we print nothing.
function sdf_password_dummy($setting) {
$string = get_option($setting);
for($ii = 0; $ii < strlen($string); $ii++) {
$string[$ii] = '*';
}
return $string;
}
// Add settings link on plugin page
function sdf_settings_link($links) {
$settings_link =
'<a href="/wp-admin/options-general.php?page=spark-form-admin">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}