<?php

/**
FRONT PAGES
 */
add_action('wp_enqueue_scripts', 'dragblock_form_front_scripts');
function dragblock_form_front_scripts()
{
    global $dragblock_form_script_required;
    if (!$dragblock_form_script_required) {
        return;
    }
    // // @fix: why this also be enqueued in the back-end editor (not the iframe)?
    dragblock_enqueue(DRAGBLOCK_FORM_ENTRY, 'build/applications/form-entries/client/index.js', array('jquery'));


    ////////////////////////////////
    // DragBlock Form Protection
    wp_add_inline_script(DRAGBLOCK_FORM_ENTRY, 'var DRAG_BLOCK_FORM_NONCE_ACTION ="' . esc_js(wp_create_nonce('dragblock/form-nonce-action')) . '"', 'before');

    // this is submitted by a js bot (to fast in filling)
    // Start the session
    if (!session_id()) {
        session_start();
    }

    // Generate a unique ID
    $unique_id = uniqid();

    // Store the ID in the session
    $_SESSION[$unique_id] = time();


    wp_add_inline_script(DRAGBLOCK_FORM_ENTRY, 'var DRAG_BLOCK_FORM_SESSION_TOKEN ="' . $unique_id . '"', 'before');
}

add_action('admin_enqueue_scripts', 'dragblock_form_admin_scripts');
function dragblock_form_admin_scripts()
{
    global $pagenow;

    // Check if we are on the edit.php page and the post type is "dragblockformmsg"
    if ('edit.php' === $pagenow && isset($_GET['post_type']) && DRAGBLOCK_FORM_ENTRY === $_GET['post_type']) {
        dragblock_enqueue(DRAGBLOCK_FORM_ENTRY, 'build/applications/form-entries/client/index.css');
    }
}
