From 7fad8c9af42edecd870cd950e92ade1cc99bbd4b Mon Sep 17 00:00:00 2001 From: Antoine M Date: Wed, 17 Apr 2024 10:24:07 +0200 Subject: [PATCH] refactoring utilities and including build_page_chapter_index function --- includes/utilities.php | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/includes/utilities.php b/includes/utilities.php index 081ff61..69f0591 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -1,5 +1,9 @@ posts, 'compareThematiquePriority'); return $postsQuery; } + + +/* ------------------------------------------- + Build The page chaptering index + ---------------------------------------------*/ + +function build_page_chapter_index($blocks) +{ + $chapterBlockIndex = []; + foreach ($blocks as $key => $block) { + if ($block['blockName'] == 'homegrade-content-blocks/chapitrage-thematique') { + foreach ($block['innerBlocks'] as $key => $innerBlock) { + if (!$innerBlock['attrs']['chapterTitle']) continue; + + $cleanedTitle = strtolower(preg_replace("/[^a-zA-Z]/", "", $innerBlock['attrs']['chapterTitle'])); + + array_push($chapterBlockIndex, [ + 'block-type' => $block['blockName'], + 'anchor' => '#' . $cleanedTitle, + 'title' => $innerBlock['attrs']['chapterTitle'], + ]); + } + } + if ($block['blockName'] == 'homegrade-content-blocks/questions-container') { + array_push($chapterBlockIndex, [ + 'block-type' => $block['blockName'], + 'anchor' => '#questions-container-' . $block['attrs']['relatedPostId'], + 'title' => get_the_title($block['attrs']['relatedPostId']), + ]); + } + if ($block['blockName'] == 'homegrade-content-blocks/vocabulaire-summary') { + array_push($chapterBlockIndex, [ + 'block-type' => $block['blockName'], + 'anchor' => "#vocabulaire-summary", + 'title' => __("Vocabulaire", "homegrade-theme__texte-fonctionnel") . " " . get_the_terms(get_the_ID(), "thematiques")[0]->name, + ]); + } + if ($block['blockName'] == 'homegrade-content-blocks/plus-loin') { + array_push($chapterBlockIndex, [ + 'block-type' => $block['blockName'], + 'anchor' => "#aller-plus-loin", + 'title' => __("Pour aller plus loin", "homegrade-blocks__texte-fonctionnel"), + ]); + } + } + return $chapterBlockIndex; +}