diff --git a/includes/api.php b/includes/api.php index aada02f..5e68fbd 100644 --- a/includes/api.php +++ b/includes/api.php @@ -14,6 +14,13 @@ add_action('rest_api_init', function () { 'permission_callback' => '__return_true', )); + // ################ FILTER REVUES ################ + register_rest_route('dynamiques-datas/v1/build', '/revues', array( + 'methods' => 'GET', + 'callback' => 'build_revues', + 'permission_callback' => '__return_true', + )); + /* ---------------- INTERACTIONS ROUTES @@ -124,6 +131,88 @@ function build_articles($request) } +// ################ FILTER REVUES ################ + +function build_revues($request) +{ + $search = esc_html($request->get_param('search')); + $current_revue_id = intval($request->get_param('current_revue_id')); + + // Récupérer les articles liés à la revue courante + $issue_related_articles = get_field('articles', $current_revue_id); + + if (!$issue_related_articles) { + $response_data = array( + 'html_template' => '
Aucun article trouvé.
', + 'post_count' => 0, + 'query_args' => array(), + ); + return new WP_REST_Response($response_data, 200); + } + + // Filtrer les articles selon la recherche + $filtered_articles = array(); + if (!empty($search)) { + foreach ($issue_related_articles as $article) { + // Recherche dans le titre et le contenu + $title = get_the_title($article->ID); + $content = get_post_field('post_content', $article->ID); + + if (stripos($title, $search) !== false || stripos($content, $search) !== false) { + $filtered_articles[] = $article; + } + } + } else { + $filtered_articles = $issue_related_articles; + } + + $post_count = count($filtered_articles); + + ob_start(); +?> +Aucun article trouvé.
'; + endif; + ?> +Une erreur est survenue lors de la recherche. Veuillez réessayer.
+Aucun article trouvé.
'; +endif; diff --git a/template-parts/search/search-results-card.php b/template-parts/search/search-results-card.php new file mode 100644 index 0000000..02a40c1 --- /dev/null +++ b/template-parts/search/search-results-card.php @@ -0,0 +1,57 @@ +labels->singular_name; +$title = get_the_title($post_id); +$permalink = get_the_permalink($post_id); +$date = get_the_date('F Y', $post_id); + +// Mise en évidence des termes recherchés dans le titre +if (!empty($search)) { + $search_terms = explode(' ', $search); + foreach ($search_terms as $term) { + if (strlen(trim($term)) > 2) { + $term_escaped = preg_quote(trim($term), '/'); + $title = preg_replace( + '/(' . $term_escaped . ')/i', + '$1', + $title + ); + } + } +} +?> + +