Skip to content

Commit

Permalink
chore(module): code uniformity
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jul 22, 2021
1 parent 48e5ffc commit c38e17f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
13 changes: 6 additions & 7 deletions src/Modules/BlockPatternCategoryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ class BlockPatternCategoryModule extends AbstractModule
*/
public function handle()
{
/** No need to load block pattern categories when loading the website frontend */
if (!is_admin() || !class_exists('WP_Block_Patterns_Registry')) {
if (! is_admin() || ! class_exists('WP_Block_Patterns_Registry')) {
return;
}

return $this->config->each(function ($categoryData, $categorySlug) {
if (empty($categorySlug) || is_int($categorySlug)) {
$categorySlug = $categoryData;
$categoryData = [];
return $this->config->each(function ($value, $key) {
if (empty($key) || is_int($key)) {
$key = $value;
$value = [];
}

register_block_pattern_category(Str::slug($categorySlug), $categoryData);
register_block_pattern_category(Str::slug($key), $value);
});
}
}
24 changes: 12 additions & 12 deletions src/Modules/BlockPatternModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Str;
use Log1x\Poet\Concerns\HasNamespace;

use function Roots\view;

class BlockPatternModule extends AbstractModule
Expand All @@ -24,33 +25,32 @@ class BlockPatternModule extends AbstractModule
*/
public function handle()
{
/** No need to load block patterns when loading the website frontend */
if (!is_admin() || !class_exists('WP_Block_Patterns_Registry')) {
if (! is_admin() || ! class_exists('WP_Block_Patterns_Registry')) {
return;
}

return $this->config->each(function ($patternData, $patternSlug) {
if (empty($patternSlug) || is_int($patternSlug)) {
return $this->config->each(function ($value, $key) {
if (empty($key) || is_int($key)) {
return;
}

$patternData = $this->collect($patternData);
$value = $this->collect($value);

if (!Str::contains($patternSlug, '/')) {
$patternSlug = Str::start($patternSlug, $this->namespace());
if (! Str::contains($key, '/')) {
$key = Str::start($key, $this->namespace());
}

$viewSlug = 'block-patterns.' . Str::after($patternSlug, '/');
$view = 'block-patterns.' . Str::after($key, '/');

if (!view()->exists($viewSlug)) {
if (! view()->exists($view)) {
return;
}

if (!$patternData->has('content')) {
$patternData['content'] = view($viewSlug)->render();
if (! $value->has('content')) {
$value['content'] = view($view)->render();
}

return register_block_pattern($patternSlug, $patternData->all());
return register_block_pattern($key, $value->all());
});
}
}

0 comments on commit c38e17f

Please sign in to comment.