|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\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
|
|
|
if (!function_exists('register_block_type_from_metadata')) { |
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
$block = (new \ReflectionClass($this))->getShortName(); |
|
23
|
|
|
$block = str_replace('_block', '', Str::snakeCase($block)); |
|
24
|
|
|
'editor_style' => "{$this->app()->id}/blocks", |
|
|
|
|
|
|
25
|
|
|
register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [ |
|
26
|
|
|
'render_callback' => [$this, 'render'], |
|
27
|
|
|
'style' => $this->app()->id, |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function render(array $attributes): string |
|
32
|
|
|
{ |
|
33
|
|
|
if ('edit' === filter_input(INPUT_GET, 'context')) { |
|
34
|
|
|
if (!$this->hasVisibleFields($attributes)) { |
|
35
|
|
|
return $this->buildEmptyBlock( |
|
36
|
|
|
_x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews') |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
return $this->shortcode()->build($attributes, 'block'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
abstract public function shortcode(): ShortcodeContract; |
|
44
|
|
|
|
|
45
|
|
|
protected function buildEmptyBlock(string $text): string |
|
46
|
|
|
{ |
|
47
|
|
|
return glsr(Builder::class)->div([ |
|
48
|
|
|
'class' => 'block-editor-warning', |
|
49
|
|
|
'text' => glsr(Builder::class)->p([ |
|
50
|
|
|
'class' => 'block-editor-warning__message', |
|
51
|
|
|
'text' => $text, |
|
52
|
|
|
]), |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function hasVisibleFields(array $attributes): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->shortcode()->hasVisibleFields($attributes); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|