From a22a179effb5c3ba5e94e5f188f1cbe3b302d087 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Mon, 12 May 2025 17:28:39 -0600 Subject: [PATCH] Added nested blocks support (inner_blocks). ACF Only. --- layouts/scripts/dist/frame-index.min.js | 2 +- layouts/scripts/frame/frame.js | 9 +++++++++ platforms/php/build.php | 10 ++-------- server.js | 2 -- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/layouts/scripts/dist/frame-index.min.js b/layouts/scripts/dist/frame-index.min.js index 1514557..fed0dc7 100644 --- a/layouts/scripts/dist/frame-index.min.js +++ b/layouts/scripts/dist/frame-index.min.js @@ -1 +1 @@ -let e;window.initBlock=function(e="",n="",r){t=function(){document.querySelectorAll(n).forEach((e=>r(e)))},t()};let t,n={};function r(r={}){r.template&&(e=r.template),r.data&&(n=r.data),e&&function(e,n,r){const a=Handlebars.compile(e);let o;Handlebars.registerHelper("esc_attr",(function(e){return e})),Handlebars.registerHelper("esc_url",(function(e){return e})),Handlebars.registerHelper("esc_html",(function(e){return e})),Handlebars.registerHelper("base_url",(function(){return"/"}));try{o=a(n)}catch(e){o=`
\n

Syntax Error:

\n
${e.toString()}
\n
`}r.innerHTML=o,t&&t()}(e,n||{},document.getElementById("hbs-container"))}!function(){function e(){const e=new URLSearchParams(window.location.search),t={};for(const[n,r]of e)t[n]=r;return t}!function(){const t=new URLSearchParams({name:e().data||"default"});fetch(`/data?${t}`).then((e=>e.json())).then((e=>{n=e.data,r({data:n})}))}(),window.addEventListener("message",(function(e){const t=e.data,a="dataUpdate:";if("string"==typeof t&&t.startsWith(a))try{n=JSON.parse(t.substring(a.length)),r({data:n})}catch(e){console.log("Error parsing incoming data.",e)}}))}(),function(){const e=window.io.connect();e.on("error",(function(e){console.log(e)})),e.on("templateUpdate",(function(e){r({template:e.template})}))}(); +let e;window.initBlock=function(e="",n="",r){t=function(){document.querySelectorAll(n).forEach((e=>r(e)))},t()};let t,n={};function r(r={}){r.template&&(e=r.template),r.data&&(n=r.data),e&&function(e,n,r){const a=Handlebars.compile(e);let o;Handlebars.registerHelper("esc_attr",(function(e){return e})),Handlebars.registerHelper("esc_url",(function(e){return e})),Handlebars.registerHelper("esc_html",(function(e){return e})),Handlebars.registerHelper("base_url",(function(){return"/"})),Handlebars.registerHelper("inner_blocks",(function(e){const t=e.data.root.inner_blocks;return t?new Handlebars.SafeString(t):""}));try{o=a(n)}catch(e){o=`
\n

Syntax Error:

\n
${e.toString()}
\n
`}r.innerHTML=o,t&&t()}(e,n||{},document.getElementById("hbs-container"))}!function(){function e(){const e=new URLSearchParams(window.location.search),t={};for(const[n,r]of e)t[n]=r;return t}!function(){const t=new URLSearchParams({name:e().data||"default"});fetch(`/data?${t}`).then((e=>e.json())).then((e=>{n=e.data,r({data:n})}))}(),window.addEventListener("message",(function(e){const t=e.data,a="dataUpdate:";if("string"==typeof t&&t.startsWith(a))try{n=JSON.parse(t.substring(a.length)),r({data:n})}catch(e){console.log("Error parsing incoming data.",e)}}))}(),function(){const e=window.io.connect();e.on("error",(function(e){console.log(e)})),e.on("templateUpdate",(function(e){r({template:e.template})}))}(); diff --git a/layouts/scripts/frame/frame.js b/layouts/scripts/frame/frame.js index 71f333e..1310fcd 100644 --- a/layouts/scripts/frame/frame.js +++ b/layouts/scripts/frame/frame.js @@ -119,6 +119,15 @@ function renderBlock(templateHbs, jsonData, target) { return '/'; }); + Handlebars.registerHelper('inner_blocks', function (options) { + const content = options.data.root['inner_blocks']; + if (!content) { + return ''; + } + + return new Handlebars.SafeString(content); + }); + let html; try { diff --git a/platforms/php/build.php b/platforms/php/build.php index bdf92cf..f9b6304 100644 --- a/platforms/php/build.php +++ b/platforms/php/build.php @@ -35,14 +35,6 @@ trait Custom_Handlebars { return $context; } ); - - $this->add_handlebar( 'safe_html', function ( $context ) { - if ( function_exists( 'wp_kses_post' ) ) { - return wp_kses_post( $context ); - } - - return $context; - } ); } public function add_handlebar( $key, $func ): void { @@ -91,6 +83,8 @@ class Component_Builder { private function get_handlebars_template( $path = '' ): string { $template = file_get_contents( $path ); + $template = preg_replace( '/{{inner_blocks}}/', '', $template ); + $phpStr = LightnCandy::compile( $template, [ 'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE | Flags::FLAG_THIS, diff --git a/server.js b/server.js index a0f39e2..4fd4dfd 100755 --- a/server.js +++ b/server.js @@ -89,8 +89,6 @@ const hbs = create({ }, esc_html(html) { // TODO: Check if we can remove this helper. return html; - }, safe_html(html) { - return sanitizeHtml(html); } } });