__( 'Total Expert', 'advanced-form-integration' ), 'tasks' => array( 'create_contact' => __( 'Create Contact', 'advanced-form-integration' ) ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_totalexpert_settings_tab', 10, 1 ); function adfoin_totalexpert_settings_tab( $providers ) { $providers['totalexpert'] = __( 'Total Expert', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_totalexpert_settings_view', 10, 1 ); function adfoin_totalexpert_settings_view( $current_tab ) { if ( 'totalexpert' !== $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' => 'client_id', 'label' => __( 'Client ID', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => true, 'show_in_table' => true, ), array( 'name' => 'client_secret', 'label' => __( 'Client Secret', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => true, ), ); $instructions = sprintf( '
  1. %s
  2. %s
  3. %s
', esc_html__( 'Total Expert Public API access is granted through Total Expert\'s partner program — contact your Total Expert representative to request a Client ID and Client Secret for the client_credentials grant.', 'advanced-form-integration' ), esc_html__( 'Paste both below. AFI exchanges them for an access token at https://public.totalexpert.net/v1/token.', 'advanced-form-integration' ), esc_html__( 'New contacts are created "as Admin" using this client credential pair.', 'advanced-form-integration' ) ); ADFOIN_Account_Manager::render_settings_view( 'totalexpert', __( 'Total Expert', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_totalexpert_credentials', 'adfoin_get_totalexpert_credentials', 10, 0 ); function adfoin_get_totalexpert_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( 'totalexpert' ); } add_action( 'wp_ajax_adfoin_save_totalexpert_credentials', 'adfoin_save_totalexpert_credentials', 10, 0 ); function adfoin_save_totalexpert_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( 'totalexpert', array( 'client_id', 'client_secret' ) ); } function adfoin_totalexpert_credentials_list() { foreach ( adfoin_read_credentials( 'totalexpert' ) as $option ) { printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) ); } } add_action( 'adfoin_action_fields', 'adfoin_totalexpert_action_fields' ); function adfoin_totalexpert_action_fields() { ?> 'firstName', 'value' => 'First Name', 'description' => '' ), array( 'key' => 'lastName', 'value' => 'Last Name', 'description' => '' ), array( 'key' => 'email', 'value' => 'Email', 'description' => '' ), array( 'key' => 'emailWork', 'value' => 'Work Email', 'description' => '' ), array( 'key' => 'phoneCell', 'value' => 'Cell Phone', 'description' => '' ), array( 'key' => 'phoneHome', 'value' => 'Home Phone', 'description' => '' ), array( 'key' => 'phoneOffice', 'value' => 'Office Phone', 'description' => '' ), array( 'key' => 'address', 'value' => 'Address', 'description' => '' ), array( 'key' => 'city', 'value' => 'City', 'description' => '' ), array( 'key' => 'state', 'value' => 'State', 'description' => '' ), array( 'key' => 'zipCode', 'value' => 'Zip Code', 'description' => '' ), array( 'key' => 'source', 'value' => 'Source', 'description' => '' ), array( 'key' => 'externalId', 'value' => 'External ID', 'description' => '' ), array( 'key' => 'birthday', 'value' => 'Birthday', 'description' => 'YYYY-MM-DD' ), ); wp_send_json_success( $fields ); } function adfoin_totalexpert_build_contact_payload( $fields ) { $body = array(); foreach ( array( 'firstName' => 'first_name', 'lastName' => 'last_name', 'email' => 'email', 'emailWork' => 'email_work', 'phoneCell' => 'phone_cell', 'phoneHome' => 'phone_home', 'phoneOffice' => 'phone_office', 'address' => 'address', 'city' => 'city', 'state' => 'state', 'zipCode' => 'zip_code', 'source' => 'source', 'externalId' => 'external_id', 'birthday' => 'birthday', ) as $local => $remote ) { if ( ! empty( $fields[ $local ] ) ) $body[ $remote ] = $fields[ $local ]; } return $body; } add_action( 'adfoin_totalexpert_job_queue', 'adfoin_totalexpert_job_queue', 10, 1 ); function adfoin_totalexpert_job_queue( $data ) { adfoin_totalexpert_send_data( $data['record'], $data['posted_data'] ); } function adfoin_totalexpert_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 = isset( $record_data['field_data'] ) ? $record_data['field_data'] : $record_data; $cred_id = isset( $data['credId'] ) ? $data['credId'] : ( isset( $record['cred_id'] ) ? $record['cred_id'] : '' ); if ( ! $cred_id ) return; $fields = array(); foreach ( $data as $k => $v ) { $parsed = adfoin_get_parsed_values( $v, $posted_data ); if ( $parsed !== '' && $parsed !== null ) $fields[ $k ] = $parsed; } if ( $record['task'] !== 'create_contact' ) return; adfoin_totalexpert_request( 'contacts', 'POST', adfoin_totalexpert_build_contact_payload( $fields ), $record, $cred_id ); } if ( ! function_exists( 'adfoin_totalexpert_get_token' ) ) : /** * Fetch (and cache) a Total Expert access token via the client_credentials * grant. Docs state tokens are valid ~1 hour; cached for 55 minutes. There * is no refresh_token in this grant, so on expiry we simply re-request one. */ function adfoin_totalexpert_get_token( $cred_id, $force_refresh = false ) { if ( empty( $cred_id ) ) { return new WP_Error( 'totalexpert_missing_cred', __( 'Total Expert credential ID is empty.', 'advanced-form-integration' ) ); } $cache_key = 'adfoin_totalexpert_token_' . md5( (string) $cred_id ); if ( ! $force_refresh ) { $cached = get_transient( $cache_key ); if ( ! empty( $cached ) && is_string( $cached ) ) { return $cached; } } $credentials = adfoin_get_credentials_by_id( 'totalexpert', $cred_id ); if ( ! is_array( $credentials ) || empty( $credentials['client_id'] ) || empty( $credentials['client_secret'] ) ) { return new WP_Error( 'totalexpert_missing_credentials', __( 'Total Expert client_id / client_secret not configured.', 'advanced-form-integration' ) ); } $response = wp_remote_post( 'https://public.totalexpert.net/v1/token', array( 'timeout' => 30, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $credentials['client_id'] . ':' . $credentials['client_secret'] ), 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'grant_type' => 'client_credentials' ) ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code || empty( $body['access_token'] ) ) { $msg = isset( $body['error_description'] ) ? $body['error_description'] : ( isset( $body['error'] ) ? $body['error'] : sprintf( 'HTTP %d', $code ) ); return new WP_Error( 'totalexpert_auth_failed', $msg ); } $token = (string) $body['access_token']; set_transient( $cache_key, $token, 55 * MINUTE_IN_SECONDS ); return $token; } endif; if ( ! function_exists( 'adfoin_totalexpert_request' ) ) : function adfoin_totalexpert_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $token = adfoin_totalexpert_get_token( $cred_id ); if ( is_wp_error( $token ) ) { if ( $record ) { adfoin_add_to_log( $token, 'https://public.totalexpert.net/v1/token', array( 'method' => 'POST' ), $record ); } return $token; } $response = adfoin_totalexpert_dispatch( $token, $endpoint, $method, $data, $record ); if ( ! is_wp_error( $response ) && 401 === (int) wp_remote_retrieve_response_code( $response ) ) { $token = adfoin_totalexpert_get_token( $cred_id, true ); if ( ! is_wp_error( $token ) ) { $response = adfoin_totalexpert_dispatch( $token, $endpoint, $method, $data, $record ); } } return $response; } endif; if ( ! function_exists( 'adfoin_totalexpert_dispatch' ) ) : function adfoin_totalexpert_dispatch( $token, $endpoint, $method, $data, $record ) { $url = 'https://public.totalexpert.net/v1/' . ltrim( $endpoint, '/' ); $method = strtoupper( $method ); $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ), ); if ( in_array( $method, array( 'POST', 'PUT', 'PATCH' ), true ) ) { $args['body'] = wp_json_encode( $data ); } $response = wp_remote_request( esc_url_raw( $url ), $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } endif;