rl-warmup-plugin/includes/rl-mailwarmer-importer.php
2025-01-15 10:49:39 -06:00

185 lines
6.4 KiB
PHP

<?php
add_action('admin_menu', function () {
add_menu_page(
__('Import Data', 'rl-mailwarmer'), // Page title
__('Import Data', 'rl-mailwarmer'), // Menu title
'manage_options', // Capability
'rl-mailwarmer-import-data', // Menu slug
'rl_mailwarmer_render_import_page', // Callback function
'dashicons-upload', // Icon
25 // Position
);
});
/**
* Render the Import Data admin page.
*/
function rl_mailwarmer_render_import_page() {
if (!current_user_can('manage_options')) {
return;
}
// Check for a nonce and handle file upload
if (isset($_POST['rl_import_nonce']) && wp_verify_nonce($_POST['rl_import_nonce'], 'rl_import_data')) {
if (!empty($_FILES['domain_csv']['tmp_name'])) {
rl_mailwarmer_process_domain_csv($_FILES['domain_csv']['tmp_name']);
}
if (!empty($_FILES['email_account_csv']['tmp_name'])) {
rl_mailwarmer_process_email_account_csv($_FILES['email_account_csv']['tmp_name']);
}
}
?>
<div class="wrap">
<h1><?php esc_html_e('Import Data', 'rl-mailwarmer'); ?></h1>
<form method="post" enctype="multipart/form-data">
<?php wp_nonce_field('rl_import_data', 'rl_import_nonce'); ?>
<h2><?php esc_html_e('Import Domains', 'rl-mailwarmer'); ?></h2>
<p><?php esc_html_e('Upload a CSV file to import domains.', 'rl-mailwarmer'); ?></p>
<input type="file" name="domain_csv" accept=".csv" />
<h2><?php esc_html_e('Import Email Accounts', 'rl-mailwarmer'); ?></h2>
<p><?php esc_html_e('Upload a CSV file to import email accounts.', 'rl-mailwarmer'); ?></p>
<input type="file" name="email_account_csv" accept=".csv" />
<p><button type="submit" class="button button-primary"><?php esc_html_e('Upload Files', 'rl-mailwarmer'); ?></button></p>
</form>
</div>
<?php
}
function rl_mailwarmer_process_domain_csv($file_path) {
$handle = fopen($file_path, 'r');
if (!$handle) {
echo '<div class="error"><p>' . __('Failed to open the domain CSV file.', 'rl-mailwarmer') . '</p></div>';
return;
}
// Read the headers
$headers = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
$data = array_combine($headers, $row);
if (empty($data['name'])) {
continue; // Skip invalid rows
}
// Create or update the domain post
$existing_domain = get_posts([
'post_type' => 'domain',
'title' => $data['name'],
'posts_per_page' => 1,
'fields' => 'ids',
]);
$post_id = $existing_domain[0] ?? null;
$post_data = [
'post_title' => $data['name'],
'post_type' => 'domain',
'post_status' => 'publish',
];
if ($post_id) {
$post_data['ID'] = $post_id;
}
$post_id = wp_insert_post($post_data);
if (!is_wp_error($post_id)) {
update_post_meta($post_id, 'cloudflare_api_email', sanitize_text_field($data['cloudflare_api_email']));
update_post_meta($post_id, 'cloudflare_api_key', sanitize_text_field($data['cloudflare_api_key']));
log_to_file("rl_mailwarmer_process_domain_csv - Imported domain: " . $data['name']);
} else {
log_to_file("rl_mailwarmer_process_domain_csv - ERROR importing domain: " . $data['name']);
}
}
fclose($handle);
echo '<div class="updated"><p>' . __('Domains imported successfully.', 'rl-mailwarmer') . '</p></div>';
}
function rl_mailwarmer_process_email_account_csv($file_path) {
$handle = fopen($file_path, 'r');
if (!$handle) {
echo '<div class="error"><p>' . __('Failed to open the email account CSV file.', 'rl-mailwarmer') . '</p></div>';
return;
}
// Read the headers
$headers = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
$data = array_combine($headers, $row);
if (empty($data['Email Address']) || empty($data['Password'])) {
continue; // Skip invalid rows
}
if (!empty($data['Name'])) {
$name = sanitize_text_field($data['Name']);
}
if (!empty($data['Signature'])) {
$signature = sanitize_textarea_field(wp_slash($data['Signature']));
// log_to_file("rl_mailwarmer_process_email_account_csv - Signature: {$signature}");
}
// Match the email account to a domain
$domain_name = strtolower(substr(strrchr($data['Email Address'], "@"), 1));
$domain = get_posts([
'post_type' => 'domain',
'title' => $domain_name,
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
]);
// Match the email provider by name
$email_provider = $data['Email Provider'];
// Check if this is a scrubber account
if (!empty($data['Is Scrubber'])) {
$include_in_scrubber_pool = true;
} else {
$include_in_scrubber_pool = false;
}
// Create or update the email account post
$post_data = [
'post_title' => strtolower($data['Email Address']),
'post_type' => 'email-account',
'post_status' => 'publish',
'meta_input' => [
'full_name' => $name,
'mail_password' => sanitize_text_field($data['Password']),
'email_provider' => $email_provider,
'email_signature' => $signature,
'include_in_scrubber_pool' => $include_in_scrubber_pool,
],
];
if (!empty($domain)) {
$post_data['meta_input']['domain'] = $domain[0];
}
$post_id = wp_insert_post($post_data);
if (!is_wp_error($post_id)) {
// $email_account_connection_status = RL_MailWarmer_Email_Helper::check_mail_login($post_id);
log_to_file("rl_mailwarmer_process_email_account_csv - Imported email {$data['Email Address']}: ");
} else {
log_to_file("rl_mailwarmer_process_email_account_csv - ERROR importing email: " . $data['Email Address']);
}
}
fclose($handle);
echo '<div class="updated"><p>' . __('Email accounts imported successfully.', 'rl-mailwarmer') . '</p></div>';
}