mailferno-theme/page-edit-domain.php
2024-12-26 12:33:40 -06:00

338 lines
12 KiB
PHP

<?php
/**
* Template Name: Domain Edit template
* Template Post Type: page
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
$current_user_id = get_current_user_id();
$message = false;
$new_post = true;
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['domain_submit'])) {
$post_title = sanitize_text_field($_POST['domain_name']);
$cloudflare_email = isset($_POST['cloudflare_api_email']) ? sanitize_email($_POST['cloudflare_api_email']) : '';
$cloudflare_key = isset($_POST['cloudflare_api_key']) ? sanitize_text_field($_POST['cloudflare_api_key']) : '';
$domain_in_use = isset($_POST['domain_in_use']) ? $_POST['domain_in_use'] : '';
$post_data = array(
'post_title' => $post_title,
'post_type' => 'domain',
'post_status' => 'publish'
);
// Check if editing existing post
if (isset($_POST['post_id']) && !empty($_POST['post_id'])) {
$post_data['ID'] = intval($_POST['post_id']);
$post_id = wp_update_post($post_data);
$new_post = false;
$message = ['status' => 'success', 'message' => 'Domain updated successfully.'];
} else {
$post_id = wp_insert_post($post_data);
$message = ['status' => 'success', 'message' => 'Domain added successfully.'];
}
if (!is_wp_error($post_id)) {
update_post_meta($post_id, 'cloudflare_api_email', $cloudflare_email);
update_post_meta($post_id, 'cloudflare_api_key', $cloudflare_key);
update_post_meta($post_id, 'domain_in_use', $domain_in_use);
if ($new_post) {
// set the owner_id to the creator of the post if this is a new domain
update_post_meta($post_id, 'owner_id', $current_user_id);
// Run a domain health check
$domain_report_id = RL_MailWarmer_Domain_Helper::save_domain_health_report($post_id);
}
} else {
$message = ['status' => 'error', 'message' => 'Error: ' . $post_id->get_error_message()];
// $message = 'Error: ' . $post_id->get_error_message();
}
if (isset($_POST['update_dns']) && !empty($_POST['update_dns'])) {
log_to_file("Domain Edit template - Running Fix Domain");
$results = RL_MailWarmer_Domain_Helper::fix_deliverability_dns_issues($post_id);
log_to_file("Domain Edit template - Results: ", $results);
}
if ( !(isset($_POST['stay_on_page']) && ($_POST['stay_on_page'] === 'on')) ) {
// log_to_file("Domain Edit template - stay_on_page not set; redirecting!");
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
// Get existing post data if editing
if (isset($_GET['edit'])) {
$post_id = intval($_GET['edit']);
$new_post = false;
} else {
$post_id = 0;
}
$domain_name = '';
$cloudflare_email = '';
$cloudflare_key = '';
$domain_in_use = '';
if ($post_id > 0) {
$post_item = get_post($post_id);
if ($post_item && $post_item->post_type === 'domain') {
$domain_name = $post_item->post_title;
$cloudflare_email = get_post_meta($post_id, 'cloudflare_api_email', true) ?: '';
$cloudflare_key = get_post_meta($post_id, 'cloudflare_api_key', true) ?: '';
$domain_in_use = get_post_meta($post_id, 'domain_in_use', true) ?: '';
}
}
get_header(); ?>
<div <?php generate_do_attr( 'content' ); ?>>
<main <?php generate_do_attr( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?>>
<div class="inside-article mf-dashboard-page">
<?php
/**
* generate_before_content hook.
*
* @since 0.1
*
* @hooked generate_featured_page_header_inside_single - 10
*/
do_action( 'generate_before_content' );
if ( generate_show_entry_header() ) :
?>
<header <?php generate_do_attr( 'entry-header' ); ?>>
<?php
/**
* generate_before_entry_title hook.
*
* @since 0.1
*/
do_action( 'generate_before_entry_title' );
if ($message): ?>
<div class="notice notice-<?php echo $message['status']; ?> is-dismissible">
<p><?php echo esc_html($message['message']); ?></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
<?php endif;
// if ( generate_show_title() ) {
// $params = generate_get_the_title_parameters();
// the_title( $params['before'], $params['after'] );
// }
/**
* generate_after_entry_title hook.
*
* @since 0.1
*
* @hooked generate_post_meta - 10
*/
do_action( 'generate_after_entry_title' );
?>
</header>
<?php
endif;
/**
* generate_after_entry_header hook.
*
* @since 0.1
*
* @hooked generate_post_image - 10
*/
do_action( 'generate_after_entry_header' );
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
?>
<div class="entry-content"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
<div class="wrap">
<h1><?php echo $post_id ? 'Edit Domain' : 'Add New Domain'; ?></h1>
<form method="post" action="" class="mf-dashboard-form">
<?php wp_nonce_field('domain_form_nonce', 'domain_nonce'); ?>
<?php if ($post_id) : ?>
<input type="hidden" name="post_id" value="<?php echo esc_attr($post_id); ?>">
<?php endif; ?>
<div class="mf-form-field-block">
<table class="mf-form-table">
<tr>
<th><label for="domain_name">Domain Name</label></th>
<td>
<input type="text"
id="domain_name"
name="domain_name"
value="<?php echo esc_attr($domain_name); ?>"
class="regular-text"
required>
</td>
</tr>
<tr>
<th><label for="update_dns">Update DNS Settings?</label></th>
<td>
<input type="checkbox"
id="update_dns"
name="update_dns"
<?php echo $new_post ? 'checked' : ''; ?>
class="">
<span>Warning! This will attempt to update your DNS records!</span>
</td>
</tr>
<tr>
<th><label for="domain_in_use">Is this domain in use (already sending & receiving email)?</label></th>
<td>
<input type="checkbox"
id="domain_in_use"
name="domain_in_use"
<?php echo $domain_in_use || $new_post ? 'checked' : ''; ?>
class="">
<span>If the domain is already in use we won't change the MX records</span>
</td>
</tr>
</table>
</div>
<div class="mf-form-field-block advanced-settings">
<h3 class="advanced-toggle">CloudFlare Settings</h3>
<p>Only if different than the credentials in your profile</p>
<div class="advanced-content" style="display: none;">
<table class="mf-form-table">
<tr>
<th><label for="cloudflare_api_email">Cloudflare API Email</label></th>
<td>
<input type="email"
id="cloudflare_api_email"
name="cloudflare_api_email"
value="<?php echo esc_attr($cloudflare_email); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="cloudflare_api_key">Cloudflare API Key</label></th>
<td>
<input type="password"
id="cloudflare_api_key"
name="cloudflare_api_key"
value="<?php echo esc_attr($cloudflare_key); ?>"
class="regular-text">
</td>
</tr>
</table>
</div>
</div>
<p class="submit">
<input type="submit"
name="domain_submit"
class="button button-primary"
value="<?php echo $post_id ? 'Update Domain' : 'Add Domain'; ?>">
</p>
<?php if ($new_post) : ?>
<div class="mf-form-field-block">
<label for="stay_on_page">Stay on this page to create another?</label>
<input type="checkbox"
id="stay_on_page"
name="stay_on_page"
class="">
</div>
<?php endif; ?>
</form>
</div>
<?php
// the_content();
// wp_link_pages(
// array(
// 'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
// 'after' => '</div>',
// )
// );
?>
</div>
<?php
/**
* generate_after_entry_content hook.
*
* @since 0.1
*
* @hooked generate_footer_meta - 10
*/
do_action( 'generate_after_entry_content' );
/**
* generate_after_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_content' );
?>
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
}
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();