__( 'Front', 'advanced-form-integration' ), 'tasks' => array( 'create_contact' => __( 'Create Contact', 'advanced-form-integration' ), 'create_conversation' => __( 'Create Conversation', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_front_settings_tab', 10, 1 ); function adfoin_front_settings_tab( $providers ) { $providers['front'] = __( 'Front', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_front_settings_view', 10, 1 ); function adfoin_front_settings_view( $current_tab ) { if ( 'front' !== $current_tab ) { return; } if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } $fields = array( array( 'name' => 'apiToken', 'label' => __( 'API Token', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => true, 'placeholder' => __( 'Paste your Front API token', 'advanced-form-integration' ), 'show_in_table' => true, ), ); $instructions = sprintf( '
  1. %s
  2. %s
  3. %s
  4. %s
  5. %s
', sprintf( __( 'Sign in to Front and open %s.', 'advanced-form-integration' ), 'Settings → Developers → API Tokens' ), esc_html__( 'Click "Create API token", give it a descriptive name (e.g. WordPress AFI), and grant it the scopes you need (Shared Resources is enough for Create Contact / Create Conversation).', 'advanced-form-integration' ), esc_html__( 'Copy the token immediately — Front only displays it once.', 'advanced-form-integration' ), esc_html__( 'Paste it below. AFI calls https://api2.frontapp.com/ with this token as Authorization: Bearer.', 'advanced-form-integration' ), esc_html__( 'For the Create Conversation task you will also need a Channel ID — find it in Front under Settings → Channels (the cha_xxx identifier of a Custom Channel).', 'advanced-form-integration' ) ); ADFOIN_Account_Manager::render_settings_view( 'front', __( 'Front', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_front_credentials', 'adfoin_get_front_credentials', 10, 0 ); function adfoin_get_front_credentials() { if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } ADFOIN_Account_Manager::ajax_get_credentials_list( 'front' ); } add_action( 'wp_ajax_adfoin_save_front_credentials', 'adfoin_save_front_credentials', 10, 0 ); function adfoin_save_front_credentials() { if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } ADFOIN_Account_Manager::ajax_save_credentials( 'front', array( 'apiToken' ) ); } function adfoin_front_credentials_list() { foreach ( adfoin_read_credentials( 'front' ) as $option ) { printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) ); } } add_action( 'adfoin_action_fields', 'adfoin_front_action_fields' ); function adfoin_front_action_fields() { ?> 'channel_id', 'value' => __( 'Channel ID (required, e.g. cha_abc123)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'sender_email', 'value' => __( 'Sender Email (required)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'sender_name', 'value' => __( 'Sender Name', 'advanced-form-integration' ) ), array( 'key' => 'subject', 'value' => __( 'Subject (required)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'body', 'value' => __( 'Body (required)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'body_format', 'value' => __( 'Body Format (markdown / html — defaults to markdown)', 'advanced-form-integration' ) ), ); } else { // create_contact (default) $fields = array( array( 'key' => 'name', 'value' => __( 'Name (required)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'email', 'value' => __( 'Email (required)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'phone', 'value' => __( 'Phone', 'advanced-form-integration' ) ), array( 'key' => 'description', 'value' => __( 'Description', 'advanced-form-integration' ) ), array( 'key' => 'links', 'value' => __( 'Links (comma-separated URLs)', 'advanced-form-integration' ) ), ); } wp_send_json_success( $fields ); } add_action( 'adfoin_front_job_queue', 'adfoin_front_job_queue', 10, 1 ); function adfoin_front_job_queue( $data ) { adfoin_front_send_data( $data['record'], $data['posted_data'] ); } function adfoin_front_send_data( $record, $posted_data ) { $task = $record['task'] ?? ''; if ( ! in_array( $task, array( 'create_contact', 'create_conversation' ), true ) ) { return; } $record_data = json_decode( $record['data'], true ); if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) { return; } $field_data = $record_data['field_data'] ?? array(); $cred_id = $field_data['credId'] ?? ''; if ( ! $cred_id ) { return; } // Resolve all field-mapped values up-front. Per-task payload assembly // happens below — the form only ever feeds us flat key=>value pairs. $reserved = array( 'credId' => 1 ); $values = array(); foreach ( $field_data as $key => $value ) { if ( isset( $reserved[ $key ] ) ) { continue; } $parsed = adfoin_get_parsed_values( $value, $posted_data ); if ( '' !== $parsed && null !== $parsed ) { $values[ $key ] = $parsed; } } if ( 'create_conversation' === $task ) { // Required: channel_id, sender_email, subject, body. if ( empty( $values['channel_id'] ) || empty( $values['sender_email'] ) || empty( $values['subject'] ) || empty( $values['body'] ) ) { return; } $sender = array( 'handle' => (string) $values['sender_email'], ); if ( ! empty( $values['sender_name'] ) ) { $sender['name'] = (string) $values['sender_name']; } // body_format accepts markdown or html; anything else falls back to // markdown so a bad mapping does not blow up the API call. $body_format = ! empty( $values['body_format'] ) ? strtolower( (string) $values['body_format'] ) : 'markdown'; if ( ! in_array( $body_format, array( 'markdown', 'html' ), true ) ) { $body_format = 'markdown'; } $payload = array( 'sender' => $sender, 'subject' => (string) $values['subject'], 'body' => (string) $values['body'], 'body_format' => $body_format, ); $channel_id = rawurlencode( (string) $values['channel_id'] ); adfoin_front_request( 'channels/' . $channel_id . '/incoming_messages', 'POST', $payload, $record, $cred_id ); return; } // create_contact // Required: name + email (email anchors the primary handle). if ( empty( $values['name'] ) || empty( $values['email'] ) ) { return; } // Build handles[]: Front's contact payload expects one entry per channel // (email, phone). Email is required and always added first; phone is // optional. We deliberately do not surface twitter/intercom/etc here — // they can be added later if there is demand. $handles = array( array( 'handle' => (string) $values['email'], 'source' => 'email', ), ); if ( ! empty( $values['phone'] ) ) { $handles[] = array( 'handle' => (string) $values['phone'], 'source' => 'phone', ); } $payload = array( 'name' => (string) $values['name'], 'handles' => $handles, ); if ( ! empty( $values['description'] ) ) { $payload['description'] = (string) $values['description']; } // links come in as a comma-separated string (the AFI field mapper has // no repeater control); split, trim, and drop blanks. if ( ! empty( $values['links'] ) ) { $links = array_filter( array_map( 'trim', explode( ',', (string) $values['links'] ) ), 'strlen' ); if ( ! empty( $links ) ) { $payload['links'] = array_values( $links ); } } adfoin_front_request( 'contacts', 'POST', $payload, $record, $cred_id ); } if ( ! function_exists( 'adfoin_front_request' ) ) : /** * Authenticated JSON request against https://api2.frontapp.com/. * Front uses a long-lived API Token — no refresh handling needed. A 401 * here means the operator revoked or rotated the token and must reconnect * via Settings → Front. */ function adfoin_front_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $credentials = adfoin_get_credentials_by_id( 'front', $cred_id ); if ( ! is_array( $credentials ) || empty( $credentials['apiToken'] ) ) { return new WP_Error( 'front_missing_credentials', __( 'Front API token not configured.', 'advanced-form-integration' ) ); } $url = 'https://api2.frontapp.com/' . ltrim( $endpoint, '/' ); $method = strtoupper( $method ); $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Authorization' => 'Bearer ' . $credentials['apiToken'], 'Accept' => 'application/json', ), ); if ( in_array( $method, array( 'POST', 'PUT', 'PATCH' ), true ) ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $data ); } elseif ( 'GET' === $method && is_array( $data ) && ! empty( $data ) ) { $url = add_query_arg( $data, $url ); } $response = wp_remote_request( esc_url_raw( $url ), $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } endif;