/**
 * Aesthetic Arches newsletter subscription.
 */
function aesthetic_handle_newsletter_subscription() {

    // Verify security nonce.
    check_ajax_referer(
        'aesthetic_newsletter_nonce',
        'security'
    );


    // Get and sanitize email.
    $email = isset( $_POST['email'] )
        ? sanitize_email( wp_unslash( $_POST['email'] ) )
        : '';


    // Validate email.
    if ( empty( $email ) || ! is_email( $email ) ) {

        wp_send_json_error(
            array(
                'message' => 'Please enter a valid email address.',
            ),
            400
        );
    }


    /*
     * MailerLite API credentials.
     *
     * Replace these with your actual values.
     *
     * IMPORTANT:
     * Do not put your API token inside newsletter-banner.php.
     */

    $api_token = defined( 'AA_MAILERLITE_API_TOKEN' )
        ? AA_MAILERLITE_API_TOKEN
        : '';

    $group_id = defined( 'AA_MAILERLITE_GROUP_ID' )
        ? AA_MAILERLITE_GROUP_ID
        : '';


    // Make sure credentials exist.
    if ( empty( $api_token ) || empty( $group_id ) ) {

        error_log(
            'Aesthetic Arches newsletter: MailerLite credentials are missing.'
        );

        wp_send_json_error(
            array(
                'message' => 'Newsletter signup is temporarily unavailable. Please try again later.',
            ),
            500
        );
    }


    // MailerLite subscriber data.
    $body = array(
        'email'  => $email,
        'groups' => array(
            (string) $group_id,
        ),
    );


    // Send subscriber to MailerLite.
    $response = wp_remote_post(
        'https://connect.mailerlite.com/api/subscribers',
        array(
            'timeout' => 15,

            'headers' => array(
                'Authorization' => 'Bearer ' . $api_token,
                'Content-Type'  => 'application/json',
                'Accept'        => 'application/json',
            ),

            'body' => wp_json_encode( $body ),
        )
    );


    // WordPress request failed.
    if ( is_wp_error( $response ) ) {

        error_log(
            'Aesthetic Arches newsletter MailerLite error: ' .
            $response->get_error_message()
        );

        wp_send_json_error(
            array(
                'message' => 'We could not connect to the newsletter service. Please try again.',
            ),
            502
        );
    }


    $status_code = wp_remote_retrieve_response_code( $response );

    $response_body = json_decode(
        wp_remote_retrieve_body( $response ),
        true
    );


    // MailerLite accepted the subscriber.
    if ( $status_code === 200 || $status_code === 201 ) {

        wp_send_json_success(
            array(
                'message' => 'You are on the list. Welcome to The Sunday Edit!',
            )
        );
    }


    // Handle MailerLite validation errors.
    if ( $status_code === 422 ) {

        $message = 'Please check your email address and try again.';

        if (
            ! empty( $response_body['errors']['email'][0] )
        ) {
            $message = sanitize_text_field(
                $response_body['errors']['email'][0]
            );
        }

        wp_send_json_error(
            array(
                'message' => $message,
            ),
            422
        );
    }


    // Handle all other MailerLite errors.
    error_log(
        'Aesthetic Arches newsletter MailerLite HTTP ' .
        $status_code .
        ': ' .
        wp_remote_retrieve_body( $response )
    );

    wp_send_json_error(
        array(
            'message' => 'Something went wrong while subscribing. Please try again.',
        ),
        500
    );
}


add_action(
    'wp_ajax_aesthetic_subscribe',
    'aesthetic_handle_newsletter_subscription'
);

add_action(
    'wp_ajax_nopriv_aesthetic_subscribe',
    'aesthetic_handle_newsletter_subscription'
);<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//aestheticarches.com/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://aestheticarches.com/page-sitemap.xml</loc>
		<lastmod>2026-09-03T07:42:11+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Rank Math SEO Plugin (c) Rank Math - rankmath.com -->