cache = Cache::for('blocks', WEEK_IN_SECONDS); $this->cache->connect('post')->connect('taxonomy'); $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']); } public function registerBlockStyles():void { do_action('jvbBlockStyles'); //Register extra block styles register_block_style( 'core/navigation', [ 'name'=>'condensed', 'label' => __('Condensed', 'jvb') ] ); register_block_style( 'core/navigation', [ 'name'=>'floating', 'label' => __('Floating', 'jvb') ] ); register_block_style( 'core/navigation', [ 'name'=>'fixed', 'label' => __('Fixed', 'jvb') ] ); register_block_style( 'core/group', [ 'name' =>'callout', 'label' => __('Callout', 'jvb') ] ); register_block_style( 'core/group', [ 'name' =>'callalt', 'label' => __('Callout Alt', 'jvb') ] ); register_block_style( 'core/separator', [ 'name' =>'logo', 'label' => __('With Logo', 'jvb') ] ); } protected function checkMethods(?string $content, array $block, ?WP_Block $parent = null, bool $isPrerender = false):?string { $blockName = $this->sanitizeBlockName($block); $base = ($isPrerender) ? 'prerender_' : 'render_'; $method = $base.$blockName; $function = BASE.$method; if (function_exists($function)) { return $function($block, $content, $parent); } else if (method_exists($this, $method)) { $content = $this->$method($block, $content, $parent); return $isPrerender ? $this->maybeOutputCustomStyles().$content : $content; } elseif (JVB_TESTING && !empty($blockName)) { if (!in_array($block['blockName'], $this->getIgnore($isPrerender))) { jvbDump('No method found for '.print_r($block['blockName'], true)); } } return $content; } protected function getIgnore(bool $isPrerender):array { //Ignore for both $base = [ 'core/null' ]; if ($isPrerender) { $base = array_merge($base, [ 'core/query-pagination', 'core/query-pagination-previous', 'core/query-pagination-next', 'core/query-pagination-numbers', 'core/query', 'core/calendar', 'core/archives', ]); } 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 $this->checkMethods($content, $block); } /*********************************** * Blocks **********************************/ /** * Common Blocks */ //For Reference: //core_form //core_form_input //core_form_submission_notification //core_form_submit_button /** * Design blocks */ 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); if (empty($url[1]) || empty($label[1])) { return ''; } $icon = ''; if (str_contains($url[1], 'google.com/maps')) { $icon = jvbIcon('google-logo'); } if (str_contains($url[1], 'maps.apple.com')) { $icon = jvbIcon('apple-logo'); } if ($icon !== '') { return sprintf( '%s Maps', $this->getClassesAndStyles($block['attrs']??[]), esc_url($url[1]), esc_html($label[1]), $icon ); } return sprintf( '%s', $this->getClassesAndStyles($block['attrs']??[]), esc_url($url[1]), esc_html($label[1]) ); } public function prerender_core_buttons(array $block, ?string $content, ?WP_Block $parent):?string { // jvbDump($block, 'buttons'); // jvbDump($parent, 'Parent'); return sprintf( '%s', $this->getClassesAndStyles($block['attrs']??[], ['buttons','row']), $this->innerBlocks($block) ); } 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 sprintf( '%s', $this->getClassesAndStyles($block['attrs']??[], ['col'], $styles), $this->innerBlocks($block) ); } public function prerender_core_columns(array $block, ?string $content, ?WP_Block $parent):?string { // jvbDump($block, 'columns'); $attrs = $block['attrs']??[]; $tagName = array_key_exists('tagName', $attrs) ? $attrs['tagName'] : 'section'; $classes = ['row', 'nowrap']; if (!array_key_exists('isStackedOnMobile', $attrs) || $attrs['isStackedOnMobile'] === true){ $classes[] = 'stack-small'; } return sprintf( '<%s%s>%s', $tagName, $this->getClassesAndStyles($attrs, $classes), $this->innerBlocks($block).'', $tagName ); } //core_comment_template public function prerender_core_group(array $block, ?string $content, ?WP_Block $parent):?string { // jvbDump($block, 'group'); // jvbDump($parent, 'Parent'); $tag = (array_key_exists('tagName', $block['attrs']??[])) ? $block['attrs']['tagName'] : 'div'; return sprintf( '<%s%s>%s', $tag, $tag === 'main' ? '' : $this->getClassesAndStyles($block['attrs']??[], ['group']), $this->innerBlocks($block), $tag ); } //core_home_link //core_more //core_nextpage public function prerender_core_nextpage(array $block, ?string $content, ?WP_Block $parent):?string { return str_replace('', '',str_replace(' '', 'nextpagelink' => __('Next '.jvbIcon('caret-circle-right'), 'jvb'), 'previouspagelink' => __(jvbIcon('caret-circle-left').' Previous', 'jvb'), 'next_or_number'=> 'next', 'echo' => false ]))); } public function prerender_core_separator(array $block, ?string $content, ?WP_Block $parent):?string { // jvbDump($block, 'separator'); // jvbDump($parent, 'Parent'); $attrs = $block['attrs']??[]; $logo = ''; if (array_key_exists('className', $attrs) && $attrs['className'] === 'is-style-logo'){ $logo = apply_filters('jvbSeparatorLogo', 'logo'); if (!empty($logo)) { $logo = jvbIcon($logo); } } return sprintf( '', $this->getClassesAndStyles($attrs), // $logo ); } public function prerender_core_spacer(array $block, ?string $content, ?WP_Block $parent):?string { // jvbDump($block, 'spsacer'); // jvbDump($parent, 'Parent'); return sprintf( '