diff --git a/includes/api.php b/includes/api.php new file mode 100644 index 0000000..05ed113 --- /dev/null +++ b/includes/api.php @@ -0,0 +1,99 @@ + 'GET', + 'callback' => 'build_search_artisan_posts_cards', + 'permission_callback' => '__return_true', + )); +}); + + +// ################ ARTISANS ################ + +function build_search_artisan_posts_cards($request) +{ + // write_log($request); + $currentLanguage = esc_html($request->get_param('current-page-language')) ?? 'fr'; + $previousActivePage = esc_html($request->get_param('active-page')) ?? 1; + $postsPerPage = esc_html($request->get_param('posts-per-page')) ?? 12; + $taxonomy = esc_html($request->get_param('taxonomy')) ?? 'metiers'; + $StringifiedTaxonomyIds = esc_html($request->get_param('taxonomy-ids')) ?? null; + + $taxonomyIds = explode(',', $StringifiedTaxonomyIds); + $taxonomyIds = array_map('intval', $taxonomyIds); + + + $activePage = is_numeric($previousActivePage) ? $previousActivePage + 1 : 1; + + $taxQuery = array( + array( + 'taxonomy' => $taxonomy, + 'terms' => $taxonomyIds, + 'field' => 'term_id', + ) + ); + + + do_action('wpml_switch_language', $currentLanguage); + + $args = array( + "status" => "publish", + "post_type" => "artisans", + "posts_per_page" => -1, + "paged" => $activePage, + "tax_query" => $taxQuery, + + ); + $newsPostsDatas = new WP_Query($args); + + + + + ob_start(); + foreach ($newsPostsDatas->posts as $key => $post) { + $post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-artisans__thumbnail')) ?? null; + $news_type = get_the_terms($post->ID, "news_type") ?? null; + $post_date = get_the_date('j.m.Y', $post->ID) ?? null; + + get_template_part( + 'template-components/artisans/card-artisans', + null, + array( + 'card_variant' => 'activite', + 'post_ID' => $post->ID, + 'post_title' => get_the_title($post->ID), + 'post_thumbnail' => $post_thumbnail, + 'news_type' => $news_type, + 'post_date' => $post_date, + 'current_taxonomy' => $taxonomy, + + ) + ); + } + + $html_template = ob_get_clean(); + + $response_data = array( + 'html_template' => $html_template, + 'total_posts_found' => $newsPostsDatas->found_posts, + 'active_page' => $activePage, + 'max_num_pages' => $newsPostsDatas->max_num_pages, + ); + + + $response = new WP_REST_Response($response_data); + + $response->set_status(200); + + return $response; +}