22
33namespace Log1x \AcfComposer ;
44
5+ use Exception ;
56use Illuminate \Support \Arr ;
67use Illuminate \Support \Collection ;
78use Illuminate \Support \Str ;
@@ -195,6 +196,16 @@ abstract class Block extends Composer implements BlockContract
195196 */
196197 public $ align_content = '' ;
197198
199+ /**
200+ * The default block spacing.
201+ *
202+ * @var array
203+ */
204+ public $ spacing = [
205+ 'padding ' => null ,
206+ 'margin ' => null ,
207+ ];
208+
198209 /**
199210 * The supported block features.
200211 *
@@ -210,7 +221,7 @@ abstract class Block extends Composer implements BlockContract
210221 public $ styles = [];
211222
212223 /**
213- * The block active style.
224+ * The current block style.
214225 *
215226 * @var string
216227 */
@@ -391,6 +402,17 @@ public function getSupportAttributes(): array
391402 ];
392403 }
393404
405+ $ spacing = array_filter ($ this ->spacing );
406+
407+ if ($ spacing ) {
408+ $ attributes ['style ' ] = [
409+ 'type ' => 'object ' ,
410+ 'default ' => [
411+ 'spacing ' => $ spacing ,
412+ ],
413+ ];
414+ }
415+
394416 return $ attributes ;
395417 }
396418
@@ -543,12 +565,41 @@ public function compose(): ?self
543565
544566 $ this ->register (fn () => $ this ->hasJson ()
545567 ? register_block_type ($ this ->jsonPath ())
546- : acf_register_block_type ( $ this ->settings ()-> all () )
568+ : $ this ->registerBlockType ( )
547569 );
548570
549571 return $ this ;
550572 }
551573
574+ /**
575+ * Register the block type.
576+ */
577+ public function registerBlockType (): void
578+ {
579+ $ block = acf_validate_block_type ($ this ->settings ()->all ());
580+ $ block = apply_filters ('acf/register_block_type_args ' , $ block );
581+
582+ if (acf_has_block_type ($ block ['name ' ])) {
583+ throw new Exception ("Block type [ {$ block ['name ' ]}] is already registered. " );
584+ }
585+
586+ $ block ['attributes ' ] = array_merge (
587+ acf_get_block_type_default_attributes ($ block ),
588+ $ block ['attributes ' ] ?? []
589+ );
590+
591+ acf_get_store ('block-types ' )->set ($ block ['name ' ], $ block );
592+
593+ $ block ['render_callback ' ] = 'acf_render_block_callback ' ;
594+
595+ register_block_type (
596+ $ block ['name ' ],
597+ $ block
598+ );
599+
600+ add_action ('enqueue_block_editor_assets ' , 'acf_enqueue_block_assets ' );
601+ }
602+
552603 /**
553604 * Retrieve the block settings.
554605 */
@@ -575,6 +626,7 @@ public function settings(): Collection
575626 'post_types ' => $ this ->post_types ,
576627 'mode ' => $ this ->mode ,
577628 'align ' => $ this ->align ,
629+ 'attributes ' => $ this ->getSupportAttributes (),
578630 'alignText ' => $ this ->align_text ?? $ this ->align ,
579631 'alignContent ' => $ this ->align_content ,
580632 'styles ' => $ this ->getStyles (),
@@ -636,7 +688,6 @@ public function toJson(): string
636688 {
637689 $ settings = $ this ->settings ()
638690 ->put ('name ' , $ this ->namespace )
639- ->put ('attributes ' , $ this ->getSupportAttributes ())
640691 ->put ('acf ' , [
641692 'blockVersion ' => $ this ->blockVersion ,
642693 'mode ' => $ this ->mode ,
0 commit comments