__( 'EasySendy', 'advanced-form-integration' ), 'tasks' => array( 'subscribe' => __( 'Subscribe To List', 'advanced-form-integration' ), ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_easysendy_settings_tab', 10, 1 ); function adfoin_easysendy_settings_tab( $providers ) { $providers['easysendy'] = __( 'EasySendy', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_easysendy_settings_view', 10, 1 ); function adfoin_easysendy_settings_view( $current_tab ) { if( $current_tab != 'easysendy' ) { 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' => __( 'Private 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
', __('Click on the profile icon > APIs then create API Key. Copy the private key here.', 'advanced-form-integration') ); ADFOIN_Account_Manager::render_settings_view( 'easysendy', __( 'EasySendy', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_easysendy_credentials', 'adfoin_get_easysendy_credentials', 10, 0 ); /* * Get EasySendy credentials */ function adfoin_get_easysendy_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( 'easysendy' ); } add_action( 'wp_ajax_adfoin_save_easysendy_credentials', 'adfoin_save_easysendy_credentials', 10, 0 ); /* * Save EasySendy credentials */ function adfoin_save_easysendy_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( 'easysendy', array( 'apiKey' ) ); } /* * EasySendy Credentials List */ function adfoin_easysendy_get_credentials_list() { $credentials = adfoin_read_credentials( 'easysendy' ); foreach ($credentials as $option) { printf('', esc_attr($option['id']), esc_html($option['title'])); } } // Legacy single-account import: surfaces old `adfoin_easysendy_*` 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( 'easysendy', array( 'apiKey' => 'adfoin_easysendy_api_key', ) ); } }, 20 ); // Deprecated - kept for backward compatibility add_action( 'admin_post_adfoin_easysendy_save_api_key', 'adfoin_save_easysendy_api_key', 10, 0 ); function adfoin_save_easysendy_api_key() { // Security Check // Authorization check adfoin_require_manage_options(); if (! wp_verify_nonce( $_POST['_nonce'], 'adfoin_easysendy_settings' ) ) { die( __( 'Security check Failed', 'advanced-form-integration' ) ); } $api_key = sanitize_text_field( wp_unslash( $_POST["adfoin_easysendy_api_key"] ) ); // Save keys update_option( "adfoin_easysendy_api_key", $api_key ); advanced_form_integration_redirect( "admin.php?page=advanced-form-integration-settings&tab=easysendy" ); } add_action( 'adfoin_add_js_fields', 'adfoin_easysendy_js_fields', 10, 1 ); function adfoin_easysendy_js_fields( $field_data ) {} add_action( 'adfoin_action_fields', 'adfoin_easysendy_action_fields' ); function adfoin_easysendy_action_fields() { ?> 20, 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json' ), ); if ('POST' == $method || 'PUT' == $method) { $data['api_key'] = $api_key; $args['body'] = wp_json_encode( $data ); } $response = wp_remote_request($url, $args); // Defensive 429 retry: EasySendy's docs don't formally document rate // limiting, but a single short-backoff retry is a harmless guard if the // API ever throttles. Honors Retry-After if present. if ( 429 == (int) wp_remote_retrieve_response_code( $response ) ) { $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ); $wait = is_numeric( $retry_after ) ? (int) $retry_after : 2; if ( $wait < 1 ) { $wait = 1; } if ( $wait > 30 ) { $wait = 30; } sleep( $wait ); $response = wp_remote_request( $url, $args ); } if ($record) { adfoin_add_to_log($response, $url, $args, $record); } return $response; } add_action( 'wp_ajax_adfoin_get_easysendy_list', 'adfoin_get_easysendy_list', 10, 0 ); /* * Get EasySendy subscriber lists */ function adfoin_get_easysendy_list() { // Security Check (capability + nonce, with isset/unslash/sanitize) if ( ! adfoin_verify_nonce() ) { return; } $cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : ''; $data = adfoin_easysendy_request( 'subscribers_list/lists', 'POST', array(), array(), $cred_id ); if( is_wp_error( $data ) || empty( $data['body'] ) ) { wp_send_json_error(); } $body = json_decode( $data['body'] ); if( ! isset( $body->lists ) || ! is_array( $body->lists ) ) { wp_send_json_error(); } $lists = wp_list_pluck( $body->lists, 'name', 'hash' ); wp_send_json_success( $lists ); } add_action( 'adfoin_easysendy_job_queue', 'adfoin_easysendy_job_queue', 10, 1 ); function adfoin_easysendy_job_queue( $data ) { adfoin_easysendy_send_data( $data['record'], $data['posted_data'] ); } /* * Handles sending data to EasySendy API */ function adfoin_easysendy_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) { return; } $data = $record_data['field_data']; $cred_id = isset( $data['credId'] ) ? $data['credId'] : ''; $task = $record['task']; // Backward compatibility: if no cred_id, use first available credential if( empty( $cred_id ) ) { $all_credentials = adfoin_read_credentials( 'easysendy' ); if( !empty( $all_credentials ) ) { $cred_id = $all_credentials[0]['id']; } } if( $task == 'subscribe' ) { $list_id = $data['listId']; $email = empty( $data['email'] ) ? '' : trim( adfoin_get_parsed_values( $data['email'], $posted_data ) ); $fname = empty( $data['firstName'] ) ? '' : adfoin_get_parsed_values( $data['firstName'], $posted_data ); $lname = empty( $data['lastName'] ) ? '' : adfoin_get_parsed_values( $data['lastName'], $posted_data ); $data = array( 'list' => $list_id, 'EMAIL' => $email, 'FNAME' => $fname, 'LNAME' => $lname ); $return = adfoin_easysendy_request( 'subscriber/add', 'POST', $data, $record, $cred_id ); } return; }