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

/**
 * Plugin Name: Kamran Under Construction Mode
 * Description: Display a customizable "Coming Soon" page while your site is in development.
 * Version: 1.0.0
 * Author: Kamranweb
 * Author URI: https://kamran-webwork.github.io/portfolio/
 * License: GPL2+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: kamran-under-construction-mode
 */



// Include settings page
require_once plugin_dir_path(__FILE__) . 'includes/settings-page.php';

// Enqueue admin scripts for media uploader
add_action('admin_enqueue_scripts', function ($hook) {
    if ($hook === 'settings_page_under-construction-mode') {
        wp_enqueue_media();
        wp_enqueue_script(
            'under-construction-admin',
            plugin_dir_url(__FILE__) . 'assets/js/script.js',
            array('jquery'),
            '1.0.0',
            true
        );
    }
});

// Enqueue front-end assets
function under_construction_mode_enqueue_assets() {
    if (!current_user_can('edit_themes') && !is_user_logged_in()) {
        $version = '1.0.0'; // Plugin version for cache busting
        wp_enqueue_style(
            'under-construction-mode-style',
            plugin_dir_url(__FILE__) . 'assets/css/style.css',
            array(),
            $version
        );
        wp_enqueue_script(
            'under-construction-mode-script',
            plugin_dir_url(__FILE__) . 'assets/js/script.js',
            array('jquery'),
            $version,
            true
        );
    }
}
add_action('wp_enqueue_scripts', 'under_construction_mode_enqueue_assets');

add_action('admin_enqueue_scripts', function($hook) {
    if ($hook === 'settings_page_kamran-under-construction-mode') {
        wp_enqueue_media();
        wp_enqueue_script(
            'ucm-admin-js',
            plugin_dir_url(__FILE__) . 'assets/js/admin.js', // ✅ FIXED path
            array('jquery'),
            '1.0.1',
            true
        );
    }
});


add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'ucm_plugin_action_links');

function ucm_plugin_action_links($links) {
    $settings_link = '<a href="' . admin_url('options-general.php?page=kamran-under-construction-mode') . '">' . __('Settings', 'kamran-under-construction-mode') . '</a>';
    array_unshift($links, $settings_link);
    return $links;
}

add_action('template_redirect', 'ucm_show_coming_soon');

function ucm_show_coming_soon() {
    // Only show to visitors (not admins or logged-in users)
    if (!current_user_can('edit_themes') && !is_user_logged_in()) {
        $enabled = get_option('ucm_enabled');
        if ($enabled) {
            // Load the custom coming soon template
            include plugin_dir_path(__FILE__) . 'templates/coming-soon-template.php';
            exit; // Prevent the rest of the theme from loading
        }
    }
}