<?php

namespace GriffinForms\Admin\App\Html\Modals\FormOptions;

trait Requests
{
    public function registerRestRoutes()
    {
        register_rest_route('griffinforms/v1', '/forms/(?P<form_id>\d+)/options', [
            'methods'             => \WP_REST_Server::CREATABLE,
            'callback'            => [$this, 'saveFormOptions'],
            'permission_callback' => [$this, 'canSaveFormOptions'],
            'args'                => [
                'form_id'   => [
                    'type'              => 'integer',
                    'required'          => true,
                    'sanitize_callback' => 'absint',
                ],
                'item_data' => [
                    'type'     => 'string',
                    'required' => true,
                ],
            ],
        ]);
    }

    public function canSaveFormOptions(): bool
    {
        return current_user_can('manage_options');
    }
}
