init_hooks();
}
/**
* Initialize hooks
*/
private function init_hooks() {
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
add_action('wp_ajax_aha_mail_smtp_save_settings', array($this, 'save_settings'));
add_action('wp_ajax_aha_mail_smtp_get_provider_settings', array($this, 'get_provider_settings'));
add_action('wp_ajax_aha_mail_smtp_reset_settings', array($this, 'reset_settings'));
add_action('wp_ajax_aha_mail_smtp_clear_logs', array($this, 'clear_logs'));
add_action('wp_ajax_aha_mail_smtp_send_test', array($this, 'send_test_email'));
}
/**
* Add admin menu
*/
public function add_admin_menu() {
add_menu_page(
__('AHA Mail SMTP', 'aha-mail-smtp'),
__('AHA Mail SMTP', 'aha-mail-smtp'),
'manage_options',
'aha-mail-smtp',
array($this, 'admin_page'),
'dashicons-email-alt',
80
);
}
/**
* Enqueue admin scripts and styles
*/
public function enqueue_admin_scripts($hook) {
if ($hook !== 'toplevel_page_aha-mail-smtp') {
return;
}
// Enqueue Font Awesome from local bundled copy (no external CDN)
wp_enqueue_style(
'font-awesome',
AHA_MAIL_SMTP_PLUGIN_URL . 'assets/css/font-awesome.css',
array(),
'6.4.0'
);
wp_enqueue_style(
'aha-mail-smtp-admin',
AHA_MAIL_SMTP_PLUGIN_URL . 'assets/css/admin.css',
array(),
AHA_MAIL_SMTP_VERSION
);
wp_enqueue_script(
'aha-mail-smtp-admin',
AHA_MAIL_SMTP_PLUGIN_URL . 'assets/js/admin.js',
array('jquery'),
AHA_MAIL_SMTP_VERSION,
true
);
wp_localize_script('aha-mail-smtp-admin', 'aha_mail_smtp_admin', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('aha_mail_smtp_admin'),
'providers' => AHA_Mail_SMTP_Core::get_smtp_providers(),
'strings' => array(
'error' => __('An error occurred. Please try again.', 'aha-mail-smtp'),
'test_failed' => __('Test email failed to send.', 'aha-mail-smtp'),
'confirm_reset' => __('Are you sure you want to reset all settings?', 'aha-mail-smtp'),
'settings_saved' => __('Settings saved successfully!', 'aha-mail-smtp'),
'test_sent' => __('Test email sent successfully!', 'aha-mail-smtp')
)
));
}
/**
* Render admin page
*/
public function admin_page() {
$settings = AHA_Mail_SMTP_Settings::get_all_settings();
$providers = AHA_Mail_SMTP_Core::get_smtp_providers();
$current_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'general';
?>
render_advanced_tab($settings);
break;
case 'test':
$this->render_test_tab($settings);
break;
case 'logs':
$this->render_logs_tab();
break;
default:
$this->render_general_tab($settings, $providers);
break;
}
?>
|
|
|
|
|
| date_sent); ?> |
to_email); ?> |
subject); ?> |
status)); ?>
|
error_message); ?> |
$value) {
if (strpos($key, 'aha_mail_smtp_') === 0) {
continue; // Skip nonce fields
}
$settings_to_save[$key] = $value;
}
// Use the settings class to save settings, which handles sanitization and updates
AHA_Mail_SMTP_Settings::save_settings($settings_to_save);
wp_send_json_success(__('Settings saved successfully!', 'aha-mail-smtp'));
}
/**
* Send test email via AJAX
*/
public function send_test_email() {
check_ajax_referer('aha_mail_smtp_test_email', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp'));
}
$to = sanitize_email($_POST['to_email']);
$subject = sanitize_text_field($_POST['subject']);
$message = sanitize_textarea_field($_POST['message']);
$result = AHA_Mail_SMTP_Core::send_test_email($to, $subject, $message);
if ($result['success']) {
wp_send_json_success($result['message']);
} else {
wp_send_json_error($result['message']);
}
}
/**
* Get provider settings via AJAX
*/
public function get_provider_settings() {
check_ajax_referer('aha_mail_smtp_admin', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp'));
}
$provider = sanitize_text_field($_POST['provider']);
$providers = AHA_Mail_SMTP_Core::get_smtp_providers();
if (isset($providers[$provider])) {
wp_send_json_success($providers[$provider]);
} else {
wp_send_json_error(__('Invalid provider', 'aha-mail-smtp'));
}
}
/**
* Reset settings via AJAX
*/
public function reset_settings() {
check_ajax_referer('aha_mail_smtp_admin', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp'));
}
// Delete all plugin options
$options = array(
'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_auth',
'smtp_username', 'smtp_password', 'from_email', 'from_name',
'force_from', 'reply_to_email', 'return_path', 'enable_logging',
'debug_mode', 'provider', 'connection_mode', 'api_key', 'api_domain', 'api_region'
);
foreach ($options as $option) {
delete_option('aha_mail_smtp_' . $option);
}
wp_send_json_success(__('Settings reset successfully!', 'aha-mail-smtp'));
}
/**
* Clear logs via AJAX
*/
public function clear_logs() {
check_ajax_referer('aha_mail_smtp_admin', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You do not have sufficient permissions', 'aha-mail-smtp'));
}
$result = AHA_Mail_SMTP_Logger::clear_all_logs();
if ($result !== false) {
wp_send_json_success(__('Logs cleared successfully!', 'aha-mail-smtp'));
} else {
wp_send_json_error(__('Failed to clear logs. Please try again.', 'aha-mail-smtp'));
}
}
}