$value) { $multi_fields = isset($value["fields"]) ? $value["fields"] : null; if ($multi_fields) { foreach ($multi_fields as $multi_field_key => $multi_field_value) { $form_field_map[$multi_field_key] = $multi_field_value["settings"]["label"]; } } else { $label = isset($value["settings"]["label"]) ? $value["settings"]["label"] : $value["settings"]["admin_field_label"]; $field_key = $value["attributes"]["name"]; $form_field_map[$field_key] = $label; } } return $form_field_map; } /** * Convert names array field to a string field */ private function format_name_field($names) { if (!is_array($names)) return $names; $result = ""; foreach ($names as $value) { $result .= $value . " "; // Concatenate each value with a space after it } $result = rtrim($result); // Remove trailing space, if any return $result; } /** * Reformat form fields into dict key value pair * So it is easier to be consumed by our webhook API */ private function format_fields($form_data, $form_fields) { $unwanted_fields = array( "__fluent_form_embded_post_id", "_fluentform_3_fluentformnonce", "_fluentform_5_fluentformnonce", "_wp_http_referer" ); $form_field_map = $this->map_field_labels($form_fields); $body = array(); foreach ($form_data as $key => $value) { if (in_array($key, $unwanted_fields)) continue; if (is_array($value)) { $sub_body = array(); foreach ($value as $_key => $_value) { $field_title = isset($form_field_map[$_key]) ? $form_field_map[$_key] : $_key; $sub_body[$field_title] = $_value; } $field_title = isset($form_field_map[$key]) ? $form_field_map[$key] : $key; $body[$field_title] = $sub_body; } else { $field_title = isset($form_field_map[$key]) ? $form_field_map[$key] : $key; $body[$field_title] = $value; } } return $body; } /** * This is the callback after the lead is submitted. * Calls a non blocking http API request to 365 CRM with lead data. */ public function submit_to_leadcrm($entryId, $formData, $form) { // print_r($formData, ); $form_fields = json_decode($form->form_fields, true); $payload = $this->format_fields($formData, $form_fields); $token = Leadcrm_Options::get_single_value('leadcrm_token'); $cf_reference = Leadcrm_Options::get_single_value('cf_reference'); // print_r($payload); $form_title = isset($form->title) ? $form->title : ''; $form_id = isset($form->id) ? $form->id : ''; $payload['form_name'] = $form_title; $payload['leadcrm_token'] = $token; $payload['cf_reference'] = $cf_reference; if (array_key_exists("names", $payload)) { $payload['names'] = $this->format_name_field($payload['names']); }; $leadcrm_api = new Leadcrm_API('fluent_form', $form_title, $form_id); $endpoint = $leadcrm_api->build_api_endpoint(); $leadcrm_api->submit_lead_to_leadcrm($endpoint, $payload, Leadcrm_Content_Type::JSON); } }