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