From 894ec8a6f2ac62edbac7b3b6a88e3666f335c673 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 15 May 2026 15:08:42 +0000
Subject: [PATCH] =Refactor of CustomBlocks.php to move a majority of the logic to the pre_render_field instead of render_field, and adding support for more blocks

---
 inc/blocks/CustomBlocks.php |  930 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 600 insertions(+), 330 deletions(-)

diff --git a/inc/blocks/CustomBlocks.php b/inc/blocks/CustomBlocks.php
index b22cb0a..0b16235 100644
--- a/inc/blocks/CustomBlocks.php
+++ b/inc/blocks/CustomBlocks.php
@@ -4,6 +4,8 @@
 use DateTime;
 use DOMDocument;
 use JVBase\managers\Cache;
+use JVBase\managers\LoginManager;
+use JVBase\managers\SEO\BreadcrumbManager;
 use WP_Block;
 use WP_Query;
 
@@ -14,11 +16,14 @@
 class CustomBlocks
 {
     protected Cache $cache;
+	protected array $shouldRender = ['core/query'];
     public function __construct()
     {
         $this->cache = Cache::for('blocks', WEEK_IN_SECONDS);
 		$this->cache->connect('post')->connect('taxonomy');
-		add_filter('render_block', [$this, 'render'], 999, 3);
+		$this->cache->flush();
+		add_filter('pre_render_block', [$this, 'prerender'], 10, 3);
+		add_filter('render_block', [$this, 'render'], 10, 2);
 
         add_action('init', [$this, 'registerBlockStyles']);
     }
@@ -63,51 +68,59 @@
             ]
         );
     }
-
-    public function render(string $content, array $block, WP_Block $instance)
-    {
+	protected function checkMethods(?string $content, array $block, ?WP_Block $parent = null, bool $isPrerender = false):?string
+	{
 		$blockName = $this->sanitizeBlockName($block);
-        $method = 'render_'.$blockName;
+
+		$base = ($isPrerender) ? 'prerender_' : 'render_';
+		$method = $base.$blockName;
 		$function = BASE.$method;
 
 		if (function_exists($function)) {
-			return $function($block, $content);
-//			return $this->cache->remember(
-//				get_the_ID(),
-//				function () use ($function, $block, $content) {
-//					return $function($block, $content);
-//				}
-//			);
+			return $function($block, $content, $parent);
 		} else if (method_exists($this, $method)) {
-			return $this->$method($block, $content);
-//
-//			return $this->cache->remember(
-//				get_the_ID(),
-//				function () use ($method, $block, $content) {
-//					return $this->$method($block, $content);
-//				}
-//			);
-        } else if (!empty($block['blockName'])){
-			//TESTING
-			$ignore = [
-				'core/null',
-				'core/post-title',
-				'core/list-item',
-				'core/site-title',
-				'jvb/forms'
-			];
-//			if (!in_array($block['blockName'], $ignore)) {
-//				jvbDump('No method found for '.print_r($block['blockName'], true));
-//			}
+			return $this->$method($block, $content, $parent);
+		} elseif (!empty($blockName) && JVB_TESTING) {
+			if (!in_array($block['blockName'], $this->getIgnore($isPrerender))) {
+				jvbDump('No method found for '.print_r($block['blockName'], true));
+			}
 		}
-        if ($block['blockName'] === 'jvb/feed') {
-            // Enqueue the feed block script (it will automatically load dependencies)
-            $this->localize_feedblock();
-        }
+		return $content;
+	}
+		protected function getIgnore(bool $isPrerender):array
+		{
+			//Ignore for both
+			$base = [
+				'core/null'
+			];
+			if ($isPrerender) {
+				$base = array_merge($base, [
+
+				]);
+			} else {
+				$base = array_merge($base, [
+
+				]);
+			}
+			return $base;
+		}
+	public function prerender(?string $content, array $block, ?WP_Block $parent = null):?string
+	{
+		$result = $this->checkMethods($content, $block, $parent, true);
+		return $result;
+	}
+
+    public function render(string $content, array $block):string
+    {
+		if ($block['blockName'] === 'jvb/feed') {
+			// Enqueue the feed block script (it will automatically load dependencies)
+			$this->localize_feedblock();
+		}
 		if ($block['blockName'] === 'jvb/forms') {
 			wp_enqueue_style('jvb-form');
 		}
-        return $content;
+
+		return $this->checkMethods($content, $block);
     }
 
     /***********************************
@@ -126,8 +139,10 @@
      */
 
 
-    public function render_core_button(array $block):string
+    public function prerender_core_button(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'Button');
+//		jvbDump($parent, 'Parent');
 		preg_match('/href="([^"]*)"/', $block['innerHTML'], $url);
 		preg_match('/>([^<]*)<\/a>/', $block['innerHTML'], $label);
 
@@ -145,7 +160,7 @@
 		if ($icon !== '') {
 			return sprintf(
 				'<li%s><a href="%s" title="Find Us On %s">%s Maps</a></li>',
-				$this->getClassesAndStyles($block['attrs']),
+				$this->getClassesAndStyles($block['attrs']??[]),
 				esc_url($url[1]),
 				esc_html($label[1]),
 				jvbIcon($icon)
@@ -154,58 +169,79 @@
 
 		return sprintf(
 			'<li%s><a href="%s">%s</a></li>',
-			$this->getClassesAndStyles($block['attrs']),
+			$this->getClassesAndStyles($block['attrs']??[]),
 			esc_url($url[1]),
 			esc_html($label[1])
 		);
     }
 
-    public function render_core_buttons(array $block):string
+    public function prerender_core_buttons(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<ul'.$this->getClassesAndStyles($block['attrs'], ['buttons','row']).'>'.
+//		jvbDump($block, 'buttons');
+//		jvbDump($parent, 'Parent');
+        return '<ul'.$this->getClassesAndStyles($block['attrs']??[], ['buttons','row']).'>'.
                $this->innerBlocks($block).'</ul>';
     }
 
-    public function render_core_column(array $block):string
+    public function prerender_core_column(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'column');
+//		jvbDump($parent, 'Parent');
         $styles = (array_key_exists('attrs', $block) &&
                    array_key_exists('width', $block['attrs'])) ?
             ['flex-basis:'.$block['attrs']['width']]
             : [];
         return '<div'.
-               $this->getClassesAndStyles($block['attrs'], ['col'], $styles).'>'.
+               $this->getClassesAndStyles($block['attrs']??[], ['col'], $styles).'>'.
                $this->innerBlocks($block).'</div>';
     }
 
-    public function render_core_columns(array $block):string
+    public function prerender_core_columns(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<section'.
-               $this->getClassesAndStyles($block['attrs'], ['columns']).'>'.
-               $this->innerBlocks($block).'</section>';
+		$tagName = array_key_exists('tagName', $block['attrs']) ? $block['attrs']['tagName'] : 'section';
+
+        return sprintf(
+			'<%s%s>%s</%s>',
+			$tagName,
+		   	$this->getClassesAndStyles($block['attrs']??[], ['row nowrap']),
+		   	$this->innerBlocks($block).'</section>',
+			$tagName
+		);
     }
     //core_comment_template
 
-    public function render_core_group(array $block):string
+    public function prerender_core_group(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        $tag = (array_key_exists('tagName', $block['attrs'])) ? $block['attrs']['tagName'] : 'div';
+//		jvbDump($block, 'group');
+//		jvbDump($parent, 'Parent');
+        $tag = (array_key_exists('tagName', $block['attrs']??[])) ? $block['attrs']['tagName'] : 'div';
 
-        $classes = ($tag === 'main') ?
-            '' :
-            $this->getClassesAndStyles($block['attrs'], ['group']);
-        return '<'.$tag.$classes.'>'.$this->innerBlocks($block).'</'.$tag.'>';
+
+        return sprintf(
+			'<%s%s>%s</%s>',
+			$tag,
+			$tag === 'main' ? '' : $this->getClassesAndStyles($block['attrs']??[], ['group']),
+			$this->innerBlocks($block),
+			$tag
+		);
     }
     //core_home_link
     //core_more
     //core_nextpage
 
-    public function render_core_separator(array $block):string
+    public function prerender_core_separator(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<hr'.$this->getClassesAndStyles($block['attrs']).'>';
+//		jvbDump($block, 'separator');
+//		jvbDump($parent, 'Parent');
+        return '<hr'.$this->getClassesAndStyles($block['attrs']??[]).'>';
     }
 
-    public function render_core_spacer(array $block):string
+    public function prerender_core_spacer(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<div'.$this->getClassesAndStyles($block['attrs'], ['spacer'], ['height:2rem']).
+
+//		jvbDump($block, 'spsacer');
+//		jvbDump($parent, 'Parent');
+        return '<div'.$this->getClassesAndStyles($block['attrs']??[], ['spacer'], ['height:2rem']).
                ' aria-hidden="true"></div>';
     }
     //core_table_of_contents
@@ -219,11 +255,12 @@
      * Media Blocks
      */
     //core_audio
-    public function render_core_cover(array $block):string
+    public function prerender_core_cover(array $block, ?string $content, ?WP_Block $parent):?string
     {
-
+//		jvbDump($block, 'cover');
+//		jvbDump($parent, 'Parent');
         // Extract block attributes
-        $attrs = $block['attrs'] ?? [];
+        $attrs = $block['attrs'] ?:[];
         $innerContent = $this->innerBlocks($block);
 
 		$position = 'object-position: center;';
@@ -266,15 +303,19 @@
 
     //core_file
 
-    public function render_core_gallery(array $block):string
+    public function prerender_core_gallery(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<ul'.$this->getClassesAndStyles($block['attrs'], ['gallery']).'>'.
+//		jvbDump($block, 'gallery');
+//		jvbDump($parent, 'Parent');
+        return '<ul'.$this->getClassesAndStyles($block['attrs']??[], ['gallery']).'>'.
                $this->innerBlocks($block,'<li>', '</li>').
                '</ul>';
     }
 
-    public function render_core_image(array $block):string
+    public function prerender_core_image(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'image');
+//		jvbDump($parent, 'Parent');
         $ID = $this->imageID('', $block);
         if (!$ID) {
             return '';
@@ -287,32 +328,34 @@
                 wp_get_attachment_caption($ID) .
             '</figcaption>' :
             '<figcaption>' . $title . '</figcaption>';
-		$size = array_key_exists('sizeSlug', $block['attrs']) ? $block['attrs']['sizeSlug'] : 'large';
+		$size = array_key_exists('sizeSlug', $block['attrs']??[]) ? $block['attrs']['sizeSlug'] : 'large';
         return '<figure'.
-               $this->getClassesAndStyles($block['attrs']).'>'.
+               $this->getClassesAndStyles($block['attrs']??[]).'>'.
                $this->imageLink(true, $ID, 'tiny', $size) .
                $caption.'</figure>';
     }
 
-    public function render_core_media_text(array $block):string
+    public function prerender_core_media_text(array $block, ?string $content, ?WP_Block $parent):?string
     {
 
+//		jvbDump($block, 'media text');
+//		jvbDump($parent, 'Parent');
         $ID = $this->imageID('', $block);
 
-		$size = array_key_exists('mediaSizeSlug', $block['attrs']) ? $block['attrs']['mediaSizeSlug'] : 'large';
+		$size = array_key_exists('mediaSizeSlug', $block['attrs']??[]) ? $block['attrs']['mediaSizeSlug'] : 'large';
         $imgLink = ($ID) ? $this->imageLink(true, $ID, 'tiny', $size) : '';
 
         $inner = $this->innerBlocks($block);
 
 
 		$classes = ['media-text', 'row'];
-		if (array_key_exists('isStackedOnMobile', $block['attrs'])) {
+		if (array_key_exists('isStackedOnMobile', $block['attrs']??[])) {
 			$classes[] = 'nowrap';
 		}
-        $content = '<div'.$this->getClassesAndStyles($block['attrs'], $classes).'>';
+        $content = '<div'.$this->getClassesAndStyles($block['attrs']??[], $classes).'>';
         $content .= (array_key_exists(
             'mediaPosition',
-            $block['attrs']
+            $block['attrs']??[]
         ) && $block['attrs']['mediaPosition'] == 'right') ?
             '<div>'.$inner.'</div><figure>'.$imgLink.'</figure>' :
             '<figure>'.$imgLink.'</figure><div>'.$inner.'</div>';
@@ -330,40 +373,49 @@
     /**
      * Text Blocks
     */
-    //render_core_code
-    //render_core_details
-    //render_core_footnotes
-    //render_core_classic
-    public function render_core_heading(array $block):string
+    //prerender_core_code
+    //prerender_core_details
+    //prerender_core_footnotes
+    //prerender_core_classic
+    public function prerender_core_heading(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        $level = (array_key_exists('level', $block['attrs'])) ? $block['attrs']['level'] : '2';
-		$content = $this->inside($block);
+//		jvbDump($block, 'heading');
+//		jvbDump($parent, 'Parent');
+        $level = (array_key_exists('level', $block['attrs']??[])) ? $block['attrs']['level'] : '2';
+		$content = $this->innerBlocks($block);
         $id = sanitize_title(wp_strip_all_tags($this->stripTagContents('small', $content)));
-        return '<h'.$level.' id="'.$id.'"'.$this->getClassesAndStyles($block['attrs']).'>'.
+        return '<h'.$level.' id="'.$id.'"'.$this->getClassesAndStyles($block['attrs']??[]).'>'.
                $content.
                '</h'.$level.'>';
     }
 
-	public function render_core_list(array $block):string
+	public function prerender_core_list(array $block, ?string $content, ?WP_Block $parent):?string
 	{
-		$tag = (array_key_exists('ordered', $block['attrs'])) ? 'ol' : 'ul';
-		return '<'.$tag.$this->getClassesAndStyles($block['attrs']).'>'.$this->innerBlocks($block).'</'.$tag.'>';
+//		jvbDump($block, 'list');
+//		jvbDump($parent, 'Parent');
+		$tag = (array_key_exists('ordered', $block['attrs']??[])) ? 'ol' : 'ul';
+		$output = '<'.$tag.$this->getClassesAndStyles($block['attrs']??[]).'>'.$this->innerBlocks($block).'</'.$tag.'>';
+		return $output;
 	}
 
-//	public function render_core_list_item(array $block):string
+//	public function prerender_core_list_item(array $block):string
 //	{
 //		return '<li'.$this->getClassesAndStyles($block['attrs']).'>'.$this->inside($block).'</li>';
 //	}
-    //render_core_missing
+    //prerender_core_missing
 
-    public function render_core_paragraph(array $block):string
+    public function prerender_core_paragraph(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        return '<p'.$this->getClassesAndStyles($block['attrs']).'>'.
-               $this->inside($block, 'p').
+//		jvbDump($block, 'paragraph');
+//		jvbDump($parent, 'Parent');
+        return '<p'.$this->getClassesAndStyles($block['attrs']??[]).'>'.
+               $this->innerBlocks($block).
                '</p>';
     }
-	public function render_core_quote(array $block): string
+	public function prerender_core_quote(array $block, ?string $content, ?WP_Block $parent): ?string
 	{
+//		jvbDump($block, 'quote');
+//		jvbDump($parent, 'Parent');
 		$innerHTML = $block['innerHTML'];
 
 		// Extract cite content first
@@ -371,20 +423,22 @@
 		$citeHtml = ($cite === '') ? '' : '<cite>—&emsp;'.$cite.'</cite>';
 
 		// Get the blockquote content
-		$content = $this->inside($block, 'blockquote');
+		$content = $this->innerBlocks($block);
 
 		// Remove the cite element from content if it exists
 		if ($cite !== '') {
 			$content = $this->stripTagContents('cite', $content);
 		}
 
-		return '<blockquote'.$this->getClassesAndStyles($block['attrs']).'>
+		return '<blockquote'.$this->getClassesAndStyles($block['attrs']??[]).'>
         <div class="content">'.$content.'</div>'.
 			$citeHtml.
 			'</blockquote>';
 	}
-	public function render_core_pullquote(array $block): string
+	public function prerender_core_pullquote(array $block, ?string $content, ?WP_Block $parent):?string
 	{
+//		jvbDump($block, 'pullquote');
+//		jvbDump($parent, 'Parent');
 		$innerHTML = $block['innerHTML'];
 
 		// Extract cite content first
@@ -400,23 +454,48 @@
 		}
 		$content = jvb_filter_content( $content);
 
-		return '<blockquote'.$this->getClassesAndStyles($block['attrs'], ['pull']).'>'.
+		return '<blockquote'.$this->getClassesAndStyles($block['attrs']??[], ['pull']).'>'.
         	$content.
 			$citeHtml.
 			'</blockquote>';
 	}
-    //render_core_table
-    //render_core_verse
+    //prerender_core_table
+    //prerender_core_verse
 
     /**
      * Theme Blocks
      */
     //core_avatar
     //core_loginout
+	public function prerender_core_loginout(array $block, ?string $content, ?WP_Block $parent):?string
+	{
+		$action = is_user_logged_in() ? 'logout' : 'login';
+		$attrs = $block['attrs'];
+		$redirect = '';
+		if (array_key_exists('redirectToCurrent', $attrs) && $attrs['redirectToCurrent']) {
+			global $wp;
+			$redirect = get_home_url(null, $wp->request);
+		}
+
+		if (array_key_exists('displayLoginAsForm', $attrs) && $attrs['displayLoginAsForm']) {
+			LoginManager::getInstance()->setAction($action);
+			return LoginManager::getInstance()->renderLoginForm($action, $redirect, '<h2>Login</h2>');
+
+		}
+
+		return sprintf(
+			'<a href="%s"%s>%s</a>',
+			wp_login_url($redirect),
+			$this->getClassesAndStyles($attrs),
+			$action === 'login' ? jvbIcon('sign-in').'<span>Log in</span>' : jvbIcon('sign-out').'<span>Logout</span>'
+		);
+	}
     //core_pattern
 
-    public function render_core_site_logo(array $block, string $content):string
+    public function prerender_core_site_logo(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'site logo');
+//		jvbDump($parent, 'Parent');
         $open = $close = '';
 
         if (!is_home() && !is_front_page()) {
@@ -425,14 +504,16 @@
         }
         $img = get_theme_mod('custom_logo');
         $img = $this->image($img, 'tiny', 'thumbnail');
-        $img = str_replace('<img', '<img'.$this->getClassesAndStyles($block['attrs']), $img);
+        $img = str_replace('<img', '<img'.$this->getClassesAndStyles($block['attrs']??[]), $img);
         return $open.$img.$close;
     }
     //core_site_title_tagline
 
-    public function render_core_site_title(array $block, string $content):string
+    public function prerender_core_site_title(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        $tag = (array_key_exists('level', $block['attrs'])) ? $block['attrs']['level'] : 1;
+//		jvbDump($block, 'site title');
+//		jvbDump($parent, 'Parent');
+        $tag = (array_key_exists('level', $block['attrs']??[])) ? $block['attrs']['level'] : 1;
         $tag = ($tag == 0) ? 'p' : 'h'.$tag;
 
         $open = $close = '';
@@ -441,8 +522,8 @@
             $close = '</a>';
         }
         $class = ($tag === 'p') ?
-            $this->getClassesAndStyles($block['attrs'], ['title']) :
-            $this->getClassesAndStyles($block['attrs']);
+            $this->getClassesAndStyles($block['attrs']??[], ['title']) :
+            $this->getClassesAndStyles($block['attrs']??[]);
 
 
         return '<'.$tag.$class.'>'.
@@ -472,15 +553,17 @@
     /**
      * Theme Navigation Blocks
      */
-    public function render_core_navigation(array $block, string $content):string
+    public function prerender_core_navigation(array $block, ?string $content, ?WP_Block $parent):?string
     {
-        $ID = (array_key_exists('ref', $block['attrs'])) ? $block['attrs']['ref'] : false;
+//		jvbDump($block, 'navigation');
+//		jvbDump($parent, 'Parent');
+        $ID = (array_key_exists('ref', $block['attrs']??[])) ? $block['attrs']['ref'] : false;
 
         if (empty($block['innerBlocks']) && $ID && get_post($ID)) {
             $block['innerBlocks'] = parse_blocks(get_post($ID)->post_content);
         }
 
-        $toggle = (array_key_exists('overlayMenu', $block['attrs'])
+        $toggle = (array_key_exists('overlayMenu', $block['attrs']??[])
                    && $block['attrs']['overlayMenu'] == 'never') ?
             '':
             '<button class="toggle main"
@@ -490,14 +573,15 @@
             aria-expanded="false">'.
             jvbIcon('list', ['title'=>'Toggle Menu']).
             jvbIcon('x', ['title'=>'Toggle Menu']).
-            '</button>';
+		'</button>';
         $class = ($toggle === '') ?
-            $this->getClassesAndStyles($block['attrs'], ['mobile']) :
-            $this->getClassesAndStyles($block['attrs']);
+            $this->getClassesAndStyles($block['attrs']??[], ['mobile']) :
+            $this->getClassesAndStyles($block['attrs']??[]);
         $helpmenu = (get_the_title($ID) === 'Main') ?
             '<nav><ul>'.jvbNotificationMenu().jvbHelpMenu().'</ul></nav>' :
             '';
 
+
 		//Allows to add custom items to a menu, based on the menu name
 		$helpmenu = apply_filters('jvbMenuExtraAfter', $helpmenu, get_the_title($ID));
 		$main = trim(apply_filters('jvbMenuExtra', $this->innerBlocks($block), get_the_title($ID), $block));
@@ -513,14 +597,19 @@
 		   '</nav>'.$helpmenu;
     }
 
-    public function render_core_navigation_link(array $block):string
+    public function prerender_core_navigation_link(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'navigation link');
+//		jvbDump($parent, 'Parent');
         global $wp;
+		if (!array_key_exists('attrs', $block)) {
+			return '';
+		}
         $url = (str_starts_with($block['attrs']['url'],'/')) ?
             home_url($block['attrs']['url']) :
             $block['attrs']['url'];
         $current = (home_url($wp->request.'/') == $url);
-		$temp = $block['attrs'];
+		$temp = $block['attrs']??[];
 		unset($temp['url']);
         $classes = ($current) ?
             $this->getClassesAndStyles($temp, ['current']):
@@ -529,21 +618,23 @@
         if ($current) {
             $aria = ' aria-current="page"';
         }
-        $linkOpen = $this->build_navigation_link($block['attrs'], $aria);
+        $linkOpen = $this->build_navigation_link($block['attrs']??[], $aria);
 
 
         return '<li'.$classes.'>'.$linkOpen.$block['attrs']['label'].'</a></li>';
     }
 
-    public function render_core_navigation_submenu(array $block):string
+    public function prerender_core_navigation_submenu(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'navigation submenu');
+//		jvbDump($parent, 'Parent');
         global $wp;
         $url = (str_starts_with($block['attrs']['url'],'/')) ?
             home_url($block['attrs']['url']) :
             $block['attrs']['url'];
         $current = (home_url($wp->request) == $url);
 
-		$temp = $block['attrs'];
+		$temp = $block['attrs']??[];
 		unset($temp['url']);
         $classes = ($current) ?
             $this->getClassesAndStyles($temp, ['has-submenu', 'current']):
@@ -605,74 +696,178 @@
     //core_post_author
     //core_post_author_biography
     //core_post_author_name
-    public function render_core_post_content(array $block, string $content = ''):string
+    public function prerender_core_post_content(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'post content');
+//		jvbDump($parent, 'Parent');
 
-        $tag = (array_key_exists('tagName', $block['attrs'])) ?
+        $tag = (array_key_exists('tagName', $block['attrs']??[])) ?
             $block['attrs']['tagName'] :
             'main';
 
         if ($content == '') {
-			global $post;
+			if(is_singular()) {
+				global $post;
 
-			$block['innerBlocks'] = parse_blocks($post->post_content);
-			return $this->innerBlocks($block);
+				$block['innerBlocks'] = parse_blocks($post->post_content);
+				$result = $this->innerBlocks($block);
+			}else {
+				$result = '';
+			}
         } else {
-            return $this->inside($block, $tag, $content);
+            $result = $this->innerBlocks($block);
         }
+
+		return apply_filters('jvb_post_content_output', $result, $block);
     }
     //core_post_date
-	public function render_core_post_date(array $block):string
+	public function prerender_core_post_date(array $block, ?string $content, ?WP_Block $parent):?string
 	{
+//		jvbDump($block, 'post date');
+//		jvbDump($parent, 'Parent');
 		$postDate = get_the_date('c');
-		return '<time datetime="'.$postDate.'" itemprop="datePublished"'.$this->getClassesAndStyles($block['attrs']).'>'.get_the_date().'</time>';
+		return '<time datetime="'.$postDate.'" itemprop="datePublished"'.$this->getClassesAndStyles($block['attrs']??[]).'>'.get_the_date().'</time>';
 	}
     //core_post_excerpt
-    public function render_core_post_featured_image(array $block):string
+	public function prerender_core_post_excerpt(array $block, ?string $content, ?WP_Block $parent):?string
+	{
+		return wpautop(get_the_excerpt());
+	}
+    public function prerender_core_post_featured_image(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'featured image');
+//		jvbDump($parent, 'Parent');
 		global $post;
 		$ID = get_post_thumbnail_id($post->ID);
 		$aOpen = $aClose = '';
-		if(!is_single($ID)) {
+		if(!is_single($post->ID)) {
 			$aOpen = '<a href="'.get_the_permalink($post->ID).'">';
 			$aClose = '</a>';
 		}
 
-        return $aOpen.'<figure'.$this->getClassesAndStyles($block['attrs']).'>'.
+        return '<figure'.$this->getClassesAndStyles($block['attrs']??[]).'>'.$aOpen.
                apply_filters('jvbCoreFeaturedImage', $this->image($ID), $post->post_type).
-               '</figure>'.$aClose;
+				$aClose.'</figure>';
     }
     //core_post_navigation_link
-    //core_post_template
-    //core_post_terms
-	public function render_core_post_terms(array $block):string
+	public function prerender_core_post_navigation_link(array $block, ?string $content, ?WP_Block $parent):?string
 	{
+		$attr = $block['attrs'];
+		$isPrevious = $attr['type']==='previous';
+		$title = array_key_exists('showTitle', $attr)&&$attr['showTitle'];
+		$linkLabel = array_key_exists('linkLabel', $attr)&&$attr['linkLabel'];
+		$label = array_key_exists('label', $attr) ? $attr['label'] : '';
+		$arrow = '';
+		if (array_key_exists('arrow', $attr)) {
+			$dir = $isPrevious ? 'left' : 'right';
+			$icon = match($attr['arrow']) {
+				'arrow'	=> 'arrow-square-',
+				'chevron' => 'caret-circle-'
+			};
+			if ($icon) {
+				$arrow = jvbIcon($icon.$dir);
+			}
+		}
+//		return $content;
+		$linkedLabel = $unlinkedLabel = '';
+		if (!empty($label)) {
+			$linkedLabel = $linkLabel ? $label : '';
+			$unlinkedLabel = $linkLabel ? '' : $label;
+		}
+		if ($title) {
+			$linkedLabel .=' %title';
+		} elseif (!empty($label)) {
+			$linkedLabel = $label;
+			$unlinkedLabel = '';
+		} else {
+			$linkedLabel = $isPrevious ? 'Previous' : 'Next';
+			$unlinkedLabel = '';
+		}
+
+		$result = $isPrevious ?
+			get_previous_post_link(
+				$arrow.$unlinkedLabel.' %link',
+				$linkedLabel
+			) :
+			get_next_post_link(
+				'%link '.$unlinkedLabel.$arrow,
+				$linkedLabel
+			);
+
+
+		return sprintf('<div%s>%s</div>',
+			$this->getClassesAndStyles($attr,['row', 'nowrap']),
+			$result
+		);
+	}
+    //core_post_template
+	public function render_core_post_template(array $block, string $content):string
+	{
+		global $wp_query;
+
+		$inner = '';
+		$block['innerBlocks'][0]['attrs']['tagName'] = 'li';
+		if ($wp_query->have_posts()) {
+			while ($wp_query->have_posts()) {
+				$wp_query->the_post();
+
+				$inner .= $this->innerBlocks($block);
+			}
+			wp_reset_postdata();
+		}
+
+		return sprintf(
+			'<ul class="loop">%s</ul>',
+			$inner
+		);
+	}
+    //core_post_terms
+	public function prerender_core_post_terms(array $block, ?string $content, ?WP_Block $parent):?string
+	{
+		if (!array_key_exists('attrs', $block)) {
+			return '';
+		}
 		$terms = get_the_terms(get_the_ID(), $block['attrs']['term']);
 		$out = '';
 		if ($terms && !is_wp_error($terms)) {
-			$out = '<ul class="term-list">';
-				if (array_key_exists('prefix', $block['attrs'])) {
-					$out .= '<li>'.$block['attrs']['prefix'].'</li>';
+			$out = sprintf(
+				'<ul%s>',
+				$this->getClassesAndStyles($block['attrs'], ['term-list', 'row', 'start'])
+			);
+				if (array_key_exists('prefix', $block['attrs']??[])) {
+					$out .= sprintf(
+						'<li class="prefix">%s</li>',
+						$block['attrs']['prefix']
+					);
 				}
 				foreach($terms as $term) {
-					$out .= '<li><a href="'.get_term_link($term).'" rel="tag">'.html_entity_decode($term->name).'</a></li>';
+					$out .= sprintf(
+						'<li><a href="%s" rel="tag">%s</a></li>',
+						get_term_link($term),
+						html_entity_decode($term->name)
+					);
 				}
 			if (array_key_exists('suffix', $block['attrs'])) {
-				$out .= '<li>'.$block['attrs']['suffix'].'</li>';
+				$out .= sprintf(
+					'<li class="suffix">%s</li>',
+					$block['attrs']['suffix']
+				);
 			}
 			$out .= '</ul>';
 		}
 		return $out;
 	}
     //core_post_time_to_read
-    public function render_core_post_title(array $block):string
+    public function prerender_core_post_title(array $block, ?string $content, ?WP_Block $parent):?string
     {
+//		jvbDump($block, 'post content');
+//		jvbDump($parent, 'Parent');
         $open = $close = '';
-        if (array_key_exists('isLink', $block['attrs'])) {
-            $rel = (array_key_exists('rel', $block['attrs'])) ?
+        if (array_key_exists('isLink', $block['attrs']??[])) {
+            $rel = (array_key_exists('rel', $block['attrs']??[])) ?
                 ' rel="'.$block['attrs']['rel'].'"' :
                 '';
-            $target = (array_key_exists('linkTarget', $block['attrs'])) ?
+            $target = (array_key_exists('linkTarget', $block['attrs']??[])) ?
                 ' target="'.$block['attrs']['linkTarget'].'"' :
                 '';
             $open = '<a href="' . get_the_permalink() . '"' . $rel . $target . '>';
@@ -685,110 +880,114 @@
                   array_key_exists('level', $block['attrs'])) ?
             $block['attrs']['level'] :
             2;
-        return '<h'.$level.$this->getClassesAndStyles($block['attrs']).'>'.
+        return '<h'.$level.$this->getClassesAndStyles($block['attrs']??[]).'>'.
                $open.get_the_title().$close.
                '</h'.$level.'>';
     }
 
-	public function render_core_query(array $block, string $content):string
+	public function render_core_query(array $block, string $content): string
 	{
-		$queryID = $block['attrs']['queryId'];
-		$args = [];
-		$inherit = $block['attrs']['inherit']??false;
-		if ($inherit) {
-			global $wp_query;
-			$loop = $wp_query;
-		} else {
-			foreach ($block['attrs']['query'] as $key => $value) {
-				if (empty($value)) {
-					continue;
-				}
-				switch ($key) {
-					case 'postType':
-						if ($value === BASE.'progress'){
-							$args['post_parent'] = 0;
-						}
-						$args['post_type'] = $value;
-						break;
-					case 'perPage':
-						$args['posts_per_page'] = $value;
-						break;
-					case 'orderBy':
-						$args['orderby'] = $value;
-						break;
-					case 'taxQuery':
-						$taxQuery = [];
-						foreach ($value as $tax => $terms) {
-							$taxQuery[] = [
-								'taxonomy' 	=> $tax,
-								'terms'		=> $terms
-							];
-						}
-						if (!empty($taxQuery)) {
-							$args['tax_query'] = $taxQuery;
-							if (count($taxQuery) > 1) {
-								$args['tax_query']['relation'] = 'OR';
-							}
-						}
-						break;
-					case 'sticky':
-						if ($value === 'ignore') {
-							$args['ignore_sticky_posts'] = true;
-						} else if ($value === 'exclude'){
-							$args['post__not_in'] = get_option('sticky_posts');
-						} else if ($value === 'only') {
-							$args['include'] = get_option('sticky_posts');
-						}
-						break;
-					case 'search':
-						$args['s'] = $value;
-						break;
-					default:
-						$args[$key] = $value;
-						break;
 
-				}
-			}
-			//Add in any args from the query string
-			$search = 'query-'.$queryID;
-			foreach ($_GET as $key => $value) {
-				if (str_contains($key, $search)) {
-					$key = str_replace($search, '', $key);
-					if ($key === 'page') {
-						$args['paged'] = (int)$value;
-					}
-				}
-			}
-			$loop = new WP_Query($args);
-		}
+//		$queryID = $block['attrs']['queryId'] ?? null;
+//		$inherit = $block['attrs']['inherit'] ?? false;
+//
+//		if ($inherit) {
+//			global $wp_query;
+//			$loop = $wp_query;
+//		} else {
+//			$args = [];
+//			foreach (($block['attrs']['query'] ?? []) as $key => $value) {
+//				if (empty($value)) {
+//					continue;
+//				}
+//				switch ($key) {
+//					case 'postType':
+//						if ($value === BASE.'progress'){
+//							$args['post_parent'] = 0;
+//						}
+//						$args['post_type'] = $value;
+//						break;
+//					case 'perPage':
+//						$args['posts_per_page'] = $value;
+//						break;
+//					case 'orderBy':
+//						$args['orderby'] = $value;
+//						break;
+//					case 'taxQuery':
+//						$taxQuery = [];
+//						foreach ($value as $tax => $terms) {
+//							$taxQuery[] = [
+//								'taxonomy' 	=> $tax,
+//								'terms'		=> $terms
+//							];
+//						}
+//						if (!empty($taxQuery)) {
+//							$args['tax_query'] = $taxQuery;
+//							if (count($taxQuery) > 1) {
+//								$args['tax_query']['relation'] = 'OR';
+//							}
+//						}
+//						break;
+//					case 'sticky':
+//						if ($value === 'ignore') {
+//							$args['ignore_sticky_posts'] = true;
+//						} else if ($value === 'exclude'){
+//							$args['post__not_in'] = get_option('sticky_posts');
+//						} else if ($value === 'only') {
+//							$args['include'] = get_option('sticky_posts');
+//						}
+//						break;
+//					case 'search':
+//						$args['s'] = $value;
+//						break;
+//					default:
+//						$args[$key] = $value;
+//						break;
+//
+//				}
+//			}
+//			$search = 'query-' . $queryID;
+//			foreach ($_GET as $key => $value) {
+//				if (str_contains($key, $search)) {
+//					$key = str_replace($search, '', $key);
+//					if ($key === 'page') {
+//						$args['paged'] = (int)$value;
+//					}
+//				}
+//			}
+//			$loop = new WP_Query($args);
+//		}
+//
+//		$inner = '';
+//		foreach ($block['innerBlocks'] as $innerBlock) {
+//			switch ($innerBlock['blockName']) {
+//				case 'core/post-template':
+//					$inner .= '<section class="item-grid">';
+//					if ($loop->have_posts()) {
+//						while ($loop->have_posts()) {
+//							$loop->the_post();
+//							$postType = get_post_type();
+//							$inner .= '<div class="item ' . jvbNoBase($postType) . '">' . $this->innerBlocks($innerBlock) . '</div>';
+//						}
+//					}
+//					$inner .= '</section>';
+//					break;
+//			}
+//		}
+//
+//		// Reset only after a custom query, not the main query
+//		if (!$inherit) {
+//			wp_reset_postdata();
+//		}
 
-		$inner = '';
-
-		foreach ($block['innerBlocks'] as $innerBlock) {
-			switch ($innerBlock['blockName']) {
-				case 'core/post-template':
-					$inner .= '<section class="item-grid">';
-					if ($loop->have_posts()) {
-						while($loop->have_posts()) {
-							$loop->the_post();
-							$postType = get_post_type();
-							$inner .= '<div class="item '.jvbNoBase($postType).'">'.$this->innerBlocks($innerBlock).'</div>';
-						}
-					}
-					$inner .= '</section>';
-					break;
-
-			}
-		}
-
-
-
-		$tagName = (array_key_exists('tagName', $block['attrs'])) ? $block['attrs']['tagName'] : 'div';
-		$out =  '<'.$tagName.' class="loop">'.$inner.'</'.$tagName.'>';
-		if ($inherit) {
-			wp_reset_postdata();
-		}
-		return $out;
+		$tagName = $block['attrs']['tagName'] ?? 'div';
+//		return sprintf(
+//			'<%s class="loop">%s</%s>',
+//			$tagName,
+//			$this->innerBlocks($block),
+//			$tagName
+//		);
+		return $this->innerBlocks($block);
 	}
 
     //core_query_no_results
@@ -796,70 +995,153 @@
     //core_query_pagination_next
     //core_query_pagination_numbers
     //core_query_pagination_previous
-    //core_query_title
+
+	public function prerender_core_query_title(array $block, ?string $content, ?WP_Block $parent):?string
+	{
+		jvbDump($block);
+		$attr = $block['attrs'];
+		$name = '';
+		$showPrefix = $attr['showPrefix']??false;
+		$obj = get_queried_object();
+		if (is_tax()) {
+			$name = $showPrefix ? $obj->label.':' : '';
+			$name .= $obj->name;
+		} else if (is_post_type_archive()) {
+			$name = $showPrefix ? 'Archive: ' : '';
+			$name .= $obj->label;
+		} elseif (is_search()) {
+			$name = '<small>Search results for:</small> '.get_search_query();
+		} elseif (is_singular()) {
+			$name = $obj->post_title;
+		}
+		$level = array_key_exists('level', $attr) ? 'h'.$attr['level'] : 'h1';
+
+		return sprintf(
+			'<%s id="%s">%s</%s>',
+			$level,
+			sanitize_title($name),
+			$name,
+			$level
+		);
+	}
     //core_read_more
-    public function render_core_template_part(array $block, string $content):string
+    public function prerender_core_template_part(array $block, ?string $content, ?WP_Block $parent):?string
     {
-
-		$isHeaderTemplate = (
-			(array_key_exists('slug', $block['attrs']) && str_contains(strtolower($block['attrs']['slug']), 'header')) ||
-			(array_key_exists('tagName', $block['attrs']) && str_contains(strtolower($block['attrs']['tagName']), 'header'))
-		) ? 'header' : false;
-		$isFooterTemplate = (
-			(array_key_exists('slug', $block['attrs']) && str_contains(strtolower($block['attrs']['slug']), 'footer')) ||
-			(array_key_exists('tagName', $block['attrs']) && str_contains(strtolower($block['attrs']['tagName']), 'footer'))
-		) ? 'footer' : false;
+//		jvbDump($block, 'template part');
+//		jvbDump($parent, 'Parent');
 
 
-        if (($isHeaderTemplate || $isFooterTemplate)) {
+		$slug  = $block['attrs']['slug'] ?? null;
+		$theme = $block['attrs']['theme'] ?? get_stylesheet();
+		$tag = $block['attrs']['tagName'] ?? 'div';
+		if (!$slug) {
+			return $content;
+		}
 
-			$tag = $isHeaderTemplate ?: $isFooterTemplate ?: 'div';
+		// Try to get the template part post (customized via FSE)
+		$template_part = get_block_template( "$theme//$slug", 'wp_template_part' );
 
-            $breadcrumbs = $themeSwitch = $afterHeader = $beforeHeader = $footerText= '';
-            if ($isHeaderTemplate) {
+		if ( $template_part && ! empty( $template_part->content ) ) {
+			$block['innerBlocks'] = parse_blocks( $template_part->content );
 
-				$beforeHeader = apply_filters('jvbAboveHeader', $beforeHeader);
-				if ($beforeHeader !== '') {
-					$beforeHeader = '<aside class="pre-header">'.$beforeHeader.'</aside>';
-				}
-                $checked = (is_user_logged_in() && current_user_can('prefers_dark_theme', true)) ? ' checked' : '';
-                $title = ($checked == '') ? 'Toggle Dark Mode' : 'Toggle Light Mode';
-				$showThemeSwitch = (bool)apply_filters('jvb_show_theme_switch', true);
-                $themeSwitch = ($showThemeSwitch) ? '<label title="'.$title.'" id="theme-switch" class="toggle-switch" for="theme-switcher">
-                    <input class="theme-switch row" id="theme-switcher" name="theme-switcher" type="checkbox"'.$checked.' data-setting="theme" data-theme role="switch" name="dark-mode" aria-label="Toggle dark mode"><span class="slider">'.
-					jvbIcon('sun-dim', ['title'=> 'Light Mode']).
-					jvbIcon('moon', ['title'=>'Dark Mode']).
-					'</span></label>' : '';
-                $breadcrumbs = jvbBuildBreadcrumbs();
-				$afterHeader = apply_filters('jvbBelowHeader', $afterHeader);
+			$before = $themeSwitch = $after = $beforeClose = '';
+			switch ($tag) {
+				case 'header':
+					$before = apply_filters('jvbAboveHeader', '');
+					if (!empty($before)) {
+						$before = sprintf(
+							'<aside class="pre header row btw">%s</aside>',
+							$before
+						);
+					}
+					$themeSwitch = jvbDarkModeToggle();
 
-				if ($afterHeader !== '') {
-					$afterHeader = '<aside class="sub-header">'.$afterHeader.'</aside>';
-				}
-				$footerText = '<div class="scroll-progress"><div class="bar"></div>
-</div>';
-            } elseif ($isFooterTemplate) {
-				$beforeHeader = apply_filters('jvbBeforeFooter', '');
-				if ($beforeHeader !== '') {
-					$beforeHeader = '<section class="pre-footer">'.$beforeHeader.'</section>';
-				}
-					$footerText = jvbRandomFooterText();
+					$after = apply_filters('jvbBelowHeader', $after);
+					if (!empty($after)) {
+						$after = sprintf(
+							'<aside class="sub header row btw">%s</aside>',
+							$after
+						);
+					}
+					$after .= BreadcrumbManager::getInstance()->renderNavigation();
+					$beforeClose = '<div class="scroll-progress"><div class="bar"></div></div>';
+					break;
+				case 'footer':
+					$before = apply_filters('jvbBeforeFooter', $before);
+					if (!empty($before)) {
+						$before = sprintf(
+							'<aside class="pre footer">%s</aside>',
+							$before
+						);
+					}
+					$beforeClose = jvbRandomFooterText();
+					break;
 			}
-//			jvbDump($beforeHeader,'beforeHeader');
-//			jvbDump('<'.$tag.$this->getClassesAndStyles($block['attrs']).'>','tag');
-//			jvbDump($themeSwitch,'themeSwitch');
-//			jvbDump($this->inside($block, $tag, $content),'inside');
-//			jvbDump($footerText,'footerText');
-//			jvbDump($afterHeader, 'afterheader');
-//			jvbDump($breadcrumbs, 'breadcrumbs');
 
-            return $beforeHeader.'<'.$tag.$this->getClassesAndStyles($block['attrs']).'>'.
-                   $themeSwitch .
-				   $this->inside($block, $tag, $content) .
-                   $footerText.'</'.$tag.'>'.$afterHeader.$breadcrumbs;
-        }
+			return sprintf(
+				'%s<%s%s>%s%s%s</%s>%s',
+				$before,
+				$tag,
+				$this->getClassesAndStyles($block['attrs']??[]),
+				$themeSwitch,
+				$this->innerBlocks($block),
+				$beforeClose,
+				$tag,
+				$after
+			);
 
-        return $content;
+		}
+		if (JVB_TESTING) {
+			jvbDump('Could not create template part block for '.$block['blockName']);
+		}
+		return $content;
+
+//		$isHeaderTemplate = (
+//			(array_key_exists('slug', $block['attrs']??[]) && str_contains(strtolower($block['attrs']['slug']), 'header')) ||
+//			(array_key_exists('tagName', $block['attrs']??[]) && str_contains(strtolower($block['attrs']['tagName']), 'header'))
+//		) ? 'header' : false;
+//		$isFooterTemplate = (
+//			(array_key_exists('slug', $block['attrs']??[]) && str_contains(strtolower($block['attrs']['slug']), 'footer')) ||
+//			(array_key_exists('tagName', $block['attrs']??[]) && str_contains(strtolower($block['attrs']['tagName']), 'footer'))
+//		) ? 'footer' : false;
+//        if (($isHeaderTemplate || $isFooterTemplate)) {
+//			$innerContent = $content;
+//
+//			$tag = $isHeaderTemplate ?: $isFooterTemplate ?: 'div';
+//
+//            $breadcrumbs = $themeSwitch = $afterHeader = $beforeHeader = $footerText= '';
+//            if ($isHeaderTemplate) {
+//
+//				$beforeHeader = apply_filters('jvbAboveHeader', $beforeHeader);
+//				if ($beforeHeader !== '') {
+//					$beforeHeader = '<aside class="pre header row btw">'.$beforeHeader.'</aside>';
+//				}
+//                $themeSwitch = jvbDarkModeToggle();
+//                $breadcrumbs = BreadcrumbManager::getInstance()->renderNavigation();
+//				$afterHeader = apply_filters('jvbBelowHeader', $afterHeader);
+//
+//				if ($afterHeader !== '') {
+//					$afterHeader = '<aside class="sub header row btw">'.$afterHeader.'</aside>';
+//				}
+//				$footerText = '<div class="scroll-progress"><div class="bar"></div>
+//</div>';
+//            } elseif ($isFooterTemplate) {
+//				$beforeHeader = apply_filters('jvbBeforeFooter', '');
+//				if ($beforeHeader !== '') {
+//					$beforeHeader = '<aside class="footer">'.$beforeHeader.'</aside>';
+//				}
+//					$footerText = jvbRandomFooterText();
+//			}
+//
+//            $content = $beforeHeader.'<'.$tag.$this->getClassesAndStyles($block['attrs']??[]).'>'.
+//                   $themeSwitch .
+//					$this->innerBlocks($block).
+////				   $this->innerBlocks($block).
+////					$innerContent.
+//                   $footerText.'</'.$tag.'>'.$afterHeader.$breadcrumbs;
+//        }
+//
+//        return $content;
     }
     //core_term_description
 
@@ -877,10 +1159,14 @@
     //core_rss
     //core_search
     //core_shortcode
-	public function render_core_social_link(array $block, string $content):string
+	public function prerender_core_social_link(array $block, ?string $content, ?WP_Block $parent):?string
 	{
-		$url = $block['attrs']['url'];
-		$service = $block['attrs']['service'];
+//		jvbDump($block, 'social link');
+//		jvbDump($parent, 'Parent');
+
+		$attrs = $block['attrs']??[];
+		$url = $attrs['url']??'';
+		$service = $attrs['service']?:'';
 		$iconName = ($service === 'bluesky') ? 'butterfly' : $service.'-logo';
 		$icon = jvbIcon($iconName);
 		if (!$icon) {
@@ -888,8 +1174,11 @@
 		}
 		return '<li><a href="'.$url.'" target="_blank" rel="nofollow" title="Find us on '.ucfirst($service).'">'.$icon.'<span class="screen-reader-text">Find us on '.ucfirst($service).'</span></a></li>';
 	}
-	public function render_core_social_links(array $block, string $content):string
+	public function prerender_core_social_links(array $block, ?string $content, ?WP_Block $parent):?string
 	{
+//		jvbDump($block, 'social links');
+//		jvbDump($parent, 'Parent');
+
 		return '<ul class="socials">'.$this->innerBlocks($block).'</ul>';
 	}
     //core_tag_cloud
@@ -912,62 +1201,43 @@
     /***********************************
      * Helpers
      **********************************/
-	public function stripTagContents(string $tag, string $content):string
+	public function stripTagContents(string $tag, ?string $content):string
 	{
 		$clean = preg_replace('/<'.$tag.'\b[^>]*>.*?<\/'.$tag.'>/is', '', $content);
 		$clean = preg_replace('/\s+/', ' ', $clean);
 		return trim($clean);
 	}
 
-    public function innerBlocks(array $block, string $before = '', string $after = ''):string
+    public function innerBlocks(array $block, string $before = '', string $after = '', bool $prerender = true):string
     {
 		$content = '';
 		foreach ($block['innerBlocks'] as $b) {
-			$method = 'render_'.$this->sanitizeBlockName($b);
-			$function = BASE.$method;
-
-			$content .= $before;
-			if (function_exists($function)) {
-				$content .= $function($b, '');
-			} else if (method_exists($this, $method)) {
-				$content .= $this->$method($b, '');
-			} else {
-				$content .= render_block($b);
-			}
-			$content .= $after;
+			$content .= sprintf('%s%s%s',
+				$before,
+				render_block($b),
+				$after
+			);
 		}
 		return $content;
     }
 
-    public function inside(array $block, mixed $tag = false, mixed $o = false):string
-    {
-        if (!$o) {
-            $o = trim($block['innerHTML']);
-        }
-        if (!$tag) {
-            //check to see if there was one dynamically set first
-            $tag = (array_key_exists('tagName', $block['attrs'])) ? $block['attrs']['tagName'] : '';
-            $tag = ($tag == '') ? str_replace('<', '', strtok($o, '>')) : '';
-            $tag = (str_contains($tag, ' class')) ? strtok($tag, ' class') : $tag;
-            $tag = trim($tag);
-        }
-		if (!str_starts_with($o, '<'.$tag)) {
-			return $o;
+	public function inside(array $block, mixed $tag = false, mixed $o = false): string
+	{
+		$html = $o ?: trim($block['innerHTML']);
+
+		if (empty($html)) {
+			return '';
 		}
 
-        $len = strlen('</'.$tag.'>');
+		if (preg_match('/^<(\w+)[^>]*>(.*)<\/\1>$/s', $html, $matches)) {
+			if ($tag && strtolower($matches[1]) !== strtolower($tag)) {
+				return $html;
+			}
+			return trim($matches[2]);
+		}
 
-        return substr_replace(
-            str_replace(
-                strtok($o, '>').'>',
-                ' ',
-                $o
-            ),
-            '',
-            -$len,
-            $len
-        );
-    }
+		return $html;
+	}
 
 	/**
 	 * Extract content from a specific nested element
@@ -1003,7 +1273,7 @@
                  (!array_key_exists('attrs', $block) && !array_key_exists('id', $block['attrs'])))) {
                 $ID = get_post_thumbnail_id();
             } else {
-                if (array_key_exists('id', $block['attrs'])) {
+                if (array_key_exists('id', $block['attrs']??[])) {
                     $ID = $block['attrs']['id'];
                 } elseif (array_key_exists('mediaId', $block['attrs'])) {
                     $ID = $block['attrs']['mediaId'];

--
Gitblit v1.10.0