templates_store = $templates_store; } /** * Handle creating a form from a template or a blank form. * * @since 2.7 * * @return void */ public function handle() { check_ajax_referer( 'create_from_template', 'nonce' ); if ( ! GFCommon::current_user_can_any( 'gravityforms_create_form' ) ) { wp_send_json_error( 'Unauthorized', 403 ); } $this->template_id = sanitize_text_field( rgpost( 'templateId' ) ); $this->form_title = sanitize_text_field( rgpost( 'form_title' ) ); $this->form_description = sanitize_text_field( rgpost( 'form_description' ) ); if ( ! $this->template_id || ! $this->form_title ) { wp_send_json_error( array( 'message' => 'Missing required parameter' ), 400 ); } if ( $this->template_id === 'blank' ) { $template = new GF_Template_Library_Template( array( 'id' => 'blank', 'title' => '', 'description' => '', 'form_meta' => array( 'fields' => array(), ), ) ); } else { $template = $this->templates_store->get( $this->template_id ); } if ( ! is_a( $template, GF_Template_Library_Template::class ) ) { wp_send_json_error( array( 'message' => 'Invalid template ID' ), 400 ); } $form_meta = $template->get_form_meta(); $form_meta['title'] = $this->form_title; $form_meta['description'] = $this->form_description; if ( $this->template_id !== 'blank' ) { $form_meta['template_id'] = $this->template_id; } $form_crud_handler = GFForms::get_service_container()->get( GF_Save_Form_Service_Provider::GF_FORM_CRUD_HANDLER ); $result = $form_crud_handler->save( 0, wp_json_encode( $form_meta ) ); $status = rgar( $result, 'status' ); $form_id = rgars( $result, 'meta/id', false ); if ( is_numeric( $form_id ) && $form_id !== 0 ) { wp_send_json_success( array( 'form_id' => abs( $form_id ), ) ); } wp_send_json_error( array( 'message' => $status, ) ); } }