diff --git a/includes/api.php b/includes/api.php index 3424549..232c47c 100644 --- a/includes/api.php +++ b/includes/api.php @@ -13,8 +13,29 @@ add_action('rest_api_init', function () { 'callback' => 'build_posts', 'permission_callback' => '__return_true', )); + + register_rest_route('carhop-datas/v1/build', '/fonds-archives', array( + 'methods' => 'GET', + 'callback' => 'build_fonds_archives_posts_by_letter', + 'permission_callback' => '__return_true', + )); }); +function carhop_posts_where_starts_with_letter($where, $query) +{ + global $wpdb; + + $start_letter = $query->get('carhop_start_letter'); + if (empty($start_letter)) { + return $where; + } + + $start_letter = mb_substr((string) $start_letter, 0, 1); + $like = $wpdb->esc_like($start_letter) . '%'; + + return $where . $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE %s", $like); +} + // ################ FILTER POSTS ################ @@ -117,3 +138,77 @@ function build_posts($request) return $response; } + + + + +function build_fonds_archives_posts_by_letter($request) +{ + $post_type = 'fonds-archives'; + $sort_by = esc_html($request->get_param('sort_by')); + $offset = esc_html($request->get_param('offset')) ?? 0; + $posts_per_page = esc_html($request->get_param('posts_per_page')) ?? -1; + + $start_letter = sanitize_text_field($request->get_param('start_letter')); + + // Construire les arguments de la query WordPress + $args = array( + 'post_type' => $post_type, + 'offset' => $offset, + 'posts_per_page' => $posts_per_page, + ); + + if (!empty($start_letter)) { + $args['carhop_start_letter'] = $start_letter; + } + + + // Gestion du tri + switch ($sort_by) { + case 'date_desc': + $args['orderby'] = 'date'; + $args['order'] = 'DESC'; + break; + case 'date_asc': + $args['orderby'] = 'date'; + $args['order'] = 'ASC'; + break; + case 'title_asc': + $args['orderby'] = 'title'; + $args['order'] = 'ASC'; + break; + default: + $args['orderby'] = 'date'; + $args['order'] = 'DESC'; + } + + add_filter('posts_where', 'carhop_posts_where_starts_with_letter', 10, 2); + $posts_query = new WP_Query($args); + remove_filter('posts_where', 'carhop_posts_where_starts_with_letter', 10); + + ob_start(); + if ($posts_query->have_posts()) : + while ($posts_query->have_posts()) : $posts_query->the_post(); + get_template_part('template-parts/components/cards/post-card', null, array( + 'ID' => get_the_ID(), + 'current_post_type' => $post_type + )); + endwhile; + else : + echo '
Aucune archive trouvée commençant par la lettre ' . '' . $start_letter . '
'; + endif; + wp_reset_postdata(); + + $html_template = ob_get_clean(); + + $response_data = array( + 'html_template' => $html_template, + 'post_count' => $posts_query->found_posts, + 'query_args' => $args, // Pour debug + ); + $response = new WP_REST_Response($response_data); + + $response->set_status(200); + + return $response; +} diff --git a/resources/css/pages/archive-fonds-archives.css b/resources/css/pages/archive-fonds-archives.css index da7554a..3a63837 100644 --- a/resources/css/pages/archive-fonds-archives.css +++ b/resources/css/pages/archive-fonds-archives.css @@ -10,6 +10,9 @@ .alphabetical-order-select__list { @apply flex gap-5; } + .alphabetical-order-select__button[aria-pressed='true'] { + @apply font-extrabold underline underline-offset-4; + } } } } diff --git a/resources/js/app.ts b/resources/js/app.ts index 53c5d15..e59b3ba 100644 --- a/resources/js/app.ts +++ b/resources/js/app.ts @@ -6,6 +6,7 @@ import alternatePictures from './alternate-pictures'; import { searchBarInit } from './search-bar'; import singlesInit from './singles/singles'; import archivesInit from './archives/archives'; +import archivesFondsArchivesInit from './archives/archives-fonds-archives'; import handleCiteButton from './singles/cite-button'; import titlesInit from './titles'; import { @@ -22,6 +23,7 @@ window.addEventListener('load', function () { searchBarInit(); singlesInit(); archivesInit(); + archivesFondsArchivesInit(); handleCiteButton(); titlesInit(); pageHeaderAnimationInit(); diff --git a/resources/js/archives/archives-fonds-archives.ts b/resources/js/archives/archives-fonds-archives.ts new file mode 100644 index 0000000..3c79811 --- /dev/null +++ b/resources/js/archives/archives-fonds-archives.ts @@ -0,0 +1,75 @@ +export default function archivesFondsArchivesInit() { + const toolbar = document.querySelector('.fonds-archives-grid__toolbar'); + if (!toolbar) return; + + const lettersButtons = toolbar.querySelectorAll( + '.alphabetical-order-select__button', + ) as NodeListOf