Test Failed
Push — develop ( c6b569...7a7654 )
by Paul
09:40
created

Block::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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')) {
52
            return;
53
        }
54
        $block = (new \ReflectionClass($this))->getShortName();
55
        $block = str_replace('_block', '', Str::snakeCase($block));
56
        register_block_type_from_metadata(glsr()->path("assets/blocks/{$block}"), [
57
            'editor_style' => "{$this->app()->id}/blocks",
58
            'render_callback' => [$this, 'render'],
59
            'style' => $this->app()->id,
60
        ]);
61
    }
62
63
    public function render(array $attributes): string
64
    {
65
        $attributes['class'] = $attributes['className'];
66
        if ('edit' === filter_input(INPUT_GET, 'context')) {
67
            $attributes = $this->normalize($attributes);
68
            if (!$this->hasVisibleFields($attributes)) {
69
                return $this->buildEmptyBlock(
70
                    _x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews')
71
                );
72
            }
73
        }
74
        return $this->shortcode()->buildBlock($attributes);
75
    }
76
77
    abstract public function shortcode(): ShortcodeContract;
78
79
    protected function buildEmptyBlock(string $text): string
80
    {
81
        return glsr(Builder::class)->div([
82
            'class' => 'block-editor-warning',
83
            'text' => glsr(Builder::class)->p([
84
                'class' => 'block-editor-warning__message',
85
                'text' => $text,
86
            ]),
87
        ]);
88
    }
89
90
    protected function hasVisibleFields(array $attributes): bool
91
    {
92
        return $this->shortcode()->hasVisibleFields($attributes);
93
    }
94
}
95