<?php
/*-----------------------------------------------*/
/*  EXTHEM.ES
/*  PREMIUM WORDRESS THEMES
/*
/*  STOP DON'T TRY EDIT
/*  IF YOU DON'T KNOW PHP 
/*
/*  As Errors In Your Themes
/*  Are Not The Responsibility
/*  Of The DEVELOPERS
/*  @EXTHEM.ES
/*  
/*  Follow Social Media Exthem.es
/*  Youtube : https://www.youtube.com/channel/UCpcZNXk6ySLtwRSBN6fVyLA
/*  Facebook : https://www.facebook.com/groups/exthem.es
/*  Twitter : https://x.com/bangreyblog
/*  Instagram : https://www.instagram.com/exthem.es/
/*  Telegram : https://t.me/exthemes_helps
/*  TikTok : https://www.tiktok.com/@exthemes
/*	More Premium Themes Visit Now On https://exthem.es/
/*-----------------------------------------------*/

if ( ! defined( 'ABSPATH' ) ) exit;

// ===============================================
// 1. GROWL-STYLE TOAST NOTIFICATIONS (Cantik & Modern)
// ===============================================
function themes_growl_toasts_init() {
    if ( ! is_admin() ) return;
    ?>
    <style>
    #growl-toasts {
        position: fixed;
        top: 50px;
        right: 20px;
        z-index: 999999;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        pointer-events: none;
    }
    #growl-toasts > * { pointer-events: all; }

    .growl-toast {
        min-width: 360px;
        max-width: 520px;
        margin-bottom: 16px;
        background: rgba(30, 41, 59, 0.98);
        color: white;
        border-radius: 16px;
        box-shadow: 0 16px 48px rgba(0,0,0,0.45);
        overflow: hidden;
        transform: translateX(170%) scale(0.92);
        opacity: 0;
        animation: growlIn 0.7s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards,
                   growlOut 0.7s 20s forwards;
        backdrop-filter: blur(16px);
        border: 1px solid rgba(255,255,255,0.15);
    }
    .growl-toast.warning { background: linear-gradient(135deg, #f59e0b, #d97706); }

    .growl-content {
        padding: 24px 28px;
        display: flex;
        align-items: center;
        gap: 20px;
    }
    .growl-icon { font-size: 36px; }
    .growl-message {
        flex: 1;
        font-size: 16.5px;
        line-height: 1.6;
    }
    .growl-message strong { font-weight: 700; }
    .growl-message a {
        color: white;
        text-decoration: underline;
        font-weight: 600;
        margin-left: 6px;
    }
    .growl-close {
        background: none;
        border: none;
        color: rgba(255,255,255,0.8);
        font-size: 32px;
        cursor: pointer;
        padding: 0 12px;
        line-height: 1;
    }
    .growl-close:hover { color: white; }

    @keyframes growlIn { to { transform: translateX(0) scale(1); opacity: 1; } }
    @keyframes growlOut { to { transform: translateX(170%) scale(0.92); opacity: 0; } }
    </style>

    <div id="growl-toasts"></div>

    <script>
    window.growl = function(message, type = 'info', duration = 5000) {
        const container = document.getElementById('growl-toasts');
        if (!container) return;

        const toast = document.createElement('div');
        toast.className = `growl-toast ${type}`;

        const icons = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' };

        toast.innerHTML = `
            <div class="growl-content">
                <div class="growl-icon">${icons[type] || 'ℹ️'}</div>
                <div class="growl-message">${message}</div>
                <button class="growl-close" onclick="this.closest('.growl-toast').remove()">&times;</button>
            </div>
        `;

        container.prepend(toast);

        if (duration > 0) {
            setTimeout(() => toast.remove(), duration + 700);
        }
    };
    </script>
    <?php
}
add_action('admin_footer', 'themes_growl_toasts_init');


// ===============================================
// 2. NOTICE STANDAR WORDPRESS (Tetap Tampil Selama Belum Update)
// ===============================================
add_action('admin_notices', 'themes_theme_update_notice');

function themes_theme_update_notice() {
    if ( ! current_user_can('update_themes') ) {
        return;
    }

    $theme_slug = get_template();
    $update_data = get_site_transient('update_themes');

    if ( empty($update_data->response[ $theme_slug ]) ) {
        return; // tidak ada update
    }

    $update      = $update_data->response[ $theme_slug ];
    $new_version = $update['new_version'];
    $theme       = wp_get_theme();
    $theme_name  = $theme->get('Name');

    ?>
    <div class="notice notice-warning is-dismissible">
        <p>
		
            <strong>🔔 Theme Update Available </strong><br>
            <?php echo esc_html($theme_name); ?> version <strong><?php echo esc_html($new_version); ?></strong> ready to be Update.<br>
            <a href="<?php echo esc_url(admin_url('themes.php')); ?>"><strong>Update now</strong></a> or check details on the themes page.
        </p>
    </div>
    <?php
}


// ===============================================
// 3. GROWL TOAST (Muncul Sekali Sehari, Tambahan Notifikasi Cantik)
// ===============================================
add_action('admin_init', 'themes_show_growl_for_theme_update');

function themes_show_growl_for_theme_update() {
    if ( ! is_admin() || ! current_user_can('update_themes') ) {
        return;
    }

    $theme_slug 	= get_template();
    $update_data 	= get_site_transient('update_themes');

    if ( empty($update_data->response[ $theme_slug ]) ) {
        return;
    }

    $update      = $update_data->response[ $theme_slug ];
    $new_version = $update['new_version'];
    $theme       = wp_get_theme();
    $theme_name  = $theme->get('Name');
    $current_ver = $theme->get('Version');

    // Hanya sekali sehari per user
    $transient_key = 'themes_growl_update_shown_' . get_current_user_id();
    if ( get_transient($transient_key) === 'yes' ) {
        return;
    }

    add_action('admin_footer', function() use ($theme_name, $new_version, $current_ver) {
        ?>
        <script>
        document.addEventListener('DOMContentLoaded', () => {
            setTimeout(() => {
                if (typeof growl === 'function') {
                    growl(
                        `<strong>Update Available! ⚠️</strong><br><strong><?php echo esc_js($theme_name); ?></strong> version <strong><?php echo esc_js($new_version); ?></strong> has been released.<br>Saat ini: <?php echo esc_js($current_ver); ?>.<br><a href="<?php echo esc_url(admin_url('update-core.php')); ?>">Update Now →</a>`,
                        'warning',
                        22000
                    );
                }
            }, 1800);
        });
        </script>
        <?php
    });

    set_transient($transient_key, 'yes', DAY_IN_SECONDS);
}