<?php
/*
Plugin Name: Elephant Maintenance
Description: Wordpress Maintenance Plugin
Version: 1.0
Author: Umapathi
License: GPL v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

/**
 * Admin Menu Configuration
 */

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

add_action('admin_menu', function () {
    add_menu_page(
        'Website Maintenance',
        'Website Maintenance',
        'manage_options',
        'elephantm-maintenance',
        'elephantm_maintenance_settings',
        //plugin_dir_url( __FILE__ ) . 'assets/images/icon.png'
    );
});

/**
 * Menu Init
 */

function elephantm_maintenance_settings() {
    if (!current_user_can('manage_options')) {
        return;
    }
    if(isset($_SERVER['REQUEST_METHOD'])){
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_admin_referer('elephantm_maintenance_save_settings')) {
            $elephantm_toggle = isset($_POST['elephantm_maintenance_toggle']) ? true : false;
            update_option('elephantm_maintenance_enabled', $elephantm_toggle);
            if(isset($_POST['elephantm_maintenance_title'])){
                update_option('elephantm_maintenance_title', sanitize_text_field(wp_unslash($_POST['elephantm_maintenance_title'])));
            }
            if(isset($_POST['elephantm_maintenance_message'])){
                update_option('elephantm_maintenance_message', sanitize_textarea_field(wp_unslash($_POST['elephantm_maintenance_message'])));
            }
            if(isset($_POST['elephantm_maintenance_phone'])){
                update_option('elephantm_maintenance_phone', sanitize_text_field(wp_unslash($_POST['elephantm_maintenance_phone'])));
            }
            if(isset($_POST['elephantm_maintenance_social'])){
                update_option('elephantm_maintenance_social', esc_url_raw(wp_unslash($_POST['elephantm_maintenance_social'])));
            }
            /**
             * Image upload
             */
            if (!empty($_FILES['elephantm_maintenance_bg']['name'])) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
                $elephantm_upload = wp_handle_upload($_FILES['elephantm_maintenance_bg'], ['test_form' => false]);
                if (!isset($elephantm_upload['error'])) {
                    update_option('elephantm_maintenance_bg', esc_url_raw($elephantm_upload['url']));
                }
            }

            echo '<div class="notice notice-success"><p>Settings saved.</p></div>';
        }
    }

    $elephantm_enabled = get_option('elephantm_maintenance_enabled', false);
    $elephantm_title = get_option('elephantm_maintenance_title', 'Maintenance mode is on');
    $elephantm_message = get_option('elephantm_maintenance_message', 'Site will be available soon. Thank you for your patience!');
    $elephantm_phone = get_option('elephantm_maintenance_phone', '');
    $elephantm_social = get_option('elephantm_maintenance_social', '');
    $elephantm_bg = get_option('elephantm_maintenance_bg', '');
    if(empty($elephantm_bg)){
        $elephantm_bg = plugin_dir_url(__FILE__).'/assets/images/maintenance.jpg';
    }
    if(empty($elephantm_title)){
       $elephantm_title = 'Maintenance mode is on'; 
    }
    if(empty($elephantm_message)){
        $elephantm_message = 'Site will be available soon. Thank you for your patience!';
    }
    ?>
    <div class="wrap elephantm-maintenance">
        <div class="elephantm-first">
            <h2>Maintenance Mode Settings</h2>
            <form method="post" enctype="multipart/form-data">
                <?php wp_nonce_field('elephantm_maintenance_save_settings'); ?>
                <label><input type="checkbox" name="elephantm_maintenance_toggle" <?php checked($elephantm_enabled, true); ?>> Enable Maintenance Mode</label><br><br>

                <label>Title:<br><input id="elephantm-title-input" type="text" name="elephantm_maintenance_title" value="<?php echo esc_attr($elephantm_title); ?>" class="regular-text"></label><br><br>
                
                <label>Message:<br><textarea id="elephantm-message-input" name="elephantm_maintenance_message" class="large-text"><?php echo esc_textarea($elephantm_message); ?></textarea></label><br><br>

                <label>Phone:<br><input id="elephantm-phone-input" type="text" name="elephantm_maintenance_phone" value="<?php echo esc_attr($elephantm_phone); ?>" class="regular-text"></label><br><br>

                <label>Social Link (e.g., https://website.com):<br>
                    <input type="url" id="elephantm-social-input" name="elephantm_maintenance_social" value="<?php echo esc_url($elephantm_social); ?>" class="regular-text">
                </label><br><br>

                <label>Background Image:<br>
                    <input type="file" id="elephantm-file-input" name="elephantm_maintenance_bg"  accept="image/png, image/gif, image/jpeg">
                    <?php if ($elephantm_bg): ?>
                        <p><img id="elephantm-image" src="<?php echo esc_url($elephantm_bg); ?>" style="max-width:200px;height:auto;"></p>
                    <?php endif; ?>
                </label><br><br>
                    <button type="submit" class="button button-primary">Save Settings</button>
            </form>
        </div>
        <div class="elephantm-second">
            <div class="elephantm-body" style="background: url('<?php echo esc_url($elephantm_bg); ?>')">
                <h1 id="elephantm-title"><?php echo esc_html($elephantm_title); ?></h1>
                <p class="elephantm-message" id="elephantm-message"><?php echo esc_html($elephantm_message); ?></p>
                <p>&copy; 2025 <?php echo esc_html( get_bloginfo( 'name' ) ); ?></p>
                <div class="icons">
                    <a id="elephantm-phone" href="tel:<?php echo esc_html($elephantm_phone); ?>"><img src="<?php echo esc_url(plugin_dir_url(__FILE__).'/assets/images/phone.png'); ?>" /></a>
                    <a id="elephantm-social" href="<?php echo esc_url($elephantm_social); ?>" class="follow-us">Follow us</a>
                </div>
            </div>
        </div>
    </div>
    <?php
}

/**
 * Enqueue admin styles (only on plugin page)
 */
function elephantm_maintenance_admin_styles($hook) {
    if ($hook !== 'toplevel_page_elephantm-maintenance') {
        return;
    }
    wp_enqueue_style('elephantm-maintenance-style', plugin_dir_url( __FILE__ ) . 'assets/css/elephantm-style.css', [], '1.0.0   ');
    wp_enqueue_script('elephantm-maintenance-js', plugin_dir_url( __FILE__ ) . 'assets/js/elephantm-maintenance-settings.js', [], '1.0.0   ');
}

add_action( 'admin_enqueue_scripts', 'elephantm_maintenance_admin_styles' );

/**
 * Maintenance status in the admin_bar config
 */

add_action('admin_bar_menu', function ($wp_admin_bar) {
    if (!current_user_can('manage_options')) return;

    $elephantm_enabled = get_option('elephantm_maintenance_enabled', false);
    $elephantm_status = $elephantm_enabled ? '🛑 ON' : '✅ OFF';

    $wp_admin_bar->add_node([
        'id'    => 'maintenance_status',
        'title' => 'Maintenance: ' . $elephantm_status,
        'href'  => admin_url('admin.php?page=elephantm-maintenance'),
    ]);
}, 100);


/**
 * Enqueue frontend styles (only if maintenance mode is ON)
 */
function elephantm_maintenance_frontend_styles() {
    $enabled = get_option('elephantm_maintenance_enabled', false);
    if ($enabled && !is_admin() && !current_user_can('manage_options')) {
        wp_enqueue_style('elephantm-frontend-style', plugin_dir_url( __FILE__ ) . 'assets/css/elephantm-frontend.css', [], '1.0.0');
    }
}
add_action( 'wp_enqueue_scripts', 'elephantm_maintenance_frontend_styles' );

/**
 * Template redirect config
 */

add_action('template_redirect', function () {
    if (is_admin() || current_user_can('manage_options')) return;
    $elephantm_enabled = get_option('elephantm_maintenance_enabled', false);
    if (!$elephantm_enabled) return;
    $elephantm_title = get_option('elephantm_maintenance_title', 'Maintenance mode is on');
    $elephantm_message = get_option('elephantm_maintenance_message', 'Site will be available soon. Thank you for your patience!');
    $elephantm_phone = get_option('elephantm_maintenance_phone', '');
    $elephantm_social = get_option('elephantm_maintenance_social', '');
    $elephantm_bg = get_option('elephantm_maintenance_bg', '');
    if(empty($elephantm_bg)){
        $elephantm_bg = plugin_dir_url(__FILE__).'/assets/images/maintenance.jpg';
    }
    if(empty($elephantm_title)){
       $elephantm_title = 'Maintenance mode is on'; 
    }
    if(empty($elephantm_message)){
        $elephantm_message = 'Site will be available soon. Thank you for your patience!';
    }
    /**
     * Front end template 
     */
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo esc_html($elephantm_title); ?></title>
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@200..800&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
        <link href="<?php echo esc_url(plugin_dir_url(__FILE__) . 'assets/css/elephantm-frontend.css'); ?>" rel="stylesheet">
    </head>
    <body class="elephantm-body" style="background: url('<?php echo esc_url($elephantm_bg); ?>')">
        <h1><?php echo esc_html($elephantm_title); ?></h1>
        <p class="elephantm-message"><?php echo esc_html($elephantm_message); ?></p>
        <p>&copy; 2025 <?php echo esc_html( get_bloginfo( 'name' ) ); ?></p>
        <div class="icons">
            <a href="tel:<?php echo esc_html($elephantm_phone); ?>"><img src="<?php echo esc_url(plugin_dir_url(__FILE__).'/assets/images/phone.png'); ?>" /></a>
            <a href="<?php echo esc_url($elephantm_social); ?>" class="follow-us">Follow us</a>
        </div>
    </body>
    </html>
    <?php
    exit;
});
