rl-warmup-plugin/includes/rl-mailwarmer-ajax.php

273 lines
10 KiB
PHP

<?php
add_action('wp_ajax_rl_mailwarmer_check_domain_health', function () {
// log_to_file("wp_ajax_rl_mailwarmer_check_domain_health called");
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'check_domain_health_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Get the domain post ID
$post_id = intval($_POST['post_id']);
if (!$post_id) {
wp_send_json_error(__('Invalid post ID', 'rl-mailwarmer'));
}
// log_to_file("wp_ajax_rl_mailwarmer_check_domain_health - Creating Domain Health Report for $post_id");
// Call the save_domain_health_report function
try {
// log_to_file("Creating new Domain Health Report for $post_id");
$report_post_id = RL_MailWarmer_Domain_Helper::save_domain_health_report($post_id);
if (is_wp_error($report_post_id)) {
wp_send_json_error($report_post_id->get_error_message());
// log_to_file("wp_ajax_rl_mailwarmer_check_domain_health - Error creating Domain Health Report: " . $report_post_id->get_error_message());
}
wp_send_json_success($report_post_id);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
// log_to_file("wp_ajax_rl_mailwarmer_check_domain_health - EXCEPTION while creating Domain Health Report: " . $e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_update_mx_record', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'update_mx_record_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// log_to_file("wp_ajax_rl_mailwarmer_update_mx_record - Running");
// Get input values
$post_id = intval($_POST['post_id']);
$action = sanitize_text_field($_POST['action_type']);
$content = sanitize_text_field($_POST['content']);
$priority = intval(sanitize_text_field($_POST['priority']));
$ttl = intval($_POST['ttl']);
if (!$post_id || !$content || !$action) {
wp_send_json_error(__('Missing required fields', 'rl-mailwarmer'));
}
$domain = RL_MailWarmer_Domain_Helper::get_domain_post($post_id);
// log_to_file("wp_ajax_rl_mailwarmer_update_mx_record - Post ID: $post_id $content $action $priority $ttl");
// Call the update_mx_record function
try {
log_to_file("wp_ajax_rl_mailwarmer_update_mx_record - Before");
$result = RL_MailWarmer_Domain_Helper::update_mx_record($domain, $content, $priority, $ttl, $action);
log_to_file("wp_ajax_rl_mailwarmer_update_mx_record - After");
if ($result) {
wp_send_json_success(__('MX record updated successfully.', 'rl-mailwarmer'));
} else {
wp_send_json_error(__('Failed to update MX record.', 'rl-mailwarmer'));
}
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_update_spf_record', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'update_spf_record_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// log_to_file("wp_ajax_rl_mailwarmer_update_spf_record - Running");
// Get input values
$post_id = intval($_POST['post_id']);
$host = sanitize_text_field($_POST['host']);
$action = sanitize_text_field($_POST['action_type']);
$all_policy = sanitize_text_field($_POST['all_policy']);
$ttl = intval($_POST['ttl']);
if (!$post_id || !$host || !$action) {
wp_send_json_error(__('Missing required fields', 'rl-mailwarmer'));
}
$domain = RL_MailWarmer_Domain_Helper::get_domain_post($post_id);
// log_to_file("wp_ajax_rl_mailwarmer_update_spf_record - Post ID: $post_id $host $action $all_policy $ttl");
// Call the update_spf_record function
try {
// log_to_file("wp_ajax_rl_mailwarmer_update_spf_record - Before");
$result = RL_MailWarmer_Domain_Helper::update_spf_record($domain, $host, $action, $all_policy, $ttl);
// log_to_file("wp_ajax_rl_mailwarmer_update_spf_record - After");
if ($result) {
wp_send_json_success(__('SPF record updated successfully.', 'rl-mailwarmer'));
} else {
wp_send_json_error(__('Failed to update SPF record.', 'rl-mailwarmer'));
}
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_update_dmarc_record', function () {
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'update_dmarc_record_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
$post_id = intval($_POST['post_id']);
if (!$post_id) {
wp_send_json_error(__('Invalid post ID', 'rl-mailwarmer'));
}
$params = array_filter([
'p' => sanitize_text_field($_POST['policy']),
'sp' => sanitize_text_field($_POST['sp']),
'pct' => intval($_POST['pct']),
'aspf' => sanitize_text_field($_POST['aspf']),
'adkim' => sanitize_text_field($_POST['adkim']),
'rua' => sanitize_email($_POST['rua']) ? 'mailto:' . sanitize_email($_POST['rua']) : null,
'ruf' => sanitize_email($_POST['ruf']) ? 'mailto:' . sanitize_email($_POST['ruf']) : null,
'fo' => sanitize_text_field($_POST['fo']),
'rf' => sanitize_text_field($_POST['rf']),
'ri' => intval($_POST['ri']),
]);
try {
$result = RL_MailWarmer_Domain_Helper::update_dmarc_record($post_id, $params);
if ($result) {
wp_send_json_success(__('DMARC record updated successfully.', 'rl-mailwarmer'));
} else {
wp_send_json_error(__('Failed to update DMARC record.', 'rl-mailwarmer'));
}
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_update_dkim_record', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'update_dkim_record_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Get input values
$post_id = intval($_POST['post_id']);
$selector = sanitize_text_field($_POST['selector']);
$action = sanitize_text_field($_POST['action_type']);
$value = sanitize_textarea_field($_POST['value']);
$ttl = intval($_POST['ttl']);
if (!$post_id || !$selector || !$action) {
wp_send_json_error(__('Missing required fields', 'rl-mailwarmer'));
}
// Call the update_dkim_record function
try {
$result = RL_MailWarmer_Domain_Helper::update_dkim_record($post_id, $selector, $action, $value, $ttl);
if ($result) {
wp_send_json_success(__('DKIM record updated successfully.', 'rl-mailwarmer'));
} else {
wp_send_json_error(__('Failed to update DKIM record.', 'rl-mailwarmer'));
}
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_fix_dns_issues', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'fix_deliverability_dns_issues_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Get the domain post ID
$post_id = intval($_POST['post_id']);
if (!$post_id) {
wp_send_json_error(__('Invalid post ID', 'rl-mailwarmer'));
}
// Call the fix_deliverability_dns_issues function
try {
$results = RL_MailWarmer_Domain_Helper::fix_deliverability_dns_issues($post_id);
wp_send_json_success($results);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_create_dns_backup', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'create_dns_backup_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Get the domain post ID
$post_id = intval($_POST['post_id']);
if (!$post_id) {
wp_send_json_error(__('Invalid post ID', 'rl-mailwarmer'));
}
$domain = get_post($post_id);
// Call the create_dns_backup function
try {
$backup_id = RL_MailWarmer_Domain_Helper::export_dns_zone($domain);
if (is_wp_error($backup_id)) {
wp_send_json_error($backup_id->get_error_message());
}
wp_send_json_success($backup_id);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_modify_email_account', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'modify_email_account_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Prepare arguments for modify_email_account
$args = [
'action' => sanitize_text_field($_POST['action_type']),
'email' => sanitize_email($_POST['email_address']),
'metadata' => [
'mail_password' => sanitize_text_field($_POST['mail_password']),
'email_provider' => intval($_POST['email_provider']),
'imap_server' => sanitize_text_field($_POST['imap_server']),
],
];
// Call modify_email_account
try {
$result = RL_MailWarmer_Email_Helper::modify_email_account($args);
wp_send_json_success('Email account successfully modified. Post ID: ' . $result);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});
add_action('wp_ajax_rl_mailwarmer_check_mail_login', function () {
// Verify nonce
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'check_mail_login_nonce')) {
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
}
// Validate and sanitize input
$post_id = intval($_POST['post_id']);
$protocol = !empty($_POST['protocol']) ? sanitize_text_field($_POST['protocol']) : null;
if (!$post_id || get_post_type($post_id) !== 'email-account') {
wp_send_json_error(__('Invalid email account ID.', 'rl-mailwarmer'));
}
// Call check_mail_login
try {
$results = RL_MailWarmer_Email_Helper::check_mail_login($post_id, $protocol);
wp_send_json_success($results);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
});