Skip to content

Commit

Permalink
feat(blocks): Make block classes available in $block->classes
Browse files Browse the repository at this point in the history
bugfix(blocks): Fix block rendering after post save
chore(deps): Bump dependencies
  • Loading branch information
Log1x committed Mar 15, 2020
1 parent 6b18d33 commit 5ec524c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 32 additions & 16 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ abstract class Block extends Composer
*/
public $post;

/**
* The block classes.
*
* @param string
*/
public $classes;

/**
* The block prefix.
*
Expand Down Expand Up @@ -139,22 +146,26 @@ public function compose($callback = null)
$this->namespace = Str::start($this->slug, $this->prefix);
}

parent::compose(function () {
acf_register_block([
'name' => $this->slug,
'title' => $this->name,
'description' => $this->description,
'category' => $this->category,
'icon' => $this->icon,
'keywords' => $this->keywords,
'post_types' => $this->post_types,
'mode' => $this->mode,
'align' => $this->align,
'supports' => $this->supports,
'enqueue_assets' => [$this,'assets'],
'render_callback' => [$this, 'render']
]);
acf_register_block([
'name' => $this->slug,
'title' => $this->name,
'description' => $this->description,
'category' => $this->category,
'icon' => $this->icon,
'keywords' => $this->keywords,
'post_types' => $this->post_types,
'mode' => $this->mode,
'align' => $this->align,
'supports' => $this->supports,
'enqueue_assets' => function () {
return $this->enqueue();
},
'render_callback' => function ($block, $content = '', $preview = false, $post = 0) {
echo $this->render($block, $content, $preview, $post);
}
]);

parent::compose(function () {
if (! Arr::has($this->fields, 'location.0.0')) {
Arr::set($this->fields, 'location.0.0', [
'param' => 'block',
Expand All @@ -180,8 +191,13 @@ public function render($block, $content = '', $preview = false, $post = 0)
$this->content = $content;
$this->preview = $preview;
$this->post = $post;
$this->classes = collect([
'slug' => Str::start(Str::slug($this->block->title), 'wp-block-'),
'align' => ! empty($this->block->align) ? Str::start($this->block->align, 'align') : false,
'classes' => $this->block->className ?? false,
])->filter()->implode(' ');

echo $this->view(
return $this->view(
Str::finish('views.blocks.', $this->slug),
['block' => $this]
);
Expand Down
10 changes: 10 additions & 0 deletions src/Console/stubs/block.full.stub
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ class DummyClass extends Block
];
}

/**
* Assets to be enqueued when rendering the block.
*
* @return void
*/
public function enqueue()
{
//
}

/**
* The block field group.
*
Expand Down

0 comments on commit 5ec524c

Please sign in to comment.