| 1 |
<?php |
| 2 |
/** |
| 3 |
* Audit Alerts AJAX handlers |
| 4 |
* |
| 5 |
* Kept in a separate trait (same pattern as class-admin-analyzer-ajax.php) so |
| 6 |
* the main class-admin-ajax.php does not keep growing. Settings are saved |
| 7 |
* through the generic ajax_save_settings handler (data-section="audit_alerts"); |
| 8 |
* this trait only adds the "Send test email" endpoint. |
| 9 |
* |
| 10 |
* @package Vigilante |
| 11 |
*/ |
| 12 |
|
| 13 |
// Prevent direct access. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Trait Vigilante_Admin_Audit_Alerts_Ajax |
| 20 |
*/ |
| 21 |
trait Vigilante_Admin_Audit_Alerts_Ajax { |
| 22 |
|
| 23 |
/** |
| 24 |
* Send a test email to the configured recipients. |
| 25 |
* |
| 26 |
* Shared by the "Send test email" button in Notification settings, File |
| 27 |
* Integrity and Audit Alerts. |
| 28 |
*/ |
| 29 |
public function ajax_send_test_email() { |
| 30 |
check_ajax_referer( 'vigilante_admin_nonce', 'nonce' ); |
| 31 |
|
| 32 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 33 |
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'vigilante' ) ) ); |
| 34 |
} |
| 35 |
|
| 36 |
$recipients = Vigilante_Email_Template::get_admin_recipients(); |
| 37 |
if ( empty( $recipients ) ) { |
| 38 |
wp_send_json_error( |
| 39 |
array( |
| 40 |
'message' => __( 'No notification recipients are configured. Set them in Settings & Tools.', 'vigilante' ), |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
$sent = Vigilante_Email_Template::send_test(); |
| 46 |
|
| 47 |
if ( $sent ) { |
| 48 |
wp_send_json_success( |
| 49 |
array( |
| 50 |
'message' => sprintf( |
| 51 |
/* translators: %s: comma-separated list of recipient email addresses */ |
| 52 |
__( 'Test email sent to %s.', 'vigilante' ), |
| 53 |
implode( ', ', $recipients ) |
| 54 |
), |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
wp_send_json_error( |
| 60 |
array( |
| 61 |
'message' => __( 'WordPress could not send the email. Check your site mail configuration.', 'vigilante' ), |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|