PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.3
2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / Blocks / BlockBase.php
kubio / lib / src / Core / Blocks Last commit date
Query 1 year ago BlockBase.php 1 year ago BlockContainerBase.php 4 years ago BlockElement.php 3 years ago BlockStyle.php 4 years ago DataHelper.php 4 years ago TemplatePartBlockBase.php 1 year ago
BlockBase.php
424 lines
1 <?php
2
3 namespace Kubio\Core\Blocks;
4
5 use Exception;
6 use Kubio\Config;
7 use Kubio\Core\LodashBasic;
8 use Kubio\Core\Registry;
9 use Kubio\Core\StyleManager\BlockStyleRender;
10 use Kubio\Core\StyleManager\StyleManager;
11 use IlluminateAgnostic\Arr\Support\Arr;
12 use Kubio\Core\Utils;
13 use WP_Block_Type_Registry;
14
15 /**
16 * @package Kubio\Core\Blocks
17 *
18 * @method serverSideRender( string $wp_block )
19 *
20 */
21 class BlockBase extends DataHelper {
22
23 public $block_data = null;
24 public $block_type = null;
25 public $parent_block_ = null;
26 public $block_context = null;
27
28 public $elements = array();
29 public $styledElementsByName = array();
30 private $defaultElement;
31
32 private $local_id = null;
33
34
35 private $mapped_props_with_defaults = null;
36
37 public function __construct( $block, $autoload = true, $context = array() ) {
38 $this->block_context = $context;
39 $this->block_data = $block;
40 $this->block_type = WP_Block_Type_Registry::get_instance()->get_registered(
41 $block['blockName']
42 );
43
44
45
46 $this->styledElementsByName = $this->getBlockStyledElementsByName();
47 $this->defaultElement = $this->getDefaultElement();
48
49 if ( ! $this->defaultElement ) {
50 throw new Exception(
51 "Kubio \"{$block['name']}\" has no default element defined"
52 );
53 }
54
55 $attributesDefinitions = $this->block_type->attributes;
56 $attributesDefaults = LodashBasic::mapValues(
57 $attributesDefinitions,
58 'default'
59 );
60 $attributesValues = LodashBasic::get( $this->block_data, 'attrs' );
61
62 parent::__construct(
63 $this->getBlockSupport( 'default' ),
64 LodashBasic::mergeSkipSeqArray(
65 $attributesDefaults,
66 $attributesValues
67 )
68 );
69
70 if ( $autoload ) {
71 $this->create();
72 }
73
74 $this->local_id = Utils::uniqueId();
75 }
76
77 public function localId() {
78 return $this->local_id;
79 }
80
81 public function getBlockStyledElementsByName() {
82 $styledElementsByName = $this->getBlockSupport( Config::$elementsKey );
83 // allow empty elements to be skiped in elementsEnum//
84 $allElements = $this->getBlockSupport( Config::$elementsEnum );
85 foreach ( $allElements as $elementName ) {
86 if ( ! isset( $styledElementsByName[ $elementName ] ) ) {
87 $styledElementsByName[ $elementName ] = array();
88 }
89 }
90
91 return $styledElementsByName;
92 }
93
94 public function getBlockSupport( $path, $default = null ) {
95 return LodashBasic::get(
96 $this->block_type->supports,
97 Config::$mainAttributeKey . '.' . $path,
98 $default
99 );
100 }
101
102 public function getDefaultElement() {
103 if ( ! $this->defaultElement ) {
104 $this->defaultElement = $this->findElementBy(
105 'default',
106 true,
107 false
108 );
109 }
110
111 return $this->defaultElement;
112 }
113
114 public function getLinkAttribute() {
115 return $this->getAttribute( 'link', null );
116 }
117
118 public function findElementBy( $path, $value, $fallbackToDefault = true ) {
119 $elements = $this->styledElementsByName;
120
121 foreach ( (array) $elements as $name => $element ) {
122 if ( LodashBasic::get( $element, $path ) === $value ) {
123 return $name;
124 }
125 }
126
127 if ( $fallbackToDefault ) {
128 return $this->defaultElement;
129 }
130 }
131
132 public function create() {
133 $template = $this->getBlockSupport( 'template', null );
134 if ( ! $template ) {
135 $template = array(
136 'type' => 'element',
137 );
138 }
139
140 $this->createFromJson( $template );
141 }
142
143 public function createFromJson( $element ) {
144 $wrapper_element = $this->getWrapperElementName();
145 $this->elements[0] = $this->toElement( $element, $wrapper_element );
146 }
147
148
149 public function getWrapperElementName() {
150 return $this->findElementBy( 'wrapper', true, true );
151 }
152 public function toElement( $element, $wrapper_element, $level = 0 ) {
153 $children = array();
154 if ( isset( $element['children'] ) ) {
155 foreach ( $element['children'] as $child ) {
156 $children[] = $this->toElement(
157 $child,
158 $wrapper_element,
159 $level + 1
160 );
161 }
162 }
163
164 $element_name = LodashBasic::get( $element, 'props.name', null );
165 $props = LodashBasic::get( $element, 'props', array() );
166
167 $has_data_debug_attribute = apply_filters( 'kubio/blocks/element_add_data_debug_attribute', Utils::isDebug() );
168 $is_wrapper = $element_name === $wrapper_element;
169 if ( $is_wrapper ) {
170 $props = LodashBasic::merge(
171 array_merge(
172 array(
173 'className' => array(
174 'wp-block',
175 $this->blockClass(),
176 implode( ' ', $this->getAppliedMigrationsClasses() ),
177 ),
178 'data-kubio' => $this->block_data['blockName'],
179 ),
180 $has_data_debug_attribute ? array( 'data-debug' => json_encode( $this->block_data ) ) : array() // add data-debug attribute
181 ),
182 $props
183 );
184
185 $hidden = $this->getPropByMedia(
186 'isHidden',
187 false,
188 array(
189 'fromRoot' => true,
190 )
191 );
192
193 foreach ( $hidden as $media => $is_hidden ) {
194 if ( $is_hidden ) {
195 $props['className'] = array_merge(
196 isset( $props['className'] )
197 ? Arr::wrap( $props['className'] )
198 : array(),
199 array( "kubio-hide-on-{$media}" )
200 );
201 } else {
202 $appearanceEffect = $this->getAttribute( 'appearanceEffect' );
203 if ( $appearanceEffect && $appearanceEffect !== 'none' ) {
204 $props = LodashBasic::merge( array( 'data-kubio-aos' => $appearanceEffect ), $props );
205 }
206 }
207 }
208 }
209
210 $element = $this->createElement( $element['type'], $props, $children );
211
212 return $element;
213 }
214
215 public function getAppliedMigrations() {
216 return $this->getAttribute( 'kubio.migrations', array() );
217 }
218
219 public function getAppliedMigrationsClasses() {
220 $result = array();
221
222 $migrations = $this->getAppliedMigrations();
223 foreach ( $migrations as $migration_id ) {
224 $result[] = "kubio-migration--{$migration_id}";
225 }
226
227 return $result;
228 }
229
230 public function blockClass() {
231 return 'wp-block-' . $this->kebabBlockName();
232 }
233
234 public function kebabBlockName() {
235 return LodashBasic::kebabCase( str_replace( '/', '-', $this->name() ) );
236 }
237
238 public function name() {
239 return $this->block_type->name;
240 }
241
242 public function createElement( $type, $props, $children ) {
243 return Registry::getInstance()->createElement(
244 $type,
245 $props,
246 $children,
247 $this
248 );
249 }
250
251
252 public function elementClass( $element ) {
253 return 'wp-block-' . $this->kebabBlockName() . '__' . $element;
254 }
255
256 public function getStyledElementConfig( $name, $path = '', $defaultValue = null ) {
257 return LodashBasic::get( $this->styledElementsByName, array( $name, $path ), $defaultValue );
258 }
259
260 public function getLocalIdClass( $styledComponentName ) {
261 $localId = $this->localId();
262 $stylePrefix = apply_filters( 'kubio/element-style-class-prefix', 'style-' );
263 $stylePrefix = "{$stylePrefix}local-";
264 $localStyleClass = "{$stylePrefix}{$localId}-{$styledComponentName}";
265 return $localStyleClass;
266 }
267
268 public function render( $wp_block ) {
269 $this->parent_block_ = Registry::getInstance()->getParentBlock();
270 $content = '';
271 if ( $this->canRender() ) {
272
273 if ( $this->canRegisterStyle() ) {
274 $this->registerStyle();
275 }
276
277 $content = $this->elements[0] . '';
278 $inner_block_content = $this->renderInnerBlocks( $wp_block );
279 $content = str_replace( '<InnerBlocks/>', $inner_block_content, $content );
280 }
281
282 return $content;
283 }
284
285 public function wrapperStyledComponent() {
286 $wrapperElement = null;
287 $styledElementsByName = $this->styledElementsByName;
288 foreach ( $styledElementsByName as $name => $styledElement ) {
289 $wrapper = LodashBasic::get( $styledElement, 'wrapper', false );
290 if ( $wrapper ) {
291 $wrapperElement = $name;
292 }
293 }
294
295 return $wrapperElement;
296 }
297
298 public function canRender() {
299 global $wp;
300
301 $is_rest_call = defined( 'REST_REQUEST' ) && REST_REQUEST;
302 $route = Arr::get( $wp->query_vars, 'rest_route', '' );
303
304 if ( strpos( $route, '/block-renderer/' ) !== false ) {
305 return true;
306 }
307
308 $can_render = apply_filters( 'kubio/can_render_block', ! $is_rest_call && ! is_admin(), $this );
309
310 return $can_render;
311 }
312
313 public function canRegisterStyle() {
314 $is_rest_call = defined( 'REST_REQUEST' ) && REST_REQUEST;
315
316 return ! $is_rest_call;
317 }
318
319 public function registerStyle() {
320 $style = new BlockStyleRender( $this, $this->parent_block_ );
321 $styleByType = $style->export( $this->getDynamicStyle() );
322 StyleManager::getInstance()->registerBlockStyle( $styleByType );
323 }
324
325 public function getDynamicStyle() {
326 return $this->mapDynamicStyleToElements();
327 }
328
329 public function mapDynamicStyleToElements() {
330 return array();
331 }
332
333 public function renderInnerBlocks( $wp_block ) {
334 $block_content = '';
335 foreach ( $wp_block->inner_blocks as $inner_block ) {
336 $block_content .= $inner_block->render();
337 }
338
339 return $block_content;
340 }
341
342 public function mapPropsToElements() {
343 return array();
344 }
345
346
347 public function mapPropsToElementsWithDefaults() {
348 if ( ! $this->mapped_props_with_defaults ) {
349
350 $mapPropsToElements = $this->mapPropsToElements();
351
352 if ( ! $mapPropsToElements ) {
353 $mapPropsToElements = array();
354 }
355 $basicAttributes = \WP_Block_Supports::get_instance()->apply_block_supports();
356
357 if ( ! $basicAttributes ) {
358 $basicAttributes = array();
359 }
360 //rename class to className to make it compatible with kubio classes
361 if ( isset( $basicAttributes['class'] ) ) {
362 $classList = array( $basicAttributes['class'] );
363 LodashBasic::set( $basicAttributes, 'className', $classList );
364 LodashBasic::unsetValue( $basicAttributes, 'class' );
365 }
366
367 /**
368 * Set anchor. Kubio sets anchor without the source and attribute parameters in the gutenberg attribute. If you
369 * set source attribute the anchor will not work anymore. When you publish any changes the anchor gets removed.
370 * Because of this when we register the anchor we only set the type: "string" paramter and we set the attribute from
371 * here
372 */
373 $anchor = LodashBasic::get( $this->block_data, array( 'attrs', 'anchor' ) );
374 if ( ! ! $anchor ) {
375 $basicAttributes['id'] = $anchor;
376 }
377
378 $wrapperStyledComponentName = $this->getWrapperElementName();
379
380 $wrapperData = LodashBasic::get( $mapPropsToElements, $wrapperStyledComponentName, array() );
381
382 foreach ( $basicAttributes as $attributeName => $attributeValue ) {
383 if ( is_array( $attributeValue ) ) {
384 $propValue = LodashBasic::get( $wrapperData, $attributeName, array() );
385
386 //if block value is string convert it to array
387 if ( ! is_array( $propValue ) ) {
388 $propValue = array( $propValue );
389 }
390 $mergedValue = array_merge( $attributeValue, $propValue );
391 LodashBasic::set( $wrapperData, $attributeName, $mergedValue );
392 }
393 if ( ! isset( $wrapperData[ $attributeName ] ) ) {
394 LodashBasic::set( $wrapperData, $attributeName, $attributeValue );
395 }
396 }
397
398 LodashBasic::set( $mapPropsToElements, $wrapperStyledComponentName, $wrapperData );
399
400 $this->mapped_props_with_defaults = $mapPropsToElements;
401
402 }
403
404 return $this->mapped_props_with_defaults;
405 }
406
407 public function getParentColibriContext() {
408 return Arr::get( $this->block_context, 'kubio/parentKubio', array() );
409 }
410
411
412 public function getBlockInnerHtml() {
413
414 $content = trim( $this->block_data['innerHTML'] );
415 $content = str_replace( array("\r\n", "\r", "\n", "\\n"), '<br/>', $content );
416
417 return $content;
418 }
419
420 public function isSandboxRender() {
421 return apply_filters( 'kubio/sandboxed_render', false );
422 }
423 }
424