Test Failed
Push — develop ( 1febff...e7b0b3 )
by Paul
09:43
created

Block::blockWrapperAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks;
4
5
use GeminiLabs\SiteReviews\Contracts\PluginContract;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use GeminiLabs\SiteReviews\Modules\Sanitizer;
10
11
abstract class Block
12
{
13
    public function app(): PluginContract
14
    {
15
        return glsr();
16
    }
17
18
    public function register(): void
19
    {
20
        $block = (new \ReflectionClass($this))->getShortName();
21
        $block = str_replace('_block', '', Str::snakeCase($block));
22
        register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [
23
            'render_callback' => [$this, 'render'],
24
        ]);
25
    }
26
27
    public function render(array $attributes): string
28
    {
29
        if ('edit' === filter_input(INPUT_GET, 'context')) {
30
            if (!$this->hasVisibleFields($attributes)) {
31
                return $this->buildEmptyBlock(
32
                    _x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews')
33
                );
34
            }
35
        }
36
        $rendered = $this->rendered($attributes);
37
        if ('edit' === filter_input(INPUT_GET, 'context')) {
38
            return $rendered;
39
        }
40
        return glsr(Builder::class)->div(
41
            $rendered,
42
            $this->blockWrapperAttributes($attributes)
43
        );
44
    }
45
46
    abstract public function shortcode(): ShortcodeContract;
47
48
    protected function blockStyle(array $attributes): string
49
    {
50
        return '';
51
    }
52
53
    protected function blockWrapperAttributes(array $attributes): array
54
    {
55
        $atts = wp_parse_args(
56
            \WP_Block_Supports::get_instance()->apply_block_supports(),
57
            array_fill_keys(['class', 'id', 'style'], '')
58
        );
59
        $style = "{$atts['style']} {$this->blockStyle($attributes)}";
60
        return array_filter([
61
            'class' => glsr(Sanitizer::class)->sanitizeAttrClass($atts['class']),
62
            'id' => glsr(Sanitizer::class)->sanitizeId($atts['id']),
63
            'style' => glsr(Sanitizer::class)->sanitizeAttrStyle($style),
64
        ]);
65
    }
66
67
    protected function buildEmptyBlock(string $text): string
68
    {
69
        return glsr(Builder::class)->div([
70
            'class' => 'block-editor-warning',
71
            'text' => glsr(Builder::class)->p([
72
                'class' => 'block-editor-warning__message',
73
                'text' => $text,
74
            ]),
75
        ]);
76
    }
77
78
    protected function hasVisibleFields(array $attributes): bool
79
    {
80
        return $this->shortcode()->hasVisibleFields($attributes);
81
    }
82
83
    protected function rendered(array $attributes): string
84
    {
85
        $doc = new \DOMDocument();
86
        $html = $this->shortcode()->build($attributes, 'block');
87
        libxml_use_internal_errors(true);
88
        $doc->loadHTML($html, \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD);
89
        $root = $doc->documentElement;
90
        $rendered = '';
91
        foreach ($root->childNodes as $node) {
92
            $rendered .= $doc->saveHTML($node);
93
        }
94
        libxml_clear_errors();
95
        libxml_use_internal_errors(false);
96
        return $rendered;
97
    }
98
}
99