Test Failed
Push — develop ( 8a2d95...1ddb5f )
by Paul
11:12
created

Block::blockStyleAttr()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
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 blockClassAttr(array $attributes): string
49
    {
50
        return '';
51
    }
52
53
    protected function blockStyleAttr(array $attributes): string
54
    {
55
        return '';
56
    }
57
58
    protected function blockWrapperAttributes(array $attributes): array
59
    {
60
        $atts = wp_parse_args(
61
            \WP_Block_Supports::get_instance()->apply_block_supports(),
62
            array_fill_keys(['class', 'id', 'style'], '')
63
        );
64
        $class = "{$atts['class']} {$this->blockClassAttr($attributes)}";
65
        $style = "{$atts['style']} {$this->blockStyleAttr($attributes)}";
66
        return array_filter([
67
            'class' => glsr(Sanitizer::class)->sanitizeAttrClass($class),
68
            'id' => glsr(Sanitizer::class)->sanitizeId($atts['id']),
69
            'style' => glsr(Sanitizer::class)->sanitizeAttrStyle($style),
70
        ]);
71
    }
72
73
    protected function buildEmptyBlock(string $text): string
74
    {
75
        return glsr(Builder::class)->div([
76
            'class' => 'block-editor-warning',
77
            'text' => glsr(Builder::class)->p([
78
                'class' => 'block-editor-warning__message',
79
                'text' => $text,
80
            ]),
81
        ]);
82
    }
83
84
    protected function hasVisibleFields(array $attributes): bool
85
    {
86
        return $this->shortcode()->hasVisibleFields($attributes);
87
    }
88
89
    protected function rendered(array $attributes): string
90
    {
91
        $html = $this->shortcode()->build($attributes, 'block');
92
        if (preg_match('/^<div[^>]*>(.*)<\/div>$/s', $html, $matches)) {
93
            return $matches[1];
94
        }
95
        return $html;
96
    }
97
}
98