rest-api.php
| 1 | <?php |
| 2 | /** |
| 3 | * Rest API Manager Class. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | */ |
| 7 | |
| 8 | namespace SRFM\Inc; |
| 9 | |
| 10 | use SRFM\Inc\Traits\Get_Instance; |
| 11 | use SRFM\Inc\AI_Form_Builder\AI_Form_Builder; |
| 12 | use SRFM\Inc\AI_Form_Builder\Field_Mapping; |
| 13 | use SRFM\Inc\AI_Form_Builder\AI_Auth; |
| 14 | use SRFM\Inc\Database\Tables\Entries; |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Rest API handler class. |
| 21 | * |
| 22 | * @since 0.0.7 |
| 23 | */ |
| 24 | class Rest_Api { |
| 25 | |
| 26 | use Get_Instance; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @since 0.0.7 |
| 32 | * @return void |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | add_action( 'rest_api_init', [ $this, 'register_endpoints' ] ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register endpoints |
| 40 | * |
| 41 | * @since 0.0.7 |
| 42 | * @return void |
| 43 | */ |
| 44 | public function register_endpoints() { |
| 45 | |
| 46 | $prefix = 'sureforms'; |
| 47 | $version_slug = 'v1'; |
| 48 | |
| 49 | $endpoints = $this->get_endpoints(); |
| 50 | |
| 51 | foreach ( $endpoints as $endpoint => $args ) { |
| 52 | register_rest_route( |
| 53 | $prefix . '/' . $version_slug, |
| 54 | $endpoint, |
| 55 | $args |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Check if user can edit posts |
| 62 | * |
| 63 | * @since 0.0.7 |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function can_edit_posts() { |
| 67 | return current_user_can( 'edit_posts' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get endpoints |
| 72 | * |
| 73 | * @since 0.0.7 |
| 74 | * @return array<array<mixed>> |
| 75 | */ |
| 76 | private function get_endpoints() { |
| 77 | return [ |
| 78 | 'generate-block-slugs' => [ |
| 79 | 'methods' => 'POST', |
| 80 | 'callback' => [ $this, 'generate_block_slugs_by_content' ], |
| 81 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 82 | ], |
| 83 | 'generate-form' => [ |
| 84 | 'methods' => 'POST', |
| 85 | 'callback' => [ AI_Form_Builder::get_instance(), 'generate_ai_form' ], |
| 86 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 87 | 'args' => [ |
| 88 | 'use_system_message' => [ |
| 89 | 'sanitize_callback' => [ $this, 'sanitize_boolean_field' ], |
| 90 | ], |
| 91 | ], |
| 92 | ], |
| 93 | // This route is used to map the AI response to SureForms fields markup. |
| 94 | 'map-fields' => [ |
| 95 | 'methods' => 'POST', |
| 96 | 'callback' => [ Field_Mapping::get_instance(), 'generate_gutenberg_fields_from_questions' ], |
| 97 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 98 | ], |
| 99 | // This route is used to initiate auth process when user tries to authenticate on billing portal. |
| 100 | 'initiate-auth' => [ |
| 101 | 'methods' => 'GET', |
| 102 | 'callback' => [ AI_Auth::get_instance(), 'get_auth_url' ], |
| 103 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 104 | ], |
| 105 | // This route is to used to decrypt the access key and save it in the database. |
| 106 | 'handle-access-key' => [ |
| 107 | 'methods' => 'POST', |
| 108 | 'callback' => [ AI_Auth::get_instance(), 'handle_access_key' ], |
| 109 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 110 | ], |
| 111 | // This route is to get the form submissions for the last 30 days. |
| 112 | 'entries-chart-data' => [ |
| 113 | 'methods' => 'GET', |
| 114 | 'callback' => [ Entries::get_instance(), 'get_entries_chart_data' ], |
| 115 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 116 | ], |
| 117 | ]; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Checks whether the value is boolean or not. |
| 122 | * |
| 123 | * @param mixed $value value to be checked. |
| 124 | * @since 0.0.8 |
| 125 | * @return boolean |
| 126 | */ |
| 127 | public function sanitize_boolean_field( $value ) { |
| 128 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Generate the block slugs as per the request by parsing the post content. |
| 133 | * |
| 134 | * @param \WP_REST_Request $request Full details about the request. |
| 135 | * @since 0.0.7 |
| 136 | * @return void |
| 137 | */ |
| 138 | public function generate_block_slugs_by_content( $request ) { |
| 139 | $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 140 | |
| 141 | if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 142 | wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) ); |
| 143 | } |
| 144 | |
| 145 | $slugs = []; |
| 146 | $updated = false; |
| 147 | $params = $request->get_params(); |
| 148 | |
| 149 | if ( empty( $params['formID'] ) ) { |
| 150 | wp_send_json_error( __( 'Invalid request. Form ID missing.', 'sureforms' ) ); |
| 151 | } |
| 152 | |
| 153 | $form = get_post( absint( $params['formID'] ) ); |
| 154 | $content = ! empty( $params['content'] ) ? wp_kses_post( $params['content'] ) : ''; |
| 155 | |
| 156 | if ( ! is_null( $form ) ) { |
| 157 | Helper::process_blocks( parse_blocks( $form->post_content ), $slugs, $updated ); |
| 158 | } |
| 159 | |
| 160 | Helper::process_blocks( parse_blocks( $content ), $slugs, $updated, '', true ); |
| 161 | |
| 162 | wp_send_json_success( $slugs ); |
| 163 | } |
| 164 | } |
| 165 |