__( 'Instantly', 'advanced-form-integration' ), 'tasks' => array( 'add_lead' => __( 'Add Lead', 'advanced-form-integration' ), ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_instantly_settings_tab', 10, 1 ); function adfoin_instantly_settings_tab( $providers ) { $providers['instantly'] = __( 'Instantly', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_instantly_settings_view', 10, 1 ); function adfoin_instantly_settings_view( $current_tab ) { if( $current_tab != 'instantly' ) { return; } $title = __( 'Instantly', 'advanced-form-integration' ); $key = 'instantly'; $arguments = wp_json_encode([ 'platform' => $key, 'fields' => [ [ 'key' => 'apiKey', 'label' => __( 'API Key', 'advanced-form-integration' ), 'required' => true ] ] ]); $instructions = '
Go to Instantly Dashboard > Settings > Integrations > API
'; echo adfoin_platform_settings_template( $title, $key, $arguments, $instructions ); } add_action( 'wp_ajax_adfoin_get_instantly_credentials', 'adfoin_get_instantly_credentials', 10, 0 ); function adfoin_get_instantly_credentials() { adfoin_verify_nonce(); $all_credentials = adfoin_read_credentials( 'instantly' ); wp_send_json_success( $all_credentials ); } add_action( 'wp_ajax_adfoin_save_instantly_credentials', 'adfoin_save_instantly_credentials', 10, 0 ); function adfoin_save_instantly_credentials() { adfoin_verify_nonce(); $platform = sanitize_text_field( wp_unslash( $_POST['platform'] ) ); if( 'instantly' == $platform ) { $data = adfoin_array_map_recursive( 'sanitize_text_field', $_POST['data'] ); adfoin_save_credentials( $platform, $data ); } wp_send_json_success(); } add_action( 'adfoin_action_fields', 'adfoin_instantly_action_fields' ); function adfoin_instantly_action_fields() { ?> ' . $option['title'] . ''; } echo $html; } function adfoin_instantly_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $credentials = adfoin_get_credentials_by_id( 'instantly', $cred_id ); $api_key = isset( $credentials['apiKey'] ) ? $credentials['apiKey'] : ''; $base_url = "https://api.instantly.ai/api/v1/"; $url = $base_url . $endpoint; $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json' ) ); if ('POST' == $method || 'PUT' == $method) { $args['body'] = wp_json_encode(array_merge(['api_key' => $api_key], $data)); } $response = wp_remote_request( $url, $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } add_action( 'adfoin_instantly_job_queue', 'adfoin_instantly_job_queue', 10, 1 ); function adfoin_instantly_job_queue( $data ) { adfoin_instantly_send_data( $data['record'], $data['posted_data'] ); } function adfoin_instantly_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if (isset($record_data['action_data']['cl']) && adfoin_check_conditional_logic($record_data['action_data']['cl'], $posted_data)) return; $data = $record_data['field_data']; $campaign_id = isset( $data['campaignId'] ) ? $data['campaignId'] : ''; $cred_id = isset( $data['credId'] ) ? $data['credId'] : ''; $task = $record['task']; if ( $task == 'add_lead' ) { $leads = []; // The editor saves one lead as flat field keys (email, first_nameā¦); // a `leads` array is only read if present for back-compat. $lead_rows = ( isset( $data['leads'] ) && is_array( $data['leads'] ) ) ? $data['leads'] : array( $data ); foreach ($lead_rows as $lead) { $lead = wp_parse_args( (array) $lead, array_fill_keys( array( 'email', 'first_name', 'last_name', 'company_name', 'personalization', 'phone', 'website' ), '' ) ); $leads[] = array_filter([ 'email' => adfoin_get_parsed_values($lead['email'], $posted_data), 'first_name' => adfoin_get_parsed_values($lead['first_name'], $posted_data), 'last_name' => adfoin_get_parsed_values($lead['last_name'], $posted_data), 'company_name' => adfoin_get_parsed_values($lead['company_name'], $posted_data), 'personalization' => adfoin_get_parsed_values($lead['personalization'], $posted_data), 'phone' => adfoin_get_parsed_values($lead['phone'], $posted_data), 'website' => adfoin_get_parsed_values($lead['website'], $posted_data) ]); } // Instantly needs an email for every lead. $leads = array_values( array_filter( $leads, function ( $lead ) { return ! empty( $lead['email'] ); } ) ); if ( empty( $leads ) ) { return; } $lead_data = [ 'campaign_id' => $campaign_id, 'leads' => $leads ]; $result = adfoin_instantly_request( "lead/add", 'POST', $lead_data, $record, $cred_id ); } } add_action( 'wp_ajax_adfoin_get_instantly_campaigns', 'adfoin_get_instantly_campaigns', 10, 0 ); function adfoin_get_instantly_campaigns() { adfoin_verify_nonce(); $cred_id = sanitize_text_field( wp_unslash( $_POST['credId'] ) ); $credentials = adfoin_get_credentials_by_id( 'instantly', $cred_id ); $api_key = isset( $credentials['apiKey'] ) ? $credentials['apiKey'] : ''; $response = adfoin_instantly_request("campaign/list?api_key={$api_key}&skip=0&limit=100"); if (is_wp_error($response)) { wp_send_json_error(__('Failed to fetch campaigns.', 'advanced-form-integration')); } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body)) { wp_send_json_error(__('No campaigns found.', 'advanced-form-integration')); } $campaigns = []; foreach ($body as $campaign) { $campaigns[$campaign['id']] = $campaign['name']; } wp_send_json_success($campaigns); }