Test Failed
Push — scrutinizer ( cca159...52e510 )
by Paul
07:17
created

Block::hasVisibleFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Blocks;
4
5
use GeminiLabs\SiteReviews\Contracts\PluginContract;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Modules\Html\Builder;
11
12
abstract class Block
13
{
14
    public function app(): PluginContract
15
    {
16
        return glsr();
17
    }
18
19
    public function attributes(): array
20
    {
21
        return [];
22
    }
23
24
    /**
25
     * Triggered on render in block editor.
26
     */
27
    public function normalize(array $attributes): array
28
    {
29
        $hide = array_flip(Cast::toArray($attributes['hide']));
30
        unset($hide['if_empty']);
31
        $attributes['hide'] = implode(',', array_keys($hide));
32
        $attributes = $this->normalizeAssignment($attributes, 'assign_to');
33
        $attributes = $this->normalizeAssignment($attributes, 'assigned_to');
34
        return $attributes;
35
    }
36
37
    public function normalizeAssignment(array $attributes, string $assignType): array
38
    {
39
        if ('post_id' === Arr::get($attributes, $assignType)) {
40
            $attributes[$assignType] = $attributes['post_id'];
41
        } elseif ('parent_id' === Arr::get($attributes, $assignType)) {
42
            $attributes[$assignType] = wp_get_post_parent_id($attributes['post_id']);
43
        } elseif ('custom' === Arr::get($attributes, $assignType)) {
44
            $attributes[$assignType] = Arr::get($attributes, $assignType.'_custom');
45
        }
46
        return $attributes;
47
    }
48
49
    public function register(): void
50
    {
51
        if (!function_exists('register_block_type_from_metadata')) {
52
            return;
53
        }
54
        $block = (new \ReflectionClass($this))->getShortName();
55
        $block = str_replace('_block', '', Str::snakeCase($block));
56
            'editor_style' => "{$this->app()->id}/blocks",
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_DOUBLE_ARROW on line 56 at column 27
Loading history...
57
        register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [
58
            'render_callback' => [$this, 'render'],
59
            'style' => $this->app()->id,
60
        ]);
61
    }
62
63
    public function render(array $attributes): string
64
    {
65
        if ('edit' === filter_input(INPUT_GET, 'context')) {
66
            if (!$this->hasVisibleFields($attributes)) {
67
                return $this->buildEmptyBlock(
68
                    _x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews')
69
                );
70
            }
71
        }
72
        return $this->shortcode()->build($attributes, 'block');
73
    }
74
75
    abstract public function shortcode(): ShortcodeContract;
76
77
    protected function buildEmptyBlock(string $text): string
78
    {
79
        return glsr(Builder::class)->div([
80
            'class' => 'block-editor-warning',
81
            'text' => glsr(Builder::class)->p([
82
                'class' => 'block-editor-warning__message',
83
                'text' => $text,
84
            ]),
85
        ]);
86
    }
87
88
    protected function hasVisibleFields(array $attributes): bool
89
    {
90
        return $this->shortcode()->hasVisibleFields($attributes);
91
    }
92
}
93