From facf0a34f9d3b5d9dae0e9e7408675a3d2f730d0 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Thu, 22 Feb 2024 19:16:47 +0100 Subject: [PATCH] updating with stat routes --- includes/api.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/includes/api.php b/includes/api.php index d26f4d6..d7f8aa8 100644 --- a/includes/api.php +++ b/includes/api.php @@ -20,6 +20,12 @@ add_action('rest_api_init', function () { 'permission_callback' => 'lhoist_datas_permission_callback', )); + register_rest_route('lhoist-datas/rating', '/post', array( + 'methods' => 'POST', + 'callback' => 'lhoist_post_rating', + 'permission_callback' => 'lhoist_datas_permission_callback', + + )); }); @@ -92,7 +98,7 @@ function get_interface_play_screen_datas($request) return $response; } -// ################ RETURN GAME STATISTICS ################ +// ################ POST GAME STATISTICS ################ function lhoist_sanitize_statistic_datas($datas) { @@ -131,6 +137,7 @@ function lhoist_check_statistics_datas_formats($datas) } } + function lhoist_post_game_datas_statistics(WP_REST_Request $request) { $datas = $request->get_json_params(); @@ -163,3 +170,57 @@ function lhoist_post_game_datas_statistics(WP_REST_Request $request) return rest_ensure_response(array('success' => false, 'message' => 'Erreur lors de l\'enregistrement des données.')); } } + + +// ################ POST APP RATING ################ + +function lhoist_sanitize_ratings_datas($datas) +{ + $cleanDatas = array(); + + $cleanDatas['note'] = isset($datas['note']) ? (int) $datas['note'] : null; + $cleanDatas['comment'] = isset($datas['comment']) ? sanitize_text_field($datas['comment']) : null; + + return $cleanDatas; +} + +function lhoist_check_ratings_datas_formats($datas) +{ + + if (!$datas['note'] || !is_numeric($datas['note'])) { + throw new Exception("La propriété 'note' est manquante ou invalide."); + exit; + } + if (isset($datas['comment']) && !is_string($datas['comment'])) { + throw new Exception("La propriété 'commentaire' est manquante ou invalide."); + exit; + } +} + +function lhoist_post_rating(WP_REST_Request $request) +{ + $datas = $request->get_json_params(); + + $cleanDatas = lhoist_sanitize_ratings_datas($datas); + lhoist_check_ratings_datas_formats($cleanDatas); + + global $wpdb; + $datetime = new DateTime("now", new DateTimeZone('Europe/Brussels')); + + $ratingsDatas = array( + 'comment' => "super", + 'rating' => 3, + 'date' => $datetime->format('Y-m-d H:i:s'), + ); + $table_name = 'wp_app_ratings'; + $result_check = $wpdb->insert( + $table_name, + $ratingsDatas + ); + + 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.')); + } +}