diff --git a/app/blocks.php b/app/blocks.php index b343a72..634366f 100644 --- a/app/blocks.php +++ b/app/blocks.php @@ -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")); diff --git a/resources/js/app.js b/resources/js/app.js index 2eaa607..648920e 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -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(); diff --git a/resources/js/blocks.js b/resources/js/blocks.js deleted file mode 100644 index e69de29..0000000 diff --git a/resources/js/editor.js b/resources/js/editor.js index 90c71c8..d94e11c 100644 --- a/resources/js/editor.js +++ b/resources/js/editor.js @@ -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 ); diff --git a/resources/json/core-block-blacklist.json b/resources/json/core-block-blacklist.json new file mode 100644 index 0000000..1a97b6c --- /dev/null +++ b/resources/json/core-block-blacklist.json @@ -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" +] diff --git a/resources/views/blocks/Editor/Editor.php b/resources/views/blocks/Editor/Editor.php index 5ea0d35..cc01fd0 100644 --- a/resources/views/blocks/Editor/Editor.php +++ b/resources/views/blocks/Editor/Editor.php @@ -6,132 +6,98 @@ use App\ACF; class Editor { - public function __construct() - { - add_action('acf/init', [$this, 'init']); - } + public function __construct() + { + add_action('acf/init', [$this, 'init']); + } - public function init() - { - acf_register_block_type([ - 'name' => 'badegg-editor', - 'title' => __('Text Editor'), - 'description' => __('Long form text content with support for things like headings, lists, and images.'), - 'render_callback' => [ $this, 'render'], - 'category' => 'badegg', - 'icon' => 'media-document', - 'supports' => [ - 'align' => false, - 'jsx' => true, - ], - 'example' => [ - 'attributes' => [ - 'mode' => 'preview', - 'data' => [ - 'inserter' => true, - ], - ], - ], - ]); - } - - public function render($block, $content = '', $is_preview = false) - { - $name = basename(__FILE__, '.php'); - $themeURL = get_template_directory_uri(); - - if($is_preview && @$block['data']['inserter']): - echo ''; - return; - endif; - - $CssClasses = new Utilities\CssClasses; - $Colour = new Utilities\Colour; - $CloneGroup = new ACF\CloneGroup; - - $data = []; - - $fields = [ - - ]; - - $fields = array_merge($fields, $CloneGroup->block_all()); - - foreach($fields as $field): - $data[$field] = get_field($field); - endforeach; - - unset($block['data']); - $block['name'] = str_replace('acf/', '', $block['name']); - - $data = array_merge($data, $block); - $data['section_classes'] = $CssClasses->section($data); - $data['allowed_blocks'] = $this->inner_blocks(); - $data['template'] = $this->default_template(); - $data['block'] = $block; - - echo \Roots\view("blocks.$name.$name", [ - 'data' => $data, - 'block' => $block, - ])->render(); - } - - public function default_template() - { - return [ - [ - 'core/heading', - [ - 'level' => 1, - 'placeholder' => 'Heading', - ], + public function init() + { + acf_register_block_type([ + 'name' => 'badegg-editor', + 'title' => __('Text Editor'), + 'description' => __('Long form text content with support for things like headings, lists, and images.'), + 'render_callback' => [ $this, 'render'], + 'category' => 'badegg', + 'icon' => 'media-document', + 'supports' => [ + 'align' => false, + 'jsx' => true, ], - [ - 'core/paragraph', - [ - 'placeholder' => 'You can type your own text, change the heading level, or delete it altogether. You can also type over this text.', - ], + 'example' => [ + 'attributes' => [ + 'mode' => 'preview', + 'data' => [ + 'inserter' => true, + ], + ], ], - [ - 'core/paragraph', - [ - 'placeholder' => 'To adjust block settings, click the Text Editor icon floating above this block to display them in the sidebar.', - ], - ], - ]; - } + ]); + } - public function inner_blocks() - { - return [ - // Design - 'core/separator', - 'core/spacer', + public function render($block, string $content = '', bool $is_preview = false, int $post_id = 0) + { + $name = basename(__FILE__, '.php'); + $themeURL = get_template_directory_uri(); - // Media - 'core/cover', - 'core/file', - 'core/gallery', - 'core/image', - 'core/media-text', - 'core/audio', - 'core/video', + if($is_preview && @$block['data']['inserter']): + echo ''; + return; + endif; - // 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', - ]; - } + $CssClasses = new Utilities\CssClasses; + $Colour = new Utilities\Colour; + $CloneGroup = new ACF\CloneGroup; + + $data = []; + + $fields = [ + + ]; + + $fields = array_merge($fields, $CloneGroup->block_all()); + + foreach($fields as $field): + $data[$field] = get_field($field); + endforeach; + + unset($block['data']); + $block['name'] = str_replace('acf/', '', $block['name']); + + $data = array_merge($data, $block); + $data['section_classes'] = $CssClasses->section($data); + $data['allowed_blocks'] = \App\block_whitelist(); + $data['template'] = $this->default_template(); + $data['block'] = $block; + + echo \Roots\view("blocks.$name.$name", [ + 'data' => $data, + 'block' => $block, + ])->render(); + } + + public function default_template() + { + return [ + [ + 'core/heading', + [ + 'level' => 1, + 'placeholder' => 'Heading', + ], + ], + [ + 'core/paragraph', + [ + 'placeholder' => 'You can type your own text, change the heading level, or delete it altogether. You can also type over this text.', + ], + ], + [ + 'core/paragraph', + [ + 'placeholder' => 'To adjust block settings, click the Text Editor icon floating above this block to display them in the sidebar.', + ], + ], + ]; + } } diff --git a/resources/js/sections/header.js b/resources/views/sections/header/header.js similarity index 97% rename from resources/js/sections/header.js rename to resources/views/sections/header/header.js index 4388622..a510d48 100644 --- a/resources/js/sections/header.js +++ b/resources/views/sections/header/header.js @@ -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 {