__('Fluent Support', 'advanced-form-integration'),
'tasks' => [
'create_ticket' => __('Create Ticket', 'advanced-form-integration'),
],
];
return $actions;
}
add_filter('adfoin_settings_tabs', 'adfoin_fluentsupport_settings_tab', 10, 1);
function adfoin_fluentsupport_settings_tab($providers)
{
$providers['fluentsupport'] = __('Fluent Support', 'advanced-form-integration');
return $providers;
}
add_action('adfoin_settings_view', 'adfoin_fluentsupport_settings_view', 10, 1);
function adfoin_fluentsupport_settings_view($current_tab)
{
if ($current_tab != 'fluentsupport') {
return;
}
if (!is_plugin_active('fluent-support/fluent-support.php')) {
echo '
' . __('Fluent Support plugin is not active or installed.', 'advanced-form-integration') . '
';
return;
}
$nonce = wp_create_nonce('adfoin_fluentsupport_settings');
?>
$mailbox->id,
'name' => $mailbox->name,
];
}
wp_send_json_success($formattedMailboxes);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
}
/**
* Get Fluent Support Agents
*/
function adfoin_get_fluentsupport_agents()
{
if ( ! adfoin_verify_nonce() ) {
return;
}
// Check if Fluent Support is active
if (!is_plugin_active('fluent-support/fluent-support.php')) {
wp_send_json_error(__('Fluent Support plugin is not active or installed.', 'advanced-form-integration'));
}
// Fetch agents
try {
$agents = \FluentSupport\App\Models\Agent::get();
if (empty($agents)) {
wp_send_json_error(__('No agents found.', 'advanced-form-integration'));
}
// Format the response
$formattedAgents = [];
foreach ($agents as $agent) {
$formattedAgents[] = [
'id' => $agent->id,
'name' => $agent->full_name,
];
}
wp_send_json_success($formattedAgents);
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
}
add_action('adfoin_job_queue', 'adfoin_fluentsupport_job_queue', 10, 1);
function adfoin_fluentsupport_job_queue($data)
{
if ($data['action_provider'] === 'fluentsupport' && $data['task'] === 'create_ticket') {
adfoin_fluentsupport_send_data($data['record'], $data['posted_data']);
}
}
function adfoin_fluentsupport_send_data($record, $posted_data) {
$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 == 'create_ticket') {
$mailbox_id = isset($data['mailboxId']) ? $data['mailboxId'] : null;
$agent_id = isset($data['agentId']) ? $data['agentId'] : null;
$subject = isset($data['subject']) ? adfoin_get_parsed_values( $data['subject'], $posted_data ) : '';
$message = isset($data['message']) ? adfoin_get_parsed_values( $data['message'], $posted_data ) : '';
$email = isset($data['email']) ? adfoin_get_parsed_values( $data['email'], $posted_data ) : '';
$first_name = isset($data['firstName']) ? adfoin_get_parsed_values( $data['firstName'], $posted_data ) : '';
$last_name = isset($data['lastName']) ? adfoin_get_parsed_values( $data['lastName'], $posted_data ) : '';
if (empty($email) || empty($subject) || empty($message)) {
return new WP_Error('REQ_FIELD_EMPTY', __('Email, Subject, and Message fields are required.', 'advanced-form-integration'));
}
if (!is_plugin_active('fluent-support/fluent-support.php')) {
return new WP_Error('PLUGIN_NOT_ACTIVE', __('Fluent Support plugin is not active or installed.', 'advanced-form-integration'));
}
// if (!function_exists('adfoin_fluentsupport_get_or_create_customer')) {
// return new WP_Error('FUNCTION_NOT_FOUND', __('Required function adfoin_fluentsupport_get_or_create_customer does not exist.', 'advanced-form-integration'));
// }
$customer_id = adfoin_fluentsupport_get_or_create_customer($email, $first_name, $last_name);
if (is_wp_error($customer_id)) {
return $customer_id;
}
if (!class_exists('\FluentSupport\App\Models\Ticket')) {
return new WP_Error('CLASS_NOT_FOUND', __('Required class \FluentSupport\App\Models\Ticket does not exist.', 'advanced-form-integration'));
}
$ticket_data = array(
'mailbox_id' => $mailbox_id,
'agent_id' => $agent_id,
'customer_id' => $customer_id,
'title' => $subject,
'content' => $message,
'status' => 'new',
'priority' => 'normal',
);
$ticket = \FluentSupport\App\Models\Ticket::create($ticket_data);
if (!isset($ticket->id)) {
return new WP_Error('TICKET_CREATION_FAILED', __('Failed to create a ticket.', 'advanced-form-integration'));
}
return $ticket;
}
return;
}
function adfoin_fluentsupport_get_or_create_customer($email, $first_name, $last_name) {
if (!class_exists('\FluentSupport\App\Models\Customer')) {
return new WP_Error('CLASS_NOT_FOUND', __('Required class \FluentSupport\App\Models\Customer does not exist.', 'advanced-form-integration'));
}
$customer = \FluentSupport\App\Models\Customer::where('email', $email)->first();
if ($customer) {
return $customer->id;
}
$customer_data = array(
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
);
$customer = \FluentSupport\App\Models\Customer::create($customer_data);
if (!isset($customer->id)) {
return new WP_Error('CUSTOMER_CREATION_FAILED', __('Failed to create a customer.', 'advanced-form-integration'));
}
return $customer->id;
}