__( 'Moosend', 'advanced-form-integration' ), 'tasks' => array( 'subscribe' => __( 'Subscribe To List', 'advanced-form-integration' ), ) ); return $actions; } /** * Get Moosend credentials by ID * * @param string $cred_id Credential ID (optional, can be from $_POST) * @return array Array with 'api_token' key, or empty string if not found */ function adfoin_moosend_get_credentials( $cred_id = '' ) { // If no cred_id provided, try to get from POST if ( empty( $cred_id ) && isset( $_POST['credId'] ) ) { $cred_id = sanitize_text_field( wp_unslash( $_POST['credId'] ) ); } $api_token = ''; if ( $cred_id ) { $credentials = adfoin_read_credentials( 'moosend' ); foreach( $credentials as $single ) { if( $single['id'] == $cred_id ) { $api_token = $single['api_token']; break; } } } else { // Fallback to old options if no cred_id provided $api_token = get_option( 'adfoin_moosend_api_token' ) ? get_option( 'adfoin_moosend_api_token' ) : ''; } return array( 'api_token' => $api_token ); } add_filter( 'adfoin_settings_tabs', 'adfoin_moosend_settings_tab', 10, 1 ); function adfoin_moosend_settings_tab( $providers ) { $providers['moosend'] = __( 'Moosend', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_moosend_settings_view', 10, 1 ); function adfoin_moosend_settings_view( $current_tab ) { if( $current_tab != 'moosend' ) { return; } // Load Account Manager if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } // Migrate old settings if they exist and no new credentials exist $old_api_token = get_option( 'adfoin_moosend_api_token' ); $existing_creds = adfoin_read_credentials( 'moosend' ); if ( $old_api_token && empty( $existing_creds ) ) { $new_cred = array( 'id' => wp_generate_uuid4(), 'title' => 'Default Account (Legacy)', 'api_token' => $old_api_token ); adfoin_save_credentials( 'moosend', array( $new_cred ) ); } $fields = array( array( 'name' => 'api_token', 'label' => __( 'API Token', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'placeholder' => __( 'Enter your API Token', 'advanced-form-integration' ), 'mask' => true, 'show_in_table' => true, ), ); $instructions = '
  1. ' . __( 'Go to Settings > API Key in your Moosend account.', 'advanced-form-integration' ) . '
  2. ' . __( 'Copy your API Token.', 'advanced-form-integration' ) . '
  3. ' . __( 'Enter the API Token above.', 'advanced-form-integration' ) . '
  4. ' . __( 'Click "Add Account" and save your credentials.', 'advanced-form-integration' ) . '
'; ADFOIN_Account_Manager::render_settings_view( 'moosend', 'Moosend', $fields, $instructions ); } // AJAX Hooks for Account Manager add_action( 'wp_ajax_adfoin_get_moosend_credentials', 'adfoin_get_moosend_credentials' ); function adfoin_get_moosend_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( 'moosend' ); } add_action( 'wp_ajax_adfoin_save_moosend_credentials', 'adfoin_save_moosend_credentials' ); function adfoin_save_moosend_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( 'moosend', array( 'api_token' ) ); } add_action( 'wp_ajax_adfoin_get_moosend_credentials_list', 'adfoin_moosend_get_credentials_list_ajax' ); function adfoin_moosend_get_credentials_list_ajax() { adfoin_verify_nonce(); if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } $fields = array( array( 'name' => 'api_token', 'mask' => true ), ); ADFOIN_Account_Manager::ajax_get_credentials_list( 'moosend', $fields ); } add_action( 'adfoin_add_js_fields', 'adfoin_moosend_js_fields', 10, 1 ); function adfoin_moosend_js_fields( $field_data ) {} add_action( 'adfoin_action_fields', 'adfoin_moosend_action_fields' ); function adfoin_moosend_action_fields() { ?> Context->MailingLists, 'Name', 'ID' ); wp_send_json_success( $lists ); } /* * Request to Moosend API */ function adfoin_moosend_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $base_url = 'https://api.moosend.com/v3/'; $credentials = adfoin_moosend_get_credentials( $cred_id ); $api_token = $credentials['api_token']; if( !$api_token ) { return new WP_Error( 'missing_credentials', 'API Token is missing' ); } $url = $base_url . $endpoint; $url = add_query_arg( array( 'apikey' => $api_token ), $url ); $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Accept' => 'application/json', 'Content-Type' => 'application/json; charset=utf-8', ), ); if ( 'POST' == $method || 'PUT' == $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( 'adfoin_moosend_job_queue', 'adfoin_moosend_job_queue', 10, 1 ); function adfoin_moosend_job_queue( $data ) { adfoin_moosend_send_data( $data['record'], $data['posted_data'] ); } /* * Handles sending data to moosend API */ function adfoin_moosend_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'] : ''; $list_id = $data['listId']; $task = $record['task']; if( $task == 'subscribe' ) { $email = empty( $data['email'] ) ? '' : adfoin_get_parsed_values( $data['email'], $posted_data ); $subscriber = array( 'email' => trim( $email ) ); if( isset( $data['name'] ) && $data['name'] ) { $subscriber['name'] = adfoin_get_parsed_values( $data['name'], $posted_data ); } if( isset( $data['mobile'] ) && $data['mobile'] ) { $subscriber['mobile'] = adfoin_get_parsed_values( $data['mobile'], $posted_data ); } $return = adfoin_moosend_request( 'subscribers/' . $list_id . '/subscribe.json', 'POST', $subscriber, $record, $cred_id ); } return; }