working on routes and api endpoints

This commit is contained in:
Antoine M 2024-02-01 18:20:20 +01:00
parent f51f851c1f
commit dd7c009db0

View File

@ -1,13 +1,27 @@
<?php
function acf_set_language()
{
return apply_filters('wpml_current_language', null);
}
add_action('rest_api_init', function () {
register_rest_route('lhoist-datas/interface-content', '/welcome', array(
register_rest_route('lhoist-datas/screen', '/welcome', array(
'methods' => 'GET',
'callback' => 'get_interface_welcome_screen',
'callback' => 'get_interface_welcome_screen_datas',
'permission_callback' => '__return_true',
));
register_rest_route('lhoist-datas/screen', '/profile', array(
'methods' => 'GET',
'callback' => 'get_interface_profile_screen_datas',
'permission_callback' => '__return_true',
));
register_rest_route('lhoist-datas', '/available-countries', array(
'methods' => 'GET',
'callback' => 'get_interface_available_countries',
'permission_callback' => '__return_true',
));
// ################ POST ################
register_rest_route('lhoist-datas/statistics', '/post', array(
@ -72,32 +86,19 @@ function lhoist_datas_permission_callback()
// ################ WELCOME SCREEN ################
function get_interface_welcome_screen($request)
function get_interface_welcome_screen_datas($request)
{
// $language = sanitize_text_field($request['language']);
$currentLanguage = $request->get_param('current-page-language') ?? 'fr';
$option_id = icl_object_id('option_id', 'option', false, $currentLanguage);
$currentLanguage = $request->get_param('current-language') ?? 'fr';
// SWITCH TO CURRENT REQUEST LANGUAGE
do_action('wpml_switch_language', $currentLanguage);
function acf_set_language()
{
return apply_filters('wpml_current_language', null);
}
add_filter('acf/settings/current_language', 'acf_set_language');
$args = array(
'post_type' => 'search-and-find',
'posts_per_page' => -1,
);
$posts = new WP_Query($args);
// GET SCREEN FIELDS CONTENT
$applicationTitle = get_field('application_title', 'option');
$applicationSubtitle = get_field('application_subtitle', 'option');
$applicationDescription = get_field('application_description', 'option');
// write_log("current language : " . $currentLanguage);
// write_log($posts->posts[0]->post_title);
// write_log($applicationDescription);
$response_data = array(
'applicationTitle' => $applicationTitle,
@ -110,3 +111,62 @@ function get_interface_welcome_screen($request)
return $response;
}
function get_interface_profile_screen_datas($request)
{
// $language = sanitize_text_field($request['language']);
$currentLanguage = $request->get_param('current-language') ?? 'fr';
// SWITCH TO CURRENT REQUEST LANGUAGE
// switch_to_locale($currentLanguage);
do_action('wpml_switch_language', $currentLanguage);
add_filter('acf/settings/current_language', 'acf_set_language');
// GET SCREEN FIELDS CONTENT
$profile_screen_title = get_field('profile_screen_title', 'option');
$profile_select_title = get_field('profile_select_title', 'option');
$profile_country_select_title = get_field('profile_country_select_title', 'option');
$profile_options = get_field('profile_options', 'option');
// $profile_options = array(
// "lhoist_employee" => __("Employé Lhoist", "lhoist-stay-safe_theme"),
// "subcontractor_employee" => __("Employé sous-traitant", "lhoist-stay-safe_theme"),
// "driver" => __("Chauffeur de camion", "lhoist-stay-safe_theme"),
// "civilian" => __("Civil", "lhoist-stay-safe_theme"),
// );
$response_data = array(
'profileScreenTitle' => $profile_screen_title,
'profileOptions' => $profile_options,
'profileSelectTitle' => $profile_select_title,
'profileCountrySelectTitle' => $profile_country_select_title,
);
$response = new WP_REST_Response($response_data);
$response->set_status(200);
return $response;
}
function get_interface_available_countries($request)
{
$currentLanguage = $request->get_param('current-language');
$json_current_lang_file_path = get_template_directory() . '/languages/countries_' . $currentLanguage . '.json' ?? null;
$json_french_file_path = get_template_directory() . '/languages/countries_fr.json';
if (!file_exists($json_current_lang_file_path) && !file_exists($json_french_file_path)) return rest_ensure_response(array('error' => 'impossible de trouver les fichiers de traductions'));
$json_content = file_exists($json_current_lang_file_path) ? file_get_contents($json_current_lang_file_path) : file_get_contents($json_french_file_path);
$translations = json_decode($json_content);
if (!$json_content) return rest_ensure_response(array('error' => 'impossible de trouver les traductions'));
$response = new WP_REST_Response($translations);
$response->set_status(200);
return $response;
}