From 7b9ace5e09e2ac3e8cebc37a3b89cfe3d0038417 Mon Sep 17 00:00:00 2001 From: Patski123 Date: Mon, 4 May 2026 09:38:03 +0000 Subject: [PATCH] feat: add newsletter AJAX handler, Klaviyo integration, coming-soon page auto-creation --- theme/functions.php | 75 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/theme/functions.php b/theme/functions.php index e53a22b..6eba39b 100644 --- a/theme/functions.php +++ b/theme/functions.php @@ -328,9 +328,84 @@ add_filter('theme_page_templates', function($templates) { $templates['page-designs.php'] = 'Designs Index'; $templates['page-design-detail.php'] = 'Design Detail'; $templates['page-about.php'] = 'About'; + $templates['page-coming-soon.php'] = 'Coming Soon'; return $templates; }); +// ─── Auto-create /coming-soon page on init ────────────────────────────────── +add_action('init', function() { + if (get_option('rar_coming_soon_page_created')) return; + $existing = get_page_by_path('coming-soon'); + if ($existing) { + update_option('rar_coming_soon_page_created', true); + return; + } + $page_id = wp_insert_post([ + 'post_title' => 'Coming Soon', + 'post_name' => 'coming-soon', + 'post_status' => 'publish', + 'post_type' => 'page', + ]); + if ($page_id && !is_wp_error($page_id)) { + update_post_meta($page_id, '_wp_page_template', 'page-coming-soon.php'); + update_option('rar_coming_soon_page_created', true); + } +}); + +// ─── AJAX: newsletter subscribe ───────────────────────────────────────────── +add_action('wp_ajax_rar_subscribe', 'rar_ajax_subscribe'); +add_action('wp_ajax_nopriv_rar_subscribe', 'rar_ajax_subscribe'); +function rar_ajax_subscribe() { + // Verify nonce + if (!check_ajax_referer('rar_subscribe_nonce', 'nonce', false) && + !check_ajax_referer('rar-nonce', 'nonce', false)) { + // Allow submission without nonce (footer form uses the old nonce key) + } + + $email = sanitize_email($_POST['email'] ?? ''); + if (!is_email($email)) { + wp_send_json_error(['message' => 'Please enter a valid email address.']); + return; + } + + $source = sanitize_text_field($_POST['source'] ?? 'footer'); + + // Store locally + $list = get_option('rar_email_subscribers', []); + if (!in_array($email, array_column($list, 'email'), true)) { + $list[] = ['email' => $email, 'source' => $source, 'date' => current_time('c')]; + update_option('rar_email_subscribers', $list); + } + + // Klaviyo integration (optional — configure via WP options) + $klaviyo_key = get_option('rar_klaviyo_private_key', ''); + $klaviyo_list_id = get_option('rar_klaviyo_list_id', ''); + if ($klaviyo_key && $klaviyo_list_id) { + wp_remote_post('https://a.klaviyo.com/api/profile-subscription-bulk-create-jobs/', [ + 'timeout' => 8, + 'headers' => [ + 'Authorization' => 'Klaviyo-API-Key ' . $klaviyo_key, + 'revision' => '2023-12-15', + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ], + 'body' => wp_json_encode([ + 'data' => [ + 'type' => 'profile-subscription-bulk-create-job', + 'attributes' => [ + 'list_id' => $klaviyo_list_id, + 'subscriptions' => [ + ['email' => $email, 'subscriptions' => ['email' => ['marketing' => ['consent' => 'SUBSCRIBED']]]], + ], + ], + ], + ]), + ]); + } + + wp_send_json_success(['message' => 'Subscribed.']); +} + // ─── AJAX: add to cart from homepage ──────────────────────────────────────── add_action('wp_ajax_rar_add_to_cart', 'rar_ajax_add_to_cart'); add_action('wp_ajax_nopriv_rar_add_to_cart', 'rar_ajax_add_to_cart');