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\AI_Form_Builder\AI_Auth; |
| 11 | use SRFM\Inc\AI_Form_Builder\AI_Form_Builder; |
| 12 | use SRFM\Inc\AI_Form_Builder\Field_Mapping; |
| 13 | use SRFM\Inc\Database\Tables\Entries; |
| 14 | use SRFM\Inc\Traits\Get_Instance; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // Exit if accessed directly. |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Rest API handler class. |
| 22 | * |
| 23 | * @since 0.0.7 |
| 24 | */ |
| 25 | class Rest_Api { |
| 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 | * Checks whether the value is boolean or not. |
| 72 | * |
| 73 | * @param mixed $value value to be checked. |
| 74 | * @since 0.0.8 |
| 75 | * @return bool |
| 76 | */ |
| 77 | public function sanitize_boolean_field( $value ) { |
| 78 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the data for generating entries chart. |
| 83 | * |
| 84 | * @param \WP_REST_Request $request Full details about the request. |
| 85 | * @since 1.0.0 |
| 86 | * @return array<mixed> |
| 87 | */ |
| 88 | public function get_entries_chart_data( $request ) { |
| 89 | $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 90 | |
| 91 | if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 92 | wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) ); |
| 93 | } |
| 94 | |
| 95 | $params = $request->get_params(); |
| 96 | |
| 97 | if ( empty( $params ) ) { |
| 98 | wp_send_json_error( __( 'Request could not be processed.', 'sureforms' ) ); |
| 99 | } |
| 100 | |
| 101 | $after = is_array( $params ) && ! empty( $params['after'] ) ? sanitize_text_field( Helper::get_string_value( $params['after'] ) ) : ''; |
| 102 | |
| 103 | if ( empty( $after ) ) { |
| 104 | wp_send_json_error( __( 'Invalid date.', 'sureforms' ) ); |
| 105 | } |
| 106 | |
| 107 | $where = [ |
| 108 | [ |
| 109 | [ |
| 110 | 'key' => 'created_at', |
| 111 | 'value' => $after, |
| 112 | 'compare' => '>=', |
| 113 | ], |
| 114 | |
| 115 | ], |
| 116 | ]; |
| 117 | |
| 118 | return Entries::get_instance()->get_results( |
| 119 | $where, |
| 120 | 'created_at', |
| 121 | [ 'ORDER BY created_at DESC' ] |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get endpoints |
| 127 | * |
| 128 | * @since 0.0.7 |
| 129 | * @return array<array<mixed>> |
| 130 | */ |
| 131 | private function get_endpoints() { |
| 132 | /* |
| 133 | * @internal This filter is used to add custom endpoints. |
| 134 | * @since 1.2.0 |
| 135 | * @param array<array<mixed>> $endpoints Endpoints. |
| 136 | */ |
| 137 | return apply_filters( |
| 138 | 'srfm_rest_api_endpoints', |
| 139 | [ |
| 140 | 'generate-form' => [ |
| 141 | 'methods' => 'POST', |
| 142 | 'callback' => [ AI_Form_Builder::get_instance(), 'generate_ai_form' ], |
| 143 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 144 | 'args' => [ |
| 145 | 'use_system_message' => [ |
| 146 | 'sanitize_callback' => [ $this, 'sanitize_boolean_field' ], |
| 147 | ], |
| 148 | ], |
| 149 | ], |
| 150 | // This route is used to map the AI response to SureForms fields markup. |
| 151 | 'map-fields' => [ |
| 152 | 'methods' => 'POST', |
| 153 | 'callback' => [ Field_Mapping::get_instance(), 'generate_gutenberg_fields_from_questions' ], |
| 154 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 155 | ], |
| 156 | // This route is used to initiate auth process when user tries to authenticate on billing portal. |
| 157 | 'initiate-auth' => [ |
| 158 | 'methods' => 'GET', |
| 159 | 'callback' => [ AI_Auth::get_instance(), 'get_auth_url' ], |
| 160 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 161 | ], |
| 162 | // This route is to used to decrypt the access key and save it in the database. |
| 163 | 'handle-access-key' => [ |
| 164 | 'methods' => 'POST', |
| 165 | 'callback' => [ AI_Auth::get_instance(), 'handle_access_key' ], |
| 166 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 167 | ], |
| 168 | // This route is to get the form submissions for the last 30 days. |
| 169 | 'entries-chart-data' => [ |
| 170 | 'methods' => 'GET', |
| 171 | 'callback' => [ $this, 'get_entries_chart_data' ], |
| 172 | 'permission_callback' => [ $this, 'can_edit_posts' ], |
| 173 | ], |
| 174 | ] |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 |