wzp
8 天以前 09e2be12b274a16ead7e04bab8ed2f0534a792b8
映星教育/snippet-5-training.php
@@ -19,7 +19,12 @@
   ============================================================ */
add_action('template_redirect', function () {
    if (!empty($_GET['im_training_token'])) {
        if (!defined('DONOTCACHEPAGE')) {
            define('DONOTCACHEPAGE', true);
        }
        nocache_headers();
        header('Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0');
        header('Pragma: no-cache');
    }
}, 1);
@@ -98,6 +103,20 @@
.im-tr-btn-loading::after{content:"";position:absolute;width:20px;height:20px;top:50%;left:50%;margin:-10px 0 0 -10px;border:3px solid rgba(255,255,255,.3);border-top-color:#fff;border-radius:50%;animation:imTrSpin .6s linear infinite}
</style>';
    echo '<script>var imTrAjaxUrl="' . esc_url(admin_url('admin-ajax.php')) . '";</script>';
}
/* ============================================================
   AJAX: Fresh nonce (avoids stale nonce from full-page cache / long sessions)
   ============================================================ */
add_action('wp_ajax_im_training_refresh_nonce', 'im_ajax_training_refresh_nonce');
add_action('wp_ajax_nopriv_im_training_refresh_nonce', 'im_ajax_training_refresh_nonce');
function im_ajax_training_refresh_nonce()
{
    $token = sanitize_text_field($_POST['im_training_token'] ?? '');
    if (!$token || !IM_Candidate::get_by_training_token($token)) {
        wp_send_json_error(['message' => 'Invalid or expired training link.']);
    }
    wp_send_json_success(['nonce' => wp_create_nonce('im_training_' . $token)]);
}
/* ============================================================
@@ -193,7 +212,6 @@
    endif;
    $name = esc_html($candidate->preferred_name ?: $candidate->first_name);
    $nonce = wp_create_nonce('im_training_' . $token_str);
    $total = count($training_posts);
    // Build training data for JS
@@ -501,15 +519,30 @@
                document.getElementById('im-tr-progress-text').textContent = 'Progress: ' + completed + ' / ' + TOTAL;
            }
            var imTrToken = '<?= esc_js($token_str) ?>';
            function submitTrainingComplete() {
                var refresh = new FormData();
                refresh.append('action', 'im_training_refresh_nonce');
                refresh.append('im_training_token', imTrToken);
                fetch(imTrAjaxUrl, { method: 'POST', body: refresh, credentials: 'same-origin' })
                    .then(function (r) { return r.json(); })
                    .then(function (nr) {
                        if (!nr.success || !nr.data || !nr.data.nonce) {
                            alert((nr.data && nr.data.message) || 'Security check failed. Please refresh the page and try again.');
                            return;
                        }
                var fd = new FormData();
                fd.append('action', 'im_training_complete');
                fd.append('im_training_token', '<?= esc_js($token_str) ?>');
                fd.append('im_nonce', '<?= esc_js($nonce) ?>');
                        fd.append('im_training_token', imTrToken);
                        fd.append('im_nonce', nr.data.nonce);
                fetch(imTrAjaxUrl, { method: 'POST', body: fd })
                    .then(function (r) { return r.json(); })
                        return fetch(imTrAjaxUrl, { method: 'POST', body: fd, credentials: 'same-origin' });
                    })
                    .then(function (r) { return r ? r.json() : null; })
                    .then(function (res) {
                        if (!res) return;
                        if (res.success) {
                            var page = document.getElementById('im-tr-page');
                            page.innerHTML = '<div class="im-tr-center im-tr-fade-in">'