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
|
|
|
|
10
|
|
|
abstract class Block |
11
|
|
|
{ |
12
|
|
|
public function app(): PluginContract |
13
|
|
|
{ |
14
|
|
|
return glsr(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function register(): void |
18
|
|
|
{ |
19
|
|
|
$block = (new \ReflectionClass($this))->getShortName(); |
20
|
|
|
$block = str_replace('_block', '', Str::snakeCase($block)); |
21
|
|
|
register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [ |
22
|
|
|
'render_callback' => [$this, 'render'], |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function render(array $attributes): string |
27
|
|
|
{ |
28
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
29
|
|
|
if (!$this->hasVisibleFields($attributes)) { |
30
|
|
|
return $this->buildEmptyBlock( |
31
|
|
|
_x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews') |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
$rendered = $this->shortcode()->build($attributes, 'block'); |
36
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
37
|
|
|
return $rendered; |
38
|
|
|
} |
39
|
|
|
$blockWrapperAtts = wp_kses_data(get_block_wrapper_attributes([ |
40
|
|
|
'style' => $this->blockStyle($attributes), |
41
|
|
|
])); |
42
|
|
|
preg_match_all('/(\w+)="([^"]*)"/', $blockWrapperAtts, $matches, PREG_SET_ORDER); |
43
|
|
|
$atts = array_column($matches, 2, 1); |
44
|
|
|
return glsr(Builder::class)->div($rendered, $atts); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
abstract public function shortcode(): ShortcodeContract; |
48
|
|
|
|
49
|
|
|
protected function blockStyle(array $attributes): string |
50
|
|
|
{ |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function buildEmptyBlock(string $text): string |
55
|
|
|
{ |
56
|
|
|
return glsr(Builder::class)->div([ |
57
|
|
|
'class' => 'block-editor-warning', |
58
|
|
|
'text' => glsr(Builder::class)->p([ |
59
|
|
|
'class' => 'block-editor-warning__message', |
60
|
|
|
'text' => $text, |
61
|
|
|
]), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function hasVisibleFields(array $attributes): bool |
66
|
|
|
{ |
67
|
|
|
return $this->shortcode()->hasVisibleFields($attributes); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|