226 lines
5.3 KiB
PHP
226 lines
5.3 KiB
PHP
<?php
|
|
|
|
|
|
|
|
add_action('rest_api_init', function () {
|
|
|
|
/* ---------------------------
|
|
SHIPPING ROUTES
|
|
---------------------------*/
|
|
// Handle exclusion parameters like http://homegrade.local/wp-json/homegrade-datas/v1/questions-thematiques/29?excluded_ids=1314,1305,1301
|
|
register_rest_route('homegrade-datas/v1', '/questions-thematiques/(?P<thematiqueId>\d+)', array(
|
|
'methods' => 'GET',
|
|
'callback' => 'get_questions_posts_per_thematique_id',
|
|
'permission_callback' => '__return_true',
|
|
'args' => array(
|
|
'excluded_ids' => array(
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
),
|
|
),
|
|
));
|
|
// * SHIP ALL NEWS
|
|
register_rest_route('homegrade-datas/v1', '/news', array(
|
|
'methods' => 'GET',
|
|
'callback' => 'get_news',
|
|
'permission_callback' => '__return_true',
|
|
|
|
));
|
|
// * SHIP NEWS BY TYPE
|
|
register_rest_route('homegrade-datas/v1', '/news/type/(?P<typeId>\d+)', array(
|
|
'methods' => 'GET',
|
|
'callback' => 'get_news_posts_per_type_id',
|
|
'permission_callback' => '__return_true',
|
|
|
|
));
|
|
|
|
|
|
/* ----------------
|
|
BUILDING ROUTES
|
|
-----------------*/
|
|
|
|
register_rest_route('homegrade-datas/v1/build', '/news', 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',
|
|
|
|
));
|
|
});
|
|
|
|
|
|
|
|
|
|
function build_card($request)
|
|
{
|
|
$cardId = $request['cardId'];
|
|
|
|
|
|
$test = get_template_part(
|
|
'template-components/cards/card-news',
|
|
null,
|
|
array(
|
|
'card_variant' => 'activite',
|
|
'post_ID' => $cardId,
|
|
'post_title' => get_the_title($cardId),
|
|
// 'post_thumbnail' => $post_thumbnail,
|
|
// 'news_type' => $news_type,
|
|
)
|
|
);
|
|
$response = new WP_REST_Response($test);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
|
|
|
|
function get_news($request)
|
|
{
|
|
$args = array(
|
|
"status" => "publish",
|
|
"post_type" => "questions",
|
|
"posts_per_page" => -1,
|
|
);
|
|
$newsPosts = get_posts($args);
|
|
$response = new WP_REST_Response($newsPosts);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
|
|
|
|
/* ----------------
|
|
NEWS BY TYPE ID
|
|
-----------------*/
|
|
|
|
function get_news_posts_per_type_id($request)
|
|
{
|
|
|
|
$typeId = $request['typeId'];
|
|
$args = array(
|
|
"post_type" => "post",
|
|
"posts_per_page" => -1,
|
|
"tax_query" => array(
|
|
array(
|
|
'taxonomy' => 'news-type',
|
|
'field' => 'term_id',
|
|
'terms' => $typeId
|
|
)
|
|
)
|
|
);
|
|
$newsPostsDatas = get_posts($args);
|
|
$response = new WP_REST_Response($newsPostsDatas);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
function build_news_posts_feed_all($request)
|
|
{
|
|
|
|
|
|
$args = array(
|
|
"post_type" => "post",
|
|
"posts_per_page" => -1,
|
|
);
|
|
$newsPostsDatas = get_posts($args);
|
|
|
|
|
|
ob_start();
|
|
foreach ($newsPostsDatas 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;
|
|
write_log($news_type);
|
|
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 = new WP_REST_Response($html_template);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
function build_news_posts_feed_per_type_id($request)
|
|
{
|
|
$typeId = esc_html($request['typeId']);
|
|
if (!$typeId || (!is_numeric($typeId))) return;
|
|
|
|
$args = array(
|
|
"post_type" => "post",
|
|
"posts_per_page" => -1,
|
|
"tax_query" => array(
|
|
array(
|
|
'taxonomy' => 'news-type',
|
|
'field' => 'term_id',
|
|
'terms' => $typeId
|
|
)
|
|
)
|
|
);
|
|
$newsPostsDatas = get_posts($args);
|
|
|
|
|
|
ob_start();
|
|
foreach ($newsPostsDatas 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 = new WP_REST_Response($html_template);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|
|
|
|
function get_questions_posts_per_thematique_id($request)
|
|
{
|
|
$excluded_ids = $request->get_param('excluded_ids');
|
|
$excludedArray = explode(',', $excluded_ids);
|
|
|
|
$thematiqueId = $request['thematiqueId'];
|
|
$args = array(
|
|
"post_type" => "questions",
|
|
"posts_per_page" => -1,
|
|
"post__not_in" => $excludedArray,
|
|
'tax_query' => array(
|
|
array(
|
|
'taxonomy' => 'thematiques',
|
|
'field' => 'term_id',
|
|
'terms' => $thematiqueId
|
|
)
|
|
)
|
|
);
|
|
$postDatas = get_posts($args);
|
|
$response = new WP_REST_Response($postDatas);
|
|
$response->set_status(200);
|
|
|
|
return $response;
|
|
}
|