· 4 years ago · Jul 05, 2021, 07:58 AM
1<?php
2
3namespace JuiceBox\Components\ContactForm;
4
5use Timber;
6use GFAPI;
7//use GFCommon;
8
9/**
10 * Add custom short codes
11 */
12class ContactForm
13{
14 public static function register()
15 {
16 add_shortcode('contact-form', __CLASS__ . '::custom_contact_form');
17 add_action('wp_ajax_nopriv_contact_form', __CLASS__ . '::jb_ajax_contact_form');
18 add_action('wp_ajax_contact_form', __CLASS__ . '::jb_ajax_contact_form');
19 }
20
21 public static function custom_contact_form( $attrs )
22 {
23 $form_id = 4;
24 $form = GFAPI::get_form($form_id);
25 $page = 1;
26 $index = 0;
27 $fields = array();
28 $has_captcha = false;
29 $captcha_field_id = 0;
30 $captcha = false;
31 $paged = 1;
32 $fields_page = array();
33 $index_page = 0;
34
35 if( $form ) {
36 foreach( $form['fields'] as $field ) {
37 if( $field->pageNumber == $page ) {
38 $fields[$index][] = $field;
39 }else{
40 $page = $field->pageNumber;
41 $index++;
42 }
43
44 if( $field->type == 'captcha' ) {
45 $has_captcha = true;
46 //$captcha_field_id = $field->id;
47 }
48
49 if ( $field->type == 'page') {
50 $fields_page[$index_page][] = $field_page;
51 }
52 else {
53 $paged = $field->pageNumber;
54 $index_page++;
55 }
56 }
57 }
58
59 if( $has_captcha ) {
60 }
61
62
63 $data = array(
64 'site_link' => get_stylesheet_directory_uri(),
65 'form_id' => (int) $form_id,
66 'form' => GFAPI::get_form($form_id),
67 'fields' => $fields,
68 'has_captcha' => $has_captcha,
69 'site_key' => get_option( 'rg_gforms_captcha_public_key' ),
70 'secret_key' => get_option( 'rg_gforms_captcha_private_key' ),
71 );
72
73 return Timber::compile('ContactForm/template.twig', $form);
74 }
75
76 public static function jb_ajax_contact_form()
77 {
78 // $result = GFAPI::submit_form(4, $input_values );
79 $result = GFAPI::submit_form(4, $input_values );
80 echo json_encode( $result );
81 die();
82 }
83}
84