__( 'SharpSpring', 'advanced-form-integration' ),
'tasks' => array(
'add_lead' => __( 'Create or Update Lead', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_sharpspring_settings_tab', 10, 1 );
function adfoin_sharpspring_settings_tab( $providers ) {
$providers['sharpspring'] = __( 'SharpSpring', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_sharpspring_settings_view', 10, 1 );
function adfoin_sharpspring_settings_view( $current_tab ) {
if ( 'sharpspring' !== $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' => 'accountId',
'label' => __( 'Account ID', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'show_in_table' => true,
),
array(
'name' => 'secretKey',
'label' => __( 'Secret Key', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'show_in_table' => false,
),
);
$instructions = sprintf(
'
- %s
- %s
',
sprintf(
/* translators: %s: link to SharpSpring API settings */
esc_html__( 'In %s, copy your Account ID and Secret Key.', 'advanced-form-integration' ),
'Settings → API Settings'
),
esc_html__( 'Paste them below. AFI calls api.sharpspring.com/pubapi/v1/ with both keys as query parameters. (Account is now branded as Constant Contact Lead Gen & CRM; the API path is unchanged.)', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'sharpspring', __( 'SharpSpring', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_sharpspring_credentials', 'adfoin_get_sharpspring_credentials', 10, 0 );
function adfoin_get_sharpspring_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( 'sharpspring' );
}
add_action( 'wp_ajax_adfoin_save_sharpspring_credentials', 'adfoin_save_sharpspring_credentials', 10, 0 );
function adfoin_save_sharpspring_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( 'sharpspring', array( 'accountId', 'secretKey' ) );
}
if ( ! function_exists( 'adfoin_sharpspring_credentials_list' ) ) :
function adfoin_sharpspring_credentials_list() {
foreach ( adfoin_read_credentials( 'sharpspring' ) as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
endif;
/**
* Migrate legacy single-option credentials into the multi-account store.
*/
add_action( 'plugins_loaded', function () {
if ( class_exists( 'ADFOIN_Account_Manager' ) ) {
ADFOIN_Account_Manager::register_legacy_option_importer( 'sharpspring', array(
'accountId' => 'adfoin_sharpspring_account_id',
'secretKey' => 'adfoin_sharpspring_secret_key',
) );
}
}, 20 );
add_action( 'adfoin_action_fields', 'adfoin_sharpspring_action_fields' );
function adfoin_sharpspring_action_fields() {
?>
$email );
foreach ( array( 'firstName', 'lastName' ) as $key ) {
if ( empty( $field_data[ $key ] ) ) {
continue;
}
$value = trim( (string) adfoin_get_parsed_values( $field_data[ $key ], $posted_data ) );
if ( '' !== $value ) {
$lead[ $key ] = $value;
}
}
$lead = apply_filters( 'adfoin_sharpspring_lead', $lead, $field_data, $posted_data );
adfoin_sharpspring_request( 'createOrUpdateLeads', array(
'objects' => array( $lead ),
'idField' => 'emailAddress',
), $record, $cred_id );
}
if ( ! function_exists( 'adfoin_sharpspring_request' ) ) :
/**
* Call the SharpSpring JSON-RPC API. accountID + secretKey are sent as
* query parameters; the body is a raw JSON-RPC envelope.
*
* @param string $method JSON-RPC method name.
* @param array $params Method parameters.
* @param array $record Submission record for logging.
* @param string $cred_id Saved credential id.
*
* @return array|WP_Error
*/
function adfoin_sharpspring_request( $method, $params = array(), $record = array(), $cred_id = '' ) {
$account_id = '';
$secret_key = '';
if ( $cred_id && function_exists( 'adfoin_get_credentials_by_id' ) ) {
$credentials = adfoin_get_credentials_by_id( 'sharpspring', $cred_id );
if ( is_array( $credentials ) ) {
$account_id = isset( $credentials['accountId'] ) ? trim( (string) $credentials['accountId'] ) : '';
$secret_key = isset( $credentials['secretKey'] ) ? trim( (string) $credentials['secretKey'] ) : '';
}
}
if ( ! $account_id ) {
$account_id = (string) get_option( 'adfoin_sharpspring_account_id', '' );
}
if ( ! $secret_key ) {
$secret_key = (string) get_option( 'adfoin_sharpspring_secret_key', '' );
}
if ( ! $account_id || ! $secret_key ) {
return new WP_Error( 'sharpspring_missing_credentials', __( 'SharpSpring credentials are missing.', 'advanced-form-integration' ) );
}
$url = add_query_arg(
array(
'accountID' => $account_id,
'secretKey' => $secret_key,
),
'https://api.sharpspring.com/pubapi/v1/'
);
$body = array(
'method' => $method,
'params' => $params,
'id' => uniqid( 'adfoin_', true ),
);
$args = array(
'timeout' => 30,
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
'body' => wp_json_encode( $body ),
);
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
endif;