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\Integrations\IntegrationShortcode; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
11
|
|
|
|
12
|
|
|
abstract class Block |
13
|
|
|
{ |
14
|
|
|
use IntegrationShortcode; |
15
|
|
|
|
16
|
|
|
public function register(): void |
17
|
|
|
{ |
18
|
|
|
$block = (new \ReflectionClass($this))->getShortName(); |
19
|
|
|
$block = str_replace('_block', '', Str::snakeCase($block)); |
20
|
|
|
register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [ |
21
|
|
|
'render_callback' => [$this, 'render'], |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function render(array $attributes): string |
26
|
|
|
{ |
27
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
28
|
|
|
if (!$this->hasVisibleFields($attributes)) { |
29
|
|
|
return $this->buildEmptyBlock( |
30
|
|
|
_x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews') |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
$rendered = $this->shortcodeInstance()->build($attributes, 'block', false); // do not wrap html |
35
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
36
|
|
|
return $rendered; |
37
|
|
|
} |
38
|
|
|
return $this->shortcodeInstance()->wrap( |
39
|
|
|
$rendered, |
40
|
|
|
$this->blockWrapperAttributes($attributes) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function blockClassAttr(array $attributes): string |
45
|
|
|
{ |
46
|
|
|
return ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function blockStyleAttr(array $attributes): string |
50
|
|
|
{ |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function blockWrapperAttributes(array $attributes): array |
55
|
|
|
{ |
56
|
|
|
$atts = wp_parse_args( |
57
|
|
|
\WP_Block_Supports::get_instance()->apply_block_supports(), |
58
|
|
|
array_fill_keys(['class', 'id', 'style'], '') |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$exclude = explode(' ', $attributes['className']); |
62
|
|
|
$include = explode(' ', $atts['class']); |
63
|
|
|
$classes = implode(' ', array_diff($include, $exclude)); |
64
|
|
|
$class = "{$classes} {$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->shortcodeInstance()->hasVisibleFields($attributes); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|