# sureforms/0.0.13/inc/rest-api.php

SureForms – Contact Form Builder, AI Forms, Payment Form, Survey &amp; Quiz, version 0.0.13. 159 lines.

- Page: https://pluginprobe.com/plugins/sureforms/0.0.13/code/inc/rest-api.php
- Raw: https://pluginprobe.com/plugins/sureforms/0.0.13/raw/inc/rest-api.php
- Modified: 2024-09-18T15:56:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sureforms/0.0.13/code/inc/rest-api.php#L10-L20`.

```php
<?php
/**
 * Rest API Manager Class.
 *
 * @package sureforms.
 */

namespace SRFM\Inc;

use SRFM\Inc\Traits\Get_Instance;
use SRFM\Inc\AI_Form_Builder\AI_Form_Builder;
use SRFM\Inc\AI_Form_Builder\Field_Mapping;
use SRFM\Inc\AI_Form_Builder\AI_Auth;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Rest API handler class.
 *
 * @since 0.0.7
 */
class Rest_Api {

	use Get_Instance;

	/**
	 * Constructor
	 *
	 * @since 0.0.7
	 * @return void
	 */
	public function __construct() {
		add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
	}

	/**
	 * Register endpoints
	 *
	 * @since 0.0.7
	 * @return void
	 */
	public function register_endpoints() {

		$prefix       = 'sureforms';
		$version_slug = 'v1';

		$endpoints = $this->get_endpoints();

		foreach ( $endpoints as $endpoint => $args ) {
			register_rest_route(
				$prefix . '/' . $version_slug,
				$endpoint,
				$args
			);
		}
	}

	/**
	 * Check if user can edit posts
	 *
	 * @since 0.0.7
	 * @return bool
	 */
	public function can_edit_posts() {
		return current_user_can( 'edit_posts' );
	}

	/**
	 * Get endpoints
	 *
	 * @since 0.0.7
	 * @return array<array<mixed>>
	 */
	private function get_endpoints() {
		return [
			'generate-block-slugs' => [
				'methods'             => 'POST',
				'callback'            => [ $this, 'generate_block_slugs_by_content' ],
				'permission_callback' => [ $this, 'can_edit_posts' ],
			],
			'generate-form'        => [
				'methods'             => 'POST',
				'callback'            => [ AI_Form_Builder::get_instance(), 'generate_ai_form' ],
				'permission_callback' => [ $this, 'can_edit_posts' ],
				'args'                => [
					'use_system_message' => [
						'sanitize_callback' => [ $this, 'sanitize_boolean_field' ],
					],
				],
			],
			// This route is used to map the AI response to SureForms fields markup.
			'map-fields'           => [
				'methods'             => 'POST',
				'callback'            => [ Field_Mapping::get_instance(), 'generate_gutenberg_fields_from_questions' ],
				'permission_callback' => [ $this, 'can_edit_posts' ],
			],
			// This route is used to initiate auth process when user tries to authenticate on billing portal.
			'initiate-auth'        => [
				'methods'             => 'GET',
				'callback'            => [ AI_Auth::get_instance(), 'get_auth_url' ],
				'permission_callback' => [ $this, 'can_edit_posts' ],
			],
			// This route is to used to decrypt the access key and save it in the database.
			'handle-access-key'    => [
				'methods'             => 'POST',
				'callback'            => [ AI_Auth::get_instance(), 'handle_access_key' ],
				'permission_callback' => [ $this, 'can_edit_posts' ],
			],
		];
	}

	/**
	 * Checks whether the value is boolean or not.
	 *
	 * @param mixed $value value to be checked.
	 * @since 0.0.8
	 * @return boolean
	 */
	public function sanitize_boolean_field( $value ) {
		return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
	}

	/**
	 * Generate the block slugs as per the request by parsing the post content.
	 *
	 * @param  \WP_REST_Request $request Full details about the request.
	 * @since 0.0.7
	 * @return void
	 */
	public function generate_block_slugs_by_content( $request ) {
		$nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );

		if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
			wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) );
		}

		$slugs   = [];
		$updated = false;
		$params  = $request->get_params();

		if ( empty( $params['formID'] ) ) {
			wp_send_json_error( __( 'Invalid request. Form ID missing.', 'sureforms' ) );
		}

		$form    = get_post( absint( $params['formID'] ) );
		$content = ! empty( $params['content'] ) ? wp_kses_post( $params['content'] ) : '';

		if ( ! is_null( $form ) ) {
			Helper::process_blocks( parse_blocks( $form->post_content ), $slugs, $updated );
		}

		Helper::process_blocks( parse_blocks( $content ), $slugs, $updated, '', true );

		wp_send_json_success( $slugs );
	}
}

```
