__( 'Salesloft', 'advanced-form-integration' ),
'tasks' => array(
'add_to_list' => __( 'Create or Update Person', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_salesloft_settings_tab', 10, 1 );
function adfoin_salesloft_settings_tab( $tabs ) {
$tabs['salesloft'] = __( 'Salesloft', 'advanced-form-integration' );
return $tabs;
}
add_action( 'adfoin_settings_view', 'adfoin_salesloft_settings_view', 10, 1 );
function adfoin_salesloft_settings_view( $current_tab ) {
if ( 'salesloft' !== $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' => 'apiKey',
'label' => __( 'API Key', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'placeholder' => __( 'Personal API key from Salesloft Settings → API Keys', 'advanced-form-integration' ),
'show_in_table' => true,
),
);
$instructions = sprintf(
'
- %s
- %s
',
esc_html__( 'In Salesloft go to Settings → API Keys and generate a personal API key.', 'advanced-form-integration' ),
esc_html__( 'Paste it below. AFI authenticates with Authorization: Bearer {key}.', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'salesloft', __( 'Salesloft', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_salesloft_credentials', 'adfoin_get_salesloft_credentials', 10, 0 );
function adfoin_get_salesloft_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( 'salesloft' );
}
add_action( 'wp_ajax_adfoin_save_salesloft_credentials', 'adfoin_save_salesloft_credentials', 10, 0 );
function adfoin_save_salesloft_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( 'salesloft', array( 'apiKey' ) );
}
function adfoin_salesloft_credentials_list() {
foreach ( adfoin_read_credentials( 'salesloft' ) as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
add_action( 'adfoin_action_fields', 'adfoin_salesloft_action_fields' );
function adfoin_salesloft_action_fields() {
?>
$email );
$simple = array(
'first_name',
'last_name',
'title',
'company',
'phone',
'mobile_phone',
'city',
'state',
'country',
);
foreach ( $simple as $key ) {
if ( empty( $field_data[ $key ] ) ) {
continue;
}
$value = trim( (string) adfoin_get_parsed_values( $field_data[ $key ], $posted_data ) );
if ( '' !== $value ) {
$payload[ $key ] = $value;
}
}
if ( ! empty( $field_data['tags'] ) ) {
$tags_raw = adfoin_get_parsed_values( $field_data['tags'], $posted_data );
if ( is_array( $tags_raw ) ) {
$tags = array_filter( array_map( 'trim', array_map( 'strval', $tags_raw ) ) );
} else {
$tags = array_filter( array_map( 'trim', explode( ',', (string) $tags_raw ) ) );
}
if ( ! empty( $tags ) ) {
$payload['tags'] = array_values( $tags );
}
}
$payload = apply_filters( 'adfoin_salesloft_person_payload', $payload, $field_data, $posted_data );
adfoin_salesloft_request( 'people', 'POST', $payload, $record, $cred_id );
}
if ( ! function_exists( 'adfoin_salesloft_request' ) ) :
/**
* Call the Salesloft v2 API.
*
* @param string $endpoint Path under /v2/.
* @param string $method HTTP verb.
* @param mixed $data Body (POST/PUT) or query (GET).
* @param array $record Submission record for logging.
* @param string $cred_id Saved credential id.
*
* @return array|WP_Error
*/
function adfoin_salesloft_request( $endpoint, $method = 'POST', $data = array(), $record = array(), $cred_id = '' ) {
$api_key = '';
if ( $cred_id && function_exists( 'adfoin_get_credentials_by_id' ) ) {
$credentials = adfoin_get_credentials_by_id( 'salesloft', $cred_id );
if ( is_array( $credentials ) && isset( $credentials['apiKey'] ) ) {
$api_key = trim( (string) $credentials['apiKey'] );
}
}
if ( ! $api_key ) {
return new WP_Error( 'salesloft_missing_key', __( 'Salesloft API key is missing.', 'advanced-form-integration' ) );
}
$url = 'https://api.salesloft.com/v2/' . ltrim( $endpoint, '/' );
$method = strtoupper( $method );
$args = array(
'timeout' => 30,
'method' => $method,
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
'Accept' => 'application/json',
),
);
if ( 'GET' === $method ) {
if ( is_array( $data ) && ! empty( $data ) ) {
$url = add_query_arg( $data, $url );
}
} else {
$args['headers']['Content-Type'] = 'application/json';
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
endif;