Passed
Push — master ( 0e4a9e...1c63d3 )
by Evgenii
01:14
created

Block::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helick\Blocks;
4
5
use Carbon_Fields\Block as CarbonFieldsBlock;
6
use Exception;
7
use Helick\Blocks\Contracts\Bootable;
8
use Helick\Blocks\Contracts\Composable;
9
use Helick\Blocks\Exceptions\BlockException;
10
11
abstract class Block implements Bootable, Composable
12
{
13
    /**
14
     * The block's display name.
15
     *
16
     * @var string
17
     */
18
    protected $name = '';
19
20
    /**
21
     * The block's description.
22
     *
23
     * @var string
24
     */
25
    protected $description = '';
26
27
    /**
28
     * The block's category.
29
     *
30
     * @var string
31
     */
32
    protected $category = '';
33
34
    /**
35
     * The block's icon.
36
     *
37
     * @see https://developer.wordpress.org/resource/dashicons/
38
     *
39
     * @var string
40
     */
41
    protected $icon = '';
42
43
    /**
44
     * The block's keywords.
45
     *
46
     * @var string[]
47
     */
48
    protected $keywords = [];
49
50
    /**
51
     * Control the block's preview mode.
52
     *
53
     * @var bool
54
     */
55
    protected $preview = true;
56
57
    /**
58
     * Control the block's ability of containing nested blocks.
59
     *
60
     * @var bool
61
     */
62
    protected $nested = false;
63
64
    /**
65
     * Control the position at which the nested blocks will render.
66
     *
67
     * The possible values are:
68
     * - above,
69
     * - below.
70
     *
71
     * @var string
72
     */
73
    protected $nestedPosition = '';
74
75
    /**
76
     * This property allows you to set a template of blocks which every new instance of your block will contain.
77
     *
78
     * @var array
79
     */
80
    protected $nestedTemplates = [];
81
82
    /**
83
     * This property allows you to lock the area that contains the nested blocks.
84
     *
85
     * The possible values are:
86
     * - all – prevents all operations.
87
     * - insert – prevents inserting or removing blocks, but allows moving existing ones.
88
     * - false – prevents locking from being applied to the nested blocks area even if a parent block contains locking.
89
     * - null – disables any locks applied to the area.
90
     *
91
     * @var string
92
     */
93
    protected $nestedLock = '';
94
95
    /**
96
     * This property allows you to restrict the type of blocks that can be inserted in the nested blocks area.
97
     *
98
     * @var null|string[]
99
     */
100
    protected $nestedBlocks = null;
101
102
    /**
103
     * This property allows you to restrict the block to be inserted to specific block types.
104
     *
105
     * @var null|string|string[]
106
     */
107
    protected $parent = null;
108
109
    /**
110
     * The block's template.
111
     *
112
     * @var string|string[]
113
     */
114
    protected $template = '';
115
116
    /**
117
     * Boot the block.
118
     *
119
     * @return void
120
     */
121
    public static function boot(): void
122
    {
123
        (new static)->compose();
124
    }
125
126
    /**
127
     * Captures the output that is generated when a template is included.
128
     * This property is static to prevent object scope resolution.
129
     *
130
     * @param string $template
131
     * @param array  $data
132
     *
133
     * @return string
134
     *
135
     * @throws Exception
136
     */
137
    protected static function capture(string $template, array $data): string
138
    {
139
        extract($data, EXTR_SKIP);
140
141
        ob_start();
142
143
        try {
144
            include $template;
145
        } catch (Exception $e) {
146
            ob_end_clean();
147
148
            throw $e;
149
        }
150
151
        return ob_get_clean();
152
    }
153
154
    /**
155
     * Fields to be attached to the block.
156
     *
157
     * @return array
158
     */
159
    public function fields(): array
160
    {
161
        return [];
162
    }
163
164
    /**
165
     * Data to be passed to the rendered block.
166
     *
167
     * @param array $fields
168
     *
169
     * @return array
170
     */
171
    public function with(array $fields): array
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

171
    public function with(/** @scrutinizer ignore-unused */ array $fields): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        return [];
174
    }
175
176
    /**
177
     * Compose the block.
178
     *
179
     * @return void
180
     */
181
    public function compose(): void
182
    {
183
        if (empty($this->name)) {
184
            throw BlockException::forEmptyName();
185
        }
186
187
        CarbonFieldsBlock::make($this->name)
188
                         ->add_fields($this->fields())
189
                         ->set_description($this->description)
190
                         ->set_category($this->category)
191
                         ->set_icon($this->icon)
192
                         ->set_keywords($this->keywords)
193
                         ->set_inner_blocks($this->nested)
194
                         ->set_inner_blocks_position($this->nestedPosition)
195
                         ->set_inner_blocks_template($this->nestedTemplates)
196
                         ->set_inner_blocks_template_lock($this->nestedLock)
197
                         ->set_allowed_inner_blocks($this->nestedBlocks)
198
                         ->set_parent($this->parent)
199
                         ->set_render_callback([$this, 'render']);
200
    }
201
202
    /**
203
     * Render the block.
204
     *
205
     * [!!] Global variables with the same key name as local variables will be
206
     * overwritten by the local variable.
207
     *
208
     * @param array $fields
209
     * @param array $attributes
210
     * @param array $blocks
211
     *
212
     * @return void
213
     *
214
     * @throws Exception
215
     */
216
    public function render(array $fields, array $attributes, array $blocks): void
217
    {
218
        $globalVariables = compact('fields', 'attributes', 'blocks');
219
        $localVariables  = $this->with($fields);
220
221
        $data = array_merge($globalVariables, $localVariables);
222
223
        echo static::capture($this->template(), $data);
224
    }
225
226
    /**
227
     * Get the block's template.
228
     *
229
     * @return string
230
     */
231
    protected function template(): string
232
    {
233
        if (empty($this->template)) {
234
            throw BlockException::forEmptyTemplate();
235
        }
236
237
        $template = locate_template($this->template);
0 ignored issues
show
Bug introduced by
The function locate_template was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

237
        $template = /** @scrutinizer ignore-call */ locate_template($this->template);
Loading history...
238
        if ($template === '') {
239
            throw BlockException::forNotFoundTemplate($this->template);
240
        }
241
242
        return $template;
243
    }
244
245
    /**
246
     * Get the block's path.
247
     *
248
     * @return string
249
     */
250
    protected function path(): string
251
    {
252
        return dirname(__FILE__);
253
    }
254
255
    /**
256
     * Get the block's uri.
257
     *
258
     * @param string $uri
259
     *
260
     * @return string
261
     */
262
    protected function uri(string $uri = ''): string
263
    {
264
        return str_replace(get_theme_file_path(), get_theme_file_uri(), home_url($uri));
0 ignored issues
show
Bug introduced by
The function home_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
        return str_replace(get_theme_file_path(), get_theme_file_uri(), /** @scrutinizer ignore-call */ home_url($uri));
Loading history...
Bug introduced by
The function get_theme_file_uri was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
        return str_replace(get_theme_file_path(), /** @scrutinizer ignore-call */ get_theme_file_uri(), home_url($uri));
Loading history...
Bug introduced by
The function get_theme_file_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
        return str_replace(/** @scrutinizer ignore-call */ get_theme_file_path(), get_theme_file_uri(), home_url($uri));
Loading history...
265
    }
266
}
267