-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathadmin.php
More file actions
executable file
·533 lines (482 loc) · 18.6 KB
/
admin.php
File metadata and controls
executable file
·533 lines (482 loc) · 18.6 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* mailgun-wordpress-plugin - Sending mail from WordPress using Mailgun
* Copyright (C) 2016 Mailgun, et al.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @package Mailgun
*/
class MailgunAdmin extends Mailgun {
/**
* @var array Array of "safe" option defaults.
*/
private array $defaults;
/**
* @var array
*/
protected array $options = array();
/**
* @var string $hook_suffix
*/
protected $hook_suffix;
/**
* Setup backend functionality in WordPress.
*
* @return void
*/
public function __construct() {
parent::__construct();
$this->init();
// Load localizations if available
load_plugin_textdomain('mailgun', false, 'mailgun/languages');
// Activation hook
register_activation_hook($this->plugin_file, array( &$this, 'activation' ));
// Hook into admin_init and register settings and potentially register an admin_notice
add_action('admin_init', array( &$this, 'admin_init' ));
// Activate the options page
add_action('admin_menu', array( &$this, 'admin_menu' ));
// Register an AJAX action for testing mail sending capabilities
add_action('wp_ajax_mailgun-test', array( &$this, 'ajax_send_test' ));
}
/**
* Adds the default options during plugin activation.
*
* @return void
*/
public function activation(): void {
if ( ! $this->options) {
$this->options = $this->defaults;
add_option('mailgun', $this->options);
}
}
/**
* Initialize the default property.
*
* @return void
*/
public function init(): void {
$sitename = sanitize_text_field(strtolower($_SERVER['SERVER_NAME'] ?? site_url()));
if (substr($sitename, 0, 4) === 'www.') {
$sitename = substr($sitename, 4);
}
$region = ( defined('MAILGUN_REGION') && MAILGUN_REGION ) ? MAILGUN_REGION : $this->get_option('region');
$regionDefault = $region ?: 'us';
$this->defaults = array(
'region' => $regionDefault,
'useAPI' => '1',
'apiKey' => '',
'domain' => '',
'username' => '',
'password' => '',
'secure' => '1',
'sectype' => 'tls',
'track-clicks' => '',
'track-opens' => '',
'campaign-id' => '',
'override-from' => '0',
'tag' => $sitename,
);
}
/**
* Add the options page.
*
* @return void
*/
public function admin_menu(): void {
if (current_user_can('manage_options')) {
$this->hook_suffix = add_options_page(
__('Mailgun', 'mailgun'),
__('Mailgun', 'mailgun'),
'manage_options',
'mailgun',
array( &$this, 'options_page' )
);
add_options_page(
__('Mailgun Lists', 'mailgun'),
__('Mailgun Lists', 'mailgun'),
'manage_options',
'mailgun-lists',
array( &$this, 'lists_page' )
);
add_action("admin_print_scripts-{$this->hook_suffix}", array( &$this, 'admin_js' ));
add_filter("plugin_action_links_{$this->plugin_basename}", array( &$this, 'filter_plugin_actions' ));
add_action("admin_footer-{$this->hook_suffix}", array( &$this, 'admin_footer_js' ));
}
}
/**
* Enqueue javascript required for the admin settings page.
*
* @return void
*/
public function admin_js(): void {
wp_enqueue_script('jquery');
}
/**
* Output JS to footer for enhanced admin page functionality.
*/
public function admin_footer_js(): void {
?>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
var mailgunApiOrNot = function () {
if (jQuery('#mailgun-api').val() == 1) {
jQuery('.mailgun-smtp').hide()
jQuery('.mailgun-api').show()
} else {
jQuery('.mailgun-api').hide()
jQuery('.mailgun-smtp').show()
}
}
var formModified = false
jQuery().ready(function () {
mailgunApiOrNot()
jQuery('#mailgun-api').change(function () {
mailgunApiOrNot()
})
jQuery('#mailgun-test').click(function (e) {
e.preventDefault()
if (formModified) {
var doTest = confirm('<?php _e('The Mailgun plugin configuration has changed since you last saved. Do you wish to test anyway?\n\nClick "Cancel" and then "Save Changes" if you wish to save your changes.',
'mailgun'); ?>')
if (!doTest) {
return false
}
}
jQuery(this).val('<?php _e('Testing...', 'mailgun'); ?>')
jQuery('#mailgun-test-result').text('')
jQuery.get(
ajaxurl,
{
action: 'mailgun-test',
_wpnonce: '<?php echo esc_attr(wp_create_nonce()); ?>'
}
)
.complete(function () {
jQuery('#mailgun-test').val('<?php _e('Test Configuration', 'mailgun'); ?>')
})
.success(function (data) {
if (typeof data.message !== 'undefined' && data.message === 'Failure') {
toastr.error('Mailgun ' + data.method + ' Test ' + data.message
+ '; status "' + data.error + '"');
} else {
toastr.success('Mailgun ' + data.method + ' Test ' + data.message
+ '; status "' + data.error + '"');
}
})
.error(function () {
toastr.error('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
})
})
jQuery('#mailgun-form').change(function () {
formModified = true
})
})
/* ]]> */
</script>
<?php
}
/**
* Output the options page.
*
* @return void
*/
public function options_page(): void {
if ( ! @include 'options-page.php') {
printf(
__(
'<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>',
'mailgun'
),
__DIR__ . '/options-page.php'
);
}
}
/**
* Output the lists page.
*
* @return void
*/
public function lists_page(): void {
if ( ! @include 'lists-page.php') {
printf(
__(
'<div id="message" class="updated fade"><p>The lists page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>',
'mailgun'
),
__DIR__ . '/lists-page.php'
);
}
}
/**
* Wrapper function hooked into admin_init to register settings
* and potentially register an admin notice if the plugin hasn't
* been configured yet.
*
* @return void
*/
public function admin_init(): void {
$this->register_settings();
add_action('admin_notices', array( &$this, 'admin_notices' ));
}
/**
* Whitelist the mailgun options.
*
* @return void
*/
public function register_settings(): void {
register_setting('mailgun', 'mailgun', array( &$this, 'validation' ));
}
/**
* Data validation callback function for options.
*
* @param array $options An array of options posted from the options page
*
* @return array
*/
public function validation( array $options ): array {
$apiKey = trim($options['apiKey']);
$username = trim($options['username']);
if ( ! empty($apiKey)) {
$pos = strpos($apiKey, 'api:');
if ($pos !== false && $pos == 0) {
$apiKey = substr($apiKey, 4);
}
if (1 === preg_match('(\w{32}-\w{8}-\w{8})', $apiKey)) {
$options['apiKey'] = $apiKey;
} else {
$pos = strpos($apiKey, 'key-');
if ($pos === false || $pos > 4) {
$apiKey = "key-{$apiKey}";
}
$options['apiKey'] = $apiKey;
}
}
if ( ! empty($username)) {
$username = preg_replace('/@.+$/', '', $username);
$options['username'] = $username;
}
foreach ($options as $key => $value) {
$options[ $key ] = trim($value);
}
if (empty($options['override-from'])) {
$options['override-from'] = $this->defaults['override-from'];
}
if (empty($options['sectype'])) {
$options['sectype'] = $this->defaults['sectype'];
}
$this->options = $options;
return $options;
}
/**
* Function to output an admin notice
* when plugin settings or constants need to be configured
*
* @return void
*/
public function admin_notices(): void {
$screen = get_current_screen();
if ( ! isset($screen)) {
return;
}
if ( ! current_user_can('manage_options') || $screen->id === $this->hook_suffix) {
return;
}
$smtpPasswordUndefined = ( ! $this->get_option('password') && ( ! defined('MAILGUN_PASSWORD') || ! MAILGUN_PASSWORD ) );
$smtpActiveNotConfigured = ( $this->get_option('useAPI') === '0' && $smtpPasswordUndefined );
$apiRegionUndefined = ( ! $this->get_option('region') && ( ! defined('MAILGUN_REGION') || ! MAILGUN_REGION ) );
$apiKeyUndefined = ( ! $this->get_option('apiKey') && ( ! defined('MAILGUN_APIKEY') || ! MAILGUN_APIKEY ) );
$apiActiveNotConfigured = ( $this->get_option('useAPI') === '1' && ( $apiRegionUndefined || $apiKeyUndefined ) );
if (isset($_SESSION) && ( ! isset($_SESSION['settings_turned_of']) || $_SESSION['settings_turned_of'] === false ) && ( $apiActiveNotConfigured || $smtpActiveNotConfigured )) {
?>
<div id='mailgun-warning' class='notice notice-warning is-dismissible'>
<p>
<?php
printf(
__(
'Use HTTP API is turned off or you do not have SMTP credentials. You can configure your Mailgun settings in your wp-config.php file or <a href="%1$s">here</a>',
'mailgun'
),
menu_page_url('mailgun', false)
);
?>
</p>
</div>
<?php $_SESSION['settings_turned_of'] = true; ?>
<?php } ?>
<?php
if ($this->get_option('override-from') === '1' &&
( ! $this->get_option('from-name') || ! $this->get_option('from-address') )
) {
?>
<div id='mailgun-warning' class='notice notice-warning is-dismissible'>
<p>
<strong>
<?php _e('Mailgun is almost ready. ', 'mailgun'); ?>
</strong>
<?php
printf(
__(
'"Override From" option requires that "From Name" and "From Address" be set to work properly! <a href="%1$s">Configure Mailgun now</a>.',
'mailgun'
),
menu_page_url('mailgun', false)
);
?>
</p>
</div>
<?php
}
}
/**
* Add a settings link to the plugin actions.
*
* @param array $links Array of the plugin action links
*
* @return array
*/
public function filter_plugin_actions( array $links ): array {
$settings_link = '<a href="' . menu_page_url('mailgun', false) . '">' . __('Settings', 'mailgun') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
/**
* AJAX callback function to test mail sending functionality.
*
* @return void
* @throws JsonException
*/
public function ajax_send_test(): void {
nocache_headers();
header('Content-Type: application/json');
if ( ! current_user_can('manage_options') || ! wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']))) {
die(
json_encode(
array(
'message' => __('Unauthorized', 'mailgun'),
'method' => null,
'error' => __('Unauthorized', 'mailgun'),
),
JSON_THROW_ON_ERROR
)
);
}
$getRegion = ( defined('MAILGUN_REGION') && MAILGUN_REGION ) ? MAILGUN_REGION : $this->get_option('region');
$useAPI = ( defined('MAILGUN_USEAPI') && MAILGUN_USEAPI ) ? MAILGUN_USEAPI : $this->get_option('useAPI');
$secure = ( defined('MAILGUN_SECURE') && MAILGUN_SECURE ) ? MAILGUN_SECURE : $this->get_option('secure');
$sectype = ( defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE ) ? MAILGUN_SECTYPE : $this->get_option('sectype');
$replyTo = ( defined('MAILGUN_REPLY_TO_ADDRESS') && MAILGUN_REPLY_TO_ADDRESS ) ? MAILGUN_REPLY_TO_ADDRESS : $this->get_option('reply_to');
if ( ! $getRegion) {
mg_api_last_error(__('Region has not been selected', 'mailgun'));
} else {
if ($getRegion === 'us') {
$region = __('U.S./North America', 'mailgun');
}
if ($getRegion === 'eu') {
$region = __('Europe', 'mailgun');
}
}
if ($useAPI) {
$method = __('HTTP API', 'mailgun');
} else {
$method = ( $secure ) ? __('Secure SMTP', 'mailgun') : __('Insecure SMTP', 'mailgun');
if ($secure) {
$method .= sprintf(__(' via %s', 'mailgun'), $sectype);
}
}
$admin_email = get_option('admin_email');
if ( ! $admin_email) {
die(
json_encode(
array(
'message' => __('Admin Email is empty', 'mailgun'),
'method' => $method,
'error' => __('Admin Email is empty', 'mailgun'),
),
JSON_THROW_ON_ERROR
)
);
}
try {
$headers = array(
'Content-Type: text/plain',
'Reply-To: ' . $replyTo,
);
$result = wp_mail(
$admin_email,
__('Mailgun WordPress Plugin Test', 'mailgun'),
sprintf(
__(
"This is a test email generated by the Mailgun WordPress plugin.\n\nIf you have received this message, the requested test has succeeded.\n\nThe sending region is set to %s.\n\nThe method used to send this email was: %s.",
'mailgun'
),
$region,
$method
),
$headers
);
} catch (Throwable $throwable) {
// Log purpose
}
if ($useAPI) {
if ( ! function_exists('mg_api_last_error')) {
if ( ! include __DIR__ . '/wp-mail-api.php') {
$this->deactivate_and_die(__DIR__ . '/wp-mail-api.php');
}
}
$error_msg = mg_api_last_error();
} else {
if ( ! function_exists('mg_smtp_last_error')) {
if ( ! include __DIR__ . '/wp-mail-smtp.php') {
$this->deactivate_and_die(__DIR__ . '/wp-mail-smtp.php');
}
}
$error_msg = mg_smtp_last_error();
}
// Admin Email is used as 'to' parameter, but in case of 'Test Configuration' this message is not clear for the user, so replaced with more appropriate one
if (str_contains($error_msg, "'to'") && str_contains($error_msg, 'is not a valid')) {
$error_msg = sprintf(
"Administration Email Address (%s) is not valid and can't be used for test, you can change it at General Setting page",
$admin_email
);
}
if ($result) {
die(
json_encode(
array(
'message' => __('Success', 'mailgun'),
'method' => $method,
'error' => __('Success', 'mailgun'),
),
JSON_THROW_ON_ERROR
)
);
}
// Error message will always be returned in case of failure, if not - connection wasn't successful
$error_msg = $error_msg ?: "Can't connect to Mailgun";
die(
json_encode(
array(
'message' => __('Failure', 'mailgun'),
'method' => $method,
'error' => $error_msg,
),
JSON_THROW_ON_ERROR
)
);
}
}