-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclass-resend-admin.php
More file actions
533 lines (463 loc) · 15.2 KB
/
Copy pathclass-resend-admin.php
File metadata and controls
533 lines (463 loc) · 15.2 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
/**
* The Resend Admin Class
*
* @package Resend
*/
/**
* Class for creating the Admin UI and options.
*/
class Resend_Admin {
public const NONCE = 'resend-nonce';
/**
* Determine if the admin hooks have been initiated.
*
* @var bool
*/
private static $initiated = false;
/**
* The list of status messages.
*
* @var array
*/
private static $status = array();
/**
* Initialize the admin class.
*
* @return void
*/
public static function init() {
if ( ! self::$initiated ) {
self::init_hooks();
}
}
/**
* Register admin hooks and AJAX callbacks.
*
* @return void
*/
public static function init_hooks() {
self::$initiated = true;
// Admin.
add_action( 'admin_init', array( 'Resend_Admin', 'admin_init' ) );
add_action( 'admin_menu', array( 'Resend_Admin', 'admin_menu' ), 5 );
add_action( 'admin_enqueue_scripts', array( 'Resend_Admin', 'load_resources' ) );
// AJAX handlers.
add_action( 'wp_ajax_resend_enter_key', array( 'Resend_Admin', 'ajax_enter_api_key' ) );
add_action( 'wp_ajax_resend_settings', array( 'Resend_Admin', 'ajax_settings' ) );
add_action( 'wp_ajax_resend_send_test', array( 'Resend_Admin', 'ajax_send_test_email' ) );
// Plugin links.
add_filter( 'plugin_action_links', array( 'Resend_Admin', 'plugin_action_links' ), 10, 2 );
add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . '/resend.php' ), array( 'Resend_Admin', 'admin_plugin_settings_link' ) );
}
/**
* Handle admin initialization tasks after plugin activation.
*
* @return void
*/
public static function admin_init() {
if ( get_option( 'Activated_Resend' ) ) {
delete_option( 'Activated_Resend' );
if ( ! headers_sent() ) {
$admin_url = self::get_page_url( 'init' );
wp_safe_redirect( $admin_url );
exit;
}
}
}
/**
* Register the Resend admin menu page.
*
* @return void
*/
public static function admin_menu() {
$hook = add_options_page( __( 'Resend', 'resend' ), __( 'Resend', 'resend' ), 'manage_options', 'resend', array( 'Resend_Admin', 'display_page' ) );
if ( $hook ) {
add_action( "load-$hook", array( 'Resend_Admin', 'admin_help' ) );
}
}
/**
* Add a settings link to the plugin action links.
*
* @param array $links Existing plugin action links.
*
* @return array Modified plugin action links.
*/
public static function admin_plugin_settings_link( $links ) {
$settings_link = '<a href="' . esc_url( self::get_page_url() ) . '">' . __( 'Settings', 'resend' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Load admin styles and scripts on Resend admin pages.
*
* @return void
*/
public static function load_resources() {
global $hook_suffix;
if ( in_array(
$hook_suffix,
apply_filters(
'resend_admin_page_hook_suffixes',
array_merge(
array(
'index.php',
'settings_page_resend',
)
)
),
true
) ) {
$resend_css_path = 'public/resend.css';
wp_register_style( 'resend', plugin_dir_url( __FILE__ ) . $resend_css_path, array(), self::get_asset_file_version( $resend_css_path ) );
wp_enqueue_style( 'resend' );
$resend_admin_css_path = 'public/resend-admin.css';
wp_register_style( 'resend-admin', plugin_dir_url( __FILE__ ) . $resend_admin_css_path, array(), self::get_asset_file_version( $resend_admin_css_path ) );
wp_enqueue_style( 'resend-admin' );
$resend_admin_js_path = 'public/resend-admin.js';
wp_register_script( 'resend-admin', plugin_dir_url( __FILE__ ) . $resend_admin_js_path, array( 'jquery' ), self::get_asset_file_version( $resend_admin_js_path ), array( 'in_footer' => true ) );
wp_enqueue_script( 'resend-admin' );
wp_localize_script(
'resend-admin',
'resendAjax',
array(
'resend_url' => self::get_page_url(),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( self::NONCE ),
)
);
}
}
/**
* Render the correct admin page based on the current view.
*
* @return void
*/
public static function display_page() {
// Read-only routing parameter; no data is processed or persisted, so no nonce is required.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : '';
if ( ! Resend::get_api_key() || 'start' === $view ) {
self::display_start_page();
} elseif ( 'stats' === $view ) {
self::display_stats_page();
} else {
self::display_configuration_page();
}
}
/**
* Display the setup start page when no API key is configured.
*
* @return void
*/
public static function display_start_page() {
$api_key = Resend::get_api_key();
if ( $api_key ) {
self::display_configuration_page();
return;
}
Resend::view( 'start' );
}
/**
* Display the stats page and clear the stored API key.
*
* @return void
*/
public static function display_stats_page() {
delete_option( 'resend_api_key' );
Resend::view( 'stats' );
}
/**
* Display the Resend configuration page.
*
* @return void
*/
public static function display_configuration_page() {
// Read-only routing parameter; no data is processed or persisted, so no nonce is required.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
$args = array();
if ( 'connected' === $status ) {
$args = array(
'notice' => array(
'message' => self::json_status( 'connected' )['message'],
'success' => true,
),
);
}
Resend::view( 'config', $args );
}
/**
* Add a notice with the given type and message.
*
* @param string $type The type of message.
* @param null|string $message The message content.
* @return void
*/
public static function add_status( $type, $message ) {
self::$status = array(
'type' => $type,
'message' => $message,
);
}
/**
* Get the JSON status payload for the given type.
*
* @param string $type Status type key.
* @param string|null $message Optional override message.
*
* @return array JSON-compatible status.
*/
public static function json_status( $type, $message = null ) {
if ( ! empty( self::$status ) ) {
$message = self::get_status_message( self::$status['type'], self::$status['message'] );
} else {
$message = self::get_status_message( $type, $message );
}
return array(
'type' => $type,
'message' => $message,
);
}
/**
* Get the human-readable status message for a notice.
*
* @param string $type Status type key.
* @param string|null $message Optional override message.
*
* @return string Unescaped status message. Callers must escape at the point of output.
*/
protected static function get_status_message( $type, $message = null ) {
if ( $message ) {
if ( is_array( $message ) ) {
$message = $message['message'] ?? $message['error'];
}
// Returned unescaped: the view escapes with esc_attr() and the admin
// script renders it with .text(), so escaping here would double-encode.
return $message;
}
$message = '';
switch ( $type ) {
case 'connected':
$message = __( 'Resend is now connected to your site.', 'resend' );
break;
case 'not-allowed':
$message = __( 'You are not allowed to perform this action!', 'resend' );
break;
case 'new-key-valid':
$message = __( 'Resend API key has updated successfull.', 'resend' );
break;
case 'no-change-to-key':
$message = __( 'Unable to update your API key.', 'resend' );
break;
case 'new-key-empty':
$message = __( 'You did not enter an API key. Please try again.', 'resend' );
break;
case 'new-key-invalid':
$message = __( 'The API key you entered is invalid. Please double-check it.', 'resend' );
break;
case 'test-email-not-set':
$message = __( 'Please provide a valid email address to send a test email.', 'resend' );
break;
case 'test-email-sent':
$message = __( 'Test email sent!', 'resend' );
break;
case 'test-email-failed':
$message = __( 'Failed to send a test email.', 'resend' );
break;
case 'from-email-invalid':
$message = __( 'Failed to update sender email address', 'resend' );
break;
case 'from-name-invalid':
$message = __( 'Failed to update sender name', 'resend' );
break;
case 'settings-updated':
$message = __( 'Settings updated successfully', 'resend' );
break;
default:
$message = __( 'Something went wrong. Please try again.', 'resend' );
}
return $message;
}
/**
* Register help tabs and sidebar content for the Resend admin page.
*
* @return void
*/
public static function admin_help() {
$current_screen = get_current_screen();
// Read-only routing parameter; no data is processed or persisted, so no nonce is required.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : '';
if ( current_user_can( 'manage_options' ) ) {
if ( ! Resend::get_api_key() || 'start' === $view ) {
// Setup page.
$current_screen->add_help_tab(
array(
'id' => 'overview',
'title' => __( 'Overview', 'resend' ),
'content' =>
'<p>' . esc_html__( 'The Resend WordPress plugin lets you send transactional and marketing emails from WordPress through the Resend API.', 'resend' ) . '</p>' .
'<p>' . esc_html__( 'On this page, you are able to connect Resend to your site.', 'resend' ) . '</p>',
)
);
$current_screen->add_help_tab(
array(
'id' => 'setup-signup',
'title' => __( 'New to Resend', 'resend' ),
'content' =>
'<p>' . esc_html__( 'You need to enter an API key to connect Resend to your site.', 'resend' ) . '</p>' .
/* translators: %s: sign up link */
'<p>' . sprintf( __( 'Sign up for an account on %s to get an API key.', 'resend' ), '<a href="https://resend.com/home" target="_blank">Resend.com</a>' ) . '</p>',
)
);
} else {
// Configuration page.
$current_screen->add_help_tab(
array(
'id' => 'overview',
'title' => __( 'Overview', 'resend' ),
'content' =>
'<p>' . esc_html__( 'The Resend WordPress plugin lets you send transactional and marketing emails from WordPress through the Resend API.', 'resend' ) . '</p>' .
'<p>' . esc_html__( 'On this page, you are able to update your Resend settings and view your email history.', 'resend' ) . '</p>',
)
);
}
}
$current_screen->set_help_sidebar(
'<p><strong>' . esc_html__( 'For more information:', 'resend' ) . '</strong></p>' .
'<p><a href="https://resend.com/docs" target="_blank">' . esc_html__( 'Resend Documentation', 'resend' ) . '</a></p>' .
'<p><a href="https://resend.com/help" target="_blank">' . esc_html__( 'Resend Support', 'resend' ) . '</a></p>'
);
}
/**
* Handle AJAX requests for entering or updating the API key.
*
* @return void
*/
public static function ajax_enter_api_key() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( self::json_status( 'not-allowed' ) );
}
check_admin_referer( self::NONCE );
$new_key = sanitize_text_field( isset( $_POST['key'] ) ? wp_unslash( $_POST['key'] ) : '' );
$old_key = Resend::get_api_key();
$result = array( false, 'no-change-to-key' );
if ( empty( $new_key ) ) {
if ( ! empty( $old_key ) ) {
delete_option( 'resend_api_key' );
}
$result = array( false, 'new-key-empty' );
} elseif ( $new_key !== $old_key ) {
if ( Resend::is_valid_key( $new_key ) ) {
update_option( 'resend_api_key', $new_key );
$result = array( true, 'new-key-valid' );
} else {
$result = array( false, 'new-key-invalid' );
}
}
list($is_successful, $status) = $result;
$is_successful
? wp_send_json_success( self::json_status( $status ) )
: wp_send_json_error( self::json_status( $status ) );
}
/**
* Handle AJAX requests for updating Resend settings.
*
* @return void
*/
public static function ajax_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( self::json_status( 'not-allowed' ) );
}
check_admin_referer( self::NONCE );
$new_from_email = sanitize_email( isset( $_POST['from_email'] ) ? wp_unslash( $_POST['from_email'] ) : '' );
$old_from_email = Resend::get_from_address();
if ( ! $new_from_email || ! is_email( $new_from_email ) ) {
wp_send_json_error( self::json_status( 'from-email-invalid' ) );
}
$new_from_name = sanitize_text_field( isset( $_POST['from_name'] ) ? wp_unslash( $_POST['from_name'] ) : '' );
$old_from_name = Resend::get_from_name();
if ( ! $new_from_name ) {
wp_send_json_error( self::json_status( 'from-name-invalid' ) );
}
// Update the from email address.
if ( $new_from_email !== $old_from_email ) {
update_option( 'resend_from_address', $new_from_email );
}
// Update the from name.
if ( $new_from_name !== $old_from_name ) {
update_option( 'resend_from_name', $new_from_name );
}
wp_send_json_success( self::json_status( 'settings-updated' ) );
}
/**
* Handle AJAX requests to send a test email.
*
* @return void
*/
public static function ajax_send_test_email() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( self::json_status( 'not-allowed' ) );
}
check_admin_referer( self::NONCE );
$email = sanitize_email( isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '' );
if ( ! $email || ! is_email( $email ) ) {
wp_send_json_error( self::json_status( 'test-email-not-set' ) );
}
$subject = 'Resend Test: ' . html_entity_decode( get_bloginfo( 'name' ) );
$message = 'This is a test email sent using the Resend plugin.';
$result = wp_mail( $email, $subject, $message );
$result
? wp_send_json_success( self::json_status( 'test-email-sent' ) )
: wp_send_json_error( self::json_status( 'test-email-failed' ) );
}
/**
* Append a settings link to the plugin action links.
*
* @param array $links Plugin action links.
* @param string $file Plugin file.
*
* @return array Modified plugin action links.
*/
public static function plugin_action_links( $links, $file ) {
if ( plugin_basename( plugin_dir_url( __FILE__ ) . '/resend.php' ) === $file ) {
$links[] = '<a href="' . esc_url( self::get_page_url() ) . '">' . esc_html__( 'Settings', 'resend' ) . '</a>';
}
return $links;
}
/**
* Get the admin page URL for Resend pages.
*
* @param string $page Optional page view (config|stats|init).
*
* @return string Admin URL.
*/
public static function get_page_url( $page = 'config' ) {
$base_url = admin_url( 'options-general.php' );
$args = array( 'page' => 'resend' );
if ( 'stats' === $page ) {
$args = array(
'page' => 'resend',
'view' => 'stats',
);
} elseif ( 'init' === $page ) {
$args = array(
'page' => 'resend',
'view' => 'start',
);
}
return add_query_arg( $args, $base_url );
}
/**
* Get the asset file version for cache busting.
*
* @param string $relative_path Relative path to the asset.
*
* @return string Asset version string.
*/
public static function get_asset_file_version( $relative_path ) {
$full_path = RESEND__PLUGIN_DIR . $relative_path;
return RESEND_VERSION;
}
}