· 4 years ago · Jul 05, 2021, 01:42 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 }
18
19 public function hooks() {
20 add_action('wp_ajax_nopriv_contact_form', array($this, 'jb_ajax_contact_form'));
21 add_action('wp_ajax_contact_form', array($this, 'jb_ajax_contact_form'));
22 }
23
24 public static function custom_contact_form( $attrs )
25 {
26 $form_id = 4;
27 $form = GFAPI::get_form($form_id);
28 $page = 1;
29 $index = 0;
30 $fields = array();
31 $has_captcha = false;
32 $captcha_field_id = 0;
33 $captcha = false;
34 $paged = 1;
35 $fields_page = array();
36 $index_page = 0;
37
38 if( $form ) {
39 foreach( $form['fields'] as $field ) {
40 if( $field->pageNumber == $page ) {
41 $fields[$index][] = $field;
42 }else{
43 $page = $field->pageNumber;
44 $index++;
45 }
46
47 if( $field->type == 'captcha' ) {
48 $has_captcha = true;
49 //$captcha_field_id = $field->id;
50 }
51
52 if ( $field->type == 'page') {
53 $fields_page[$index_page][] = $field_page;
54 }
55 else {
56 $paged = $field->pageNumber;
57 $index_page++;
58 }
59 }
60 }
61
62 if( $has_captcha ) {
63 //$field = GFAPI::get_field( $form_id, $captcha_field_id );
64 //$captcha = GFCommon::get_field_input($field, '' , 0, $form_id);
65 }
66
67
68 $data = array(
69 'site_link' => get_stylesheet_directory_uri(),
70 'form_id' => (int) $form_id,
71 'form' => GFAPI::get_form($form_id),
72 'fields' => $fields,
73 'has_captcha' => $has_captcha,
74 'site_key' => get_option( 'rg_gforms_captcha_public_key' ),
75 'secret_key' => get_option( 'rg_gforms_captcha_private_key' ),
76 );
77
78 return Timber::compile('ContactForm/template.twig', $form);
79 }
80
81 public function jb_ajax_contact_form()
82 {
83 $result = GFAPI::submit_form(4, $input_values );
84 echo json_encode( $result );
85 die();
86 }
87}
88