restrict core blocks to inner blocks of specified parent blocks. blocks blocks blocks

This commit is contained in:
2025-12-09 22:40:44 +00:00
parent 7d1beee2da
commit b16bd77355
7 changed files with 260 additions and 131 deletions

View File

@@ -19,13 +19,44 @@ add_filter( 'block_categories_all' , function ( $categories ) {
return $categories;
});
// add_action('init', function () {
// $blocks = glob(get_theme_file_path('resources/blocks/*/block.json'));
// foreach ($blocks as $block_json) {
// register_block_type($block_json, [
// 'render_callback' => function ($attributes, $content, $block) {
// $slug = basename($block->name);
// $view = "blocks.{$slug}.render";
// if (\Roots\view()->exists($view)) {
// return \Roots\view($view, [
// 'attributes' => $attributes,
// 'content' => $content,
// 'block' => $block,
// ]);
// }
// return $content;
// }
// ]);
// }
// });
add_action('allowed_block_types_all', function(){
$blocks = block_all();
$blacklist = array_diff($blocks, block_whitelist());
$blocks = array_keys( \WP_Block_Type_Registry::get_instance()->get_all_registered() );
$blacklist = array_diff(block_blacklist(), block_whitelist());
return array_values( array_diff( $blocks, $blacklist ) );
}, 100, 2);
function block_blacklist()
{
$file = file_get_contents(get_theme_file_path("resources/json/core-block-blacklist.json"));
$json = json_decode($file);
return $json;
}
function block_whitelist()
{
$file = file_get_contents(get_theme_file_path("resources/json/core-block-whitelist.json"));

View File

@@ -3,7 +3,7 @@ import.meta.glob([
'../fonts/**',
]);
import Header from './sections/header.js';
import Header from '../views/sections/header/header.js';
import LazyLoad from './lib/Lazy.js';
LazyLoad();

View File

View File

@@ -1,23 +1,51 @@
import domReady from '@wordpress/dom-ready';
import blockWhitelist from '../json/core-block-whitelist.json';
domReady(() => {
const restrictEditorParentBlocks = (settings, name) => {
const TEXT_EDITOR_BLOCKS = blockWhitelist;
const TEXT_EDITOR_BLOCKS = [
// Design
'core/separator',
'core/spacer',
// Media
'core/cover',
'core/file',
'core/gallery',
'core/image',
'core/media-text',
'core/audio',
'core/video',
// Text
'core/footnotes',
'core/heading',
'core/list',
'core/code',
'core/details',
'core/freeform',
'core/list-item',
'core/missing',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
];
if (TEXT_EDITOR_BLOCKS.includes(name)) {
settings.parent = [
'acf/badegg-editor',
];
settings.parent = ['acf/badegg-editor']
}
// console.log(settings, name)
return settings
}
wp.hooks.addFilter(
'blocks.registerBlockType',
'badegg/restrict-parent-blocks',
'your-project-name/restrict-parent-blocks',
restrictEditorParentBlocks
);

View File

@@ -0,0 +1,102 @@
[
"core/button",
"core/comment-template",
"core/home-link",
"core/navigation-link",
"core/navigation-submenu",
"core/buttons",
"core/column",
"core/columns",
"core/group",
"core/more",
"core/nextpage",
"core/separator",
"core/spacer",
"core/text-columns",
"core/embed",
"core/cover",
"core/file",
"core/gallery",
"core/image",
"core/media-text",
"core/audio",
"core/video",
"core/block",
"core/footnotes",
"core/heading",
"core/list",
"core/code",
"core/details",
"core/freeform",
"core/list-item",
"core/missing",
"core/paragraph",
"core/preformatted",
"core/pullquote",
"core/quote",
"core/table",
"core/verse",
"core/avatar",
"core/comment-author-name",
"core/comment-content",
"core/comment-date",
"core/comment-edit-link",
"core/comment-reply-link",
"core/comments",
"core/comments-pagination",
"core/comments-pagination-next",
"core/comments-pagination-numbers",
"core/comments-pagination-previous",
"core/comments-title",
"core/loginout",
"core/navigation",
"core/pattern",
"core/post-author",
"core/post-author-biography",
"core/post-author-name",
"core/post-comments-form",
"core/post-content",
"core/post-date",
"core/post-excerpt",
"core/post-featured-image",
"core/post-navigation-link",
"core/post-template",
"core/post-terms",
"core/post-title",
"core/query",
"core/query-no-results",
"core/query-pagination",
"core/query-pagination-next",
"core/query-pagination-numbers",
"core/query-pagination-previous",
"core/query-title",
"core/read-more",
"core/site-logo",
"core/site-tagline",
"core/site-title",
"core/template-part",
"core/term-description",
"core/post-comments",
"core/legacy-widget",
"core/widget-group",
"core/archives",
"core/calendar",
"core/categories",
"core/latest-comments",
"core/latest-posts",
"core/page-list",
"core/page-list-item",
"core/rss",
"core/search",
"core/shortcode",
"core/social-link",
"core/tag-cloud",
"core/html",
"core/social-links"
]

View File

@@ -35,7 +35,7 @@ class Editor
]);
}
public function render($block, $content = '', $is_preview = false)
public function render($block, string $content = '', bool $is_preview = false, int $post_id = 0)
{
$name = basename(__FILE__, '.php');
$themeURL = get_template_directory_uri();
@@ -66,7 +66,7 @@ class Editor
$data = array_merge($data, $block);
$data['section_classes'] = $CssClasses->section($data);
$data['allowed_blocks'] = $this->inner_blocks();
$data['allowed_blocks'] = \App\block_whitelist();
$data['template'] = $this->default_template();
$data['block'] = $block;
@@ -100,38 +100,4 @@ class Editor
],
];
}
public function inner_blocks()
{
return [
// Design
'core/separator',
'core/spacer',
// Media
'core/cover',
'core/file',
'core/gallery',
'core/image',
'core/media-text',
'core/audio',
'core/video',
// Text
'core/footnotes',
'core/heading',
'core/list',
'core/code',
'core/details',
'core/freeform',
'core/list-item',
'core/missing',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
];
}
}

View File

@@ -24,6 +24,8 @@ export default function Header() {
const position = body.offsetTop;
const header = document.querySelector(".site-header");
if(!header) return;
if (scrolled > position + header.offsetHeight) {
body.classList.add("scrolled");
} else {