refactoring with only one news building function
This commit is contained in:
parent
2e755ff102
commit
957891afd9
141
includes/api.php
141
includes/api.php
|
|
@ -10,24 +10,26 @@ add_action('rest_api_init', function () {
|
|||
|
||||
// ################ NEWS ################
|
||||
|
||||
// * BUILD ALL NEWS CARDS
|
||||
register_rest_route('homegrade-datas/v1/build', '/news/all', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'build_news_posts_feed_all',
|
||||
'permission_callback' => '__return_true',
|
||||
));
|
||||
// * BUILD MORE NEWS CARDS
|
||||
register_rest_route('homegrade-datas/v1/build', '/news/more', array(
|
||||
register_rest_route('homegrade-datas/v1/build', '/news', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'build_news_posts_more_posts',
|
||||
'callback' => 'build_news_posts_cards',
|
||||
'permission_callback' => '__return_true',
|
||||
));
|
||||
|
||||
// * BUILD ALL NEWS CARDS
|
||||
// register_rest_route('homegrade-datas/v1/build', '/news/all', array(
|
||||
// 'methods' => 'GET',
|
||||
// 'callback' => 'build_news_posts_feed_all',
|
||||
// 'permission_callback' => '__return_true',
|
||||
// ));
|
||||
|
||||
// * BUILD NEWS CARDS BY NEWS TYPE
|
||||
register_rest_route('homegrade-datas/v1/build', '/news/type/(?P<typeId>\d+)', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'build_news_posts_feed_per_type_id',
|
||||
'permission_callback' => '__return_true',
|
||||
));
|
||||
// register_rest_route('homegrade-datas/v1/build', '/news/type/(?P<typeId>\d+)', array(
|
||||
// 'methods' => 'GET',
|
||||
// 'callback' => 'build_news_posts_feed_per_type_id',
|
||||
// 'permission_callback' => '__return_true',
|
||||
// ));
|
||||
|
||||
// ################ PUBLICATIONS ################
|
||||
|
||||
|
|
@ -73,112 +75,7 @@ add_action('rest_api_init', function () {
|
|||
|
||||
// ################ NEWS ################
|
||||
|
||||
function build_news_posts_feed_all($request)
|
||||
{
|
||||
$currentLanguage = $request->get_param('current-page-language') ?? 'fr';
|
||||
do_action('wpml_switch_language', $currentLanguage);
|
||||
|
||||
$args = array(
|
||||
"status" => "publish",
|
||||
"post_type" => "post",
|
||||
"posts_per_page" => -1,
|
||||
);
|
||||
$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-news__thumbnail card-post__thumbnail')) ?? null;
|
||||
$news_type = get_the_terms($post->ID, "news_type") ?? null;
|
||||
get_template_part(
|
||||
'template-components/cards/card-news',
|
||||
null,
|
||||
array(
|
||||
'card_variant' => 'activite',
|
||||
'post_ID' => $post->ID,
|
||||
'post_title' => get_the_title($post->ID),
|
||||
'post_thumbnail' => $post_thumbnail,
|
||||
'news_type' => $news_type,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$html_template = ob_get_clean();
|
||||
|
||||
$response_data = array(
|
||||
'html_template' => $html_template,
|
||||
'total_posts_found' => $newsPostsDatas->found_posts,
|
||||
'active_page' => 1,
|
||||
'max_num_pages' => 1,
|
||||
);
|
||||
|
||||
$response = new WP_REST_Response($response_data);
|
||||
$response->set_status(200);
|
||||
|
||||
return $response;
|
||||
}
|
||||
function build_news_posts_feed_per_type_id($request)
|
||||
{
|
||||
$currentLanguage = $request->get_param('current-page-language') ?? 'fr';
|
||||
$activePage = $request->get_param('active-page') ?? 1;
|
||||
$postsPerPage = esc_html($request->get_param('posts-per-page')) ?? 8;
|
||||
|
||||
do_action('wpml_switch_language', $currentLanguage);
|
||||
|
||||
$typeId = esc_html($request['typeId']);
|
||||
if (!$typeId || (!is_numeric($typeId))) return;
|
||||
|
||||
$args = array(
|
||||
"status" => "publish",
|
||||
"post_type" => "post",
|
||||
"posts_per_page" => $postsPerPage,
|
||||
|
||||
"tax_query" => array(
|
||||
array(
|
||||
'taxonomy' => 'news_type',
|
||||
'field' => 'term_id',
|
||||
'terms' => $typeId
|
||||
)
|
||||
)
|
||||
);
|
||||
$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-news__thumbnail card-post__thumbnail')) ?? null;
|
||||
$news_type = get_the_terms($post->ID, "news_type") ?? null;
|
||||
get_template_part(
|
||||
'template-components/cards/card-news',
|
||||
null,
|
||||
array(
|
||||
'card_variant' => 'activite',
|
||||
'post_ID' => $post->ID,
|
||||
'post_title' => get_the_title($post->ID),
|
||||
'post_thumbnail' => $post_thumbnail,
|
||||
'news_type' => $news_type,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$html_template = ob_get_clean();
|
||||
|
||||
$response_data = array(
|
||||
'html_template' => $html_template,
|
||||
'total_posts_found' => $newsPostsDatas->found_posts,
|
||||
'active_page' => 1,
|
||||
'max_num_pages' => $newsPostsDatas->max_num_pages,
|
||||
);
|
||||
|
||||
|
||||
$response = new WP_REST_Response($response_data);
|
||||
|
||||
$response->set_status(200);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function build_news_posts_more_posts($request)
|
||||
function build_news_posts_cards($request)
|
||||
{
|
||||
$currentLanguage = esc_html($request->get_param('current-page-language')) ?? 'fr';
|
||||
$previousActivePage = esc_html($request->get_param('active-page')) ?? 1;
|
||||
|
|
@ -211,6 +108,8 @@ function build_news_posts_more_posts($request)
|
|||
foreach ($newsPostsDatas->posts as $key => $post) {
|
||||
$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-news__thumbnail card-post__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/cards/card-news',
|
||||
null,
|
||||
|
|
@ -220,6 +119,7 @@ function build_news_posts_more_posts($request)
|
|||
'post_title' => get_the_title($post->ID),
|
||||
'post_thumbnail' => $post_thumbnail,
|
||||
'news_type' => $news_type,
|
||||
'post_date' => $post_date,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
@ -241,6 +141,9 @@ function build_news_posts_more_posts($request)
|
|||
return $response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ################ PUBLICATIONS ################
|
||||
|
||||
function build_brochure_archive_rows(WP_REST_Request $request)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user