Skip to content

Commit

Permalink
Add block category support (#3)
Browse files Browse the repository at this point in the history
* feat(poet): Add block category support
* fix(post): Fix post type registration when using multiple string keys.
* fix(taxonomy): Fix taxonomy registration when using multiple string keys.
* enhance(poet): Make the post type, taxonomy, and block registration loop more performant.
* chore(deps): Bump dependencies
* chore(docs): Add block category examples to README
  • Loading branch information
Log1x authored Apr 30, 2020
1 parent 71f4d71 commit 814e063
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 28 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v1.0.7 (04/30/20)

- feat(poet): Add block category support
- fix(post): Fix post type registration when using multiple string keys.
- fix(taxonomy): Fix taxonomy registration when using multiple string keys.
- enhance(poet): Make the post type, taxonomy, and block registration loop more performant.
- chore(deps): Bump dependencies
- chore(docs): Add block category examples to README

## v1.0.6 (03/31/20)

- fix(post): Actually fix post type registration
Expand Down
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Build Status](https://img.shields.io/circleci/build/github/Log1x/poet?style=flat-square)
![Total Downloads](https://img.shields.io/packagist/dt/log1x/poet?style=flat-square)

Poet provides simple configuration-based post type and taxonomy registration as well as the ability to register Gutenberg blocks to be rendered with Laravel Blade.
Poet provides simple configuration-based post type, taxonomy, and block category registration as well as the ability to register Gutenberg blocks rendered with Laravel Blade.

Post types and taxonomies are registered utilizing [Extended CPTs](https://github.com/johnbillion/extended-cpts).

Expand Down Expand Up @@ -142,6 +142,63 @@ For additional configuration options for taxonomies, please see:

> **Note**: Do not nest configuration in a `config` key like shown in the Extended CPTs documentation.
### Register a Block Category

Poet provides an easy to way register, modify, and unregister Gutenberg block categories. Looking in the config, you will see a commented out example for a Call to Action category:

```php
'categories' => [
'cta' => [
'title' => 'Call to Action',
'icon' => 'star-filled',
],
],
```

This would result in a block category with a slug of `cta`. Once your block category is registered, you must register a block to its slug before the category will appear in the editor.

In it's simplest form, you can simply pass a string:

```php
'categories' => [
'my-cool-blocks',
],
```

which would result in a `my-cool-blocks` category automatically converting the slug to title case.

You can also specify the title by passing a value to your slug:

```php
'categories' => [
'my-cool-blocks' => 'Best Blocks, World.',
],
```

Like post types and taxonomies, modifying an existing block category is the same as registering one:

```php
'categories' => [
'common' => ['icon' => 'star-filled']
],
```

You can also easily rename existing categories by passing it a string instead of an array:

```php
'categories' => [
'common' => 'Uncommon Blocks',
],
```

You can unregister an existing block category by simply passing `false`:

```php
'categories' => [
'common' => false,
],
```

### Registering a Block

Poet provides an easy way to register a Gutenberg block with the editor using an accompanying Blade view for rendering the block on the frontend.
Expand Down
25 changes: 13 additions & 12 deletions composer.lock

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

20 changes: 19 additions & 1 deletion config/poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@
*/

'block' => [
// 'sage/accordion'
// 'sage/accordion',
],

/*
|--------------------------------------------------------------------------
| Block Categories
|--------------------------------------------------------------------------
|
| Here you may specify block categories to be registered by Poet for use
| in the editor.
|
*/

'categories' => [
// 'cta' => [
// 'title' => 'Call to Action',
// 'icon' => 'star-filled',
// ],
],

];
92 changes: 78 additions & 14 deletions src/Poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct($config = [])
$this->registerPosts();
$this->registerTaxonomies();
$this->registerBlocks();
$this->registerCategories();
}, 20);
}

Expand All @@ -48,9 +49,11 @@ public function __construct($config = [])
*/
protected function registerPosts()
{
$this->config->only('post')->each(function ($post) {
return collect($post)->each(function ($value, $key) {
if (empty($key)) {
$this->config
->only('post')
->collapse()
->each(function ($value, $key) {
if (empty($key) || is_int($key)) {
return register_extended_post_type(...Arr::wrap($value));
}

Expand All @@ -68,7 +71,6 @@ protected function registerPosts()
Arr::get($value, 'labels', [])
);
});
});
}

/**
Expand All @@ -87,9 +89,11 @@ protected function registerPosts()
*/
protected function registerTaxonomies()
{
$this->config->only('taxonomy')->each(function ($taxonomy) {
return collect($taxonomy)->each(function ($value, $key) {
if (empty($key)) {
$this->config
->only('taxonomy')
->collapse()
->each(function ($value, $key) {
if (empty($key) || is_int($key)) {
return register_extended_taxonomy($value, 'post');
}

Expand All @@ -108,7 +112,6 @@ protected function registerTaxonomies()
Arr::get($value, 'labels', [])
);
});
});
}

/**
Expand All @@ -134,9 +137,11 @@ protected function registerTaxonomies()
*/
protected function registerBlocks()
{
return $this->config->only('block')->each(function ($block) {
foreach ($block as $key => $value) {
if (empty($key)) {
return $this->config
->only('block')
->collapse()
->each(function ($value, $key) {
if (empty($key) || is_int($key)) {
$key = $value;
}

Expand All @@ -146,16 +151,75 @@ protected function registerBlocks()
$key = Str::start($key, $this->namespace());
}

return register_block_type($key, [
return register_block_type($key, [
'attributes' => $value->get('attributes', []),
'render_callback' => function ($data, $content) use ($key, $value) {
return view($value->get('view', 'blocks.' . Str::after($key, '/')), [
'data' => (object) $data,
'content' => $value->get('strip', true) && $this->isEmpty($content) ? false : $content
]);
},
]);
}
]);
});
}

/**
* Register the configured block categories with the editor.
*
* If a category already exists, it will be modified instead.
*
* If a category already exists and is set to `false`, the category
* will be unregistered.
*
* @return void
*/
protected function registerCategories()
{
add_filter('block_categories', function ($categories) {
$categories = collect($categories)->keyBy('slug');

return $this->config
->only('categories')
->collapse()
->map(function ($value, $key) use ($categories) {
if (empty($key) || is_int($key)) {
$key = $value;
}

if ($categories->has($key)) {
if (is_bool($value) && $value === false) {
return $categories->forget($key);
}

if (is_string($value)) {
$value = ['title' => Str::title($value)];
}

return $categories->put(
$key,
array_merge($categories->get($key), $value)
);
}

if (! is_array($value)) {
return [
'slug' => Str::slug($key),
'title' => Str::title($value ?? $key),
'icon' => null,
];
}

return array_merge([
'slug' => Str::slug($key),
'title' => Str::title($key),
'icon' => null,
], $value ?? []);
})
->merge($categories->all())
->filter()
->sort()
->values()
->all();
});
}

Expand Down

0 comments on commit 814e063

Please sign in to comment.