<?php
/*
* This is Refine URL login setup page
* This page handles all the login page related functions
*/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

// Fetch and manage login URL
$saprfurl_current_url = get_site_url() . "/";
$saprfurl_default_extension = get_option('saprfurl_custom_redirect', '404');
$saprfurl_full_url = esc_url($saprfurl_current_url . $saprfurl_default_extension);

// Fetch current setting and escape for output
$saprfurl_hide_login_page = get_option('saprfurl_hide_login_page', 0);
$saprfurl_hide_redirect_page = get_option('saprfurl_hide_redirect_page', 0);


// Handle form submission securely
if (isset($_SERVER['REQUEST_METHOD']) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset($_POST['saprfurl_save_settings'])) {

    $nonce = isset($_POST['saprfurl_nonce']) ? sanitize_text_field(wp_unslash($_POST['saprfurl_nonce'])) : '';
    if (!wp_verify_nonce($nonce, 'saprfurl_save_action')) {
        die(esc_html__('Security check failed', 'refineurl'));
    }

    $saprfurl_hide_login_page = isset($_POST['saprfurl_hide_login_page']) ? 1 : 0;
    $saprfurl_hide_redirect_page = isset($_POST['saprfurl_hide_redirect_page']) ? 1 : 0;

    update_option('saprfurl_hide_login_page', $saprfurl_hide_login_page);
    update_option('saprfurl_hide_redirect_page', $saprfurl_hide_redirect_page);

    if ($saprfurl_hide_login_page && !$saprfurl_hide_redirect_page) {
        // Hide Only → clear redirect value
        update_option('saprfurl_custom_redirect', '');
        $saprfurl_default_extension = '';
    } elseif ($saprfurl_hide_redirect_page && isset($_POST['saprfurl_custom_redirect_input'])) {
        // Hide & Redirect → save custom input
        $saprfurl_custom_redirect_value = sanitize_text_field(wp_unslash($_POST['saprfurl_custom_redirect_input']));
        update_option('saprfurl_custom_redirect', $saprfurl_custom_redirect_value);
        $saprfurl_default_extension = $saprfurl_custom_redirect_value;
    } else {
        // No change
        $saprfurl_default_extension = get_option('saprfurl_custom_redirect', '404');
    }
}

// Handle redirection form
if (
    isset($_POST['saprfurl_save_user_redirect']) &&
    isset($_POST['saprfurl_redirect_user_nonce']) &&
    wp_verify_nonce($_POST['saprfurl_redirect_user_nonce'], 'saprfurl_redirect_user_action')
) {
    $saprfurl_redirect_url_login = isset($_POST['saprfurl_user_redirect_url_after_login'])
        ? sanitize_text_field(wp_unslash($_POST['saprfurl_user_redirect_url_after_login']))
        : '';

    $saprfurl_redirect_url_logout = isset($_POST['saprfurl_user_redirect_url_after_logout'])
        ? sanitize_text_field(wp_unslash($_POST['saprfurl_user_redirect_url_after_logout']))
        : '';

    $saprfurl_redirect_url_registration = isset($_POST['saprfurl_user_redirect_url_after_registration'])
        ? sanitize_text_field(wp_unslash($_POST['saprfurl_user_redirect_url_after_registration']))
        : '';

    update_option('saprfurl_user_redirect_url_after_login', $saprfurl_redirect_url_login);
    update_option('saprfurl_user_redirect_url_after_logout', $saprfurl_redirect_url_logout);
    update_option('saprfurl_user_redirect_url_after_registration', $saprfurl_redirect_url_registration);
}

?>

<div class="rfurl-main-page">
    <!-- include promotional and header area -->
    <?php
    $admin_promotion_notice = SAPRFURL_TEMPLATES . 'admin-promotion-notice.php';

    if (file_exists($admin_promotion_notice)) {
        require_once $admin_promotion_notice;
    }
    ?>

    <!-- include promotional and header area -->
    <?php
    $saprfurl_promotional_header_template = SAPRFURL_TEMPLATES . 'global-header-design.php';

    if (file_exists($saprfurl_promotional_header_template)) {
        require_once $saprfurl_promotional_header_template;
    }
    ?>

    <div class="wrap">

        <!-- include promotional and header area -->
        <?php
        $saprfurl_promotional_header_template = SAPRFURL_TEMPLATES . 'global-header-design.php';

        if (file_exists($saprfurl_promotional_header_template)) {
            require_once $saprfurl_promotional_header_template;
        }
        ?>

        <!-- Page heading starts here -->
        <div class="saprfurl-page-heading">
            <h1><?php echo esc_html__('Login Setup', 'refineurl'); ?></h1>
        </div>
        <!-- Page heading ends here -->

        <div class="saprfurl-settings-area saprfurl-settings-page saprfurl-gbg">
            <h2><?php echo esc_html__('Control login page status', 'refineurl'); ?></h2>
            <p><?php echo esc_html__('Enable or disable access to the login page with a single toggle. Useful for maintenance, added security, or restricting access during specific times.', 'refineurl'); ?></p>

            <!-- login page user input form started -->
            <form method="post">
                <?php wp_nonce_field('saprfurl_save_action', 'saprfurl_nonce'); ?>
                <input type="hidden" name="saprfurl_save_settings" value="1">

                <label class="switch initial-switch">
                    <p><?php echo esc_html__('Display', 'refineurl'); ?>&nbsp;&nbsp;</p>
                    <input type="checkbox" name="saprfurl_hide_login_page" <?php checked($saprfurl_hide_login_page, 1); ?>>
                    <span class="slider round"></span>
                    <p><?php echo esc_html__('Hide Only', 'refineurl'); ?>&nbsp;&nbsp;</p>
                </label>

                <label class="switch">
                    <input type="checkbox" name="saprfurl_hide_redirect_page" <?php checked($saprfurl_hide_redirect_page, 1); ?>>
                    <span class="slider round"></span>
                    <p><?php echo esc_html__('Hide & Redirect', 'refineurl'); ?>&nbsp;&nbsp;</p>
                </label>

                <label class="switch saprfurl-coming-soon-block">
                    <input type="checkbox" name="saprfurl_redirect_url" disabled>
                    <span class="slider round"></span>
                    <p>
                        <?php echo esc_html__('Change', 'refineurl'); ?>&nbsp;&nbsp;
                        <span style="font-size: 12px; color: gray;">(<?php echo esc_html__('Coming soon', 'refineurl'); ?>)</span>
                    </p>
                </label>

                <br />

                <!-- Default message for login setup page status update -->
                <div id="saprfurl-off-message" class="" style="display: <?php echo (!$saprfurl_hide_login_page && !$saprfurl_hide_redirect_page) ? 'block' : 'none'; ?>;">
                    <p><?php echo esc_html__('Anyone can access your login page like normally they do. Your current login URL is default stage like following!', 'refineurl'); ?></p>
                    <p><?php echo esc_url($saprfurl_current_url) . "wp-admin"; ?></p>
                </div>
                
                <!-- Warning message for hide page enable -->
                <div id="saprfurl-hide-page-warning-message" class="" style="display: <?php echo ($saprfurl_hide_login_page) ? 'block' : 'none'; ?>;">
                    <p><?php echo esc_html__('None can access your login page/url. You can login from your cPanel again. All login attempters will be redirected to the following custom URL:', 'refineurl'); ?></p>
                    <p><?php echo esc_url($saprfurl_current_url); ?></p>
                </div>

                <!-- Warning message for hide and change page enable -->
                <div id="saprfurl-hide-change-page-warning-message" class="" style="display: <?php echo ($saprfurl_hide_redirect_page) ? 'block' : 'none'; ?>;">
                    <p><?php echo esc_html__('Update None can access your login page/url. You can login from your cPanel again. All login attempters will be redirected to the following custom URL:', 'refineurl'); ?></p>
                    <p>
                        <span><?php echo esc_url($saprfurl_current_url); ?></span>
                        <input type="text" name="saprfurl_custom_redirect_input" value="<?php echo esc_attr($saprfurl_default_extension); ?>" style="" />
                    </p>
                </div>

                <br />
                <button type="submit" class="saprfurl-settings-area-button button button-primary">
                    <?php echo esc_html__('Save Now', 'refineurl'); ?>
                </button>
            </form>
            <br />
        </div>
        <br/>
        <div class="saprfurl-settings-area saprfurl-settings-page saprfurl-gbg">
            <h2><?php echo esc_html__('Setup Redirect URL', 'refineurl'); ?></h2>

            <p style="color: #cc0000; font-style: italic;">
                <?php echo esc_html__('This redirection works only if your site uses the default wp-admin and wp-login.php URLs. If you have hide or changed them, it won\'t work currently.', 'refineurl'); ?>
            </p>

            <form method="post">
                <?php wp_nonce_field('saprfurl_redirect_user_action', 'saprfurl_redirect_user_nonce'); ?>
                <input type="hidden" name="saprfurl_save_user_redirect" value="1">

                <label class="saprfurl-redirect-after-login">
                    <p><?php echo esc_html__('After Login URL- ', 'refineurl'); ?>&nbsp;&nbsp;</p>
                    <input type="url" name="saprfurl_user_redirect_url_after_login" id="saprfurl_user_redirect_url_after_login"
                        value="<?php echo esc_attr(get_option('saprfurl_user_redirect_url_after_login', '')); ?>"
                        style="width: 100%; max-width: 500px; height:10px; margin-top: 4px;" placeholder="" />
                </label>

                <label class="saprfurl-redirect-after-logout">
                    <p><?php echo esc_html__('After Logout URL- ', 'refineurl'); ?>&nbsp;&nbsp;</p>
                    <input type="url" name="saprfurl_user_redirect_url_after_logout" id="saprfurl_user_redirect_url_after_logout"
                        value="<?php echo esc_attr(get_option('saprfurl_user_redirect_url_after_logout', '')); ?>"
                        style="width: 100%; max-width: 500px; height:10px; margin-top: 4px;" placeholder="" />
                </label>

                <label class="saprfurl-redirect-after-registration">
                    <p><?php echo esc_html__('After Registration URL- ', 'refineurl'); ?>&nbsp;&nbsp;</p>
                    <input type="url" name="saprfurl_user_redirect_url_after_registration" id="saprfurl_user_redirect_url_after_registration"
                        value="<?php echo esc_attr(get_option('saprfurl_user_redirect_url_after_registration', '')); ?>"
                        style="width: 100%; max-width: 500px; height:10px; margin-top: 4px;" placeholder="" />
                </label>

                <br />

                <button type="submit" class="saprfurl-settings-area-button button button-primary">
                    <?php echo esc_html__('Save Now', 'refineurl'); ?>
                </button>
            </form>
            <br />
        </div>

    </div>
</div>