__( 'Listmonk', 'advanced-form-integration' ), 'tasks' => array( 'add_to_list' => __( 'Create / Subscribe Subscriber', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_listmonk_settings_tab', 10, 1 ); function adfoin_listmonk_settings_tab( $tabs ) { $tabs['listmonk'] = __( 'Listmonk', 'advanced-form-integration' ); return $tabs; } add_action( 'adfoin_settings_view', 'adfoin_listmonk_settings_view', 10, 1 ); function adfoin_listmonk_settings_view( $current_tab ) { if ( 'listmonk' !== $current_tab ) { return; } $title = __( 'Listmonk', 'advanced-form-integration' ); $key = 'listmonk'; $arguments = wp_json_encode( array( 'platform' => $key, 'fields' => array( array( 'key' => 'baseUrl', 'label' => __( 'Base URL (e.g. https://listmonk.example.com)', 'advanced-form-integration' ), ), array( 'key' => 'username', 'label' => __( 'API Username', 'advanced-form-integration' ), ), array( 'key' => 'password', 'label' => __( 'API Password / Token', 'advanced-form-integration' ), 'hidden' => true, ), ), ) ); $instructions = sprintf( /* translators: 1: opening link tag, 2: closing tag */ __( '

Create an API user/token in Listmonk (Admin → Users) and note the username and token. Provide the instance base URL (no trailing slash). AFI authenticates using HTTP Basic auth for every API call. Refer to the %1$sListmonk API docs%2$s for full details.

', 'advanced-form-integration' ), '', '' ); echo adfoin_platform_settings_template( $title, $key, $arguments, $instructions ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } add_action( 'wp_ajax_adfoin_get_listmonk_credentials', 'adfoin_get_listmonk_credentials', 10, 0 ); function adfoin_get_listmonk_credentials() { adfoin_verify_nonce(); $credentials = adfoin_read_credentials( 'listmonk' ); wp_send_json_success( $credentials ); } add_action( 'wp_ajax_adfoin_save_listmonk_credentials', 'adfoin_save_listmonk_credentials', 10, 0 ); function adfoin_save_listmonk_credentials() { adfoin_verify_nonce(); $platform = isset( $_POST['platform'] ) ? sanitize_text_field( wp_unslash( $_POST['platform'] ) ) : ''; if ( 'listmonk' === $platform ) { $data = isset( $_POST['data'] ) ? adfoin_array_map_recursive( 'sanitize_text_field', wp_unslash( $_POST['data'] ) ) : array(); adfoin_save_credentials( $platform, $data ); } wp_send_json_success(); } function adfoin_listmonk_credentials_list() { $credentials = adfoin_read_credentials( 'listmonk' ); foreach ( $credentials as $credential ) { $label = isset( $credential['title'] ) ? $credential['title'] : ''; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } add_action( 'adfoin_action_fields', 'adfoin_listmonk_action_fields' ); function adfoin_listmonk_action_fields() { ?> __( 'Select a Listmonk credential first.', 'advanced-form-integration' ), ) ); } $credentials = adfoin_get_credentials_by_id( 'listmonk', $cred_id ); if ( empty( $credentials ) ) { wp_send_json_error( array( 'message' => __( 'Invalid credential.', 'advanced-form-integration' ), ) ); } $response = adfoin_listmonk_api_request( $credentials, 'api/lists', 'GET', array(), array(), array( 'per_page' => 'all' ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } $status = wp_remote_retrieve_response_code( $response ); if ( 200 !== (int) $status ) { wp_send_json_error( array( 'message' => adfoin_listmonk_extract_error_message( $response ), ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['data']['results'] ) || ! is_array( $body['data']['results'] ) ) { wp_send_json_error( array( 'message' => __( 'Unable to parse list response.', 'advanced-form-integration' ), ) ); } $lists = array(); foreach ( $body['data']['results'] as $list ) { if ( isset( $list['id'], $list['name'] ) ) { $lists[ $list['id'] ] = $list['name']; } } wp_send_json_success( $lists ); } add_action( 'adfoin_listmonk_job_queue', 'adfoin_listmonk_job_queue', 10, 1 ); function adfoin_listmonk_job_queue( $data ) { adfoin_listmonk_send_data( $data['record'], $data['posted_data'] ); } function adfoin_listmonk_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; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $cred_id = isset( $field_data['credId'] ) ? $field_data['credId'] : ''; $list_id = isset( $field_data['listId'] ) ? intval( $field_data['listId'] ) : 0; $email_raw = isset( $field_data['email'] ) ? $field_data['email'] : ''; $email = $email_raw ? sanitize_email( adfoin_get_parsed_values( $email_raw, $posted_data ) ) : ''; if ( empty( $cred_id ) || ! $list_id || empty( $email ) ) { return; } $credentials = adfoin_get_credentials_by_id( 'listmonk', $cred_id ); if ( empty( $credentials ) ) { return; } $name_raw = isset( $field_data['name'] ) ? $field_data['name'] : ''; $name = $name_raw ? adfoin_get_parsed_values( $name_raw, $posted_data ) : ''; if ( empty( $name ) ) { $name = $email; } $status = isset( $field_data['status'] ) && 'blocklisted' === $field_data['status'] ? 'blocklisted' : 'enabled'; $payload = array( 'email' => $email, 'name' => $name, 'status' => $status, 'lists' => array( $list_id ), ); if ( ! empty( $field_data['customFields'] ) ) { $attribs_raw = adfoin_get_parsed_values( $field_data['customFields'], $posted_data ); $attribs = json_decode( $attribs_raw, true ); if ( json_last_error() === JSON_ERROR_NONE && is_array( $attribs ) ) { $payload['attribs'] = $attribs; } } if ( ! empty( $field_data['preconfirm'] ) && 'true' === $field_data['preconfirm'] ) { $payload['preconfirm_subscriptions'] = true; } $response = adfoin_listmonk_api_request( $credentials, 'api/subscribers', 'POST', $payload, $record ); if ( is_wp_error( $response ) ) { return; } $status_code = wp_remote_retrieve_response_code( $response ); if ( $status_code < 200 || $status_code >= 300 ) { return; } } function adfoin_listmonk_api_request( $credentials, $endpoint, $method = 'GET', $payload = array(), $record = array(), $query = array() ) { $base_url = adfoin_listmonk_normalize_base_url( $credentials ); if ( is_wp_error( $base_url ) ) { return $base_url; } $username = isset( $credentials['username'] ) ? trim( $credentials['username'] ) : ''; $password = isset( $credentials['password'] ) ? trim( $credentials['password'] ) : ''; if ( empty( $username ) || empty( $password ) ) { return new WP_Error( 'listmonk_missing_auth', __( 'Listmonk username or password/token is missing.', 'advanced-form-integration' ) ); } $url = trailingslashit( $base_url ) . ltrim( $endpoint, '/' ); if ( ! empty( $query ) ) { $url = add_query_arg( $query, $url ); } $args = array( 'timeout' => 30, 'method' => strtoupper( $method ), 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), ); if ( in_array( strtoupper( $method ), array( 'POST', 'PUT', 'PATCH' ), true ) ) { $args['body'] = wp_json_encode( $payload ); } $response = wp_remote_request( $url, $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } function adfoin_listmonk_normalize_base_url( $credentials ) { $base_url = isset( $credentials['baseUrl'] ) ? trim( $credentials['baseUrl'] ) : ''; if ( empty( $base_url ) ) { return new WP_Error( 'listmonk_missing_base', __( 'Listmonk base URL is missing.', 'advanced-form-integration' ) ); } if ( ! preg_match( '#^https?://#i', $base_url ) ) { $base_url = 'https://' . $base_url; } return untrailingslashit( $base_url ); } function adfoin_listmonk_extract_error_message( $response ) { if ( is_wp_error( $response ) ) { return $response->get_error_message(); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['message'] ) ) { return $body['message']; } return __( 'Unexpected Listmonk API error.', 'advanced-form-integration' ); }