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(); +?> +
+

Recherche : “

+

+ + + + + résultats + +

+
+ get_post_type($article->ID), + 'post_id' => $article->ID, + 'search' => $search, + 'isInnerSearch' => true, + )); + endforeach; + else : + echo '

Aucun article trouvé.

'; + endif; + ?> +
+
+ $html_template, + 'post_count' => $post_count, + 'query_args' => array('search' => $search, 'revue_id' => $current_revue_id), // Pour debug + ); + $response = new WP_REST_Response($response_data); + + $response->set_status(200); + + return $response; +} + + function like_post($request) { diff --git a/resources/css/app.css b/resources/css/app.css index da3ab56..9e19808 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -35,6 +35,7 @@ @import './components/article-informations.css'; @import './components/search-module.css'; @import './components/post-count.css'; +@import './components/search-results-card.css'; /* ########### PAGES ############ */ @import './pages/singles.css'; diff --git a/resources/css/components/search-module.css b/resources/css/components/search-module.css index 7ea67c7..3e2e95c 100644 --- a/resources/css/components/search-module.css +++ b/resources/css/components/search-module.css @@ -67,7 +67,7 @@ @apply text-white; } input::placeholder { - @apply text-white; + @apply !text-white; } &__wrapper-container { @apply max-w-screen-xl mx-auto; @@ -82,9 +82,10 @@ @apply mt-2 mb-8 bg-neutral-500 border-none opacity-50 w-full; height: 1px; } + &__input { box-sizing: border-box; - @apply block max-w-full w-full flex-grow !py-4 !border-white px-4 focus-visible:ring-primary focus-visible:ring-2; + @apply block max-w-full w-full flex-grow !py-4 !border-white px-4 !pl-12 focus-visible:ring-primary focus-visible:ring-2; @apply border rounded-none; outline: none !important; /* border-right: none; @@ -105,7 +106,12 @@ } } &__searchbar-group { - @apply w-full flex flex-col md:flex-row gap-y-4; + @apply w-full flex flex-col md:flex-row gap-y-4 relative; + &:before { + @apply content-[''] absolute inset-0 bg-contain bg-center bg-no-repeat w-6 h-6 left-4 top-1/2 -translate-y-1/2; + background-image: url('../resources/img/icons/carhop-rechercher.svg'); + filter: invert(1); + } } &__suggestions { @apply pt-4; diff --git a/resources/css/components/search-results-card.css b/resources/css/components/search-results-card.css new file mode 100644 index 0000000..e0f79dc --- /dev/null +++ b/resources/css/components/search-results-card.css @@ -0,0 +1,39 @@ +.search-results-card { + @apply bg-white p-8 border border-primary; + &__title { + @apply !text-lg !font-normal !mb-2; + font-family: 'Nunito Sans', sans-serif !important; + line-height: 1.2; + } + &--inner-search { + @apply bg-transparent border-none px-0; + } + + .content-meta { + } + &__type { + @apply inline-block mb-3 px-3 py-1 font-semibold; + @apply text-primary; + } + + h2 { + @apply mb-4; + + a { + @apply text-primary hover:underline; + } + } + + &__link { + @apply inline-block mt-4 text-primary font-semibold; + @apply hover:underline; + } + + &__parution-date { + @apply block text-lg font-normal text-primary !mb-4; + } + + .search-highlight { + @apply text-primary bg-carhop-green-50 font-bold; + } +} diff --git a/resources/css/pages/search-results.css b/resources/css/pages/search-results.css index b364205..42e03c2 100644 --- a/resources/css/pages/search-results.css +++ b/resources/css/pages/search-results.css @@ -33,44 +33,6 @@ @apply mb-4; } - .search-results-card { - @apply bg-white p-8 border border-primary; - @apply transition-shadow hover:shadow-lg; - - &__title { - @apply text-xl !mb-2; - 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; - - a { - @apply text-primary hover:underline; - } - } - - &__link { - @apply inline-block mt-4 text-primary font-semibold; - @apply hover:underline; - } - - &__parution-date { - @apply block text-lg font-normal text-primary !mb-4; - } - - .search-highlight { - @apply text-primary bg-carhop-green-50 font-bold; - } - } - .search-results-pagination { @apply mt-12 mb-8; diff --git a/resources/css/pages/single-revues.css b/resources/css/pages/single-revues.css index e69de29..a8d6c31 100644 --- a/resources/css/pages/single-revues.css +++ b/resources/css/pages/single-revues.css @@ -0,0 +1,13 @@ +.page--single-revue { + .search-results__title { + @apply mb-12; + } + + .search-results-card { + @apply p-0 my-3; + } + + .post-count { + @apply mb-12; + } +} diff --git a/resources/img/icons/carhop-rechercher.svg b/resources/img/icons/carhop-rechercher.svg new file mode 100644 index 0000000..388bd76 --- /dev/null +++ b/resources/img/icons/carhop-rechercher.svg @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/resources/js/singles/search-revue.ts b/resources/js/singles/search-revue.ts new file mode 100644 index 0000000..aede782 --- /dev/null +++ b/resources/js/singles/search-revue.ts @@ -0,0 +1,43 @@ +export default function handleSearchRevue() { + const searchRevue = document.querySelector( + '.page--single-revue .sidebar .search-field input' + ) as HTMLInputElement; + const contentArea = document.querySelector('.page--single-revue .content-area') as HTMLElement; + const currentRevueId = document + .querySelector('.page--single-revue') + ?.getAttribute('data-revue-id') as string; + + if (!searchRevue || !contentArea) return; + + // Sauvegarder le contenu original + const originalContent = contentArea.innerHTML; + + searchRevue.addEventListener('input', async () => { + const searchValue = searchRevue.value.trim(); + + // Si la recherche est vide, restaurer le contenu original + if (searchValue === '') { + contentArea.innerHTML = originalContent; + return; + } + + try { + const response = await fetch( + `/wp-json/dynamiques-datas/v1/build/revues?search=${encodeURIComponent( + searchValue + )}¤t_revue_id=${currentRevueId}` + ); + const data = await response.json(); + + // Afficher les résultats dans content-area + contentArea.innerHTML = data.html_template; + } catch (error) { + console.error('Erreur lors de la récupération des revues:', error); + contentArea.innerHTML = ` +
+

Une erreur est survenue lors de la recherche. Veuillez réessayer.

+
+ `; + } + }); +} diff --git a/resources/js/singles/singles.ts b/resources/js/singles/singles.ts index 15623d2..02043b2 100644 --- a/resources/js/singles/singles.ts +++ b/resources/js/singles/singles.ts @@ -7,7 +7,7 @@ import handleArticleReader from './reader.ts'; import { handleArticleToolbar } from './article-toolbar.ts'; import { handleRevueToolbar } from './revue-toolbar.ts'; - +import handleSearchRevue from './search-revue.ts'; export default function singles(): void { const isSingleRevue: HTMLElement | null = document.querySelector('.page--single-revue'); const isSingleArticle: HTMLElement | null = document.querySelector('.page--single-articles'); @@ -21,4 +21,5 @@ export default function singles(): void { handleRevueToolbar(); handleShareButton(); handleArticleReader(); + handleSearchRevue(); } diff --git a/search.php b/search.php index 7f8740a..0145cb4 100644 --- a/search.php +++ b/search.php @@ -22,54 +22,11 @@ -
- labels->singular_name; - ?> - -
- -
- - -

- - 2) { - $term_escaped = preg_quote(trim($term), '/'); - $title = preg_replace( - '/(' . $term_escaped . ')/i', - '$1', - $title - ); - } - } - } - echo $title; - ?> - -

- - - -
- -
- - get_the_permalink(), - 'label' => 'Lire la suite', - 'target' => '_self', - )); ?> - -
+ get_post_type(), + 'post_id' => get_the_ID(), + 'search' => get_search_query(), + )); ?> diff --git a/single-revues.php b/single-revues.php index e27f9ea..3594d97 100644 --- a/single-revues.php +++ b/single-revues.php @@ -41,7 +41,6 @@ $articles = get_field('articles', $current_issue->ID); -
@@ -75,12 +74,9 @@ $articles = get_field('articles', $current_issue->ID); 'revueID' => get_the_ID() )); ?> - get_the_ID() )); ?> - -
diff --git a/template-parts/search/revue-innersearch-grid.php b/template-parts/search/revue-innersearch-grid.php new file mode 100644 index 0000000..8e2663f --- /dev/null +++ b/template-parts/search/revue-innersearch-grid.php @@ -0,0 +1,15 @@ +have_posts()) : + while ($articles->have_posts()) : $articles->the_post(); + get_template_part('template-parts/articles/card-article', null, array( + 'date' => get_the_date(), + 'image' => get_the_post_thumbnail_url(), + 'link' => get_the_permalink(), + 'ID' => get_the_ID(), + 'showAuthors' => true, + )); + endwhile; +else : + echo '

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 + ); + } + } +} +?> + +
+ +
+ +
+ + +

+ + + +

+ + + +
+ +
+ + $permalink, + 'label' => 'Lire la suite', + 'target' => '_self', + )); ?> + +
\ No newline at end of file