__( 'Mailercloud', 'advanced-form-integration' ), 'tasks' => array( 'subscribe' => __( 'Subscribe To List', 'advanced-form-integration' ), ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_mailercloud_settings_tab', 10, 1 ); function adfoin_mailercloud_settings_tab( $providers ) { $providers['mailercloud'] = __( 'Mailercloud', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_mailercloud_settings_view', 10, 1 ); function adfoin_mailercloud_settings_view( $current_tab ) { if( $current_tab != 'mailercloud' ) { 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( '
  1. %s
  2. %s
  3. %s
', __( 'Go to Profile > Account > Integrations > API Integrations.', 'advanced-form-integration' ), __( 'Create a new API Key.', 'advanced-form-integration' ), __( 'Paste the key here and save.', 'advanced-form-integration' ) ); ADFOIN_Account_Manager::render_settings_view( 'mailercloud', __( 'Mailercloud', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_mailercloud_credentials', 'adfoin_get_mailercloud_credentials', 10, 0 ); function adfoin_get_mailercloud_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( 'mailercloud' ); } add_action( 'wp_ajax_adfoin_save_mailercloud_credentials', 'adfoin_save_mailercloud_credentials', 10, 0 ); function adfoin_save_mailercloud_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( 'mailercloud', array( 'apiKey' ) ); } // Legacy single-account import: surfaces old `adfoin_mailercloud_*` 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( 'mailercloud', array( 'apiKey' => 'adfoin_mailercloud_api_key', ) ); } }, 20 ); add_action( 'adfoin_add_js_fields', 'adfoin_mailercloud_js_fields', 10, 1 ); function adfoin_mailercloud_js_fields( $field_data ) {} add_action( 'adfoin_action_fields', 'adfoin_mailercloud_action_fields' ); function adfoin_mailercloud_action_fields() { ?> 30, 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => $api_token, ), ); if ( 'POST' == $method || 'PUT' == $method|| 'PATCH' == $method ) { $args['body'] = wp_json_encode($data); } $response = wp_remote_request($url, $args); if ($record) { adfoin_add_to_log($response, $url, $args, $record); } return $response; } add_action( 'wp_ajax_adfoin_get_mailercloud_list', 'adfoin_get_mailercloud_list', 10, 0 ); /* * Get Mailercloud subscriber lists */ function adfoin_get_mailercloud_list() { // Security Check adfoin_verify_nonce(); $cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : ''; $lists = array(); $final_list = array(); $limit = 100; $data = adfoin_mailercloud_request( 'lists/search', 'POST', array( 'limit' => $limit, 'page' => 1, 'credId' => $cred_id ) ); if( !is_wp_error( $data ) ) { $body = json_decode( wp_remote_retrieve_body( $data ), true ); $lists = $body['data']; $lists_total = absint( $body['list_count'] ); $pagination_needed = absint( $lists_total / $limit ) + 1; if( $pagination_needed >= 2) { $response_pages = array(); $response_body = array(); for( $i = 2; $i <= $pagination_needed; $i++ ){ $response_pages[$i] = adfoin_mailercloud_request( 'lists/search', 'POST', array( 'limit' => $limit, 'page' => $i, 'credId' => $cred_id ) ); $response_body[$i] = json_decode( wp_remote_retrieve_body( $response_pages[$i] ), true ); if( $response_body[$i]['data'] && is_array( $response_body[$i]['data'] ) ){ $lists = array_merge( $lists, $response_body[$i]['data'] ); } } } $final_list = wp_list_pluck( $lists, 'name', 'id' ); } wp_send_json_success( $final_list ); } add_action( 'wp_ajax_adfoin_get_mailercloud_contact_fields', 'adfoin_get_mailercloud_contact_fields', 10, 0 ); /* * Get contact fields */ function adfoin_get_mailercloud_contact_fields() { // Security Check adfoin_verify_nonce(); $cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : ''; $contact_fidlds = array(); $endpoint = "contact/property/search"; $params = array( 'limit' => 100, 'page' => 1, 'search' => '' ); $params['credId'] = $cred_id; $data = adfoin_mailercloud_request( $endpoint, 'POST', $params ); if( is_wp_error( $data ) ) { wp_send_json_error(); } $body = json_decode( wp_remote_retrieve_body( $data ) ); foreach( $body->data as $single ) { if( $single->is_default == 1 ) { array_push( $contact_fidlds, array( 'key' => $single->field_value, 'value' => $single->field_name ) ); } } wp_send_json_success( array_reverse( $contact_fidlds ) ); } add_action( 'adfoin_mailercloud_job_queue', 'adfoin_mailercloud_job_queue', 10, 1 ); function adfoin_mailercloud_job_queue( $data ) { adfoin_mailercloud_send_data( $data['record'], $data['posted_data'] ); } /* * Handles sending data to mailercloud API */ function adfoin_mailercloud_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']; $list_id = $data['listId']; $task = $record['task']; $holder = array(); $cred_id = isset( $data['credId'] ) ? $data['credId'] : ''; // Backward compatibility: default to first credential if none selected if ( empty( $cred_id ) ) { $creds = adfoin_read_credentials( 'mailercloud' ); if ( ! empty( $creds ) ) { $cred_id = $creds[0]['id']; } } unset( $data['listId'] ); unset( $data['credId'] ); foreach( $data as $key => $value ) { $holder[$key] = adfoin_get_parsed_values( $value, $posted_data ); } if( $task == 'subscribe' ) { $holder = array_filter( $holder ); if( $list_id ) { $holder['list_id'] = $list_id; } $holder['credId'] = $cred_id; $return = adfoin_mailercloud_request( 'contacts', 'POST', $holder, $record ); if( 400 == wp_remote_retrieve_response_code( $return ) ) { $body = json_decode( wp_remote_retrieve_body( $return ), true ); if( isset( $body['errors'][0]['message'] ) && $body['errors'][0]['message'] == 'Email already exist' ) { $email = $holder['email']; unset( $holder['list_id'] ); unset( $holder['email'] ); $holder['credId'] = $cred_id; $return = adfoin_mailercloud_request( 'contacts/' . $email, 'PUT', $holder, $record ); } } } return; }