__('CiviCRM', 'advanced-form-integration'), 'tasks' => array( 'add_contact' => __('Add Contact', 'advanced-form-integration'), ) ); return $actions; } add_action('adfoin_action_fields', 'adfoin_civicrm_action_fields'); function adfoin_civicrm_action_fields() { ?> ['limit' => 0] to get all groups $civicrm_api = civicrm_api3('Group', 'get', [ 'options' => ['limit' => 0] ]); $groups = []; foreach ($civicrm_api['values'] as $group) { $groups[$group['id']] = $group['title']; } wp_send_json_success($groups); } add_action('wp_ajax_adfoin_get_civicrm_contact_fields', 'adfoin_get_civicrm_contact_fields', 10); /* * Get CiviCRM contact fields */ function adfoin_get_civicrm_contact_fields() { adfoin_verify_nonce(); if ( ! function_exists( 'civicrm_api3' ) ) { wp_send_json_error( __( 'CiviCRM is not active.', 'advanced-form-integration' ) ); } $contact_fields = civicrm_api3('Contact', 'getfields', []); $all_fields = []; $contact_types = adfoin_get_civicrm_contact_types(); foreach ($contact_fields['values'] as $field => $details) { if( 'contact_type' == $field ) { $all_fields[] = [ 'key' => $field, 'value' => $details['title'], 'description' => __('Required field. Possible values are: ', 'advanced-form-integration') . implode(', ', $contact_types), ]; continue; } $all_fields[] = [ 'key' => $field, 'value' => $details['title'], 'description' => $details['description'] ? $details['description'] : '', ]; } wp_send_json_success($all_fields); } function adfoin_get_civicrm_contact_types() { if ( ! function_exists( 'civicrm_api3' ) ) { return array(); } $contact_types = civicrm_api3('Contact', 'getoptions', [ 'field' => 'contact_type', ]); return array_values( $contact_types['values'] ); } add_action('adfoin_civicrm_job_queue', 'adfoin_civicrm_job_queue', 10, 1); function adfoin_civicrm_job_queue($data) { adfoin_civicrm_send_data($data['record'], $data['posted_data']); } /* * Handles sending data to CiviCRM API */ function adfoin_civicrm_send_data($record, $posted_data) { if ( ! function_exists( 'civicrm_api3' ) ) { return; } $record_data = json_decode($record['data'], true); if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) { return; } $data = $record_data['field_data']; $task = $record['task']; if ($task == 'add_contact') { $group_id = isset($data['groupId']) ? $data['groupId'] : ''; $contact_data = []; foreach ($data as $key => $value) { if ($key != 'groupId' && $value != '') { $contact_data[$key] = adfoin_get_parsed_values($value, $posted_data); } } try { $contact = civicrm_api3('Contact', 'create', $contact_data); if (!empty($contact['id']) && $group_id) { civicrm_api3('GroupContact', 'create', [ 'contact_id' => $contact['id'], 'group_id' => $group_id, 'status' => 'Added', ]); } } catch (\Exception $e) { error_log('ADFOIN CiviCRM: ' . $e->getMessage()); return; } } return; }