feat: add newsletter AJAX handler, Klaviyo integration, coming-soon page auto-creation

This commit is contained in:
2026-05-04 09:38:03 +00:00
parent a5e953ccec
commit 7b9ace5e09

View File

@@ -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');