|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\BlockContract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Block implements BlockContract |
|
12
|
|
|
{ |
|
13
|
|
|
use IntegrationShortcode; |
|
14
|
|
|
|
|
15
|
|
|
public function register(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$block = (new \ReflectionClass($this))->getShortName(); |
|
18
|
|
|
$block = str_replace('_block', '', Str::snakeCase($block)); |
|
19
|
|
|
register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [ |
|
20
|
|
|
'render_callback' => [$this, 'render'], |
|
21
|
|
|
]); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function render(array $attributes): string |
|
25
|
|
|
{ |
|
26
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
|
27
|
|
|
if (!$this->hasVisibleFields($attributes)) { |
|
28
|
|
|
return $this->buildEmptyBlock( |
|
29
|
|
|
_x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews') |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
return $this->shortcodeInstance()->build($attributes, 'block', isWrapped: false); |
|
33
|
|
|
} |
|
34
|
|
|
return glsr(Builder::class)->div( |
|
35
|
|
|
$this->shortcodeInstance()->build($attributes, 'block', isWrapped: false), |
|
36
|
|
|
$this->wrapperAttributes($attributes) |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string[] |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function blockClasses(array $args): array |
|
44
|
|
|
{ |
|
45
|
|
|
return []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function blockStyles(array $args): array |
|
49
|
|
|
{ |
|
50
|
|
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function buildEmptyBlock(string $text): string |
|
54
|
|
|
{ |
|
55
|
|
|
return glsr(Builder::class)->div([ |
|
56
|
|
|
'class' => 'block-editor-warning', |
|
57
|
|
|
'text' => glsr(Builder::class)->p([ |
|
58
|
|
|
'class' => 'block-editor-warning__message', |
|
59
|
|
|
'text' => $text, |
|
60
|
|
|
]), |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function buildInlineStyles(array $styles): string |
|
65
|
|
|
{ |
|
66
|
|
|
return array_reduce( |
|
67
|
|
|
array_keys($styles), |
|
68
|
|
|
fn (string $carry, string $key) => $carry."$key:{$styles[$key]};", |
|
69
|
|
|
'' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function hasVisibleFields(array $attributes): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->shortcodeInstance()->hasVisibleFields($attributes); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function wrapperAttributes(array $args): array |
|
79
|
|
|
{ |
|
80
|
|
|
$blockSupports = \WP_Block_Supports::get_instance()->apply_block_supports(); |
|
81
|
|
|
$blockClass = $blockSupports['class'] ?? ''; |
|
82
|
|
|
$blockStyle = $blockSupports['style'] ?? ''; |
|
83
|
|
|
$blockId = $blockSupports['id'] ?? ''; |
|
84
|
|
|
$customClasses = glsr()->filterArray('block/classes', $this->blockClasses($args), $args, $this); |
|
85
|
|
|
$customStyles = glsr()->filterArray('block/styles', $this->blockStyles($args), $args, $this); |
|
86
|
|
|
$rootClass = $this->shortcodeInstance()->classAttr($args['className'] ?? '', isWrapper: false); |
|
|
|
|
|
|
87
|
|
|
$wrapperOnlyClasses = array_diff(explode(' ', $blockClass), explode(' ', $rootClass)); |
|
88
|
|
|
$mergedClasses = array_unique(array_merge($customClasses, $wrapperOnlyClasses)); |
|
89
|
|
|
$inlineStyles = $this->buildInlineStyles($customStyles).$blockStyle; |
|
90
|
|
|
$attributes = [ |
|
91
|
|
|
'class' => trim(implode(' ', $mergedClasses)), |
|
92
|
|
|
'id' => $blockId, |
|
93
|
|
|
'style' => $inlineStyles, |
|
94
|
|
|
]; |
|
95
|
|
|
$attributes = glsr()->filterArray('block/wrap/attributes', $attributes, $args, $this); |
|
96
|
|
|
return array_filter([ |
|
97
|
|
|
'class' => glsr(Sanitizer::class)->sanitizeAttrClass($attributes['class'] ?? ''), |
|
98
|
|
|
'id' => glsr(Sanitizer::class)->sanitizeId($attributes['id'] ?? ''), |
|
99
|
|
|
'style' => glsr(Sanitizer::class)->sanitizeAttrStyle($attributes['style'] ?? ''), |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|