From e5079b99764d4a30b9e28f8ff4a50fa3807d972e Mon Sep 17 00:00:00 2001 From: Duskell Date: Sun, 1 Feb 2026 12:42:49 +0100 Subject: [PATCH] first test on ajax --- public/class-partnerexpo-core-public.php | 33 ++++++++++++++++++++++++ public/js/searchbox.js | 10 ++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/public/class-partnerexpo-core-public.php b/public/class-partnerexpo-core-public.php index d0ee3d6..aefa587 100644 --- a/public/class-partnerexpo-core-public.php +++ b/public/class-partnerexpo-core-public.php @@ -83,4 +83,37 @@ class Partnerexpo_Core_Public { ); } + public function register_endpoint() { + register_rest_route('pexpo/v1', '/query', [ + 'methods' => 'GET', + 'callback' => 'query_partners', + 'permission_callback' => '__return_true', + ]); + } + + function query_partners(WP_REST_Request $request) { + $args = [ + 'post_type' => 'pexpo_partners', + 'posts_per_page' => 15, + 'post_status' => 'publish', + ]; + + $query = new WP_Query($args); + + $posts = []; + + foreach ($query->posts as $post) { + $posts[] = [ + 'id' => $post->ID, + 'title' => $post->post_title, + 'slug' => $post->post_name, + 'url' => get_permalink($post), + ]; + } + + wp_reset_postdata(); + + return rest_ensure_response($posts); + } + } diff --git a/public/js/searchbox.js b/public/js/searchbox.js index ffc36a2..4c6d881 100644 --- a/public/js/searchbox.js +++ b/public/js/searchbox.js @@ -90,14 +90,16 @@ document.addEventListener('DOMContentLoaded', () => { try { // REPLACE THIS URL with your actual endpoint - // const response = await fetch(`/api/search?q=${encodeURIComponent(query)}&limit=${filters.maxResults}`); - // const data = await response.json(); + const response = await fetch(`/pexpo/v1/query?q=${encodeURIComponent(query)}&limit=${filters.maxResults}`); + const data = await response.json(); // --- SIMULATED FETCH FOR DEMO (Remove this block in production) --- - await new Promise(r => setTimeout(r, 600)); // Fake network delay - const data = simulateBackendResponse(query); + // await new Promise(r => setTimeout(r, 600)); // Fake network delay + // const data = simulateBackendResponse(query); // ------------------------------------------------------------------ + console.log(data); + results = data; // 2. Compute local relevance if the API doesn't return it