public function process_cf7_submission( $contact_form, &$abort, $submission ) { if ( ! $submission ) { return; } $form_id = (int) $contact_form->id(); $enabled_forms = get_option('lasso_leader_cf7_enabled_forms', []); if ( ! is_array($enabled_forms) || ! in_array( $form_id, $enabled_forms ) ) { return; } $api_key = (string) get_option('lasso_leader_api_key', ''); if ( empty( $api_key ) ) { return; } $all_mappings = get_option('lasso_leader_cf7_mappings', []); $field_map = $all_mappings[$form_id] ?? []; if ( empty($field_map) ) { lasso_leader_write_log('Submission stopped: No field mappings have been configured for CF7 form ID ' . $form_id); return; } $posted_data = $submission->get_posted_data(); $project_id = (string) get_option('lasso_leader_project_id', ''); // This is the payload for the FIRST API call (registrant creation) $registrant_data = [ 'person' => [], 'emails' => [], 'phones' => [], 'notes' => [], 'ProjectId' => intval($project_id), 'sourceType' => ['sourceType' => 'Online Registration'], ]; // This will hold the payload for the SECOND API call (questions) $questions_data = []; foreach ( $posted_data as $field_name => $value ) { if ( !isset($field_map[$field_name]) ) { continue; } $mapping_rules = $field_map[$field_name]; $processed_value = is_array($value) ? implode(', ', $value) : sanitize_text_field($value); if ( empty($processed_value) ) continue; if ( $mapping_rules['type'] === 'standard' ) { $lasso_prop = $mapping_rules['value']; switch ( strtolower( (string)$lasso_prop ) ) { case 'firstname': $registrant_data['person']['firstName'] = $processed_value; break; case 'lastname': $registrant_data['person']['lastName'] = $processed_value; break; case 'email': if ( is_email($processed_value) ) { $registrant_data['emails'][] = ['type' => 'primary', 'email' => sanitize_email($processed_value)]; } break; case 'phone': $registrant_data['phones'][] = ['type' => 'primary', 'phone' => preg_replace('/[^0-9+]/', '', $processed_value)]; break; case 'message': $registrant_data['notes'][] = ['note' => sanitize_textarea_field($processed_value)]; break; case 'isagent': $registrant_data['person']['isAgent'] = filter_var($processed_value, FILTER_VALIDATE_BOOLEAN); break; } } elseif ( $mapping_rules['type'] === 'question' ) { $question_id = absint($mapping_rules['question_id']); $question_type = $mapping_rules['question_type']; if ($question_type === 'answer_id') { $answer_ids = is_array($value) ? $value : array_map('trim', explode(',', $processed_value)); foreach ($answer_ids as $answer_id) { if (is_numeric($answer_id)) { if (!isset($questions_data[$question_id])) { $questions_data[$question_id] = []; } $questions_data[$question_id][] = absint($answer_id); } } } else { $questions_data[$question_id] = $processed_value; } } } foreach(['person', 'emails', 'phones', 'notes'] as $key) { if(empty($registrant_data[$key])) { unset($registrant_data[$key]); } } // --- STEP 1: Create the Registrant --- $response = $this->api_handler->send_to_lasso_crm( $api_key, $registrant_data, 'contact_form_7', $form_id ); // --- STEP 2: Submit Questions if they exist --- if ( ! is_wp_error( $response ) && ! empty( $questions_data ) ) { $registrant_id = $response['registrantId'] ?? null; if ( $registrant_id ) { $this->api_handler->send_questions_to_lasso( $api_key, $registrant_id, $questions_data ); } } }