__( 'Smartlead.ai', 'advanced-form-integration' ),
'tasks' => array(
'add_lead' => __( 'Add Lead to Campaign', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_smartlead_settings_tab', 10, 1 );
function adfoin_smartlead_settings_tab( $providers ) {
$providers['smartlead'] = __( 'Smartlead', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_smartlead_settings_view', 10, 1 );
function adfoin_smartlead_settings_view( $current_tab ) {
if ( 'smartlead' !== $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' => __( 'Smartlead dashboard → Settings → API Keys', 'advanced-form-integration' ),
'show_in_table' => true,
),
);
$instructions = sprintf(
'
- %s
- %s
',
esc_html__( 'In Smartlead go to Settings → API Keys and create / copy your key.', 'advanced-form-integration' ),
esc_html__( 'Paste it below. AFI sends ?api_key={key} on every call to server.smartlead.ai/api/v1/.', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'smartlead', __( 'Smartlead.ai', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_smartlead_credentials', 'adfoin_get_smartlead_credentials', 10, 0 );
function adfoin_get_smartlead_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( 'smartlead' );
}
add_action( 'wp_ajax_adfoin_save_smartlead_credentials', 'adfoin_save_smartlead_credentials', 10, 0 );
function adfoin_save_smartlead_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( 'smartlead', array( 'apiKey' ) );
}
function adfoin_smartlead_credentials_list() {
foreach ( adfoin_read_credentials( 'smartlead' ) as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
add_action( 'wp_ajax_adfoin_get_smartlead_campaigns', 'adfoin_get_smartlead_campaigns', 10, 0 );
function adfoin_get_smartlead_campaigns() {
adfoin_verify_nonce();
$cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : '';
if ( ! $cred_id ) {
wp_send_json_error( array( 'message' => __( 'No Smartlead account selected.', 'advanced-form-integration' ) ) );
}
$response = adfoin_smartlead_request( 'campaigns', 'GET', array(), array(), $cred_id );
if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => $response->get_error_message() ) );
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $body ) ) {
wp_send_json_error();
}
$campaigns = array();
foreach ( $body as $campaign ) {
if ( isset( $campaign['id'], $campaign['name'] ) ) {
$campaigns[ (string) $campaign['id'] ] = (string) $campaign['name'];
}
}
wp_send_json_success( $campaigns );
}
add_action( 'adfoin_action_fields', 'adfoin_smartlead_action_fields' );
function adfoin_smartlead_action_fields() {
?>
$email );
$reserved = array( 'credId' => 1, 'campaignId' => 1, 'campaigns' => 1, 'email' => 1, 'customFields' => 1, 'settings' => 1 );
$documented = array( 'first_name', 'last_name', 'phone_number', 'company_name', 'website', 'location', 'linkedin_profile', 'company_url' );
foreach ( $documented as $key ) {
if ( empty( $field_data[ $key ] ) ) {
continue;
}
$value = adfoin_get_parsed_values( $field_data[ $key ], $posted_data );
if ( '' !== $value && null !== $value ) {
$lead[ $key ] = $value;
}
}
$lead = apply_filters( 'adfoin_smartlead_lead', $lead, $field_data, $posted_data );
$body = array(
'lead_list' => array( $lead ),
'settings' => apply_filters( 'adfoin_smartlead_settings', array(
'ignore_global_block_list' => false,
'ignore_unsubscribe_list' => false,
'ignore_community_bounce_list' => false,
'ignore_duplicate_leads_in_other_campaign' => false,
), $field_data, $posted_data ),
);
adfoin_smartlead_request( 'campaigns/' . $campaign . '/leads', 'POST', $body, $record, $cred_id );
}
if ( ! function_exists( 'adfoin_smartlead_request' ) ) :
/**
* Call the Smartlead API.
*
* @param string $endpoint Path under /api/v1/.
* @param string $method HTTP verb.
* @param mixed $data Body (POST/PUT/PATCH) or query (GET, merged with api_key).
* @param array $record Submission record for logging.
* @param string $cred_id Saved credential id.
*
* @return array|WP_Error
*/
function adfoin_smartlead_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( 'smartlead', $cred_id );
if ( is_array( $credentials ) && isset( $credentials['apiKey'] ) ) {
$api_key = trim( (string) $credentials['apiKey'] );
}
}
if ( ! $api_key ) {
$error = new WP_Error( 'smartlead_missing_key', __( 'Smartlead API key is missing.', 'advanced-form-integration' ) );
if ( $record ) {
adfoin_add_to_log( $error, 'https://server.smartlead.ai/api/v1/' . ltrim( $endpoint, '/' ), array( 'method' => $method ), $record );
}
return $error;
}
$url = 'https://server.smartlead.ai/api/v1/' . ltrim( $endpoint, '/' );
$method = strtoupper( $method );
$query = array( 'api_key' => $api_key );
if ( 'GET' === $method && is_array( $data ) && ! empty( $data ) ) {
$query = array_merge( $data, $query );
}
$url = add_query_arg( $query, $url );
$args = array(
'timeout' => 30,
'method' => $method,
'headers' => array(
'Accept' => 'application/json',
),
);
if ( in_array( $method, array( 'POST', 'PUT', 'PATCH' ), true ) ) {
$args['headers']['Content-Type'] = 'application/json';
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if ( $record ) {
// Smartlead only accepts the key in the URL; keep it out of the log.
adfoin_add_to_log( adfoin_smartlead_response_for_log( $response ), add_query_arg( 'api_key', '***', $url ), $args, $record );
}
return $response;
}
endif;
/**
* Smartlead answers an add-leads call with HTTP 200 even when the lead was
* not added, e.g. {"ok":false,"error":"The lead credit limit has been reached..."}
* or {"ok":true,"total_leads":0,"block_count":1}. Log those as failures.
* A lead that is already in the campaign is not a failure, only noted.
*/
function adfoin_smartlead_response_for_log( $response ) {
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return $response;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $body ) || ! array_key_exists( 'upload_count', $body ) ) {
return $response;
}
if ( isset( $body['ok'] ) && false === $body['ok'] ) {
$response['response'] = array(
'code' => 400,
'message' => ! empty( $body['error'] ) ? 'Smartlead: ' . $body['error'] : 'Smartlead: lead not added',
);
return $response;
}
if ( ! empty( $body['total_leads'] ) ) {
return $response;
}
$reasons = array(
'block_count' => 'on the block list',
'invalid_email_count' => 'invalid email',
'unsubscribed_leads' => 'unsubscribed',
'bounce_count' => 'bounced before',
'skipped_in_other_campaign_count' => 'already in another campaign',
'duplicate_count' => 'duplicate lead',
'lead_import_stopped_count' => 'import stopped',
);
foreach ( $reasons as $key => $reason ) {
if ( ! empty( $body[ $key ] ) ) {
$response['response'] = array(
'code' => 400,
'message' => 'Smartlead: lead not added (' . $reason . ')',
);
return $response;
}
}
if ( ! empty( $body['already_added_to_campaign'] ) ) {
$response['response']['message'] = 'OK (lead already in campaign)';
}
return $response;
}
/**
* Log a submission that is not sent, so users can see why nothing happened.
*/
function adfoin_smartlead_log_skipped( $record, $message, $campaign = 0 ) {
$endpoint = 'https://server.smartlead.ai/api/v1/campaigns/' . ( $campaign ? $campaign : '{campaign}' ) . '/leads';
adfoin_add_to_log( new WP_Error( 'smartlead_not_sent', $message ), $endpoint, array( 'method' => 'POST' ), $record );
}
/**
* Checkbox values: older editors saved "on", newer ones "yes".
*/
function adfoin_smartlead_is_checked( $field_data, $key ) {
return isset( $field_data[ $key ] ) && in_array( strtolower( (string) $field_data[ $key ] ), array( 'yes', 'on', '1', 'true' ), true );
}