PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.2.3
Kubio AI Page Builder v1.2.3
2.8.5 2.8.4 2.8.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 4 years ago BlockBase.php 4 years ago BlockContainerBase.php 4 years ago BlockElement.php 4 years ago BlockStyle.php 4 years ago DataHelper.php 4 years ago TemplatePartBlockBase.php 4 years ago
BlockBase.php
372 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 $wrapper_element = null;
33
34 public function __construct( $block, $autoload = true, $context = array() ) {
35 $this->block_context = $context;
36 $this->block_data = $block;
37 $this->block_type = WP_Block_Type_Registry::get_instance()->get_registered(
38 $block['blockName']
39 );
40
41 $this->styledElementsByName = $this->getBlockStyledElementsByName();
42 $this->defaultElement = $this->getDefaultElement();
43
44 if ( ! $this->defaultElement ) {
45 throw new Exception(
46 "Kubio \"{$block['name']}\" has no default element defined"
47 );
48 }
49
50 $attributesDefinitions = $this->block_type->attributes;
51 $attributesDefaults = LodashBasic::mapValues(
52 $attributesDefinitions,
53 'default'
54 );
55 $attributesValues = LodashBasic::get( $this->block_data, 'attrs' );
56
57 parent::__construct(
58 $this->getBlockSupport( 'default' ),
59 LodashBasic::mergeSkipSeqArray(
60 $attributesDefaults,
61 $attributesValues
62 )
63 );
64
65 if ( $autoload ) {
66 $this->create();
67 }
68 }
69
70 public function getBlockStyledElementsByName() {
71 $styledElementsByName = $this->getBlockSupport( Config::$elementsKey );
72 // allow empty elements to be skiped in elementsEnum//
73 $allElements = $this->getBlockSupport( Config::$elementsEnum );
74 foreach ( $allElements as $elementName ) {
75 if ( ! isset( $styledElementsByName[ $elementName ] ) ) {
76 $styledElementsByName[ $elementName ] = array();
77 }
78 }
79
80 return $styledElementsByName;
81 }
82
83 public function getBlockSupport( $path, $default = null ) {
84 return LodashBasic::get(
85 $this->block_type->supports,
86 Config::$mainAttributeKey . '.' . $path,
87 $default
88 );
89 }
90
91 public function getDefaultElement() {
92 if ( ! $this->defaultElement ) {
93 $this->defaultElement = $this->findElementBy(
94 'default',
95 true,
96 false
97 );
98 }
99
100 return $this->defaultElement;
101 }
102
103 public function findElementBy( $path, $value, $fallbackToDefault = true ) {
104 $elements = $this->styledElementsByName;
105
106 foreach ( (array) $elements as $name => $element ) {
107 if ( LodashBasic::get( $element, $path ) === $value ) {
108 return $name;
109 }
110 }
111
112 if ( $fallbackToDefault ) {
113 return $this->defaultElement;
114 }
115 }
116
117 public function create() {
118 $template = $this->getBlockSupport( 'template', null );
119 if ( ! $template ) {
120 $template = array(
121 'type' => 'element',
122 );
123 }
124
125 $this->createFromJson( $template );
126 }
127
128 public function createFromJson( $element ) {
129 $wrapper_element = $this->getWrapperElementName();
130 $this->elements[0] = $this->toElement( $element, $wrapper_element );
131 }
132
133
134 public function getWrapperElementName() {
135 return $this->findElementBy( 'wrapper', true, true );
136 }
137 public function toElement( $element, $wrapper_element, $level = 0 ) {
138 $children = array();
139 if ( isset( $element['children'] ) ) {
140 foreach ( $element['children'] as $child ) {
141 $children[] = $this->toElement(
142 $child,
143 $wrapper_element,
144 $level + 1
145 );
146 }
147 }
148
149 $element_name = LodashBasic::get( $element, 'props.name', null );
150 $props = LodashBasic::get( $element, 'props', array() );
151
152 $has_data_debug_attribute = apply_filters( 'kubio/blocks/element_add_data_debug_attribute', Utils::isDebug() );
153 $is_wrapper = $element_name === $wrapper_element;
154 if ( $is_wrapper ) {
155 $props = LodashBasic::merge(
156 array_merge(
157 array(
158 'className' => array(
159 'wp-block',
160 $this->blockClass(),
161 ),
162 'data-kubio' => $this->block_data['blockName'],
163 ),
164 $has_data_debug_attribute ? array( 'data-debug' => json_encode( $this->block_data ) ) : array() // add data-debug attribute
165 ),
166 $props
167 );
168
169 $hidden = $this->getPropByMedia(
170 'isHidden',
171 false,
172 array(
173 'fromRoot' => true,
174 )
175 );
176
177 foreach ( $hidden as $media => $is_hidden ) {
178 if ( $is_hidden ) {
179 $props['className'] = array_merge(
180 isset( $props['className'] )
181 ? Arr::wrap( $props['className'] )
182 : array(),
183 array( "kubio-hide-on-{$media}" )
184 );
185 }
186 }
187 }
188
189 $element = $this->createElement( $element['type'], $props, $children );
190
191 return $element;
192 }
193
194 public function blockClass() {
195 return 'wp-block-' . $this->kebabBlockName();
196 }
197
198 public function kebabBlockName() {
199 return LodashBasic::kebabCase( str_replace( '/', '-', $this->name() ) );
200 }
201
202 public function name() {
203 return $this->block_type->name;
204 }
205
206 public function createElement( $type, $props, $children ) {
207 return Registry::getInstance()->createElement(
208 $type,
209 $props,
210 $children,
211 $this
212 );
213 }
214
215
216 public function elementClass( $element ) {
217 return 'wp-block-' . $this->kebabBlockName() . '__' . $element;
218 }
219
220 public function getStyledElementConfig( $name, $path = '', $defaultValue = null ) {
221 return LodashBasic::get( $this->styledElementsByName, array( $name, $path ), $defaultValue );
222 }
223
224 public function render( $wp_block ) {
225 $this->parent_block_ = Registry::getInstance()->getParentBlock();
226 $content = '';
227 if ( $this->canRender() ) {
228
229 if ( $this->canRegisterStyle() ) {
230 $this->registerStyle();
231 }
232
233 $content = $this->elements[0] . '';
234 $inner_block_content = $this->renderInnerBlocks( $wp_block );
235 $content = str_replace( '<InnerBlocks/>', $inner_block_content, $content );
236 }
237
238 return $content;
239 }
240
241 public function wrapperStyledComponent() {
242 $wrapperElement = null;
243 $styledElementsByName = $this->styledElementsByName;
244 foreach ( $styledElementsByName as $name => $styledElement ) {
245 $wrapper = LodashBasic::get( $styledElement, 'wrapper', false );
246 if ( $wrapper ) {
247 $wrapperElement = $name;
248 }
249 }
250
251 return $wrapperElement;
252 }
253
254 public function canRender() {
255 global $wp;
256
257 $is_rest_call = defined( 'REST_REQUEST' ) && REST_REQUEST;
258 $route = Arr::get( $wp->query_vars, 'rest_route', '' );
259
260 if ( strpos( $route, '/block-renderer/' ) !== false ) {
261 return true;
262 }
263
264 $can_render = apply_filters( 'kubio/can_render_block', ! $is_rest_call && ! is_admin(), $this );
265
266 return $can_render;
267 }
268
269 public function canRegisterStyle() {
270 $is_rest_call = defined( 'REST_REQUEST' ) && REST_REQUEST;
271
272 return ! $is_rest_call;
273 }
274
275 public function registerStyle() {
276 $style = new BlockStyleRender( $this, $this->parent_block_ );
277 $styleByType = $style->export( $this->getDynamicStyle() );
278 StyleManager::getInstance()->registerBlockStyle( $styleByType );
279 }
280
281 public function getDynamicStyle() {
282 return $this->mapDynamicStyleToElements();
283 }
284
285 public function mapDynamicStyleToElements() {
286 return array();
287 }
288
289 public function renderInnerBlocks( $wp_block ) {
290 $block_content = '';
291 foreach ( $wp_block->inner_blocks as $inner_block ) {
292 $block_content .= $inner_block->render();
293 }
294
295 return $block_content;
296 }
297
298 public function mapPropsToElements() {
299 return array();
300 }
301
302 public function mapPropsToElementsWithDefaults() {
303 $mapPropsToElements = $this->mapPropsToElements();
304
305 if ( ! $mapPropsToElements ) {
306 $mapPropsToElements = array();
307 }
308 $basicAttributes = \WP_Block_Supports::get_instance()->apply_block_supports();
309
310 if ( ! $basicAttributes ) {
311 $basicAttributes = array();
312 }
313 //rename class to className to make it compatible with kubio classes
314 if ( isset( $basicAttributes['class'] ) ) {
315 $classList = array( $basicAttributes['class'] );
316 LodashBasic::set( $basicAttributes, 'className', $classList );
317 LodashBasic::unsetValue( $basicAttributes, 'class' );
318 }
319
320 /**
321 * Set anchor. Kubio sets anchor without the source and attribute parameters in the gutenberg attribute. If you
322 * set source attribute the anchor will not work anymore. When you publish any changes the anchor gets removed.
323 * Because of this when we register the anchor we only set the type: "string" paramter and we set the attribute from
324 * here
325 */
326 $anchor = LodashBasic::get( $this->block_data, array( 'attrs', 'anchor' ) );
327 if ( ! ! $anchor ) {
328 $basicAttributes['id'] = $anchor;
329 }
330
331 $wrapperStyledComponentName = $this->getWrapperElementName();
332
333 $wrapperData = LodashBasic::get( $mapPropsToElements, $wrapperStyledComponentName, array() );
334
335 foreach ( $basicAttributes as $attributeName => $attributeValue ) {
336 if ( is_array( $attributeValue ) ) {
337 $propValue = LodashBasic::get( $wrapperData, $attributeName, array() );
338
339 //if block value is string convert it to array
340 if ( ! is_array( $propValue ) ) {
341 $propValue = array( $propValue );
342 }
343 $mergedValue = array_merge( $attributeValue, $propValue );
344 LodashBasic::set( $wrapperData, $attributeName, $mergedValue );
345 }
346 if ( ! isset( $wrapperData[ $attributeName ] ) ) {
347 LodashBasic::set( $wrapperData, $attributeName, $attributeValue );
348 }
349 }
350
351 LodashBasic::set( $mapPropsToElements, $wrapperStyledComponentName, $wrapperData );
352
353 return $mapPropsToElements;
354 }
355
356 public function getParentColibriContext() {
357 return Arr::get( $this->block_context, 'kubio/parentKubio', array() );
358 }
359
360
361 public function getBlockInnerHtml() {
362
363 $content = trim( $this->block_data['innerHTML'] );
364
365 return $content;
366 }
367
368 public function isSandboxRender() {
369 return apply_filters( 'kubio/sandboxed_render', false );
370 }
371 }
372