lhoist-stay-safe__backend-t.../includes/api.php

173 lines
5.8 KiB
PHP

<?php
function acf_set_language()
{
return apply_filters('wpml_current_language', null);
}
add_action('rest_api_init', function () {
register_rest_route('lhoist-datas/screen', '/welcome', array(
'methods' => 'GET',
'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(
'methods' => 'POST',
'callback' => 'wp_learn_create_database_table',
'permission_callback' => 'lhoist_datas_permission_callback',
));
});
function wp_learn_create_database_table(WP_REST_Request $request)
{
$data = $request->get_json_params(); // Récupération des données envoyées avec la requête POST
$user_name = $data['user_name'] ?? null;
$user_locale = $data['user_locale'] ?? null;
$user_country = $data['user_country'] ?? null;
$level_post_id = $data['level_post_id'] ?? null;
$level_is_completed = $data['level_is_completed'] ?? null;
$level_completion_time = $data['level_completion_time'] ?? null;
$level_score = $data['level_score'] ?? null;
if (!$data) {
return rest_ensure_response(array('success' => false, 'message' => 'Erreur lors de l\'enregistrement des données.'));
exit;
}
global $wpdb;
$table_name = 'wp_app_users_statistics';
$post_id = uniqid();
write_log("user_locale : " . $user_locale);
$result_check = $wpdb->insert(
$table_name,
array(
'session_ID' => $post_id,
'user_name' => $user_name,
'user_locale' => $user_locale,
'user_country' => $user_country,
'level_post_id' => $level_post_id,
'level_is_completed' => $level_is_completed,
'level_completion_time' => $level_completion_time,
'level_score' => $level_score,
)
);
if ($result_check) {
return rest_ensure_response(array('success' => true, 'message' => 'Données enregistrées avec succès.'));
} else {
return rest_ensure_response(array('success' => false, 'message' => 'Erreur lors de l\'enregistrement des données.'));
}
}
function lhoist_datas_permission_callback()
{
if (is_user_logged_in() && current_user_can('administrator')) {
return true;
}
return false;
}
// ################ WELCOME SCREEN ################
function get_interface_welcome_screen_datas($request)
{
// $language = sanitize_text_field($request['language']);
$currentLanguage = $request->get_param('current-language') ?? 'fr';
// SWITCH TO CURRENT REQUEST LANGUAGE
do_action('wpml_switch_language', $currentLanguage);
add_filter('acf/settings/current_language', 'acf_set_language');
// GET SCREEN FIELDS CONTENT
$applicationTitle = get_field('application_title', 'option');
$applicationSubtitle = get_field('application_subtitle', 'option');
$applicationDescription = get_field('application_description', 'option');
$response_data = array(
'applicationTitle' => $applicationTitle,
'applicationSubtitle' => $applicationSubtitle,
'applicationDescription' => $applicationDescription,
);
$response = new WP_REST_Response($response_data);
$response->set_status(200);
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;
}