sanitize_text_field($fields['king_addons_mailchimp_firstname'] ?? ''), 'LNAME' => sanitize_text_field($fields['king_addons_mailchimp_lastname'] ?? ''), 'PHONE' => sanitize_text_field($fields['king_addons_mailchimp_phone_number'] ?? ''), ]; // Build Mailchimp API endpoint $api_url = sprintf( 'https://%s.api.mailchimp.com/3.0/lists/%s/members/%s', explode('-', $api_key)[1], $list_id, wp_hash(strtolower($email)) ); // Set up request args $api_args = [ 'method' => 'PUT', 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'apikey ' . $api_key, ], 'body' => json_encode([ 'email_address' => $email, 'status' => 'subscribed', 'merge_fields' => $merge_fields, ]), ]; // Send request $response = wp_remote_post($api_url, $api_args); // Check response if (!is_wp_error($response)) { $body = json_decode(wp_remote_retrieve_body($response)); if (!empty($body)) { if (isset($body->status) && $body->status === 'subscribed') { wp_send_json(['status' => 'subscribed']); } else { // Security fix: Sanitize title from remote API response to prevent XSS wp_send_json(['status' => esc_html($body->title ?? '')]); } } } } } new MailChimp_Ajax();