<?php

namespace GriffinForms\Admin\App\Html\Widgets;

class Format extends \GriffinForms\Admin\App\Html\Format
{
    protected $response = array();

    protected function checkNonce($action)
    {
        $nonce_status = isset($_POST['nonce']) ? wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), $action) : false;

        if ($nonce_status === false) {
            $action = str_replace('_', ' ', $action);
            $this->response['success'] = false;
            // translators: %s refers to the action that was aborted due to a nonce error
            $this->response['msg'] = sprintf(__('Nonce error. Action "%s" aborted. Please try again later.', 'griffinforms-form-builder'), $action);
            echo wp_json_encode($this->response);
            wp_die();
        }
    }

    protected function checkCapabilities()
    {
        if (!current_user_can('manage_options')) {
            $this->response['success'] = false;
            $this->response['msg'] = __('You are not allowed to do this.', 'griffinforms-form-builder');
            echo wp_json_encode($this->response);
            wp_die();
        }
    }

    protected function nonceError()
    {
        echo '<div class="gf-notice gf-notice-danger m-2" role="alert">';
        echo esc_html(__('Nonce error. Please try again later.', 'griffinforms-form-builder'));
        echo '</div>';
        wp_die();
    }
    
    protected function missingItemError($type)
    {
        echo '<div class="gf-notice gf-notice-danger mt-4" role="alert">';
        // translators: %s refers to the type of item that does not exist
        echo esc_html(sprintf(__('This %s does not exist.', 'griffinforms-form-builder'), $type));
        echo '</div>';
        wp_die();
    }
}
