carhop__dynamiques-theme__P.../includes/api.php
Nonimart d94fcf4b90
All checks were successful
continuous-integration/drone/push Build is passing
FEATURE Showing authors on all articles cards
2025-09-16 15:29:20 +02:00

142 lines
3.3 KiB
PHP

<?php
add_action('rest_api_init', function () {
/* ----------------
BUILDING ROUTES
-----------------*/
// ################ Authors ################
register_rest_route('dynamiques-datas/v1/build', '/revue/authors', array(
'methods' => 'GET',
'callback' => 'build_revue_authors',
'permission_callback' => '__return_true',
));
// ################ FILTER ARTICLES ################
register_rest_route('dynamiques-datas/v1/build', '/articles', array(
'methods' => 'GET',
'callback' => 'build_articles',
'permission_callback' => '__return_true',
));
});
// ################ REVUE AUTHORS ################
function build_revue_authors($request)
{
$revueID = esc_html($request->get_param('revue-id'));
ob_start();
get_template_part('template-parts/authors/revue-authors-list', null, array(
'revueID' => $revueID,
));
$html_template = ob_get_clean();
$response_data = array(
'html_template' => $html_template,
'message' => 'Hello World',
);
$response = new WP_REST_Response($response_data);
$response->set_status(200);
return $response;
}
// ################ FILTER ARTICLES ################
function build_articles($request)
{
$etiquette = esc_html($request->get_param('etiquette'));
$auteur = esc_html($request->get_param('auteur'));
$sort_by = esc_html($request->get_param('sort_by'));
$recherche = esc_html($request->get_param('recherche'));
// Construire les arguments de la query WordPress
$args = array(
'post_type' => 'articles',
'posts_per_page' => -1,
);
// 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';
}
// Filtre par étiquette (taxonomie)
if (!empty($etiquette) && $etiquette != '1') {
$args['tax_query'] = array(
array(
'taxonomy' => 'etiquettes',
'field' => 'term_id',
'terms' => $etiquette,
),
);
}
// Filtre par auteur
if (!empty($auteur) && $auteur != '1') {
$args['meta_query'] = array(
array(
'key' => 'authors', // Ajustez selon votre structure
'value' => $auteur,
'compare' => 'LIKE'
)
);
}
// Recherche par mot-clé
if (!empty($recherche)) {
$args['s'] = $recherche;
}
$articles = new WP_Query($args);
ob_start();
if ($articles->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 '<p>Aucun article trouvé.</p>';
endif;
wp_reset_postdata();
$html_template = ob_get_clean();
$response_data = array(
'html_template' => $html_template,
'post_count' => $articles->found_posts,
'query_args' => $args, // Pour debug
);
$response = new WP_REST_Response($response_data);
$response->set_status(200);
return $response;
}