From 8a057c15a9347bec3311cfdb7c1b2b877d6f1515 Mon Sep 17 00:00:00 2001 From: Nonimart Date: Wed, 15 Oct 2025 14:52:00 +0200 Subject: [PATCH] FEATURE Refining search behaviour --- includes/utilities.php | 84 +++++++++++++++++++++++++- resources/css/pages/search-results.css | 15 +++++ search.php | 48 ++++++++++++--- searchform.php | 4 -- 4 files changed, 138 insertions(+), 13 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index 72b8e76..de2b559 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -297,7 +297,7 @@ function get_post_likes_count($post_id) */ function display_likes_count($post_id, $show_icon = true) { - $likes_count = get_likes_count($post_id); + $likes_count = get_post_likes_count($post_id); $icon = $show_icon ? '❤️ ' : ''; return $icon . $likes_count . ' like' . ($likes_count > 1 ? 's' : ''); @@ -435,3 +435,85 @@ function generate_og_meta_tags() return implode("\n\t", $meta_tags); } + +/** + * Génère un extrait de recherche avec le terme surligné + * + * Cette fonction cherche le terme de recherche dans le contenu du post + * et retourne un extrait avec le terme mis en évidence. + * + * @param int $post_id L'ID du post + * @param string $search_term Le terme à rechercher et surligner + * @param int $context_length Nombre de caractères de contexte autour du terme (défaut: 150) + * @return string L'extrait avec le terme surligné en HTML + */ +function get_search_snippet($post_id, $search_term, $context_length = 150) +{ + if (empty($search_term)) { + return wp_trim_words(get_the_excerpt($post_id), 30); + } + + // Récupérer le contenu complet + $content = get_post_field('post_content', $post_id); + $title = get_the_title($post_id); + + // Nettoyer le contenu des balises HTML et shortcodes + $content = wp_strip_all_tags($content); + $content = strip_shortcodes($content); + + // Recherche insensible à la casse + $search_term_escaped = preg_quote($search_term, '/'); + $position = stripos($content, $search_term); + + // Si le terme n'est pas trouvé dans le contenu, chercher dans le titre + if ($position === false) { + $position_in_title = stripos($title, $search_term); + if ($position_in_title !== false) { + // Si trouvé dans le titre, retourner juste le début du contenu + $snippet = substr($content, 0, $context_length * 2); + $snippet = wp_trim_words($snippet, 30); + + // Surligner dans le titre et le snippet + $highlighted_title = preg_replace( + '/(' . $search_term_escaped . ')/i', + '$1', + $title + ); + + return '' . $highlighted_title . ': ' . $snippet; + } + + // Si vraiment pas trouvé, retourner l'excerpt normal + return wp_trim_words(get_the_excerpt($post_id), 30); + } + + // Calculer les positions de début et fin de l'extrait + $start = max(0, $position - $context_length); + $end = min(strlen($content), $position + strlen($search_term) + $context_length); + + // Extraire le snippet + $snippet = substr($content, $start, $end - $start); + + // Ajouter des points de suspension si nécessaire + if ($start > 0) { + $snippet = '...' . $snippet; + } + if ($end < strlen($content)) { + $snippet = $snippet . '...'; + } + + // Surligner tous les termes de recherche (supporte les recherches multiples) + $search_terms = explode(' ', $search_term); + foreach ($search_terms as $term) { + if (strlen(trim($term)) > 2) { // Ignorer les mots trop courts + $term_escaped = preg_quote(trim($term), '/'); + $snippet = preg_replace( + '/(' . $term_escaped . ')/i', + '$1', + $snippet + ); + } + } + + return $snippet; +} diff --git a/resources/css/pages/search-results.css b/resources/css/pages/search-results.css index 1406565..3da4bea 100644 --- a/resources/css/pages/search-results.css +++ b/resources/css/pages/search-results.css @@ -37,6 +37,17 @@ @apply bg-white p-8 border border-primary; @apply transition-shadow hover:shadow-lg; + &__title { + line-height: 1.2; + } + + .content-meta { + } + &__type { + @apply inline-block mb-3 px-3 py-1 font-semibold; + @apply text-primary; + } + h2 { @apply mb-4; @@ -49,6 +60,10 @@ @apply inline-block mt-4 text-primary font-semibold; @apply hover:underline; } + + .search-highlight { + @apply text-primary bg-carhop-green-50 font-bold; + } } .search-results-pagination { diff --git a/search.php b/search.php index d87040b..f04b75d 100644 --- a/search.php +++ b/search.php @@ -23,16 +23,48 @@
+ labels->singular_name; + ?> -

- -

- diff --git a/searchform.php b/searchform.php index 3fc9277..50e5d4c 100644 --- a/searchform.php +++ b/searchform.php @@ -6,10 +6,6 @@ - - - -