diff --git a/includes/api.php b/includes/api.php index e704887..abee497 100644 --- a/includes/api.php +++ b/includes/api.php @@ -9,16 +9,26 @@ add_action('rest_api_init', function () { register_rest_route('lhoist-datas/screen', '/play/latest', array( 'methods' => 'GET', - 'callback' => 'get_interface_play_screen_datas', + 'callback' => 'get_latest_game_datas', 'permission_callback' => '__return_true', )); + register_rest_route('lhoist-datas/screen', '/play/all', array( + 'methods' => 'GET', + 'callback' => 'get_all_games_datas', + 'permission_callback' => '__return_true', + )); + register_rest_route('lhoist-datas/screen', '/play/(?P\d+)', array( + 'methods' => 'GET', + 'callback' => 'get_game_datas', + 'permission_callback' => '__return_true', + )); + register_rest_route('lhoist-datas/page', '/(?P\d+)', array( 'methods' => 'GET', 'callback' => 'get_interface_page_screen', 'permission_callback' => '__return_true', )); - register_rest_route('lhoist-datas/statistics', '/post', array( 'methods' => 'POST', 'callback' => 'lhoist_post_game_datas_statistics', @@ -51,7 +61,7 @@ function lhoist_datas_permission_callback( // ################ GET GAME DATAS ################ -function get_interface_play_screen_datas($request) +function get_latest_game_datas($request) { // $language = sanitize_text_field($request['language']); $currentLanguage = strtolower($request->get_param('current-language')) ?? 'fr'; @@ -101,6 +111,104 @@ function get_interface_play_screen_datas($request) return $response; } +function get_game_datas($request) +{ + $id = $request->get_param('id'); + $currentLanguage = strtolower($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'); + + + $pageTranslatedID = apply_filters('wpml_object_id', $id, 'search-and-find', true); + $gamePage = get_post($pageTranslatedID); + + // RETURN IF NO GAME DATAS + if (!$gamePage || !$gamePage->post_content) return rest_ensure_response(array('error' => 'impossible de trouver les données du jeu')); + + + $gamePageBlocks = parse_blocks($gamePage->post_content); + $gameBlock = null; + + + foreach ($gamePageBlocks as $block) { + + if ('lhoist-blocks/search-and-find' === $block['blockName']) { + $gameBlock = $block; + break; + } + } + $responseDatas = array( + 'gameId' => $gamePage->ID, + 'gameHtmlTemplate' => $gamePage->post_content, + 'gameBlockDatas' => $gameBlock, + 'gameObjects' => $gameBlock['innerBlocks'], + ); + + $response = new WP_REST_Response($responseDatas); + $response->set_status(200); + + return $response; +} +function get_all_games_datas($request) +{ + + + $currentLanguage = strtolower($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 LATESTS PLAY LEVEL + $args = array( + 'post_type' => 'search-and-find', + 'posts_per_page' => -1, + ); + $searchAndFindsQuery = new WP_Query($args); + + if (!$searchAndFindsQuery->post) { + do_action('wpml_switch_language', "fr"); + $searchAndFindsQuery = new WP_Query($args); + write_log("passed"); + } + + $gamesDatas = array(); + + // RETURN IF NO GAME DATAS + if (!$searchAndFindsQuery->posts || !$searchAndFindsQuery->posts[0]) return rest_ensure_response(array('error' => 'impossible de trouver les données du jeu')); + + + + foreach ($searchAndFindsQuery->posts as $key => $post) { + + $currentGameDatas = array( + 'gameId' => $post->ID, + 'gameTitle' => $post->post_title, + ); + + if (!$post->post_content || empty($post->post_content)) continue; + + + $first_block = !empty($post->post_content) && parse_blocks($post->post_content) ? parse_blocks($post->post_content)[0] : null; + + if ($first_block['blockName'] === 'lhoist-blocks/search-and-find' && !empty($first_block['attrs']['coverId'])) { + $currentGameDatas['gameCoverId'] = $first_block['attrs']['coverId']; + $currentGameDatas['gameCoverUrl'] = $first_block['attrs']['coverUrl']; + } + array_push($gamesDatas, $currentGameDatas); + } + + + $responseDatas = $gamesDatas; + $response = new WP_REST_Response($responseDatas); + $response->set_status(200); + + return $response; +} + + function get_interface_page_screen($request) { @@ -121,6 +229,7 @@ function get_interface_page_screen($request) return $response; } + // ################ POST GAME STATISTICS ################ function lhoist_sanitize_statistic_datas($datas)