254 lines
6.1 KiB
PHP
254 lines
6.1 KiB
PHP
<?php
|
|
|
|
add_action('rest_api_init', function () {
|
|
|
|
/* ----------------
|
|
BUILDING ROUTES
|
|
-----------------*/
|
|
|
|
|
|
// ################ FILTER ARTICLES ################
|
|
register_rest_route('dynamiques-datas/v1/build', '/articles', array(
|
|
'methods' => 'GET',
|
|
'callback' => 'build_articles',
|
|
'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
|
|
-----------------*/
|
|
|
|
// ################ LIKE POST ################
|
|
|
|
register_rest_route('dynamiques-datas/v1/build', '/articles/like', array(
|
|
'methods' => 'POST',
|
|
'callback' => 'like_post',
|
|
'permission_callback' => '__return_true',
|
|
));
|
|
});
|
|
|
|
|
|
|
|
// ################ 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;
|
|
}
|
|
|
|
|
|
// ################ 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' => '<p>Aucun article trouvé.</p>',
|
|
'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();
|
|
?>
|
|
<div class="search-results">
|
|
<h2 class="search-results__title">Recherche : “<?php echo $search; ?>”</h2>
|
|
<h2 class="post-count">
|
|
<span class="post-count__count">
|
|
<?php echo $post_count; ?>
|
|
</span>
|
|
<span class="post-count__text">
|
|
résultats
|
|
</span>
|
|
</h2>
|
|
<div class="search-results__content">
|
|
<?php
|
|
if (!empty($filtered_articles)) :
|
|
foreach ($filtered_articles as $article) :
|
|
get_template_part('template-parts/search/search-results-card', null, array(
|
|
'post_type' => get_post_type($article->ID),
|
|
'post_id' => $article->ID,
|
|
'search' => $search,
|
|
'isInnerSearch' => true,
|
|
));
|
|
endforeach;
|
|
else :
|
|
echo '<p>Aucun article trouvé.</p>';
|
|
endif;
|
|
?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
$html_template = ob_get_clean();
|
|
|
|
$response_data = array(
|
|
'html_template' => $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)
|
|
{
|
|
|
|
$post_id = $request->get_param('post_id');
|
|
if (!$post_id) {
|
|
return new WP_Error('post_id_required', 'Post ID is required', array('status' => 400));
|
|
}
|
|
|
|
|
|
$post_id = intval($post_id);
|
|
|
|
// Vérifier que le post existe
|
|
if (!get_post($post_id)) {
|
|
return new WP_Error('post_not_found', 'Post non trouvé', array('status' => 404));
|
|
}
|
|
|
|
|
|
$likes_count = get_post_likes_count($post_id);
|
|
|
|
// Incrémenter le compteur
|
|
$new_likes = $likes_count + 1;
|
|
|
|
// Mettre à jour la meta
|
|
update_post_meta($post_id, 'likes_count', $new_likes);
|
|
|
|
$response_data = array(
|
|
'success' => true,
|
|
'post_id' => $post_id,
|
|
'likes_count' => $new_likes,
|
|
'message' => 'Like ajouté avec succès'
|
|
);
|
|
|
|
$response = new WP_REST_Response($response_data);
|
|
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|