__( 'EngageBay', 'advanced-form-integration' ), 'tasks' => array( 'subscribe' => __( 'Create New Contact', 'advanced-form-integration' ), ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_engagebay_settings_tab', 10, 1 ); function adfoin_engagebay_settings_tab( $providers ) { $providers['engagebay'] = __( 'EngageBay', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_engagebay_settings_view', 10, 1 ); function adfoin_engagebay_settings_view( $current_tab ) { if( $current_tab != 'engagebay' ) { 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' => __( 'Enter your API Key', 'advanced-form-integration' ), 'show_in_table' => true ) ); $instructions = sprintf( '
%s
', __('Go to Account Settings > API & Tracking Code and copy REST API Key', 'advanced-form-integration') ); ADFOIN_Account_Manager::render_settings_view( 'engagebay', __( 'EngageBay', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_engagebay_credentials', 'adfoin_get_engagebay_credentials', 10, 0 ); /* * Get EngageBay credentials */ function adfoin_get_engagebay_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( 'engagebay' ); } add_action( 'wp_ajax_adfoin_save_engagebay_credentials', 'adfoin_save_engagebay_credentials', 10, 0 ); /* * Save EngageBay credentials */ function adfoin_save_engagebay_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( 'engagebay', array( 'apiKey' ) ); } /* * EngageBay Credentials List */ function adfoin_engagebay_get_credentials_list() { $credentials = adfoin_read_credentials( 'engagebay' ); foreach ($credentials as $option) { printf('', esc_attr($option['id']), esc_html($option['title'])); } } // Legacy single-account import: surfaces old `adfoin_engagebay_*` options // as a Legacy Account record when the new credentials store is empty. add_action( 'plugins_loaded', function() { if ( class_exists( 'ADFOIN_Account_Manager' ) ) { ADFOIN_Account_Manager::register_legacy_option_importer( 'engagebay', array( 'apiKey' => 'adfoin_engagebay_api_key', ) ); } }, 20 ); // Deprecated - kept for backward compatibility add_action( 'admin_post_adfoin_engagebay_save_api_key', 'adfoin_save_engagebay_api_key', 10, 0 ); function adfoin_save_engagebay_api_key() { // Security Check // Authorization check adfoin_require_manage_options(); if (! wp_verify_nonce( $_POST['_nonce'], 'adfoin_engagebay_settings' ) ) { die( __( 'Security check Failed', 'advanced-form-integration' ) ); } $api_key = sanitize_text_field( wp_unslash( $_POST['adfoin_engagebay_api_key'] ) ); // Save keys update_option( 'adfoin_engagebay_api_key', $api_key ); advanced_form_integration_redirect( 'admin.php?page=advanced-form-integration-settings&tab=engagebay' ); } add_action( 'adfoin_action_fields', 'adfoin_engagebay_action_fields' ); function adfoin_engagebay_action_fields() { ?> array( array( 'name' => 'email', 'value' => $email ), array( 'name' => 'name', 'value' => $first_name ), ) ); if( $last_name ) { array_push( $contact_data['properties'], array( 'name' => 'last_name', 'value' => $last_name ) ); } if( $phone ) { array_push( $contact_data['properties'], array( 'name' => 'phone', 'value' => $phone ) ); } if( $role ) { array_push( $contact_data['properties'], array( 'name' => 'role', 'value' => $role ) ); } if( $website ) { array_push( $contact_data['properties'], array( 'name' => 'website', 'value' => $website ) ); } if( $country ) { array_push( $contact_data['properties'], array( 'name' => 'country', 'value' => $country ) ); } if( $address || $city || $zip || $state || $country ) { if( isset( $is_contact['body']['properties'] ) ) { $old_address_data = array(); foreach( $is_contact['body']['properties'] as $property ) { if( $property['name'] == 'address' ) { $old_address_data = json_decode( $property['value'], true ); } } } $address_data = array(); if( $old_address_data ) { $address_data = $old_address_data; } if( $address ) { $address_data['address'] = $address; } if( $city ) { $address_data['city'] = $city; } if( $zip ) { $address_data['zip'] = $zip; } if( $state ) { $address_data['state'] = $state; } if( $country ) { $address_data['country'] = $country; } array_push( $contact_data['properties'], array( 'name' => 'address', 'value' => wp_json_encode( $address_data ) ) ); } if( $company_id ) { $contact_data['companyIds'] = array( $company_id ); } if( $list_id ) { $contact_data['listIds'] = array( $list_id ); } if( $contact_id ) { $contact_data['id'] = $contact_id; $return = adfoin_engagebay_request( 'panel/subscribers/update-partial', 'PUT', $contact_data, $record, $cred_id ); } else { $return = adfoin_engagebay_request( 'panel/subscribers/subscriber', 'POST', $contact_data, $record, $cred_id ); } } return; } function adfoin_engagebay_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $credentials = adfoin_get_credentials_by_id( 'engagebay', $cred_id ); $api_key = isset( $credentials['apiKey'] ) ? $credentials['apiKey'] : ''; // Backward compatibility: fallback to old options if credentials not found if( empty( $api_key ) ) { $api_key = get_option( 'adfoin_engagebay_api_key' ) ? get_option( 'adfoin_engagebay_api_key' ) : ''; } if( !$api_key ) { return array(); } $base_url = 'https://app.engagebay.com/dev/api/'; $url = $base_url . $endpoint; $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => $api_key ) ); // Body JSON-encoded for any verb that may carry a payload // (some new pro tasks use PATCH/DELETE with bodies). if ( ! empty( $data ) && in_array( $method, array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) ) { // If caller already passed a JSON string (form-encoded paths do this), // don't re-encode it. Detect with a quick is_string()/is_array() check. $args['body'] = is_string( $data ) ? $data : wp_json_encode( $data ); } $args = apply_filters( 'adfoin_engagebay_before_sent', $args, $url ); $response = wp_remote_request( $url, $args ); // Retry once on 429 (rate limit), honoring Retry-After when present. if ( ! is_wp_error( $response ) && 429 === (int) wp_remote_retrieve_response_code( $response ) ) { $retry_after = (int) wp_remote_retrieve_header( $response, 'retry-after' ); if ( $retry_after < 1 ) { $retry_after = 2; } if ( $retry_after > 30 ) { $retry_after = 30; } sleep( $retry_after ); $response = wp_remote_request( $url, $args ); } if( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } function adfoin_engagebay_maybe_record_exists( $keyword, $type, $cred_id = '' ) { $record_id = ''; $body = ''; // URL encode the search terms to handle special characters (e.g. plus-addressing). $result = adfoin_engagebay_request( 'search?type=' . rawurlencode( $type ) . '&q=' . rawurlencode( $keyword ), 'GET', array(), array(), $cred_id ); if( !is_wp_error( $result ) ) { $body = json_decode( wp_remote_retrieve_body( $result ), true ); if( is_array( $body ) && isset( $body[0], $body[0]['id'] ) ) { $record_id = $body[0]['id']; $body = $body[0]; } } return array( 'id' => $record_id, 'body' => $body ); } function adfoin_engagebay_create_company( $company, $record, $cred_id = '' ) { $company_id = ''; $result = adfoin_engagebay_request( 'panel/companies/company', 'POST', array( 'properties' => array( array( 'name' => 'name', 'value' => $company ) ) ), $record, $cred_id ); if( ! is_wp_error( $result ) ) { $body = json_decode( wp_remote_retrieve_body( $result ), true ); if( isset( $body['id'], $body['name'] ) && $body['name'] == $company ) { $company_id = $body['id']; } } return $company_id; }