create-new-form.php
| 1 | <?php |
| 2 | /** |
| 3 | * Create new Form with Template and return the form ID. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc; |
| 10 | |
| 11 | use SRFM\Inc\Traits\Get_Instance; |
| 12 | use WP_Error; |
| 13 | use WP_REST_Response; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Create New Form. |
| 21 | * |
| 22 | * @since 0.0.1 |
| 23 | */ |
| 24 | class Create_New_Form { |
| 25 | use Get_Instance; |
| 26 | |
| 27 | /** |
| 28 | * Constructor |
| 29 | * |
| 30 | * @since 0.0.1 |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add custom API Route create-new-form. |
| 38 | * |
| 39 | * @return void |
| 40 | * @since 0.0.1 |
| 41 | */ |
| 42 | public function register_custom_endpoint() { |
| 43 | register_rest_route( |
| 44 | 'sureforms/v1', |
| 45 | '/create-new-form', |
| 46 | [ |
| 47 | 'methods' => 'POST', |
| 48 | 'callback' => [ $this, 'create_form' ], |
| 49 | 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ], |
| 50 | ] |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get default post metas for form when creating using template. |
| 56 | * |
| 57 | * @return array<string, int|string|bool> Default meta keys. |
| 58 | * @since 2.0.0 updated the return type. |
| 59 | * @since 0.0.2 |
| 60 | */ |
| 61 | public static function get_default_meta_keys() { |
| 62 | $default_values = [ |
| 63 | '_srfm_submit_button_text' => __( 'Submit', 'sureforms' ), |
| 64 | '_srfm_use_label_as_placeholder' => false, |
| 65 | '_srfm_single_page_form_title' => 1, |
| 66 | '_srfm_instant_form' => '', |
| 67 | '_srfm_form_container_width' => 650, |
| 68 | '_srfm_bg_type' => 'image', |
| 69 | '_srfm_bg_image' => '', |
| 70 | '_srfm_cover_image' => '', |
| 71 | '_srfm_bg_color' => '#ffffff', |
| 72 | '_srfm_submit_width_backend' => 'max-content', |
| 73 | '_srfm_submit_alignment' => 'left', |
| 74 | '_srfm_submit_alignment_backend' => '100%', |
| 75 | '_srfm_submit_width' => '', |
| 76 | '_srfm_inherit_theme_button' => false, |
| 77 | '_srfm_additional_classes' => '', |
| 78 | '_srfm_submit_type' => 'message', |
| 79 | '_srfm_form_recaptcha' => 'none', |
| 80 | '_srfm_is_inline_button' => false, // @since 2.0.0 -- adding required default value. |
| 81 | ]; |
| 82 | |
| 83 | return apply_filters( 'srfm_add_post_meta_defaults', $default_values ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create new form post from selected template. |
| 88 | * |
| 89 | * @param \WP_REST_Request $data Form Markup Data. |
| 90 | * |
| 91 | * @return WP_Error|WP_REST_Response |
| 92 | * @since 0.0.1 |
| 93 | */ |
| 94 | public static function create_form( $data ) { |
| 95 | $nonce = Helper::get_string_value( $data->get_header( 'X-WP-Nonce' ) ); |
| 96 | $nonce = sanitize_text_field( $nonce ); |
| 97 | |
| 98 | if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 99 | wp_send_json_error( |
| 100 | [ |
| 101 | 'message' => __( 'Nonce verification failed.', 'sureforms' ), |
| 102 | ] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | $form_info = $data->get_body(); |
| 107 | $form_info_obj = json_decode( $form_info ); |
| 108 | |
| 109 | // Check if JSON decoding was successful and $form_info_obj is an object. |
| 110 | if ( json_last_error() !== JSON_ERROR_NONE || ! is_object( $form_info_obj ) ) { |
| 111 | wp_send_json_error( |
| 112 | [ |
| 113 | 'message' => __( 'Invalid JSON format.', 'sureforms' ), |
| 114 | ] |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | $required_properties = [ 'template_name', 'form_data' ]; |
| 119 | |
| 120 | // Check if required properties exist in the $form_info_obj. |
| 121 | foreach ( $required_properties as $property ) { |
| 122 | if ( ! property_exists( $form_info_obj, $property ) ) { |
| 123 | wp_send_json_error( |
| 124 | [ |
| 125 | 'message' => __( 'Missing required properties in form info.', 'sureforms' ), |
| 126 | ] |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | $title = $form_info_obj->template_name ?? ''; |
| 132 | $content = $form_info_obj->form_data ?? ''; |
| 133 | |
| 134 | // Create post metas for the creating form. |
| 135 | $post_metas = apply_filters( |
| 136 | 'srfm_modify_ai_post_metas', |
| 137 | [], |
| 138 | $form_info_obj, |
| 139 | ); |
| 140 | |
| 141 | $post_id = wp_insert_post( |
| 142 | [ |
| 143 | 'post_title' => $title, |
| 144 | 'post_content' => $content, |
| 145 | 'meta_input' => $post_metas, |
| 146 | 'post_status' => 'draft', |
| 147 | 'post_type' => 'sureforms_form', |
| 148 | ] |
| 149 | ); |
| 150 | |
| 151 | if ( ! empty( $post_id ) ) { |
| 152 | |
| 153 | /** |
| 154 | * Update _srfm_is_ai_generated meta to true. |
| 155 | * If the request is coming here then the form is AI generated. |
| 156 | */ |
| 157 | update_post_meta( $post_id, '_srfm_is_ai_generated', true ); |
| 158 | |
| 159 | return new WP_REST_Response( |
| 160 | [ |
| 161 | 'message' => __( 'SureForms Form created successfully.', 'sureforms' ), |
| 162 | 'id' => $post_id, |
| 163 | ] |
| 164 | ); |
| 165 | } |
| 166 | wp_send_json_error( |
| 167 | [ |
| 168 | 'message' => __( 'Error creating SureForms Form, ', 'sureforms' ), |
| 169 | ] |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 |